1## tests of options in system() and system2. 2 3options(warn = 1) 4 5opts <- list("", NULL, FALSE, TRUE, "o1.txt", "o2.txt") 6outs <- c("o1.txt", "o2.txt") 7tos <- c(0, 10) 8 9process <- function(res) 10{ 11 unlink(outs) 12 if(is.character(res)) { 13 cat("value:\n") 14 writeLines(res) 15 } 16 for(f in outs) 17 if(file.exists(f)) { 18 cat(f, ":\n", sep = "") 19 writeLines(readLines(f)) 20 } 21} 22 23for(to in tos) 24 for(out in opts) 25 for(err in opts) { 26 ## skip this for the sake of Unix-alikes 27 if(identical(err, TRUE) && !identical(out,TRUE)) next 28 cat(sprintf("\ntesting stdout = %s, stderr = %s\n", 29 deparse(out), deparse(err))) 30 process(system2("test-system2", stdout = out, stderr = err, 31 timeout = to)) 32 } 33 34for(to in tos) { 35 # timeout 0 uses different implementations from timeout 10 36 # the outputs should be identical 37 38 process(system("test-system2", timeout = to)) 39 process(system("test-system2", ignore.stdout = TRUE, timeout = to)) 40 process(system("test-system2", ignore.stderr = TRUE, timeout = to)) 41 process(system("test-system2", ignore.stdout = TRUE, ignore.stderr = TRUE, 42 timeout = to)) 43 44 process(system("test-system2", TRUE, timeout = to)) 45 process(system("test-system2", TRUE, ignore.stdout = TRUE, timeout = to)) 46 process(system("test-system2", TRUE, ignore.stdout = TRUE, 47 ignore.stderr = TRUE, timeout = to)) 48 49 process(system2("test-system2", "1", input=letters[1:4], timeout = to)) 50 process(system2("test-system2", "1", input=letters[1:4], stdout = TRUE, 51 timeout = to)) 52 53 process(system("test-system2 1", input=letters[1:4], timeout = to)) 54 process(system("test-system2 1", input=letters[1:4], intern = TRUE, 55 timeout = to)) 56 57 tmp <- tempfile() 58 writeLines(letters[5:7], tmp) 59 process(system2("test-system2", "1", stdin = tmp, timeout = to)) 60 process(system2("test-system2", "1", stdin = tmp, stdout = TRUE, 61 timeout = to)) 62 process(system2("test-system2", "1", stdin = tmp, stdout = TRUE, 63 stderr = TRUE, timeout = to)) 64 process(system2("test-system2", "1", stdin = tmp, stdout = "o1.txt", 65 stderr = "o1.txt", timeout = to)) 66 process(system2("test-system2", "1", stdin = tmp, stdout = "o1.txt", 67 stderr = "o2.txt", timeout = to)) 68 69 unlink(c(tmp, outs)) 70 71 print(system("test-system2 5", timeout = to)) 72 system("test-system2 6", intern = TRUE, timeout = to) 73 print(system2("test-system2", "7", timeout = to)) 74 system2("test-system2", "8", stdout=TRUE, timeout = to) 75} 76 77# tests that time out 78# (each runs for a second) 79 80system("./test-system2 sleep 10", timeout = 1) 81system("./test-system2 infinite_loop", timeout = 1) 82system("./test-system2 sleep 10", timeout = 1, intern = T) 83system("./test-system2 infinite_loop", timeout = 1, intern = T) 84 85## test results with timeout set 86 87stopifnot(identical(system("./test-system2 2", timeout = 1), 2L)) 88stopifnot(identical(system("./test-system2 2", timeout = 1, intern = T), 89 structure("stdout 1", status = 2L))) 90