Skip to content

Commit

Permalink
Make consistent assert_package() and update test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Feb 28, 2024
1 parent faaa301 commit 4caad52
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 44 deletions.
13 changes: 6 additions & 7 deletions R/assert_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
#' otherwise `NULL` if there are no issues.
assert_package <- function(path) {
if (!is_character_scalar(path)) {
return("Invalid package file path.")
stop("Invalid package file path")
}
if (!file.exists(path)) {
return(paste("file", shQuote(path), "does not exist."))
stop(paste("file", shQuote(path), "does not exist"))
}
name <- basename(path)
url <- try(readLines(path, warn = FALSE), silent = TRUE)
if (inherits(url, "try-error")) {
return(paste("Problem reading file", shQuote(path)))
stop(paste("Problem reading file", shQuote(path)))
}
assert_package_contents(name = name, url = url)
}
Expand All @@ -25,19 +25,18 @@ assert_package_contents <- function(name, url) {
x = name
)
if (!isTRUE(good_package_name)) {
return(paste("Found invalid package name: ", shQuote(name)))
stop(paste("Found invalid package name: ", shQuote(name)))
}
if (!is_character_scalar(url)) {
return("Invalid package URL.")
stop("Invalid package URL")
}
good_url <- grepl(
pattern = "^https?://[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,3}(/\\S*)?$",
x = url
)
if (!isTRUE(good_url)) {
return(
stop(
paste("Found malformed URL", shQuote(url), "of package", shQuote(name))
)
}
NULL
}
7 changes: 1 addition & 6 deletions R/build_universe.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ build_universe <- function(input = getwd(), output = "packages.json") {
assert_character_scalar(output, "invalid output")
assert_file(input)
packages <- list.files(input, all.files = FALSE, full.names = TRUE)
for (package in packages) {
result <- assert_package(package)
if (!is.null(result)) {
stop(result, call. = FALSE)
}
}
lapply(packages, assert_package)
urls <- vapply(
X = packages,
FUN = readLines,
Expand Down
2 changes: 1 addition & 1 deletion R/utils_assert.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
assert_file <- function(x) {
if (!file.exists(x)) {
stop("File ", shQuote(x), " does not exist.", call. = FALSE)
stop("File ", shQuote(x), " does not exist", call. = FALSE)
}
}

Expand Down
38 changes: 8 additions & 30 deletions tests/test-assert_package.R
Original file line number Diff line number Diff line change
@@ -1,48 +1,26 @@
stopifnot(
grepl(
"Invalid package file path",
r.releases.utils::assert_package(path = c(1L, 2L))
)
)
expect_error <- function(x, e = "") {
invisible(grepl(e, tryCatch(x, error = identity)[["message"]], fixed = TRUE) || stop("Error '", e, "' not generated"))

Check warning on line 2 in tests/test-assert_package.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/test-assert_package.R,line=2,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 120 characters.
}

stopifnot(
grepl(
"does not exist",
r.releases.utils::assert_package(path = tempfile())
)
)
expect_error(r.releases.utils::assert_package(path = c(1L, 2L)), "Invalid package file path")

Check warning on line 5 in tests/test-assert_package.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/test-assert_package.R,line=5,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 93 characters.
expect_error(r.releases.utils::assert_package(path = tempfile()), "does not exist")

Check warning on line 6 in tests/test-assert_package.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/test-assert_package.R,line=6,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 83 characters.

path <- file.path(tempfile(), "hy-phens")
dir.create(dirname(path))
file.create(path)
stopifnot(
grepl(
"invalid package name",
r.releases.utils::assert_package(path = path)
)
)
expect_error(r.releases.utils::assert_package(path = path), "invalid package name")

Check warning on line 11 in tests/test-assert_package.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/test-assert_package.R,line=11,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 83 characters.
unlink(dirname(path), recursive = TRUE)

path <- file.path(tempfile(), "package")
dir.create(dirname(path))
writeLines(letters, path)
stopifnot(
grepl(
"Invalid package URL",
r.releases.utils::assert_package(path = path)
)
)
expect_error(r.releases.utils::assert_package(path = path), "Invalid package URL")

Check warning on line 17 in tests/test-assert_package.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/test-assert_package.R,line=17,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 82 characters.
unlink(dirname(path), recursive = TRUE)

path <- file.path(tempfile(), "package")
dir.create(dirname(path))
writeLines("b a d", path)
stopifnot(
grepl(
"Found malformed URL",
r.releases.utils::assert_package(path = path)
)
)
expect_error(r.releases.utils::assert_package(path = path), "Found malformed URL")

Check warning on line 23 in tests/test-assert_package.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/test-assert_package.R,line=23,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 82 characters.
unlink(dirname(path), recursive = TRUE)

path <- file.path(tempfile(), "package")
Expand Down

0 comments on commit 4caad52

Please sign in to comment.