Skip to content

Commit d97f840

Browse files
committed
test: testing functions
1 parent 756501a commit d97f840

File tree

8 files changed

+158
-8
lines changed

8 files changed

+158
-8
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: pw
22
Title: Test your 'golem' Apps with 'Playwright'
3-
Version: 0.0.0.9004
3+
Version: 0.0.0.9005
44
Authors@R:
55
person("Colin", "Fay", , "contact@colinfay.me", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0001-7343-1846"))
@@ -16,6 +16,6 @@ Imports:
1616
Encoding: UTF-8
1717
Roxygen: list(markdown = TRUE)
1818
RoxygenNote: 7.3.2
19-
Suggests:
19+
Suggests:
2020
testthat (>= 3.0.0)
2121
Config/testthat/edition: 3

R/npx-is-available.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# For mocking purpose
2+
sys_which <- Sys.which
3+
cli__abort <- cli::cli_abort
14
#' Check if 'npx' is available in the system PATH
25
#'
36
#' @return TRUE if 'npx' is available, FALSE otherwise
@@ -14,7 +17,7 @@ npx_is_available <- function() {
1417
#' @rdname npx-is-available
1518
stop_if_npx_not_available <- function() {
1619
if (!npx_is_available()) {
17-
cli_abort(
20+
cli__abort(
1821
"npx is not installed. Please install Node.js to use Playwright."
1922
)
2023
}

R/pw-init.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ pw_init <- function(
2525
with_dir(
2626
where,
2727
{
28-
if (!file.exists(here("app.R"))) {
29-
add_positconnect_file(open = FALSE)
28+
if (!file.exists(fs::path(where, "app.R"))) {
29+
add_positconnect_file(pkg = where, open = FALSE)
3030
}
3131
if (!dir_exists("tests")) {
32-
cli_abort(
32+
cli__abort(
3333
"No tests folder found. Please run `usethis::use_testthat()` first."
3434
)
3535
}

R/utils.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ stop_if_playwright_skeleton_not_present <- function(
77
where = golem::get_golem_wd()
88
) {
99
error_msg <- "Playwright skeleton not found. Please run `pw::pw_init()` first."
10-
if (!fs::dir_exists(path(where, "tests", "playwright"))) {
10+
if (!fs::dir_exists(fs::path(where, "tests", "playwright"))) {
1111
cli_abort(
1212
error_msg
1313
)
1414
}
15-
if (!file.exists(path(where, "tests", "playwright", "playwright.config.ts"))) {
15+
if (!file.exists(fs::path(where, "tests", "playwright", "playwright.config.ts"))) {
1616
cli_abort(
1717
error_msg
1818
)

tests/testthat.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is part of the standard setup for testthat.
2+
# It is recommended that you do not modify it.
3+
#
4+
# Where should you do additional test configuration?
5+
# Learn more about the roles of various files in:
6+
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
7+
# * https://testthat.r-lib.org/articles/special-files.html
8+
9+
library(testthat)
10+
library(pw)
11+
12+
test_check("pw")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
test_that("npx_is_available works as expected", {
2+
testthat::with_mocked_bindings(
3+
sys_which = function(...) {
4+
return("npx")
5+
},
6+
expect_true(npx_is_available())
7+
)
8+
testthat::with_mocked_bindings(
9+
npx_is_available = function(...) {
10+
return(FALSE)
11+
},
12+
cli__abort = function(...){
13+
stop()
14+
},
15+
{
16+
expect_false(npx_is_available())
17+
expect_error(stop_if_npx_not_available())
18+
}
19+
)
20+
})

tests/testthat/test-pw-init-test.R

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
test_that("multiplication works", {
2+
skip_if_not(
3+
npx_is_available()
4+
)
5+
temp_golem <- file.path(tempdir(), "golem")
6+
on.exit({
7+
unlink(temp_golem, recursive = TRUE)
8+
})
9+
options("usethis.quiet" = TRUE)
10+
res <- golem::create_golem(
11+
path = temp_golem,
12+
package_name = "pwtest",
13+
open = FALSE,
14+
overwrite = TRUE
15+
)
16+
17+
testthat::with_mocked_bindings(
18+
cli__abort = function(...) {
19+
stop()
20+
},
21+
{
22+
expect_error(
23+
pw_init(where = temp_golem)
24+
)
25+
}
26+
)
27+
dir.create(
28+
file.path(
29+
temp_golem,
30+
"tests/testthat"
31+
),
32+
recursive = TRUE
33+
)
34+
pw_init(temp_golem, "--quiet")
35+
expect_true(
36+
file.exists(
37+
file.path(
38+
temp_golem,
39+
"tests/playwright"
40+
)
41+
)
42+
)
43+
expect_true(
44+
file.exists(
45+
file.path(
46+
temp_golem,
47+
"tests/playwright/playwright.config.ts"
48+
)
49+
)
50+
)
51+
expect_true(
52+
file.exists(
53+
file.path(
54+
temp_golem,
55+
"tests/playwright/tests/default.test.ts"
56+
)
57+
)
58+
)
59+
expect_true(
60+
file.exists(
61+
file.path(
62+
temp_golem,
63+
"tests/testthat/test-playwright.R"
64+
)
65+
)
66+
)
67+
withr::with_dir(
68+
temp_golem,
69+
{
70+
expect_silent(
71+
pw_test(where = ".")
72+
)
73+
}
74+
)
75+
})

tests/testthat/test-utils.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
test_that("pw_sys_files returns the correct paths", {
2+
result <- pw_sys_files("DESCRIPTION")
3+
expect_true(file.exists(result), info = "The DESCRIPTION file should exist in the package directory.")
4+
5+
result <- pw_sys_files("non_existent_file.txt")
6+
expect_equal(result, "", info = "A non-existent file should return an empty string.")
7+
8+
result <- pw_sys_files()
9+
expect_true(dir.exists(result), info = "Calling without arguments should return the root of the package.")
10+
})
11+
12+
test_that("stop_if_playwright_skeleton_not_present works as expected", {
13+
mock_golem_wd <- tempdir()
14+
on.exit({
15+
unlink(file.path(mock_golem_wd, "tests", "playwright"), recursive = TRUE)
16+
})
17+
dir.create(file.path(mock_golem_wd, "tests", "playwright"), recursive = TRUE)
18+
file.create(file.path(mock_golem_wd, "tests", "playwright", "playwright.config.ts"))
19+
20+
# Test when the Playwright skeleton is present
21+
expect_silent(stop_if_playwright_skeleton_not_present(where = mock_golem_wd))
22+
23+
# Test when the Playwright skeleton directory is missing
24+
unlink(file.path(mock_golem_wd, "tests", "playwright"), recursive = TRUE)
25+
expect_error(
26+
stop_if_playwright_skeleton_not_present(where = mock_golem_wd),
27+
"Playwright skeleton not found. Please run `pw::pw_init()` first.",
28+
fixed = TRUE
29+
)
30+
31+
# Test when the Playwright configuration file is missing
32+
dir.create(file.path(mock_golem_wd, "tests", "playwright"), recursive = TRUE)
33+
unlink(file.path(mock_golem_wd, "tests", "playwright", "playwright.config.ts"))
34+
expect_error(
35+
stop_if_playwright_skeleton_not_present(where = mock_golem_wd),
36+
"Playwright skeleton not found. Please run `pw::pw_init()` first.",
37+
fixed = TRUE
38+
)
39+
})
40+

0 commit comments

Comments
 (0)