Skip to content

Commit bc8a8dd

Browse files
ilyaZarColinFay
authored andcommitted
tests: improve test coverage for config.R to 100 %
1 parent 6b5b75a commit bc8a8dd

File tree

1 file changed

+87
-30
lines changed

1 file changed

+87
-30
lines changed

tests/testthat/test-config.R

Lines changed: 87 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,60 @@
11
test_that("config finding works", {
22
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
35
config <- guess_where_config(
46
path = "."
57
)
68
expect_exists(
79
config
810
)
9-
config <- try_user_config_location(
10-
pth = "."
11+
# II. Testing behavior of the helper function - second case:
12+
# file lives in default path, and envir-variable set correctly
13+
Sys.setenv("GOLEM_CONFIG_PATH" = "./inst/golem-config.yml")
14+
config <- guess_where_config(
15+
path = "."
1116
)
1217
expect_exists(
1318
config
1419
)
15-
expect_null(
16-
try_user_config_location(tempdir())
20+
# III. Testing behavior of the helper function - third case:
21+
# file lives in default path, and envir-variable set incorrectly
22+
Sys.setenv("GOLEM_CONFIG_PATH" = "./inst/golem-config2.yml")
23+
testthat::expect_error(
24+
config <- guess_where_config(
25+
path = "."
26+
),
27+
regexp = "Unable to locate a config file using the environment variable"
1728
)
18-
19-
config <- get_current_config(
20-
path = "."
29+
Sys.setenv("GOLEM_CONFIG_PATH" = "")
30+
# IV. Testing behavior of the helper function - fourth case:
31+
# file lives in another path, and hard coded path is wrongly passed
32+
file.copy(from = "./inst/golem-config.yml",
33+
to = "./inst/golem-config3.yml")
34+
file.remove("./inst/golem-config.yml")
35+
testthat::expect_error(
36+
config <- guess_where_config(
37+
path = ".",
38+
file = "inst/golem-config2.yml"
39+
),
40+
regexp = "Unable to locate a config file from either the 'path' and 'file'"
2141
)
22-
42+
# V. Testing behavior of the helper function:
43+
# file lives in another path, and envir-var or path to file is wrong
44+
Sys.setenv("GOLEM_CONFIG_PATHHHHH" = "./inst/golem-config2.yml")
45+
testthat::expect_error(
46+
config <- guess_where_config(
47+
path = ".",
48+
file = "inst/golem-config2.yml"
49+
),
50+
regexp = "Unable to locate a config file from either the 'path' and 'file'"
51+
)
52+
file.copy(from = "./inst/golem-config3.yml",
53+
to = "./inst/golem-config.yml")
54+
file.remove("./inst/golem-config3.yml")
2355
expect_exists(
2456
config
2557
)
26-
2758
unlink(
2859
"R/app_config.R",
2960
force = TRUE
@@ -52,9 +83,6 @@ test_that("config finding works", {
5283
}
5384
)
5485
testthat::with_mocked_bindings(
55-
guess_where_config = function(...) {
56-
return(NULL)
57-
},
5886
fs_file_exists = function(...) {
5987
return(FALSE)
6088
},
@@ -71,27 +99,56 @@ test_that("config finding works", {
7199
)
72100
}
73101
)
102+
testthat::with_mocked_bindings(
103+
fs_file_exists = function(...) {
104+
return(FALSE)
105+
},
106+
rlang_is_interactive = function(...) {
107+
return(TRUE)
108+
},
109+
ask_golem_creation_upon_config = function(...) {
110+
return(FALSE)
111+
},
112+
{
113+
config <- get_current_config()
114+
expect_equal(config, NULL)
115+
}
116+
)
117+
testthat::with_mocked_bindings(
118+
fs_file_exists = function(...) {
119+
return(FALSE)
120+
},
121+
rlang_is_interactive = function(...) {
122+
return(FALSE)
123+
},
124+
ask_golem_creation_upon_config = function(...) {
125+
return(FALSE)
126+
},
127+
{
128+
expect_error(get_current_config())
129+
}
130+
)
74131
})
75132

76133

77-
testthat::with_mocked_bindings(
78-
fs_file_exists = function(...) {
79-
return(FALSE)
80-
},
81-
rlang_is_interactive = function(...) {
82-
return(TRUE)
83-
},
84-
ask_golem_creation_upon_config = function(...) {
85-
return(FALSE)
86-
},
87-
{
88-
config <- get_current_config()
89-
90-
expect_null(
91-
config
92-
)
93-
}
94-
)
134+
# testthat::with_mocked_bindings(
135+
# fs_file_exists = function(...) {
136+
# return(FALSE)
137+
# },
138+
# rlang_is_interactive = function(...) {
139+
# return(TRUE)
140+
# },
141+
# ask_golem_creation_upon_config = function(...) {
142+
# return(FALSE)
143+
# },
144+
# {
145+
# config <- get_current_config()
146+
#
147+
# expect_null(
148+
# config
149+
# )
150+
# }
151+
# )
95152
})
96153

97154
test_that("ask_golem_creation_upon_config works", {

0 commit comments

Comments
 (0)