1#
2# test-source-progress.R
3#
4# Copyright (C) 2021 by RStudio, PBC
5#
6# Unless you have received this program directly from RStudio pursuant
7# to the terms of a commercial license agreement with RStudio, then
8# this program is licensed to you under the terms of version 3 of the
9# GNU Affero General Public License. This program is distributed WITHOUT
10# ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12# AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13#
14#
15
16context("source-progress")
17
18# one-time setup; load progress script
19source(file.path(.rs.sessionModulePath(), "SourceWithProgress.R"))
20
21# given the output of an R script, extract the progress markers and build a list with name/value
22# pairs for each one (note that this will capture only the last value emitted when multiple markers
23# with the same category exist)
24processOutput <- function(output) {
25   values <- list()
26   for (line in seq_along(output)) {
27      result <- regexec("__rs_progress_0__ (\\w+) __ (.*) __rs_progress_1__", output[[line]])
28      if (length(result[[1]]) > 2) {
29         matches <- regmatches(output[[line]], result)[[1]]
30         values[[matches[[2]]]] <- matches[[3]]
31      }
32   }
33   values
34}
35
36# sources the given script with progress and returns the value of all emitted progress markers
37progressResult <- function(path) {
38   p <- tempfile(fileext = ".txt")
39   on.exit(file.remove(p), add = TRUE)
40   con <- file(p, open = "at")
41
42   sourceWithProgress(script = path, con = con)
43   close(con)
44
45   processOutput(readLines(p))
46}
47
48test_that("progress reports include expected values", {
49   output <- progressResult("resources/source-progress/three.R")
50
51   expect_equal("3", output[["count"]])
52   expect_equal("3", output[["statement"]])
53   expect_equal("1", output[["completed"]])
54})
55
56test_that("sections in source are emitted as status", {
57   output <- progressResult("resources/source-progress/sections.R")
58
59   expect_equal("Compute", output[["section"]])
60})
61
62test_that("environment import/export works", {
63   # create a new environment with a couple of values
64   inputEnv <- new.env(parent = emptyenv())
65   assign("x", 1, envir = inputEnv)
66   assign("y", 2, envir = inputEnv)
67
68   # write the environment to disk
69   inputRdata <- tempfile(fileext = ".Rdata")
70   on.exit(file.remove(inputRdata), add = TRUE)
71   save(list = ls(inputEnv), file = inputRdata, envir = inputEnv)
72
73   # create filename for output
74   outputRdata <- tempfile(fileext = ".Rdata")
75   on.exit(file.remove(outputRdata), add = TRUE)
76
77   # source the script; it simply adds x and y to make z
78   sourceWithProgress(script = "resources/source-progress/assignment.R",
79                      con = NULL,
80                      importRdata = inputRdata,
81                      exportRdata = outputRdata)
82
83   # load results into a new environment
84   outputEnv <- new.env(parent = emptyenv())
85   load(file = outputRdata, envir = outputEnv)
86
87   # validate results
88   expect_equal(3, get("z", envir = outputEnv))
89})
90