-
Notifications
You must be signed in to change notification settings - Fork 10
/
test-inflate-part1.R
567 lines (508 loc) · 19.1 KB
/
test-inflate-part1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
print("==== inflate-part1 ====")
# Test full ----
dummypackage <- tempfile("inflate.tests")
dir.create(dummypackage)
# {fusen} steps
fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package"))
dev_file <- suppressMessages(add_flat_template(pkg = dummypackage, overwrite = TRUE, open = FALSE))
flat_file <- dev_file[grepl("flat_", dev_file)]
usethis::with_project(dummypackage, {
# More complicated example for tests
testfile <- "tests-templates/dev-template-tests.Rmd"
file.copy(
system.file(testfile, package = "fusen"),
flat_file,
overwrite = TRUE
)
usethis::use_mit_license("Statnmap")
suppressMessages(
inflate(
pkg = dummypackage, flat_file = flat_file,
vignette_name = "Get started", check = FALSE,
open_vignette = FALSE
)
)
test_that("inflate() worked correctly", {
# Number of files
expect_equal(length(list.files(file.path(dummypackage, "R"))), 11)
expect_equal(length(list.files(file.path(dummypackage, "vignettes"))), 1)
expect_equal(length(list.files(file.path(dummypackage, "tests", "testthat"))), 4)
# R files
my_median_file <- file.path(dummypackage, "R", "my_median.R")
expect_true(file.exists(my_median_file))
my_other_median_file <- file.path(dummypackage, "R", "my_other_median.R")
expect_true(file.exists(my_other_median_file))
my_third_median_file <- file.path(dummypackage, "R", "my_third_median.R")
expect_true(file.exists(my_third_median_file))
my_sixth_median_file <- file.path(dummypackage, "R", "my-sixth-median_function.R")
expect_true(file.exists(my_sixth_median_file))
myuppercasefunctionfile <- file.path(dummypackage, "R", "myuppercasefunction.R")
expect_true(file.exists(myuppercasefunctionfile))
# Found with chunk named `fun`
my_noroxfunctionfile <- file.path(dummypackage, "R", "my_norox.R")
expect_true(file.exists(my_noroxfunctionfile))
# Found with chunk named `fun-norox2`
my_norox2functionfile <- file.path(dummypackage, "R", "my_norox2.R")
expect_true(file.exists(my_norox2functionfile))
# Found with chunk named `fun_space`
my_spacefunctionfile <- file.path(dummypackage, "R", "my_space.R")
expect_true(file.exists(my_spacefunctionfile))
my_space2functionfile <- file.path(dummypackage, "R", "my_space2.R")
expect_true(file.exists(my_space2functionfile))
# examples in R files
my_median_lines <- readLines(my_median_file)
expect_equal(my_median_lines[1], "# WARNING - Generated by {fusen} from /dev/flat_full.Rmd: do not edit by hand")
expect_true(all(my_median_lines[12:14] == c(
"#' @examples", "#' my_median(2:20)", "#' my_median(1:12)"
)))
my_other_median_lines <- readLines(my_other_median_file)
expect_equal(my_other_median_lines[1], "# WARNING - Generated by {fusen} from /dev/flat_full.Rmd: do not edit by hand")
expect_true(all(my_other_median_lines[12:15] == c(
"#' @examples", "#' my_other_median(1:12)",
"#' my_other_median(8:20)", "#' my_other_median(20:50)"
)))
my_third_median_lines <- readLines(my_third_median_file)
# _no example
expect_true(all(!grepl("#' @examples", my_third_median_lines)))
# dot in name
my_sixth_median_lines <- readLines(my_sixth_median_file)
expect_true(all(my_sixth_median_lines[11:13] == c(
"#' @examples", "#' my.sixth.median_function(1:12)", "#' my.sixth.median_function(8:20)"
)))
# _no roxygen at all
my_norox_lines <- readLines(my_noroxfunctionfile)
expect_true(all(my_norox_lines == c(
"# WARNING - Generated by {fusen} from /dev/flat_full.Rmd: do not edit by hand", "",
"#' @noRd", "my_norox <- function(x) {", " x + 10", "}"
)))
# _no roxygen but examples
my_norox2_lines <- readLines(my_norox2functionfile)
expect_equal(my_norox2_lines, c(
"# WARNING - Generated by {fusen} from /dev/flat_full.Rmd: do not edit by hand", "",
"#' @noRd", "#' @examples",
"#' \\dontrun{", "#' # comment",
"#' my_norox2(10)", "#' }",
"#' ",
"#' \\dontrun{",
"#' # comment",
"#' my_norox2(12)", "#' }",
"my_norox2 <- function(x) {", " x + 10", "}"
))
# _extra empty line and examples
my_space_lines <- readLines(my_spacefunctionfile)
expect_true(all(my_space_lines[8:12] == c(
"#' @examples", "#' my_space(10)", "#' @export", "", "my_space <- function(x) {"
)))
# _extra empty line and noRd
my_space2_lines <- readLines(my_space2functionfile)
expect_true(all(my_space2_lines[8:10] == c(
"#' @noRd", "", "my_space2 <- function(x) {"
)))
# vignette
the_vignette <- file.path(dummypackage, "vignettes", "get-started.Rmd")
expect_true(file.exists(the_vignette))
vignette_lines <- readLines(the_vignette)
# Do not edit by hand after yaml
pos.dash <- grep("---", vignette_lines)
donotedit <- grep(
"<!-- WARNING - This vignette is generated by \\{fusen\\} from /dev/flat_full.Rmd: do not edit by hand -->",
vignette_lines
)
expect_equal(length(donotedit), 1)
expect_true(pos.dash[2] < donotedit)
# No dev chunks in the vignette
expect_false(any(grepl("```{r dev}", vignette_lines, fixed = TRUE)))
expect_false(any(grepl("```{r development-1", vignette_lines, fixed = TRUE)))
# tests
test_file <- file.path(dummypackage, "tests", "testthat", "test-my_median.R")
expect_true(file.exists(test_file))
test_lines <- readLines(test_file)
expect_equal(
test_lines,
c(
"# WARNING - Generated by {fusen} from /dev/flat_full.Rmd: do not edit by hand",
"",
"test_that(\"my_median works properly and show error if needed\", {",
" expect_error(my_median(\"text\"))",
"})"
)
)
expect_true(file.exists(
file.path(dummypackage, "tests", "testthat", "test-my_other_median.R")
))
expect_true(file.exists(
file.path(dummypackage, "tests", "testthat", "test-my-sixth-median_function.R")
))
expect_true(file.exists(
file.path(dummypackage, "tests", "testthat", "test-myuppercasefunction.R")
))
# no test
expect_false(file.exists(
file.path(dummypackage, "tests", "testthat", "test-my_norox.R")
))
expect_false(file.exists(
file.path(dummypackage, "tests", "testthat", "test-my_space2.R")
))
# Namespace
expect_true(file.exists(file.path(dummypackage, "NAMESPACE")))
})
})
# Test package no check errors ----
usethis::with_project(dummypackage, {
test_that("inflate() output error", {
# Do not check inside check if on CRAN
skip_on_os(os = c("windows", "solaris"))
# If this check is run inside a not "--as-cran" check, then it wont work as expected
check_out <- rcmdcheck::rcmdcheck(dummypackage,
quiet = TRUE,
args = c("--no-manual")
)
# No errors
expect_true(length(check_out[["errors"]]) == 0)
# 1 warning = License => Now a note...
# expect_true(length(check_out[["warnings"]]) == 1)
# expect_true(grepl("license", check_out[["warnings"]][1]))
# No Notes or only one if CRAN ?
skip_on_cran()
expect_true(length(check_out[["notes"]]) <= 1)
if (length(check_out[["notes"]]) == 1) {
# if tested as cran
# 1 note on CRAN for new submission
print(check_out[["notes"]])
expect_true(grepl("New submission", check_out[["notes"]][1]))
} else {
expect_true(length(check_out[["notes"]]) == 0)
}
})
})
# Clean R, tests and vignettes
unlink(file.path(dummypackage, "R"), recursive = TRUE)
unlink(file.path(dummypackage, "vignettes"), recursive = TRUE)
unlink(file.path(dummypackage, "tests"), recursive = TRUE)
# Test no problem with special character in YAML ----
if (packageVersion("parsermd") > "0.1.2") {
dummypackage.special <- tempfile("dummypackage.special")
dir.create(dummypackage.special)
# {fusen} steps
fill_description(pkg = dummypackage.special, fields = list(Title = "Dummy Package"))
dev_file <- add_flat_template(pkg = dummypackage.special, overwrite = TRUE, open = FALSE)
flat_file <- dev_file[grepl("flat_", dev_file)]
usethis::with_project(dummypackage.special, {
testfile <- "tests-templates/dev-template-tests-special-char.Rmd"
file.copy(
system.file(testfile, package = "fusen"),
flat_file,
overwrite = TRUE
)
suppressMessages(
inflate(
pkg = dummypackage.special, flat_file = flat_file,
vignette_name = "Get started", check = FALSE,
open_vignette = FALSE
)
)
test_that("inflate with special yaml worked correctly", {
# R files
my_median_file <- file.path(dummypackage.special, "R", "my_median.R")
expect_true(file.exists(my_median_file))
})
})
}
# Test no attachment and no check when asked ----
unlink(file.path(dummypackage, "DESCRIPTION"), recursive = TRUE)
fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package"))
dev_file <- add_flat_template(pkg = dummypackage, overwrite = TRUE, open = FALSE)
flat_file <- dev_file[grepl("flat_", dev_file)]
usethis::with_project(dummypackage, {
suppressMessages(
inflate(
pkg = dummypackage, flat_file = flat_file, vignette_name = "Get started",
check = FALSE, document = FALSE, open_vignette = FALSE
)
)
desc_lines <- readLines(file.path(dummypackage, "DESCRIPTION"))
test_that("no attachment run", {
expect_false("Imports:" %in% desc_lines)
})
# Clean R, tests and vignettes
unlink(file.path(dummypackage, "R"), recursive = TRUE)
unlink(file.path(dummypackage, "vignettes"), recursive = TRUE)
unlink(file.path(dummypackage, "tests"), recursive = TRUE)
})
# Tests no errors - no example, no tests ----
usethis::with_project(dummypackage, {
file.copy(
system.file("tests-templates/dev-template-no-example-no-tests.Rmd", package = "fusen"),
flat_file,
overwrite = TRUE
)
suppressMessages(
inflate(
pkg = dummypackage, flat_file = flat_file,
vignette_name = "Get started", check = FALSE,
open_vignette = FALSE
)
)
test_that("inflate() output no error", {
expect_true(file.exists(file.path(dummypackage, "vignettes", "get-started.Rmd")))
expect_true(file.exists(file.path(dummypackage, "R", "my_median.R")))
expect_true(!file.exists(file.path(dummypackage, "tests", "testthat", "test-my_median.R")))
})
# Clean R, tests and vignettes
unlink(file.path(dummypackage, "R"), recursive = TRUE)
unlink(file.path(dummypackage, "vignettes"), recursive = TRUE)
unlink(file.path(dummypackage, "tests"), recursive = TRUE)
})
# Tests no errors - empty ----
usethis::with_project(dummypackage, {
file.copy(
system.file("tests-templates/dev-template-test-parse-nothing.Rmd", package = "fusen"),
flat_file,
overwrite = TRUE
)
test_that("inflate() output message", {
suppressMessages(
expect_message(
inflate(
pkg = dummypackage, flat_file = flat_file,
vignette_name = "Get started", check = FALSE,
open_vignette = FALSE
)
)
)
})
# Clean R, tests and vignettes
unlink(file.path(dummypackage, "R"), recursive = TRUE)
unlink(file.path(dummypackage, "vignettes"), recursive = TRUE)
unlink(file.path(dummypackage, "tests"), recursive = TRUE)
})
# Tests errors - vignette already exists ----
usethis::with_project(dummypackage, {
suppressMessages(
inflate(
pkg = dummypackage, flat_file = flat_file,
vignette_name = "Get started",
check = FALSE, overwrite = "yes",
open_vignette = FALSE
)
)
test_that("inflate() output error when second time (not interactive)", {
expect_error(
suppressMessages(
inflate(
pkg = dummypackage, flat_file = flat_file,
vignette_name = "Get started",
check = FALSE,
open_vignette = FALSE
)
)
)
expect_error(
suppressMessages(
inflate(
pkg = dummypackage, flat_file = flat_file,
vignette_name = "Get started",
check = FALSE, overwrite = "no",
open_vignette = FALSE
)
)
)
})
# No error with overwrite = 'yes'
suppressMessages(
inflate(
pkg = dummypackage, flat_file = flat_file, vignette_name = "Get started",
check = FALSE, overwrite = "yes", open_vignette = FALSE
)
)
test_that("inflate() output no error", {
expect_true(file.exists(file.path(dummypackage, "vignettes", "get-started.Rmd")))
})
# Clean R, tests and vignettes
unlink(file.path(dummypackage, "R"), recursive = TRUE)
unlink(file.path(dummypackage, "vignettes"), recursive = TRUE)
unlink(file.path(dummypackage, "tests"), recursive = TRUE)
})
# Tests errors - duplicate functions ----
usethis::with_project(dummypackage, {
file.copy(
system.file("tests-templates/dev-template-stop-duplicate-fun.Rmd", package = "fusen"),
flat_file,
overwrite = TRUE
)
test_that("inflate() output error duplicate functions", {
expect_error(
suppressMessages(
inflate(
pkg = dummypackage, flat_file = flat_file,
vignette_name = "Get started", check = FALSE,
open_vignette = FALSE
)
)
)
})
# Clean R, tests and vignettes
unlink(file.path(dummypackage, "R"), recursive = TRUE)
unlink(file.path(dummypackage, "vignettes"), recursive = TRUE)
unlink(file.path(dummypackage, "tests"), recursive = TRUE)
# Tests errors - duplicate chunk names
file.copy(
system.file("tests-templates/dev-template-stop-duplicate-label.Rmd", package = "fusen"),
flat_file,
overwrite = TRUE
)
test_that("inflate() output error duplicate label names for vignette", {
expect_error(
suppressMessages(
inflate(
pkg = dummypackage, flat_file = flat_file,
vignette_name = "Get started", check = FALSE,
open_vignette = FALSE
)
)
)
})
# Clean R, tests and vignettes
unlink(file.path(dummypackage, "R"), recursive = TRUE)
unlink(file.path(dummypackage, "vignettes"), recursive = TRUE)
unlink(file.path(dummypackage, "tests"), recursive = TRUE)
})
# Test no errors - inflate with .Rproj and no .here ----
usethis::with_project(dummypackage, {
file.remove(file.path(dummypackage, ".here"))
file.remove(file.path(dummypackage, ".Rbuildignore"))
cat("", file = file.path(dummypackage, "dummy.Rproj"))
# Add
# {fusen} steps
dev_file <- add_flat_template(pkg = dummypackage, overwrite = TRUE, open = FALSE)
suppressMessages(
inflate(
pkg = dummypackage, flat_file = flat_file,
vignette_name = "Get started", check = FALSE,
open_vignette = FALSE
)
)
test_that("add_flat_template inflates with .Rproj and no .here", {
expect_true(file.exists(flat_file))
expect_false(file.exists(file.path(dummypackage, ".here")))
rbuildignore_file <- file.path(dummypackage, ".Rbuildignore")
expect_true(file.exists(rbuildignore_file))
rbuildignore_lines <- readLines(rbuildignore_file)
expect_true(any(grepl("dev", rbuildignore_lines, fixed = TRUE)))
expect_false(any(grepl("[.]here", rbuildignore_lines)))
# R files
my_median_file <- file.path(dummypackage, "R", "my_median.R")
expect_true(file.exists(my_median_file))
# vignette
expect_true(file.exists(file.path(dummypackage, "vignettes", "get-started.Rmd")))
# tests
expect_true(file.exists(
file.path(dummypackage, "tests", "testthat", "test-my_median.R")
))
})
# Clean R, tests and vignettes
unlink(file.path(dummypackage, "R"), recursive = TRUE)
unlink(file.path(dummypackage, "vignettes"), recursive = TRUE)
unlink(file.path(dummypackage, "tests"), recursive = TRUE)
})
# Delete dummy package
unlink(dummypackage, recursive = TRUE)
# Test no errors - clean vignette_name for vignette ----
# Create a new project
dummypackage <- tempfile("clean.vignette")
dir.create(dummypackage)
# {fusen} steps
fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package"))
dev_file <- suppressMessages(add_flat_template(pkg = dummypackage, overwrite = TRUE, open = FALSE))
flat_file <- dev_file[grepl("flat_", dev_file)]
usethis::with_project(dummypackage, {
suppressMessages(
inflate(
pkg = dummypackage, flat_file = flat_file,
vignette_name = "# y _ p n@ \u00E9 ! 1", check = FALSE,
open_vignette = FALSE
)
)
# Vignette name is also cleaned by {usethis} for special characters
vignette_path <- file.path(dummypackage, "vignettes", "y-p-n-e-1.Rmd")
test_that("vignette is created with clean vignette_name", {
expect_true(file.exists(vignette_path))
# usethis::use_vignette writes in UTF-8
vig_lines <- readLines(vignette_path, encoding = "UTF-8")
expect_true(sum(grepl(enc2utf8("# y _ p n@ \u00E9 ! 1"), vig_lines, fixed = TRUE)) == 1)
expect_equal(vig_lines[2], enc2utf8('title: "# y _ p n@ \u00E9 ! 1"'))
expect_true(sum(grepl("y-p-n-e-1", vig_lines, fixed = TRUE)) == 1)
})
# Clean R, tests and vignettes
unlink(file.path(dummypackage, "R"), recursive = TRUE)
unlink(file.path(dummypackage, "vignettes"), recursive = TRUE)
unlink(file.path(dummypackage, "tests"), recursive = TRUE)
})
# Delete dummy package
unlink(dummypackage, recursive = TRUE)
# Test stop when no DESCRIPTION file ----
dummypackage <- tempfile("descpackage")
dir.create(dummypackage)
dev_file <- add_flat_template(pkg = dummypackage, overwrite = TRUE, open = FALSE)
flat_file <- dev_file[grepl("flat_", dev_file)]
usethis::with_project(dummypackage, {
test_that("stop when no DESCRIPTION file", {
expect_error(
suppressMessages(
inflate(
pkg = dummypackage, flat_file = flat_file,
check = FALSE, open_vignette = FALSE
)
),
"DESCRIPTION file"
)
})
})
# Delete dummy package
unlink(dummypackage, recursive = TRUE)
if (exists("dummypackage.special")) {
unlink(dummypackage.special, recursive = TRUE)
}
# Inflate fails or not if no flat_file ----
test_that("inflate fails without flat file", {
expect_error(inflate(overwrite = FALSE), regexp = "directly in the console")
})
dummypackage <- tempfile("flatfile.empty")
dir.create(dummypackage)
# {fusen} steps
fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package"))
dev_file <- suppressMessages(add_flat_template(pkg = dummypackage, overwrite = TRUE, open = FALSE))
flat_file <- dev_file[grepl("flat_", dev_file)]
# If interactive in RStudio
if (
requireNamespace("rstudioapi") &&
rstudioapi::isAvailable() &&
rstudioapi::hasFun("navigateToFile") &&
rstudioapi::hasFun("documentId")
) {
print("Test with RStudio")
# current position
curr_editor <- rstudioapi::getSourceEditorContext()
curr_position <- curr_editor$selection[[1L]]$range$start
# Change file
rstudioapi::navigateToFile(flat_file)
Sys.sleep(1)
id <- rstudioapi::documentId()
usethis::with_project(dummypackage, {
test_that("inflate works without flat file when current is Rmd", {
expect_message(
inflate(
pkg = dummypackage, # flat_file = flat_file,
vignette_name = "Get started", check = FALSE,
open_vignette = FALSE, overwrite = TRUE
),
regexp = "The current file will be inflated"
)
})
})
# # Back to current position
rstudioapi::navigateToFile(curr_editor$path, line = curr_position[1])
if (rstudioapi::hasFun("documentClose")) {
rstudioapi::documentClose(id)
}
}