Skip to content

Commit

Permalink
Update tests (#36)
Browse files Browse the repository at this point in the history
* migrate tests to testtthat edition 3
* update github actions
  • Loading branch information
jonathanvoelkle authored Oct 11, 2023
1 parent 4a7363e commit a7616d6
Show file tree
Hide file tree
Showing 15 changed files with 627 additions and 548 deletions.
23 changes: 14 additions & 9 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Workflow derived from https://github.com/r-lib/actions/tree/master/examples
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
workflow_dispatch:
Expand Down Expand Up @@ -30,22 +30,27 @@ jobs:
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-pandoc@v1
- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v1
- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v1
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
rcmdcheck
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v1
- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true

- name: Check drivers
run: Rscript inst/etc/checks.R
run: |
Rscript -e "install.packages('devtools')"
Rscript -e "devtools::install('.')"
Rscript inst/etc/checks.R
16 changes: 10 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ Type: Package
Title: 'Webdriver'/'Selenium' Binary Manager
Version: 0.2.6
Authors@R: c(
person("John", "Harrison", , "johndharrison0@gmail.com",
role = "aut", comment = "original author"),
person("Ju Yeong", "Kim", , "jkim2345@fredhutch.org",
role = "cre", comment = "rOpenSci maintainer"))
person("John", "Harrison", , "johndharrison0@gmail.com", role = "aut",
comment = "original author"),
person("Ju Yeong", "Kim", , "jkim2345@fredhutch.org", role = "aut",
comment = "rOpenSci maintainer"),
person("Jonathan", "Völkle", , "jonathan.voelkle@web.de", role = "cre")
)
Description: There are a number of binary files associated with the
'Webdriver'/'Selenium' project. This package provides functions to download
these binaries and to manage processes involving them.
Expand All @@ -15,10 +17,11 @@ Encoding: UTF-8
Depends:
R (>= 3.2)
Suggests:
testthat,
testthat (>= 3.1.7),
covr,
knitr,
rmarkdown
rmarkdown,
R6
Imports:
binman,
assertthat,
Expand All @@ -31,3 +34,4 @@ URLNote: https://github.com/ropensci/wdman
BugReports: https://github.com/ropensci/wdman/issues
RoxygenNote: 7.2.1
VignetteBuilder: knitr
Config/testthat/edition: 3
2 changes: 1 addition & 1 deletion R/chrome.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ chrome_check <- function(verbose, check = TRUE) {
}

