Skip to content

Commit c6dbea8

Browse files
committed
test: rework config test
1 parent e5c899b commit c6dbea8

File tree

1 file changed

+44
-114
lines changed

1 file changed

+44
-114
lines changed

tests/testthat/test-config.R

Lines changed: 44 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1-
test_that("config finding works", {
1+
test_that("guess_where_config works", {
2+
# Default behavior, default structure
23
run_quietly_in_a_dummy_golem({
3-
# I. Testing behavior of the helper function - first case:
4-
# file lives in default path and no envir-variable
54
config <- guess_where_config(
65
path = "."
76
)
87
expect_exists(
98
config
109
)
11-
# II. Testing behavior of the helper function - second case:
12-
# file lives in default path, and envir-variable set correctly
10+
})
11+
# Default behavior, default structure
12+
run_quietly_in_a_dummy_golem({
13+
unlink("./inst/golem-config.yml")
14+
expect_error({
15+
guess_where_config(
16+
path = "."
17+
)
18+
})
19+
})
1320

21+
# Using the envvar, case one with a file that exists
22+
run_quietly_in_a_dummy_golem({
23+
fs_file_copy(
24+
"./inst/golem-config.yml",
25+
"./inst/golem-config2.yml"
26+
)
1427
withr::with_envvar(
15-
c("GOLEM_CONFIG_PATH" = "./inst/golem-config.yml"),
28+
c("GOLEM_CONFIG_PATH" = "./inst/golem-config2.yml"),
1629
{
1730
config <- guess_where_config(
1831
path = "."
@@ -22,146 +35,63 @@ test_that("config finding works", {
2235
)
2336
}
2437
)
38+
})
2539

26-
# III. Testing behavior of the helper function - third case:
27-
# file lives in default path, and envir-variable set incorrectly
40+
# Using the envvar, case one with a file that exists
41+
run_quietly_in_a_dummy_golem({
2842
withr::with_envvar(
2943
c("GOLEM_CONFIG_PATH" = "./inst/golem-config2.yml"),
3044
{
31-
testthat::expect_error(
45+
expect_error(
3246
guess_where_config(
3347
path = "."
34-
),
35-
regexp = "but we were unable to locate"
48+
)
3649
)
3750
}
3851
)
52+
})
53+
})
3954

40-
# IV. Testing behavior of the helper function - fourth case:
41-
# file lives in another path, and hard coded path is wrongly passed
42-
file.copy(
43-
from = "./inst/golem-config.yml",
44-
to = "./inst/golem-config3.yml"
45-
)
46-
file.remove("./inst/golem-config.yml")
47-
testthat::expect_error(
48-
guess_where_config(
49-
path = ".",
50-
file = "inst/golem-config2.yml"
51-
),
52-
regexp = "Unable to locate a config file"
53-
)
54-
# V. Testing behavior of the helper function:
55-
# file lives in another path, and envir-var or path to file is wrong
56-
Sys.setenv("GOLEM_CONFIG_PATHHHHH" = "./inst/golem-config2.yml")
57-
testthat::expect_error(
58-
guess_where_config(
59-
path = ".",
60-
file = "inst/golem-config2.yml"
61-
),
62-
regexp = "Unable to locate a config file"
63-
)
64-
file.copy(
65-
from = "./inst/golem-config3.yml",
66-
to = "./inst/golem-config.yml"
67-
)
68-
file.remove("./inst/golem-config3.yml")
69-
expect_exists(
70-
config
71-
)
72-
unlink(
73-
"R/app_config.R",
74-
force = TRUE
75-
)
76-
unlink(
77-
"inst/golem-config.yml",
78-
force = TRUE
79-
)
55+
test_that("get_current_config works", {
56+
run_quietly_in_a_dummy_golem({
57+
# We don't need to retest guess_where_config
8058
testthat::with_mocked_bindings(
81-
guess_where_config = function(...) {
82-
return(NULL)
83-
},
84-
rlang_is_interactive = function(...) {
59+
fs_file_exists = function(...) {
8560
return(FALSE)
8661
},
8762
{
88-
expect_error(
89-
get_current_config()
90-
)
91-
expect_false(
92-
file.exists("R/app_config.R")
93-
)
94-
expect_false(
95-
file.exists("inst/golem-config.yml")
96-
)
63+
expect_error(get_current_config())
9764
}
9865
)
66+
})
67+
run_quietly_in_a_dummy_golem({
68+
# We don't need to retest guess_where_config
9969
testthat::with_mocked_bindings(
10070
fs_file_exists = function(...) {
10171
return(FALSE)
10272
},
73+
guess_where_config = function(...) {
74+
return("")
75+
},
10376
rlang_is_interactive = function(...) {
10477
return(TRUE)
10578
},
10679
ask_golem_creation_upon_config = function(...) {
10780
return(TRUE)
10881
},
10982
{
110-
config <- get_current_config()
83+
unlink("inst/golem-config.yml")
84+
unlink("R/app_config.R")
85+
get_current_config()
11186
expect_exists(
112-
config
87+
"inst/golem-config.yml"
88+
)
89+
expect_exists(
90+
"R/app_config.R"
11391
)
114-
}
115-
)
116-
testthat::with_mocked_bindings(
117-
fs_file_exists = function(...) {
118-
return(FALSE)
119-
},
120-
rlang_is_interactive = function(...) {
121-
return(TRUE)
122-
},
123-
ask_golem_creation_upon_config = function(...) {
124-
return(FALSE)
125-
},
126-
{
127-
config <- get_current_config()
128-
expect_equal(config, NULL)
129-
}
130-
)
131-
testthat::with_mocked_bindings(
132-
fs_file_exists = function(...) {
133-
return(FALSE)
134-
},
135-
rlang_is_interactive = function(...) {
136-
return(FALSE)
137-
},
138-
ask_golem_creation_upon_config = function(...) {
139-
return(FALSE)
140-
},
141-
{
142-
expect_error(get_current_config())
14392
}
14493
)
14594
})
146-
147-
# testthat::with_mocked_bindings(
148-
# fs_file_exists = function(...) {
149-
# return(FALSE)
150-
# },
151-
# rlang_is_interactive = function(...) {
152-
# return(TRUE)
153-
# },
154-
# ask_golem_creation_upon_config = function(...) {
155-
# return(FALSE)
156-
# },
157-
# {
158-
# config <- get_current_config()
159-
#
160-
# expect_null(
161-
# config
162-
# )
163-
# }
164-
# )
16595
})
16696

16797
test_that("ask_golem_creation_upon_config works", {

0 commit comments

Comments
 (0)