Skip to content

Commit 9bb40e6

Browse files
authored
Merge pull request #7 from ThinkR-open/1-pw_init
feat: pw_install
2 parents 7cf111a + 5193e70 commit 9bb40e6

16 files changed

+394
-180
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.9009
3+
Version: 0.0.0.9011
44
Authors@R:
55
person("Colin", "Fay", , "contact@colinfay.me", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0001-7343-1846"))
@@ -21,7 +21,7 @@ Encoding: UTF-8
2121
Roxygen: list(markdown = TRUE)
2222
RoxygenNote: 7.3.2
2323
Suggests:
24-
future,
24+
callr,
2525
httpuv,
2626
testthat (>= 3.0.0),
2727
usethis

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
export(npx_is_available)
44
export(pw_codegen)
55
export(pw_init)
6+
export(pw_install)
67
export(pw_show_report)
78
export(pw_test)
89
export(stop_if_npx_not_available)

NEWS.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# pw (development version)
22

3-
* `with_test_id()` adds a `data-testid` attribute to a shiny tag. This is useful for testing with `getByTestId` in Playwright.
3+
- `pw_install()` run both `npm install` & `npx playwright install` in the test directory. This is mainly to be used when a package has been inited somewhere else.
44

5-
* `pw_show_report()` can be called to open the test report
5+
- `with_test_id()` adds a `data-testid` attribute to a shiny tag. This is useful for testing with `getByTestId` in Playwright.
66

7-
* `pw_codegen()` can be called to launch the code generator
7+
- `pw_show_report()` can be called to open the test report
88

9-
* `pw_test()` stops if the playwright skeleton is missing
9+
- `pw_codegen()` can be called to launch the code generator
1010

11-
* Functions to test if npx is in the PATH
11+
- `pw_test()` stops if the playwright skeleton is missing
1212

13-
* Initial version with `pw_init()` & `pw_test()`
13+
- Functions to test if npx is in the PATH
14+
15+
- Initial version with `pw_init()` & `pw_test()`

R/npx-is-available.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# For mocking purpose
2-
sys_which <- function(...){
2+
sys_which <- function(...) {
33
Sys.which(...)
44
}
5-
cli__abort <- function(...){
5+
cli__abort <- function(...) {
66
cli::cli_abort(...)
77
}
88
#' Check if 'npx' is available in the system PATH
@@ -25,4 +25,4 @@ stop_if_npx_not_available <- function() {
2525
"npx is not installed. Please install Node.js to use Playwright."
2626
)
2727
}
28-
}
28+
}

