File: test-parallel.R

package info (click to toggle)
r-cran-testthat 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,048 kB
  • sloc: cpp: 9,269; sh: 14; ansic: 14; makefile: 5
file content (171 lines) | stat: -rw-r--r-- 5,710 bytes parent folder | download
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
test_that("detect number of cpus to use", {
  withr::local_options(Ncpus = 100L)
  withr::local_envvar(TESTTHAT_CPUS = NA)
  expect_equal(default_num_cpus(), 100L)

  withr::local_options(Ncpus = 100L)
  withr::local_envvar(TESTTHAT_CPUS = 10)
  expect_equal(default_num_cpus(), 100L)

  withr::local_options(list(Ncpus = NULL))
  withr::local_envvar(TESTTHAT_CPUS = NA)
  expect_equal(default_num_cpus(), 2L)

  withr::local_options(list(Ncpus = NULL))
  withr::local_envvar(TESTTHAT_CPUS = NA)
  expect_equal(default_num_cpus(), 2L)

  withr::local_options(list(Ncpus = NULL))
  withr::local_envvar(TESTTHAT_CPUS = 13)
  expect_equal(default_num_cpus(), 13L)
})

test_that("good error if bad option", {
  withr::local_options(Ncpus = "bad")
  expect_snapshot(default_num_cpus(), error = TRUE)
})

test_that("ok", {
  skip_on_covr()
  withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
  # we cannot run these with the silent reporter, because it is not
  # parallel compatible, and they'll not run in parallel
  capture.output(suppressMessages(
    ret <- test_local(
      test_path("test-parallel", "ok"),
      reporter = "summary",
      stop_on_failure = FALSE
    )
  ))
  tdf <- as.data.frame(ret)
  tdf <- tdf[order(tdf$file), ]
  expect_equal(tdf$failed, c(0, 1, 0))
  expect_equal(tdf$skipped, c(FALSE, FALSE, TRUE))
})

test_that("fail", {
  skip_on_covr()
  withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
  # we cannot run these with the silent reporter, because it is not
  # parallel compatible, and they'll not run in parallel
  capture.output(suppressMessages(
    ret <- test_local(
      test_path("test-parallel", "fail"),
      reporter = "summary",
      stop_on_failure = FALSE
    )
  ))
  tdf <- as.data.frame(ret)
  tdf <- tdf[order(tdf$file), ]
  expect_equal(tdf$failed, c(1))
})

test_that("snapshots", {
  skip_on_covr()
  skip_on_cran()
  withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))

  tmp <- withr::local_tempdir("testthat-snap-")
  file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
  # we cannot run these with the silent reporter, because it is not
  # parallel compatible, and they'll not run in parallel
  capture.output(suppressMessages(
    ret <- test_local(
      file.path(tmp, "snap"),
      reporter = "summary",
      stop_on_failure = FALSE
    )
  ))
  tdf <- as.data.frame(ret)
  tdf <- tdf[order(tdf$file), ]
  expect_equal(tdf$failed, c(0, 0, 1))
  snaps <- file.path(tmp, "snap", "tests", "testthat", "_snaps")
  expect_true(file.exists(file.path(snaps, "snap-1.md")))
  expect_true(file.exists(file.path(snaps, "snap-2.md")))
  expect_true(file.exists(file.path(snaps, "snap-3.md")))
})

test_that("new snapshots are added", {
  skip_on_covr()
  skip_on_cran()
  withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE", CI = "false"))

  tmp <- withr::local_tempdir("testthat-snap-")
  file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
  unlink(file.path(tmp, "snap", "tests", "testthat", "_snaps", "snap-2.md"))

  # we cannot run these with the silent reporter, because it is not
  # parallel compatible, and they'll not run in parallel
  capture.output(suppressMessages(
    ret <- test_local(
      file.path(tmp, "snap"),
      reporter = "summary",
      stop_on_failure = FALSE
    )
  ))
  tdf <- as.data.frame(ret)
  tdf <- tdf[order(tdf$file), ]
  expect_equal(tdf$failed, c(0, 0, 1))
  snaps <- file.path(tmp, "snap", "tests", "testthat", "_snaps")
  expect_true(file.exists(file.path(snaps, "snap-1.md")))
  expect_true(file.exists(file.path(snaps, "snap-2.md")))
  expect_true(file.exists(file.path(snaps, "snap-3.md")))
})

test_that("snapshots are removed if test file has no snapshots", {
  skip_on_covr()
  skip_on_cran()
  withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))

  tmp <- withr::local_tempdir("testthat-snap-")
  file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
  writeLines(
    "test_that(\"2\", { expect_true(TRUE) })",
    file.path(tmp, "snap", "tests", "testthat", "test-snap-2.R")
  )

  # we cannot run these with the silent reporter, because it is not
  # parallel compatible, and they'll not run in parallel
  capture.output(suppressMessages(
    ret <- test_local(
      file.path(tmp, "snap"),
      reporter = "summary",
      stop_on_failure = FALSE
    )
  ))
  tdf <- as.data.frame(ret)
  tdf <- tdf[order(tdf$file), ]
  expect_equal(tdf$failed, c(0, 0, 1))
  snaps <- file.path(tmp, "snap", "tests", "testthat", "_snaps")
  expect_true(file.exists(file.path(snaps, "snap-1.md")))
  expect_false(file.exists(file.path(snaps, "snap-2.md")))
  expect_true(file.exists(file.path(snaps, "snap-3.md")))
})

test_that("snapshots are removed if test file is removed", {
  skip_on_covr()
  skip_on_cran()

  withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
  withr::defer(unlink(tmp, recursive = TRUE))
  dir.create(tmp <- tempfile("testthat-snap-"))
  file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
  unlink(file.path(tmp, "snap", "tests", "testthat", "test-snap-2.R"))
  withr::local_envvar(CI = NA_character_)
  # we cannot run these with the silent reporter, because it is not
  # parallel compatible, and they'll not run in parallel
  capture.output(suppressMessages(
    ret <- test_local(
      file.path(tmp, "snap"),
      reporter = "summary",
      stop_on_failure = FALSE
    )
  ))
  tdf <- as.data.frame(ret)
  tdf <- tdf[order(tdf$file), ]
  expect_equal(tdf$failed, c(0, 1))
  snaps <- file.path(tmp, "snap", "tests", "testthat", "_snaps")
  expect_true(file.exists(file.path(snaps, "snap-1.md")))
  expect_false(file.exists(file.path(snaps, "snap-2.md")))
  expect_true(file.exists(file.path(snaps, "snap-3.md")))
})