Skip to content

Commit

Permalink
simplify tests with helper-temp-pkg.R
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Nov 20, 2024
1 parent 010d699 commit bdddbb1
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 77 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: repometrics
Title: Metrics for Your Code Repository
Version: 0.1.1.038
Version: 0.1.1.039
Authors@R:
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2172-5265"))
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"codeRepository": "https://github.com/ropensci-review-tools/repometrics",
"issueTracker": "https://github.com/ropensci-review-tools/repometrics/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.1.1.038",
"version": "0.1.1.039",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/helper-temp-pkg.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
generate_test_pkg <- function (add_url = TRUE) {

pkg <- system.file ("extdata", "testpkg.zip", package = "repometrics")
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])

if (add_url) {
desc_path <- fs::dir_ls (path, type = "file", regexp = "DESCRIPTION$")
url <- "https://github.com/ropensci-review-tools/goodpractice"
desc <- c (
readLines (desc_path),
paste0 ("URL: ", url)
)
writeLines (desc, desc_path)
}

return (path)
}
18 changes: 4 additions & 14 deletions tests/testthat/test-chaoss-metrics-external.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
test_that ("chaoss external util fns", {
pkg <- system.file ("extdata", "testpkg.zip", package = "repometrics")
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])

path <- generate_test_pkg (add_url = FALSE)

