File: test-stdin.R

package info (click to toggle)
r-cran-processx 3.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,068 kB
  • sloc: ansic: 6,485; sh: 13; makefile: 2
file content (121 lines) | stat: -rw-r--r-- 2,576 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

test_that("stdin", {

  skip_on_cran()
  skip_if_no_tool("cat")

  tmp <- tempfile()
  on.exit(unlink(tmp), add = TRUE)
  p <- process$new("cat", stdin = "|", stdout = tmp, stderr = "|")
  expect_true(p$is_alive())

  p$write_input("foo\n")
  p$write_input("bar\n")
  expect_true(p$is_alive())

  close(p$get_input_connection())
  p$wait(5000)
  expect_false(p$is_alive())
  p$kill()

  expect_equal(readLines(tmp), c("foo", "bar"))
})

test_that("stdin & stdout", {

  skip_on_cran()
  skip_if_no_tool("cat")

  p <- process$new("cat", stdin = "|", stdout = "|")
  expect_true(p$is_alive())

  p$write_input("foo\n")
  p$poll_io(1000)
  expect_equal(p$read_output_lines(), "foo")

  p$write_input("bar\n")
  p$poll_io(1000)
  expect_equal(p$read_output_lines(), "bar")

  close(p$get_input_connection())
  p$wait(10)
  expect_false(p$is_alive())
  p$kill()
})

test_that("stdin buffer full", {

  skip_on_cran()
  skip_other_platforms("unix")

  px <- get_tool("px")
  p <- process$new(px, c("sleep", 100), stdin = "|")
  on.exit(p$kill(), add = TRUE)
  for (i in 1:100000) {
    ret <- p$write_input("foobar")
    if (length(ret) > 0) break
  }

  expect_true(length(ret) > 0)
})

test_that("file as stdin", {

  skip_on_cran()
  skip_if_no_tool("cat")

  tmp <- tempfile()
  tmp2 <- tempfile()
  on.exit(unlink(c(tmp, tmp2), recursive = TRUE), add = TRUE)

  txt <- strrep(paste(sample(letters, 10), collapse = ""), 100)
  cat(txt, file = tmp)

  p <- process$new("cat", stdin = tmp, stdout = tmp2)
  on.exit(p$kill(), add = TRUE)
  p$wait()
  expect_true(file.exists(tmp2))
  expect_equal(readChar(tmp2, nchar(txt)), txt)
})

test_that("large file as stdin", {

  skip_on_cran()
  skip_if_no_tool("cat")

  tmp <- tempfile()
  tmp2 <- tempfile()
  on.exit(unlink(c(tmp, tmp2), recursive = TRUE), add = TRUE)

  txt <- strrep(paste(sample(letters, 10), collapse = ""), 10000)
  cat(txt, file = tmp)

  p <- process$new("cat", stdin = tmp, stdout = tmp2)
  on.exit(p$kill(), add = TRUE)
  p$wait()
  expect_true(file.exists(tmp2))
  expect_equal(file.info(tmp2)$size, nchar(txt))
})

test_that("writing raw", {
  skip_on_cran()
  skip_if_no_tool("cat")

  tmp <- tempfile()
  on.exit(unlink(tmp), add = TRUE)
  p <- process$new("cat", stdin = "|", stdout = tmp, stderr = "|")
  expect_true(p$is_alive())

  foo <- charToRaw("foo\n")
  bar <- charToRaw("bar\n")
  p$write_input(foo)
  p$write_input(bar)
  expect_true(p$is_alive())

  close(p$get_input_connection())
  p$wait(5000)
  expect_false(p$is_alive())
  p$kill()

  expect_equal(readLines(tmp), c("foo", "bar"))
})