Skip to content

Commit f26d4c9

Browse files
committed
Add helper-state.R from packagetemplate
1 parent 1fc0c94 commit f26d4c9

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

tests/testthat/helper-state.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This helper ensures the package does not modify the session global state. As
2+
# per CRAN policy, packages should not interfere with the user's session state.
3+
# If global settings need to be modified, they should be restored to their
4+
# original values on exit. This can be achieved with the `on.exit()` base
5+
# function, or more conveniently with the `withr` package.
6+
testthat::set_state_inspector(function() {
7+
list(
8+
attached = search(),
9+
connections = getAllConnections(),
10+
cwd = getwd(),
11+
envvars = Sys.getenv(),
12+
handlers = globalCallingHandlers(),
13+
libpaths = .libPaths(),
14+
locale = Sys.getlocale(),
15+
options = options(),
16+
par = par(),
17+
packages = .packages(all.available = TRUE),
18+
sink = sink.number(),
19+
timezone = Sys.timezone(),
20+
NULL
21+
)
22+
})

tests/testthat/test-square_bracket.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
test_that("tests for [ operator", {
22
x <- make_linelist(cars, id = "speed", age = "dist")
3-
3+
on.exit(lost_tags_action())
4+
45
# errors
56
lost_tags_action("warning", quiet = TRUE)
67
msg <- "The following tags have lost their variable:\n age:dist"
@@ -52,6 +53,7 @@ test_that("tests for [ operator", {
5253
})
5354

5455
test_that("tests for [<- operator", {
56+
on.exit(lost_tags_action())
5557

5658
# errors
5759
lost_tags_action("warning", quiet = TRUE)
@@ -75,6 +77,7 @@ test_that("tests for [<- operator", {
7577
})
7678

7779
test_that("tests for [[<- operator", {
80+
on.exit(lost_tags_action())
7881

7982
# errors
8083
lost_tags_action("warning", quiet = TRUE)
@@ -99,6 +102,7 @@ test_that("tests for [[<- operator", {
99102
})
100103

101104
test_that("$<- operator detects tag loss", {
105+
on.exit(lost_tags_action())
102106

103107
# errors
104108
lost_tags_action("warning", quiet = TRUE)

0 commit comments

Comments
 (0)