pkg_name <- pkg_name_from_path (path)
expect_equal (pkg_name, "testpkg")
Expand Down Expand Up @@ -68,17 +67,8 @@ test_that ("chaoss external commits in prs", {
test_that ("chaoss external prop commits in change req", {

Sys.setenv ("REPOMETRICS_TESTS" = "true")
pkg <- system.file ("extdata", "testpkg.zip", package = "repometrics")
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])

desc_path <- fs::dir_ls (path, type = "file", regexp = "DESCRIPTION$")
url <- "https://github.com/ropensci-review-tools/goodpractice"
desc <- c (
readLines (desc_path),
paste0 ("URL: ", url)
)
writeLines (desc, desc_path)

path <- generate_test_pkg (add_url = TRUE)

end_date <- as.Date ("2024-01-01")
prop_commits <- with_mock_dir ("gh_pr_qry", {
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-chaoss-metrics-hybrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ test_that ("chaoss metric has_ci", {

Sys.setenv ("REPOMETRICS_TESTS" = "true")

pkg <- system.file ("extdata", "testpkg.zip", package = "repometrics")
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])
path <- generate_test_pkg (add_url = FALSE)

chk <- repo_has_ci_files (path)
expect_length (chk, 0L)
Expand Down
27 changes: 9 additions & 18 deletions tests/testthat/test-chaoss-metrics-internal.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
end_date <- as.Date ("2024-08-01")

test_that ("chaoss internal num_commits", {
pkg <- system.file ("extdata", "testpkg.zip", package = "repometrics")
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])

path <- generate_test_pkg (add_url = FALSE)

n <- chaoss_internal_num_commits (path, end_date = end_date)
expect_equal (n, 4L)
Expand All @@ -15,36 +14,28 @@ test_that ("chaoss internal num_commits", {
})

test_that ("chaoss has CI internal", {
pkg <- system.file ("extdata", "testpkg.zip", package = "repometrics")
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])

path <- generate_test_pkg (add_url = FALSE)
has_ci <- repo_has_ci_files (path)
expect_length (has_ci, 0L) # No CI files

url <- pkg_gh_url_from_path (path)
expect_null (url)
fs::dir_delete (path)

desc_path <- fs::dir_ls (path, type = "file", regexp = "DESCRIPTION$")
url <- "https://github.com/my/pkg"
desc <- c (
readLines (desc_path),
paste0 ("URL: ", url)
)
writeLines (desc, desc_path)

path <- generate_test_pkg (add_url = TRUE)
url <- "https://github.com/ropensci-review-tools/goodpractice"
expect_identical (url, pkg_gh_url_from_path (path))

org_repo <- org_repo_from_path (path)
expect_identical (org_repo, c ("my", "pkg"))
expect_identical (org_repo, c ("ropensci-review-tools", "goodpractice"))

fs::dir_delete (path)
})

test_that ("chaoss internal change requests", {
pkg <- system.file ("extdata", "testpkg.zip", package = "repometrics")
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])

path <- generate_test_pkg (add_url = FALSE)

x <- chaoss_internal_change_req (path, end_date = end_date)
expect_equal (x, 0)
Expand Down
29 changes: 3 additions & 26 deletions tests/testthat/test-cm-data.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
test_that ("cm data git", {

pkg <- system.file ("extdata", "testpkg.zip", package = "repometrics")
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])
path <- generate_test_pkg (add_url = FALSE)

log <- cm_data_gitlog (path)

Expand Down Expand Up @@ -32,18 +30,8 @@ test_that ("cm data git", {
test_that ("cm data gh contribs", {

Sys.setenv ("REPOMETRICS_TESTS" = "true")
pkg <- system.file ("extdata", "testpkg.zip", package = "repometrics")
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])

desc_path <- fs::dir_ls (path, type = "file", regexp = "DESCRIPTION$")
url <- "https://github.com/ropensci-review-tools/goodpractice"
desc <- c (
readLines (desc_path),
paste0 ("URL: ", url)
)
writeLines (desc, desc_path)

path <- generate_test_pkg (add_url = TRUE)
ctbs <- with_mock_dir ("gh_api_ctbs", {
contribs_from_gh_api (path, n_per_page = 2L)
})
Expand Down Expand Up @@ -72,18 +60,7 @@ test_that ("cm data gh contribs", {

test_that ("cm data gh repo", {

pkg <- system.file ("extdata", "testpkg.zip", package = "repometrics")
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])

desc_path <- fs::dir_ls (path, type = "file", regexp = "DESCRIPTION$")
url <- "https://github.com/ropensci-review-tools/goodpractice"
desc <- c (
readLines (desc_path),
paste0 ("URL: ", url)
)
writeLines (desc, desc_path)

path <- generate_test_pkg (add_url = TRUE)
repo <- with_mock_dir ("gh_api_repo", {
repo_from_gh_api (path)
})
Expand Down
7 changes: 2 additions & 5 deletions tests/testthat/test-multicore.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
test_that ("multicore", {
pkg <- system.file ("extdata", "testpkg.zip", package = "repometrics")
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])

path <- generate_test_pkg (add_url = FALSE)
res1 <- githist (path, num_cores = 1L)
# Becuase of hard reset of history, entire pkg dir needs to be deleted and
# re-created:
fs::dir_delete (path)

flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])
path <- generate_test_pkg (add_url = FALSE)
res2 <- githist (path, num_cores = 2L)
fs::dir_delete (path)

Expand Down
14 changes: 5 additions & 9 deletions tests/testthat/test-testpkg.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ test_that ("testpkg and input errors", {
expect_error (githist (1))
expect_error (githist ("a"))

pkg <- system.file ("extdata", "testpkg.zip", package = "repometrics")
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])
path <- generate_test_pkg (add_url = FALSE)

expect_error (githist (path, step_size = 1:2, num_cores = 1L))
expect_error (githist (path, step_size = "1", num_cores = 1L))
Expand All @@ -19,14 +17,12 @@ test_that ("testpkg and input errors", {
})

test_that ("githist parameters", {
pkg <- system.file ("extdata", "testpkg.zip", package = "repometrics")
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- fs::path_dir (flist [1])

path <- generate_test_pkg (add_url = FALSE)
res0 <- githist (path, step_days = 0L, num_cores = 1L)
fs::dir_delete (path)

flist <- unzip (pkg, exdir = fs::path_temp ())
path <- generate_test_pkg (add_url = FALSE)
res1 <- githist (path, n = 2L, step_days = 0L, num_cores = 1L)
fs::dir_delete (path)

Expand All @@ -36,7 +32,7 @@ test_that ("githist parameters", {
# `n = 2L` selects dates [1:2] from original data:
expect_identical (res0$desc_data$date [1:2], res1$desc_data$date)

flist <- unzip (pkg, exdir = fs::path_temp ())
path <- generate_test_pkg (add_url = FALSE)
res2 <- githist (path, n = 2L, step_days = 1L, num_cores = 1L)
fs::dir_delete (path)

Expand All @@ -50,7 +46,7 @@ test_that ("githist parameters", {

# Finally, test step_days > 1, which has no effect anyway, as all commits
# are on same day
flist <- unzip (pkg, exdir = fs::path_temp ())
path <- generate_test_pkg (add_url = FALSE)
res3 <- githist (path, n = 2L, step_days = 2L, num_cores = 1L)
fs::dir_delete (path)

Expand Down

0 comments on commit bdddbb1

Please sign in to comment.