chrome_ver <- function(platform, version) {
chromever <- binman::list_versions("chromedriver")[[platform]]
chromever <- list_versions("chromedriver")[[platform]]
chromever <- if (identical(version, "latest")) {
as.character(max(package_version(chromever)))
} else {
Expand Down
2 changes: 1 addition & 1 deletion R/gecko.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ gecko_check <- function(verbose, check = TRUE) {
}

gecko_ver <- function(platform, version) {
geckover <- binman::list_versions("geckodriver")[[platform]]
geckover <- list_versions("geckodriver")[[platform]]
geckover <- if (identical(version, "latest")) {
as.character(max(semver::parse_version(geckover)))
} else {
Expand Down
2 changes: 1 addition & 1 deletion R/phantom.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ phantom_check <- function(verbose, check = TRUE) {
}

phantom_ver <- function(platform, version) {
phantomver <- binman::list_versions("phantomjs")[[platform]]
phantomver <- list_versions("phantomjs")[[platform]]
phantomver <- if (identical(version, "latest")) {
as.character(max(semver::parse_version(phantomver)))
} else {
Expand Down
4 changes: 2 additions & 2 deletions R/selenium.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ selenium <- function(port = 4567L,
)
if (isFALSE(seleniumdrv$is_alive())) {
err <- paste0(readLines(pfile[["err"]]), collapse = "\n")
stop("Selenium server couldn't be started\n", err)
stop("Selenium server couldn't be started\n", err)
}
startlog <- generic_start_log(seleniumdrv, # poll = 10000L,
outfile = pfile[["out"]],
Expand Down Expand Up @@ -156,7 +156,7 @@ selenium_check <- function(verbose, check = TRUE) {
}

selenium_ver <- function(platform, version) {
selver <- binman::list_versions("seleniumserver")[[platform]]
selver <- list_versions("seleniumserver")[[platform]]
selver <- if (identical(version, "latest")) {
as.character(max(semver::parse_version(selver)))
} else {
Expand Down
7 changes: 4 additions & 3 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
library(testthat)
library(wdman)

# if (identical(tolower(Sys.getenv("NOT_CRAN")), "true")) {
# test_check("wdman")
# }
if (identical(tolower(Sys.getenv("NOT_CRAN")), "true")) {
test_check("wdman")
}

32 changes: 32 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mock_binman_list_versions_chrome <- function(...) {
list(
linux64 = c("2.23", "2.24", "2.25", "2.26"),
mac64 = c("2.23", "2.24", "2.25"),
mac64_m1 = c("2.23", "2.24", "2.25"),
win32 = c("2.23", "2.24", "2.25")
)
}
Expand All @@ -21,6 +22,7 @@ mock_binman_list_versions_gecko <- function(...) {
list(
linux64 = c("0.10.0", "0.11.0", "0.11.1"),
macos = c("0.10.0", "0.11.0", "0.11.1"),
`macos-aarch64` = c("0.10.0", "0.11.0", "0.11.1"),
win32 = c("0.10.0", "0.11.0", "0.11.1"),
win64 = c("0.10.0", "0.11.0", "0.11.1")
)
Expand Down Expand Up @@ -51,6 +53,35 @@ mock_binman_app_dir <- function(...) {
"some.dir"
}

mock_processx_process <- R6::R6Class(
"process_test",
public = list(
initialize = function(...) {},
is_alive = function() TRUE,
test = "hello"
)
)

mock_processx_process_fail <- R6::R6Class(
"process_test",
public = list(
initialize = function(...) {},
is_alive = function() FALSE
)
)

mock_generic_check <- function(...) {
list(platform = "some.plat")
}

mock_generic_ver <- function(...) {
list(path = "some.path")
}

mock_base_Sys.info_windows <- function(...) {
structure("Windows", .Names = "sysname")
}

mock_subprocess_spawn_process <- function(...) {
"hello"
}
Expand All @@ -75,3 +106,4 @@ mock_subprocess_process_read_utils <- function(...) {
mock_generic_start_log <- function(...) {
list(stdout = "super duper", stderr = "no error here")
}

2 changes: 0 additions & 2 deletions tests/testthat/test-assertions.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("test-assertions")

test_that("assertionsWork", {
expect_error(assertthat::assert_that(is_list("")), "is not a list")
expect_error(
Expand Down
151 changes: 86 additions & 65 deletions tests/testthat/test-chrome.R
Original file line number Diff line number Diff line change
@@ -1,96 +1,117 @@
context("chrome")

normalizePath <- function(...) base::normalizePath(...)
list.files <- function(...) base::list.files(...)

test_that("canCallChrome", {
with_mock(
`binman::process_yaml` = binman_process_yaml,
`binman::list_versions` = mock_binman_list_versions_chrome,
`binman::app_dir` = mock_binman_app_dir,
local_mocked_bindings(
process_yaml = binman_process_yaml,
list_versions = mock_binman_list_versions_chrome,
app_dir = mock_binman_app_dir,
)
local_mocked_bindings(
normalizePath = mock_base_normalizePath,
list.files = mock_base_list.files,
`subprocess::spawn_process` = mock_subprocess_spawn_process,
`subprocess::process_return_code` =
mock_subprocess_process_return_code,
`subprocess::process_kill` = mock_subprocess_process_kill,
`wdman:::generic_start_log` = mock_generic_start_log,
`wdman:::infun_read` = function(...) {
.package = "base",
)
local_mocked_bindings(
check_bindings = function(...) {},
.package = "testthat"
)
local_mocked_bindings(
process = mock_processx_process,
.package = "processx"
)
local_mocked_bindings(
generic_start_log = mock_generic_start_log,
infun_read = function(...) {
"infun"
},
{
cDrv <- chrome()
retCommand <- chrome(retcommand = TRUE)
expect_identical(cDrv$output(), "infun")
expect_identical(cDrv$error(), "infun")
logOut <- cDrv$log()[["stdout"]]
logErr <- cDrv$log()[["stderr"]]
expect_identical(logOut, "super duper")
expect_identical(logErr, "no error here")
expect_identical(cDrv$stop(), "stopped")
}
)
expect_identical(cDrv$process, "hello")
kill_process = mock_subprocess_process_kill,
)

cDrv <- chrome()
retCommand <- chrome(retcommand = TRUE)
expect_identical(cDrv$output(), "infun")
expect_identical(cDrv$error(), "infun")
logOut <- cDrv$log()[["stdout"]]
logErr <- cDrv$log()[["stderr"]]
expect_identical(logOut, "super duper")
expect_identical(logErr, "no error here")
expect_identical(cDrv$stop(), "stopped")
expect_identical(cDrv$process$test, "hello")
expect_identical(
retCommand,
"some.path --port=4567 --url-base=wd/hub --verbose"
)
})

test_that("chrome_verErrorWorks", {
with_mock(
`binman::list_versions` = mock_binman_list_versions_chrome,
expect_error(
wdman:::chrome_ver("linux64", "noversion"),
"doesnt match versions"
)
local_mocked_bindings(
list_versions = mock_binman_list_versions_chrome,
)

expect_error(
wdman:::chrome_ver("linux64", "noversion"),
"doesnt match versions"
)
})

test_that("pickUpErrorFromReturnCode", {
with_mock(
`binman::process_yaml` = function(...) {},
`binman::list_versions` = mock_binman_list_versions_chrome,
`binman::app_dir` = mock_binman_app_dir,
local_mocked_bindings(
process_yaml = binman_process_yaml,
list_versions = mock_binman_list_versions_chrome,
app_dir = mock_binman_app_dir,
)
local_mocked_bindings(
normalizePath = mock_base_normalizePath,
list.files = mock_base_list.files,
`subprocess::spawn_process` = mock_subprocess_spawn_process,
`subprocess::process_return_code` = function(...) {
"some error"
},
`subprocess::process_read` =
mock_subprocess_process_read_selenium,
`wdman:::generic_start_log` = mock_generic_start_log,
{
expect_error(
chrome(version = "2.24"),
"Chromedriver couldn't be started"
)
}
.package = "base"
)
local_mocked_bindings(
check_bindings = function(...) {},
.package = "testthat"
)
local_mocked_bindings(
process = mock_processx_process_fail,
.package = "processx"
)
local_mocked_bindings(
generic_start_log = mock_generic_start_log,
)

expect_error(
chrome(),
"Chromedriver couldn't be started"
)
})

test_that("pickUpErrorFromPortInUse", {
with_mock(
`binman::process_yaml` = function(...) {},
`binman::list_versions` = mock_binman_list_versions_chrome,
`binman::app_dir` = mock_binman_app_dir,
local_mocked_bindings(
process_yaml = binman_process_yaml,
list_versions = mock_binman_list_versions_chrome,
app_dir = mock_binman_app_dir,
)
local_mocked_bindings(
normalizePath = mock_base_normalizePath,
list.files = mock_base_list.files,
`subprocess::spawn_process` = mock_subprocess_spawn_process,
`subprocess::process_return_code` =
mock_subprocess_process_return_code,
`subprocess::process_read` =
mock_subprocess_process_read_selenium,
`subprocess::process_kill` = mock_subprocess_process_kill,
`wdman:::generic_start_log` = function(...) {
.package = "base"
)
local_mocked_bindings(
check_bindings = function(...) {},
.package = "testthat"
)
local_mocked_bindings(
process = mock_processx_process,
.package = "processx"
)
local_mocked_bindings(
generic_start_log = function(...) {
list(stderr = "Address already in use")
},
{
expect_error(
chrome(version = "2.24"),
"Chrome Driver signals port"
)
}
kill_process = mock_subprocess_process_kill
)

expect_error(
chrome(),
"Chrome Driver signals port"
)
})
Loading

0 comments on commit a7616d6

Please sign in to comment.