Skip to content

Commit 33eb4df

Browse files
committed
feat: use_workflow_version_update and relevant test
1 parent f4fe160 commit 33eb4df

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

tests/testthat/test-create_colorful_cli_env.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ test_that("create_colorful_cli_env with empty character vector returns empty env
3030
})
3131

3232
test_that("create_colorful_cli_env returned functions are callable", {
33-
env <- rpkgkit:::create_colorful_cli_env(c("cli_alert_info", "cli_alert_success"))
33+
env <- rpkgkit:::create_colorful_cli_env(c(
34+
"cli_alert_info",
35+
"cli_alert_success"
36+
))
3437

3538
expect_type(env$cli_alert_info, "closure")
3639
expect_type(env$cli_alert_success, "closure")
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
test_that("aborts when path is not a package root", {
2+
tmp <- tempfile("not_pkg")
3+
dir.create(tmp)
4+
on.exit(unlink(tmp, recursive = TRUE))
5+
6+
expect_error(
7+
use_workflow_version_update(path = tmp),
8+
"not an R package root"
9+
)
10+
})
11+
12+
test_that("aborts when workflow file already exists and overwrite = FALSE", {
13+
tmp <- tempfile("pkg")
14+
dir.create(tmp)
15+
on.exit(unlink(tmp, recursive = TRUE))
16+
file.create(file.path(tmp, "DESCRIPTION"))
17+
18+
workflow_file <- file.path(tmp, ".github", "workflows", "version_update.yml")
19+
dir.create(dirname(workflow_file), recursive = TRUE)
20+
file.create(workflow_file)
21+
22+
expect_error(
23+
use_workflow_version_update(path = tmp, overwrite = FALSE),
24+
"already exists"
25+
)
26+
})
27+
28+
test_that("creates workflow directory and copies template", {
29+
tmp <- tempfile("pkg")
30+
dir.create(tmp)
31+
on.exit(unlink(tmp, recursive = TRUE))
32+
file.create(file.path(tmp, "DESCRIPTION"))
33+
34+
result <- use_workflow_version_update(path = tmp)
35+
36+
expected_path <- file.path(tmp, ".github", "workflows", "version_update.yml")
37+
expect_equal(result, expected_path)
38+
expect_true(file.exists(expected_path))
39+
40+
lines <- readLines(expected_path, warn = FALSE)
41+
expect_match(lines[1L], "rpkgkit::use_workflow_version_update", fixed = TRUE)
42+
expect_match(lines[2L], "name: Update R Package Version", fixed = TRUE)
43+
})
44+
45+
test_that("returns invisible file path", {
46+
tmp <- tempfile("pkg")
47+
dir.create(tmp)
48+
on.exit(unlink(tmp, recursive = TRUE))
49+
file.create(file.path(tmp, "DESCRIPTION"))
50+
51+
result <- withVisible(use_workflow_version_update(path = tmp))
52+
53+
expect_true(
54+
result$value == file.path(tmp, ".github", "workflows", "version_update.yml")
55+
)
56+
expect_false(result$visible)
57+
})
58+
59+
test_that("overwrite = TRUE replaces existing workflow file", {
60+
tmp <- tempfile("pkg")
61+
dir.create(tmp)
62+
on.exit(unlink(tmp, recursive = TRUE))
63+
file.create(file.path(tmp, "DESCRIPTION"))
64+
65+
workflow_file <- file.path(tmp, ".github", "workflows", "version_update.yml")
66+
dir.create(dirname(workflow_file), recursive = TRUE)
67+
writeLines(c("# old content"), workflow_file)
68+
69+
result <- use_workflow_version_update(path = tmp, overwrite = TRUE)
70+
71+
lines <- readLines(workflow_file, warn = FALSE)
72+
expect_match(lines[1L], "rpkgkit::use_workflow_version_update", fixed = TRUE)
73+
expect_equal(result, workflow_file)
74+
})
75+
76+
test_that("uses get_wd() when path is NULL", {
77+
tmp <- tempfile("pkg")
78+
dir.create(tmp)
79+
on.exit(unlink(tmp, recursive = TRUE))
80+
file.create(file.path(tmp, "DESCRIPTION"))
81+
82+
local_mocked_bindings(
83+
get_wd = function() tmp,
84+
.package = "rpkgkit"
85+
)
86+
87+
result <- use_workflow_version_update(path = NULL)
88+
expect_equal(
89+
result,
90+
file.path(tmp, ".github", "workflows", "version_update.yml")
91+
)
92+
expect_true(file.exists(result))
93+
})

0 commit comments

Comments
 (0)