Skip to content

Commit d174ce1

Browse files
committed
Fix windows issue
1 parent 2de26ef commit d174ce1

File tree

1 file changed

+87
-22
lines changed

1 file changed

+87
-22
lines changed

tests/testthat/test-model-compile-user_header.R

Lines changed: 87 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ skip_if(os_is_macos())
22

33
file_that_exists <- "placeholder_exists"
44
file_that_doesnt_exist <- "placeholder_doesnt_exist"
5-
file.create(file_that_exists)
6-
withr::defer(
7-
if (file.exists(file_that_exists)) file.remove(file_that_exists),
8-
teardown_env()
9-
)
5+
withr::local_file(file_that_exists)
6+
7+
w_path <- function(f) {
8+
x <- sapply(f, function(fi) wsl_safe_path(absolute_path(fi)))
9+
names(x) <- NULL
10+
x
11+
}
1012

1113
make_local_orig <- cmdstan_make_local()
1214
cmdstan_make_local(cpp_options = list("PRECOMPILED_HEADERS" = "false"))
@@ -34,8 +36,7 @@ namespace bernoulli_external_model_namespace
3436
}"
3537

3638
test_that("cmdstan_model works with user_header with mock", {
37-
tmpfile <- tempfile(fileext = ".hpp")
38-
cat(hpp, file = tmpfile, sep = "\n")
39+
tmpfile <- withr::local_tempfile(lines = hpp, fileext = ".hpp")
3940

4041
with_mocked_cli(
4142
compile_ret = list(status = 0),
@@ -138,12 +139,76 @@ test_that("cmdstan_model works with user_header with mock", {
138139
)
139140
})
140141

141-
test_that("user_header precedence order is correct", {
142+
test_that("wsl path conversion is done as expected", {
143+
tmp_file <- withr::local_tempfile(lines = hpp, fileext = ".hpp")
144+
# Case 1: arg
145+
with_mocked_cli(
146+
compile_ret = list(status = 1),
147+
info_ret = list(),
148+
code = {
149+
mod <- cmdstan_model(
150+
stan_file = testing_stan_file("bernoulli_external"),
151+
user_header = tmp_file,
152+
dry_run = TRUE
153+
)
154+
}
155+
)
142156

143-
tmp_files <- sapply(1:3, function(n) wsl_safe_path(absolute_path(tempfile(fileext = ".hpp"))))
144-
sapply(tmp_files, function(filename) cat(hpp, file = filename, sep = "\n"))
145-
withr::defer(lapply(tmp_files, function(filename) file.remove(filename)))
157+
# USER_HEADER is converted
158+
# user_header is NULL
159+
expect_equal(mod$cpp_options()[['USER_HEADER']], w_path(tmp_file))
160+
expect_true(is.null(mod$cpp_options()[['user_header']]))
146161

162+
# Case 2: cpp opt USER_HEADER
163+
with_mocked_cli(
164+
compile_ret = list(status = 1),
165+
info_ret = list(),
166+
code = {
167+
mod <- cmdstan_model(
168+
stan_file = testing_stan_file("bernoulli_external"),
169+
cpp_options = list(
170+
USER_HEADER = tmp_file
171+
),
172+
dry_run = TRUE
173+
)
174+
}
175+
)
176+
177+
# USER_HEADER is converted
178+
# user_header is unconverted
179+
expect_equal(mod$cpp_options()[['USER_HEADER']], w_path(tmp_file))
180+
expect_true(is.null(mod$cpp_options()[['user_header']]))
181+
182+
# Case # 3: only user_header opt
183+
with_mocked_cli(
184+
compile_ret = list(status = 1),
185+
info_ret = list(),
186+
code = {
187+
mod <- cmdstan_model(
188+
stan_file = testing_stan_file("bernoulli_external"),
189+
cpp_options = list(
190+
user_header = tmp_file
191+
),
192+
dry_run = TRUE
193+
)
194+
}
195+
)
196+
197+
198+
# In other cases, in the *output* USER_HEADER is windows style user_header is not.
199+
# In this case, USER_HEADER is null.
200+
expect_true(is.null(mod$cpp_options()[['USER_HEADER']]))
201+
expect_equal(mod$cpp_options()[['user_header']], w_path(tmp_file))
202+
})
203+
204+
test_that("user_header precedence order is correct", {
205+
tmp_files <- sapply(1:3, function(n) withr::local_tempfile(
206+
lines = hpp,
207+
fileext = ".hpp",
208+
.local_envir = parent.frame(3)
209+
))
210+
211+
# Case # 1: all 3 specified
147212
with_mocked_cli(
148213
compile_ret = list(status = 1),
149214
info_ret = list(),
@@ -159,20 +224,20 @@ test_that("user_header precedence order is correct", {
159224
)
160225
}, "User header specified both")
161226
)
162-
163227
# In this case:
164228
# cpp_options[['USER_HEADER']] == tmp_files[1] <- actually used
165229
# cpp_options[['user_header']] == tmp_files[3] <- ignored
166230
# tmp_files[2] is not stored
167231
expect_equal(
168-
which(mod$cpp_options()[['USER_HEADER']] == tmp_files),
232+
match(!!(mod$cpp_options()[['USER_HEADER']]), w_path(tmp_files)),
169233
1
170234
)
171235
expect_equal(
172-
which(mod$cpp_options()[['user_header']] == tmp_files),
236+
match(!!(mod$cpp_options()[['user_header']]), tmp_files),
173237
3
174238
)
175239

240+
# Case # 2: Both opts, but no arg
176241
with_mocked_cli(
177242
compile_ret = list(status = 1),
178243
info_ret = list(),
@@ -188,18 +253,19 @@ test_that("user_header precedence order is correct", {
188253
}, "User header specified both")
189254
)
190255
# In this case:
191-
# cpp_options[['USER_HEADER']] == tmp_files[2] <- actually used
192-
# cpp_options[['user_header']] == tmp_files[3] <- ignored
256+
# cpp_options[['USER_HEADER']] == tmp_files[2]
257+
# cpp_options[['user_header']] == tmp_files[3]
193258
# tmp_files[2] is not stored
194259
expect_equal(
195-
which(mod$cpp_options()[["USER_HEADER"]] == tmp_files),
260+
match(!!(mod$cpp_options()[['USER_HEADER']]), w_path(tmp_files)),
196261
2
197262
)
198263
expect_equal(
199-
which(mod$cpp_options()[["user_header"]] == tmp_files),
264+
match(!!(mod$cpp_options()[['user_header']]), tmp_files),
200265
3
201266
)
202267

268+
# Case # 3: Both opts, other order
203269
with_mocked_cli(
204270
compile_ret = list(status = 1),
205271
info_ret = list(),
@@ -214,14 +280,13 @@ test_that("user_header precedence order is correct", {
214280
)
215281
}, "User header specified both")
216282
)
217-
# Same as above
283+
# Same as Case #2
218284
expect_equal(
219-
which(mod$cpp_options()[["USER_HEADER"]] == tmp_files),
285+
match(!!(mod$cpp_options()[['USER_HEADER']]), w_path(tmp_files)),
220286
2
221287
)
222288
expect_equal(
223-
which(mod$cpp_options()[["user_header"]] == tmp_files),
289+
match(!!(mod$cpp_options()[['user_header']]), tmp_files),
224290
3
225291
)
226-
227292
})

0 commit comments

Comments
 (0)