Skip to content

Commit 996935b

Browse files
committed
lint
1 parent 84ef9c8 commit 996935b

10 files changed

+464
-225
lines changed

R/model.R

Lines changed: 150 additions & 58 deletions
Large diffs are not rendered by default.

R/path.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ unset_cmdstan_path <- function() {
234234
}
235235

236236
# fake a cmdstan version (only used in tests)
237-
fake_cmdstan_version <- function(version, mod=NULL) {
237+
fake_cmdstan_version <- function(version, mod = NULL) {
238238
.cmdstanr$VERSION <- version
239-
if(!is.null(mod)) {
239+
if (!is.null(mod)) {
240240
if (!is.null(mod$.__enclos_env__$private$exe_info_)) {
241241
mod$.__enclos_env__$private$exe_info_$stan_version <- version
242242
}

tests/testthat/helper-custom-expectations.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ expect_noninteractive_silent <- function(object) {
101101
expect_silent(object))
102102
}
103103

104-
expect_equal_ignore_order <- function(object, expected, ...){
104+
expect_equal_ignore_order <- function(object, expected, ...) {
105105
object <- expected[sort(names(object))]
106106
expected <- expected[sort(names(expected))]
107107
expect_equal(object, expected, ...)
108108
}
109109

110-
expect_not_true <- function(...) expect_false(isTRUE(...))
110+
expect_not_true <- function(...) expect_false(isTRUE(...))

tests/testthat/helper-mock-cli.R

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
real_wcr <- wsl_compatible_run
22

3-
with_mocked_cli <- function(code, compile_ret, info_ret){
3+
with_mocked_cli <- function(code, compile_ret, info_ret) {
44
with_mocked_bindings(
55
code,
66
wsl_compatible_run = function(command, args, ...) {
77
if (
88
!is.null(command)
9-
&& command == 'make'
9+
&& command == "make"
1010
&& !is.null(args)
11-
&& startsWith(basename(args[1]), 'model-')
11+
&& startsWith(basename(args[1]), "model-")
1212
) {
1313
message("mock-compile-was-called")
1414
compile_ret
15-
} else if (!is.null(args) && args[1] == "info") info_ret
16-
else real_wcr(command = command, args = args, ...)
15+
} else if (!is.null(args) && args[1] == "info") {
16+
info_ret
17+
} else {
18+
real_wcr(command = command, args = args, ...)
19+
}
1720
}
1821
)
1922
}
@@ -31,15 +34,21 @@ with_mocked_cli <- function(code, compile_ret, info_ret){
3134
# fails if mock_compile is called (even once)
3235
#
3336
# Implementation:
34-
# `with_mocked_cli` emits a message with the contents `mock-compile-was-called` if a compile is triggered
37+
# `with_mocked_cli`
38+
# if a compile is triggered
39+
# emits a message with the contents `mock-compile-was-called`
3540
# (defined as wsl_compatible_run being called with make model-*)
3641
# `expect_mock_compile` checks for this message:
3742
# passes if it detects such a message
3843
# fails if it does not
3944
# `expect_no_mock_compile`
40-
# fails if a message with exactly this text is detected
45+
# fails if a message with exactly this text is detected
4146
# passes if no such message is detected
4247
# messages with any other text does not impact `expect_no_mock_compile`
4348

44-
expect_mock_compile <- function(object, ...) expect_message(object, regexp = 'mock-compile-was-called', ...)
45-
expect_no_mock_compile <- function(object, ...) expect_no_message(object, message = 'mock-compile-was-called' , ...)
49+
expect_mock_compile <- function(object, ...) {
50+
expect_message(object, regexp = "mock-compile-was-called", ...)
51+
}
52+
expect_no_mock_compile <- function(object, ...) {
53+
expect_no_message(object, message = "mock-compile-was-called", ...)
54+
}
Lines changed: 154 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11

2-
file_that_exists <- 'placeholder_exists'
3-
file_that_doesnt_exist <- 'placeholder_doesnt_exist'
2+
file_that_exists <- "placeholder_exists"
3+
file_that_doesnt_exist <- "placeholder_doesnt_exist"
44
file.create(file_that_exists)
5-
on.exit(if(file.exists(file_that_exists)) file.remove(file_that_exists), add=TRUE, after=FALSE)
5+
on.exit(
6+
if (file.exists(file_that_exists)) file.remove(file_that_exists),
7+
add = TRUE,
8+
after = FALSE
9+
)
610

711
make_local_orig <- cmdstan_make_local()
8-
cmdstan_make_local(cpp_options = list("PRECOMPILED_HEADERS"="false"))
9-
on.exit(cmdstan_make_local(cpp_options = make_local_orig, append = FALSE), add = TRUE, after = FALSE)
10-
hpp <-
11-
"
12+
cmdstan_make_local(cpp_options = list("PRECOMPILED_HEADERS" = "false"))
13+
on.exit(
14+
cmdstan_make_local(cpp_options = make_local_orig, append = FALSE),
15+
add = TRUE,
16+
after = FALSE
17+
)
18+
hpp <- "
1219
#include <stan/math.hpp>
1320
#include <boost/math/tools/promotion.hpp>
1421
#include <ostream>
@@ -17,9 +24,10 @@ namespace bernoulli_external_model_namespace
1724
{
1825
template <typename T0__,
1926
stan::require_all_t<stan::is_stan_scalar<T0__>>* = nullptr>
20-
inline typename boost::math::tools::promote_args<T0__>::type make_odds(const T0__ &
21-
theta,
22-
std::ostream *pstream__)
27+
inline typename boost::math::tools::promote_args<T0__>::type make_odds(
28+
const T0__ & theta,
29+
std::ostream *pstream__
30+
)
2331
{
2432
return theta / (1 - theta);
2533
}
@@ -30,77 +38,112 @@ test_that("cmdstan_model works with user_header with mock", {
3038
tmpfile <- tempfile(fileext = ".hpp")
3139
cat(hpp, file = tmpfile, sep = "\n")
3240

33-
with_mocked_cli(compile_ret = list(status = 0), info_ret = list(), code = expect_mock_compile(
34-
expect_warning(
35-
expect_no_warning({
36-
mod <- cmdstan_model(
37-
stan_file = testing_stan_file("bernoulli_external"),
38-
exe_file = file_that_exists,
39-
user_header = tmpfile
40-
)
41-
}, message = 'Recompiling is recommended'), # this warning should not occur because recompile happens automatically
42-
'Retrieving exe_file info failed' # this warning should occur
43-
)
44-
))
45-
46-
with_mocked_cli(compile_ret = list(status = 0), info_ret = list(), code = expect_mock_compile({
47-
mod_2 <- cmdstan_model(
48-
stan_file = testing_stan_file("bernoulli_external"),
49-
exe_file = file_that_doesnt_exist,
50-
cpp_options=list(USER_HEADER=tmpfile),
51-
stanc_options = list("allow-undefined")
41+
with_mocked_cli(
42+
compile_ret = list(status = 0),
43+
info_ret = list(),
44+
code = expect_mock_compile(
45+
expect_warning(
46+
expect_no_warning({
47+
mod <- cmdstan_model(
48+
stan_file = testing_stan_file("bernoulli_external"),
49+
exe_file = file_that_exists,
50+
user_header = tmpfile
51+
)
52+
}, message = "Recompiling is recommended"),
53+
# ^ this warning should not occur because recompile happens automatically
54+
"Retrieving exe_file info failed"
55+
# ^ this warning should occur
56+
)
5257
)
53-
}))
58+
)
59+
60+
with_mocked_cli(
61+
compile_ret = list(status = 0),
62+
info_ret = list(),
63+
code = expect_mock_compile({
64+
mod_2 <- cmdstan_model(
65+
stan_file = testing_stan_file("bernoulli_external"),
66+
exe_file = file_that_doesnt_exist,
67+
cpp_options = list(USER_HEADER = tmpfile),
68+
stanc_options = list("allow-undefined")
69+
)
70+
})
71+
)
5472

5573
# Check recompilation upon changing header
5674
file.create(file_that_exists)
57-
with_mocked_cli(compile_ret = list(status = 0), info_ret = list(), code = expect_no_mock_compile({
58-
mod$compile(quiet = TRUE, user_header = tmpfile)
59-
}))
75+
with_mocked_cli(
76+
compile_ret = list(status = 0),
77+
info_ret = list(),
78+
code = expect_no_mock_compile({
79+
mod$compile(quiet = TRUE, user_header = tmpfile)
80+
})
81+
)
6082

6183
Sys.setFileTime(tmpfile, Sys.time() + 1) # touch file to trigger recompile
62-
with_mocked_cli(compile_ret = list(status = 0), info_ret = list(), code = expect_mock_compile({
63-
mod$compile(quiet = TRUE, user_header = tmpfile)
64-
}))
84+
with_mocked_cli(
85+
compile_ret = list(status = 0),
86+
info_ret = list(),
87+
code = expect_mock_compile({
88+
mod$compile(quiet = TRUE, user_header = tmpfile)
89+
})
90+
)
6591

6692
# mock does not automatically update file mtime
6793
Sys.setFileTime(mod$exe_file(), Sys.time() + 1) # touch file to trigger recompile
6894

6995
# Alternative spec of user header
70-
with_mocked_cli(compile_ret = list(status = 0), info_ret = list(), code = expect_no_mock_compile({
71-
mod$compile(
72-
quiet = TRUE,
73-
cpp_options = list(user_header = tmpfile),
74-
dry_run = TRUE
75-
)}))
96+
with_mocked_cli(
97+
compile_ret = list(status = 0),
98+
info_ret = list(),
99+
code = expect_no_mock_compile({
100+
mod$compile(
101+
quiet = TRUE,
102+
cpp_options = list(user_header = tmpfile),
103+
dry_run = TRUE
104+
)
105+
})
106+
)
76107

77108
# Error/warning messages
78-
with_mocked_cli(compile_ret = list(status = 1), info_ret = list(), code = expect_error(
79-
cmdstan_model(
80-
stan_file = testing_stan_file("bernoulli_external"),
81-
cpp_options = list(USER_HEADER = "non_existent.hpp"),
82-
stanc_options = list("allow-undefined")
83-
),
84-
"header file '[^']*' does not exist"
85-
))
86-
87-
with_mocked_cli(compile_ret = list(status = 1), info_ret = list(), code = expect_warning(
88-
cmdstan_model(
89-
stan_file = testing_stan_file("bernoulli_external"),
90-
cpp_options = list(USER_HEADER = tmpfile, user_header = tmpfile),
91-
dry_run = TRUE
92-
),
93-
"User header specified both"
94-
))
95-
with_mocked_cli(compile_ret = list(status = 1), info_ret = list(), code = expect_warning(
96-
cmdstan_model(
97-
stan_file = testing_stan_file("bernoulli_external"),
98-
user_header = tmpfile,
99-
cpp_options = list(USER_HEADER = tmpfile),
100-
dry_run = TRUE
101-
),
102-
"User header specified both"
103-
))
109+
with_mocked_cli(
110+
compile_ret = list(status = 1),
111+
info_ret = list(),
112+
code = expect_error(
113+
cmdstan_model(
114+
stan_file = testing_stan_file("bernoulli_external"),
115+
cpp_options = list(USER_HEADER = "non_existent.hpp"),
116+
stanc_options = list("allow-undefined")
117+
),
118+
"header file '[^']*' does not exist"
119+
)
120+
)
121+
122+
with_mocked_cli(
123+
compile_ret = list(status = 1),
124+
info_ret = list(),
125+
code = expect_warning(
126+
cmdstan_model(
127+
stan_file = testing_stan_file("bernoulli_external"),
128+
cpp_options = list(USER_HEADER = tmpfile, user_header = tmpfile),
129+
dry_run = TRUE
130+
),
131+
"User header specified both"
132+
)
133+
)
134+
with_mocked_cli(
135+
compile_ret = list(status = 1),
136+
info_ret = list(),
137+
code = expect_warning(
138+
cmdstan_model(
139+
stan_file = testing_stan_file("bernoulli_external"),
140+
user_header = tmpfile,
141+
cpp_options = list(USER_HEADER = tmpfile),
142+
dry_run = TRUE
143+
),
144+
"User header specified both"
145+
)
146+
)
104147
})
105148

106149
test_that("user_header precedence order is correct", {
@@ -112,35 +155,53 @@ test_that("user_header precedence order is correct", {
112155
add = TRUE
113156
)
114157

115-
with_mocked_cli(compile_ret = list(status = 1), info_ret = list(), code = expect_warning(
116-
{mod <- cmdstan_model(
117-
stan_file = testing_stan_file("bernoulli_external"),
118-
user_header = tmp_files[[1]],
119-
cpp_options = list(USER_HEADER = tmp_files[[2]], user_header = tmp_files[[3]]),
120-
dry_run = TRUE
121-
)},
122-
"User header specified both"
123-
))
158+
with_mocked_cli(
159+
compile_ret = list(status = 1),
160+
info_ret = list(),
161+
code = expect_warning({
162+
mod <- cmdstan_model(
163+
stan_file = testing_stan_file("bernoulli_external"),
164+
user_header = tmp_files[[1]],
165+
cpp_options = list(
166+
USER_HEADER = tmp_files[[2]],
167+
user_header = tmp_files[[3]]
168+
),
169+
dry_run = TRUE
170+
)
171+
}, "User header specified both")
172+
)
124173
expect_equal(mod$precompile_cpp_options()$user_header, tmp_files[[1]])
125174

126-
with_mocked_cli(compile_ret = list(status = 1), info_ret = list(), code = expect_warning(
127-
{mod <- cmdstan_model(
128-
stan_file = testing_stan_file("bernoulli_external"),
129-
cpp_options = list(USER_HEADER = tmp_files[[2]], user_header = tmp_files[[3]]),
130-
dry_run = TRUE
131-
)},
132-
"User header specified both"
133-
))
175+
with_mocked_cli(
176+
compile_ret = list(status = 1),
177+
info_ret = list(),
178+
code = expect_warning({
179+
mod <- cmdstan_model(
180+
stan_file = testing_stan_file("bernoulli_external"),
181+
cpp_options = list(
182+
USER_HEADER = tmp_files[[2]],
183+
user_header = tmp_files[[3]]
184+
),
185+
dry_run = TRUE
186+
)
187+
}, "User header specified both")
188+
)
134189
expect_equal(mod$precompile_cpp_options()$user_header, tmp_files[[2]])
135190

136-
with_mocked_cli(compile_ret = list(status = 1), info_ret = list(), code = expect_warning(
137-
{mod <- cmdstan_model(
138-
stan_file = testing_stan_file("bernoulli_external"),
139-
cpp_options = list(user_header = tmp_files[[3]], USER_HEADER = tmp_files[[2]] ),
140-
dry_run = TRUE
141-
)},
142-
"User header specified both"
143-
))
191+
with_mocked_cli(
192+
compile_ret = list(status = 1),
193+
info_ret = list(),
194+
code = expect_warning({
195+
mod <- cmdstan_model(
196+
stan_file = testing_stan_file("bernoulli_external"),
197+
cpp_options = list(
198+
user_header = tmp_files[[3]],
199+
USER_HEADER = tmp_files[[2]]
200+
),
201+
dry_run = TRUE
202+
)
203+
}, "User header specified both")
204+
)
144205
expect_equal(mod$precompile_cpp_options()$user_header, tmp_files[[3]])
145206

146207
})

0 commit comments

Comments
 (0)