@@ -2,11 +2,13 @@ skip_if(os_is_macos())
2
2
3
3
file_that_exists <- " placeholder_exists"
4
4
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
+ }
10
12
11
13
make_local_orig <- cmdstan_make_local()
12
14
cmdstan_make_local(cpp_options = list (" PRECOMPILED_HEADERS" = " false" ))
@@ -34,8 +36,7 @@ namespace bernoulli_external_model_namespace
34
36
}"
35
37
36
38
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" )
39
40
40
41
with_mocked_cli(
41
42
compile_ret = list (status = 0 ),
@@ -138,12 +139,76 @@ test_that("cmdstan_model works with user_header with mock", {
138
139
)
139
140
})
140
141
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
+ )
142
156
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' ]]))
146
161
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
147
212
with_mocked_cli(
148
213
compile_ret = list (status = 1 ),
149
214
info_ret = list (),
@@ -159,20 +224,20 @@ test_that("user_header precedence order is correct", {
159
224
)
160
225
}, " User header specified both" )
161
226
)
162
-
163
227
# In this case:
164
228
# cpp_options[['USER_HEADER']] == tmp_files[1] <- actually used
165
229
# cpp_options[['user_header']] == tmp_files[3] <- ignored
166
230
# tmp_files[2] is not stored
167
231
expect_equal(
168
- which( mod $ cpp_options()[[' USER_HEADER' ]] == tmp_files ),
232
+ match( !! ( mod $ cpp_options()[[' USER_HEADER' ]]), w_path( tmp_files ) ),
169
233
1
170
234
)
171
235
expect_equal(
172
- which( mod $ cpp_options()[[' user_header' ]] == tmp_files ),
236
+ match( !! ( mod $ cpp_options()[[' user_header' ]]), tmp_files ),
173
237
3
174
238
)
175
239
240
+ # Case # 2: Both opts, but no arg
176
241
with_mocked_cli(
177
242
compile_ret = list (status = 1 ),
178
243
info_ret = list (),
@@ -188,18 +253,19 @@ test_that("user_header precedence order is correct", {
188
253
}, " User header specified both" )
189
254
)
190
255
# 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]
193
258
# tmp_files[2] is not stored
194
259
expect_equal(
195
- which( mod $ cpp_options()[[" USER_HEADER" ]] == tmp_files ),
260
+ match( !! ( mod $ cpp_options()[[' USER_HEADER' ]]), w_path( tmp_files ) ),
196
261
2
197
262
)
198
263
expect_equal(
199
- which( mod $ cpp_options()[[" user_header" ]] == tmp_files ),
264
+ match( !! ( mod $ cpp_options()[[' user_header' ]]), tmp_files ),
200
265
3
201
266
)
202
267
268
+ # Case # 3: Both opts, other order
203
269
with_mocked_cli(
204
270
compile_ret = list (status = 1 ),
205
271
info_ret = list (),
@@ -214,14 +280,13 @@ test_that("user_header precedence order is correct", {
214
280
)
215
281
}, " User header specified both" )
216
282
)
217
- # Same as above
283
+ # Same as Case #2
218
284
expect_equal(
219
- which( mod $ cpp_options()[[" USER_HEADER" ]] == tmp_files ),
285
+ match( !! ( mod $ cpp_options()[[' USER_HEADER' ]]), w_path( tmp_files ) ),
220
286
2
221
287
)
222
288
expect_equal(
223
- which( mod $ cpp_options()[[" user_header" ]] == tmp_files ),
289
+ match( !! ( mod $ cpp_options()[[' user_header' ]]), tmp_files ),
224
290
3
225
291
)
226
-
227
292
})
0 commit comments