-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.R
More file actions
72 lines (61 loc) · 2.1 KB
/
helpers.R
File metadata and controls
72 lines (61 loc) · 2.1 KB
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
# copy a package to tmp, then overwrite any changes on exit
restore_package <- function(dir, expr, tmp_conn) {
# this is way uglier than it should be. i'm missing something.
tdir <- tempdir()
file.copy(dir, tdir, recursive = TRUE)
on.exit({
unlink(dir, recursive = TRUE)
dir.create(dir)
file.copy(file.path(tdir, basename(dir)), dirname(dir), recursive = TRUE)
unlink(file.path(tdir, basename(dir)), recursive = TRUE)
})
if (!missing(tmp_conn)) {
old = options("__potools_testing_prompt_connection__" = tmp_conn)
on.exit(options(old), add = TRUE)
}
invisible(expr)
}
# TODO: I think this can just be replaced by expect_match and expect_no_match in current testthat dev
expect_all_match = function(inputs, targets, ..., invert=FALSE) {
matched <- vapply(
targets,
function(target) length(grep(target, inputs, ..., invert=invert)) > 0L,
logical(1L)
)
expect(
all(matched),
sprintf(
if (invert) {
"Unwanted messages found:\n Observed: %s\n Didn't want: %s\n"
} else {
"Not all messages found:\n Observed: %s\n Wanted: %s\n"
},
toString(sQuote(inputs)),
toString(sQuote(targets[!matched]))
)
)
}
expect_messages = function(expr, msgs, ..., invert=FALSE) {
observed_messages = capture_messages(expr)
expect_all_match(observed_messages, msgs, ..., invert=invert)
}
test_package = function(pkg) test_path(file.path("test_packages", pkg))
mock_translation = function(mocks) test_path(file.path("mock_translations", mocks))
local_test_package <- function(..., .envir = parent.frame()) {
temp <- withr::local_tempdir(.local_envir = .envir)
writeLines(con = file.path(temp, "DESCRIPTION"), c(
"Package: test",
"Version: 1.0.0"
))
dir_create(file.path(temp, c("po", "R")))
files <- list(...)
for (i in seq_along(files)) {
writeLines(files[[i]], file.path(temp, names(files)[[i]]))
}
temp
}
# different platforms/installations of gettext apparently
# produce a different number of "." in "progress" output; normalize
standardize_dots <- standardise_dots <- function(x) {
gsub("\\.{2,}", ".", x)
}