R/pw-codegen.R

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,31 @@ pw_codegen <- function(
2525
go_for_pkgload <- FALSE
2626
}
2727
if (is.null(R_path)) {
28-
if (tolower(.Platform$OS.type) == "windows") {
29-
r_ <- normalizePath(file.path(
30-
Sys.getenv("R_HOME"),
31-
"bin",
32-
"R.exe"
33-
))
28+
if (
29+
tolower(
30+
.Platform$OS.type
31+
) ==
32+
"windows"
33+
) {
34+
r_ <- normalizePath(
35+
file.path(
36+
Sys.getenv(
37+
"R_HOME"
38+
),
39+
"bin",
40+
"R.exe"
41+
)
42+
)
3443
} else {
35-
r_ <- normalizePath(file.path(
36-
Sys.getenv("R_HOME"),
37-
"bin",
38-
"R"
39-
))
44+
r_ <- normalizePath(
45+
file.path(
46+
Sys.getenv(
47+
"R_HOME"
48+
),
49+
"bin",
50+
"R"
51+
)
52+
)
4053
}
4154
} else {
4255
r_ <- R_path
@@ -67,50 +80,52 @@ pw_codegen <- function(
6780
}
6881

6982
if (!shinyproc$is_alive()) {
70-
cli::cli_alert_danger("Failed to start the app, please check the logs")
71-
cat(shinyproc$read_error())
83+
cli::cli_alert_danger(
84+
"Failed to start the app, please check the logs"
85+
)
86+
cat(
87+
shinyproc$read_error()
88+
)
7289
stop()
7390
}
7491
with_dir(
7592
where,
7693
{
77-
with_dir(
78-
"tests",
79-
{
80-
with_dir(
81-
"playwright",
82-
{
83-
output_file <- file.path(
84-
"tests",
85-
sprintf(
86-
"%s.test.ts",
87-
uuid::UUIDgenerate()
88-
)
89-
)
90-
fs::file_create(output_file)
91-
output_file <- fs::path_abs(output_file)
92-
on.exit({
93-
cat(
94-
"Code generation done, the file is available at:",
95-
output_file,
96-
"\n"
97-
)
98-
})
99-
system2(
100-
"npx",
101-
c(
102-
"playwright",
103-
"codegen",
104-
"http://localhost:3000",
105-
"--output",
106-
output_file,
107-
...
108-
)
109-
)
110-
}
94+
with_dir("tests", {
95+
with_dir("playwright", {
96+
output_file <- file.path(
97+
"tests",
98+
sprintf(
99+
"%s.test.ts",
100+
uuid::UUIDgenerate()
101+
)
111102
)
112-
}
113-
)
103+
fs::file_create(
104+
output_file
105+
)
106+
output_file <- fs::path_abs(
107+
output_file
108+
)
109+
on.exit({
110+
cat(
111+
"Code generation done, the file is available at:",
112+
output_file,
113+
"\n"
114+
)
115+
})
116+
system2(
117+
"npx",
118+
c(
119+
"playwright",
120+
"codegen",
121+
"http://localhost:3000",
122+
"--output",
123+
output_file,
124+
...
125+
)
126+
)
127+
})
128+
})
114129
}
115130
)
116-
}
131+
}

R/pw-init.R

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,45 +33,48 @@ pw_init <- function(
3333
"No tests folder found. Please run `usethis::use_testthat()` first."
3434
)
3535
}
36-
with_dir(
37-
"tests",
38-
{
39-
dir_create("playwright")
40-
with_dir(
41-
"playwright",
42-
{
43-
system2(
44-
"npx",
45-
c(
46-
"create-playwright@latest",
47-
"--",
48-
"--yes",
49-
"--no-examples",
50-
"--quiet"
51-
)
52-
)
53-
file_delete("playwright.config.ts")
54-
file_copy(
55-
pw_sys_files("playwright.config.ts"),
56-
"playwright.config.ts"
57-
)
58-
dir_create("tests")
59-
file_copy(
60-
pw_sys_files("default.test.ts"),
61-
"tests/default.test.ts"
62-
)
63-
}
36+
with_dir("tests", {
37+
dir_create("playwright")
38+
with_dir("playwright", {
39+
system2(
40+
"npx",
41+
c(
42+
"create-playwright@latest",
43+
"--",
44+
"--yes",
45+
"--no-examples",
46+
"--quiet"
47+
)
48+
)
49+
file_delete(
50+
"playwright.config.ts"
51+
)
52+
file_copy(
53+
pw_sys_files(
54+
"playwright.config.ts"
55+
),
56+
"playwright.config.ts"
57+
)
58+
dir_create(
59+
"tests"
6460
)
6561
file_copy(
66-
pw_sys_files("test-playwright.R"),
67-
"testthat/test-playwright.R"
62+
pw_sys_files(
63+
"default.test.ts"
64+
),
65+
"tests/default.test.ts"
6866
)
69-
}
70-
)
67+
})
68+
file_copy(
69+
pw_sys_files(
70+
"test-playwright.R"
71+
),
72+
"testthat/test-playwright.R"
73+
)
74+
})
7175
}
7276
)
7377

74-
7578
cli_alert_success(
7679
"Playwright project initialized successfully.\nTry running devtools::test() to see the tests in action."
7780
)

R/pw-tests.R

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,27 @@
99
pw_test <- function(
1010
where = golem::get_golem_wd(),
1111
...
12-
){
12+
) {
1313
stop_if_npx_not_available()
1414
stop_if_playwright_skeleton_not_present(
1515
where = where
1616
)
1717
with_dir(
1818
where,
1919
{
20-
with_dir(
21-
"tests",
22-
{
23-
with_dir(
24-
"playwright",
25-
{
26-
system2(
27-
"npx",
28-
c(
29-
"playwright",
30-
"test",
31-
"--config=playwright.config.ts",
32-
...
33-
)
34-
)
35-
}
20+
with_dir("tests", {
21+
with_dir("playwright", {
22+
system2(
23+
"npx",
24+
c(
25+
"playwright",
26+
"test",
27+
"--config=playwright.config.ts",
28+
...
29+
)
3630
)
37-
}
38-
)
31+
})
32+
})
3933
}
4034
)
4135
}

R/pw_install.R

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#' Install playwright in an already inited project
2+
#'
3+
#' This function runs playwright init in a project
4+
#' that has been previously initiated with `{pw}`
5+
#'
6+
#' @inheritParams pw_init
7+
#'
8+
#' @export
9+
10+
pw_install <- function(
11+
where = golem::get_golem_wd()
12+
) {
13+
stop_if_npx_not_available()
14+
playwright_test_folder <- file.path(
15+
where,
16+
"tests",
17+
"playwright"
18+
)
19+
if (
20+
Negate(dir.exists)(
21+
playwright_test_folder
22+
)
23+
) {
24+
stop(
25+
"The playwright test folder doesn't exist, please run pw_init() first."
26+
)
27+
}
28+
withr::with_dir(
29+
playwright_test_folder,
30+
{
31+
system2(
32+
"npm",
33+
c(
34+
"install"
35+
)
36+
)
37+
system2(
38+
"npx",
39+
c(
40+
"playwright",
41+
"install"
42+
)
43+
)
44+
}
45+
)
46+
}

0 commit comments

Comments
 (0)