Skip to content

Commit

Permalink
revert - need to return assert package error
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Feb 28, 2024
1 parent 6fad05f commit cce9f4f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
12 changes: 6 additions & 6 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)) {
stop("Invalid package file path")
return("Invalid package file path")
}
if (!file.exists(path)) {
stop(paste("file", shQuote(path), "does not exist"))
return(paste("file", shQuote(path), "does not exist"))
}
name <- trimws(basename(path))
url <- try(readLines(path, warn = FALSE), silent = TRUE)
if (inherits(url, "try-error")) {
stop(paste("Problem reading file", shQuote(path)))
return(paste("Problem reading file", shQuote(path)))
}
assert_package_contents(name = name, url = url)
}
Expand All @@ -25,18 +25,18 @@ assert_package_contents <- function(name, url) {
x = name
)
if (!isTRUE(good_package_name)) {
stop(paste("Found invalid package name: ", shQuote(name)))
return(paste("Found invalid package name: ", shQuote(name)))
}
if (!is_character_scalar(url)) {
stop("Invalid package URL")
return("Invalid package URL")
}
url <- trimws(url)
good_url <- grepl(
pattern = "^https?://[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,3}(/\\S*)?$",
x = url
)
if (!isTRUE(good_url)) {
stop(
return(
paste("Found malformed URL", shQuote(url), "of package", shQuote(name))
)
}
Expand Down
5 changes: 4 additions & 1 deletion R/build_universe.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ 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)
lapply(packages, assert_package)
for (package in packages) {
result <- assert_package(package)
is.null(result) || stop(result, call. = FALSE)
}
urls <- vapply(
X = packages,
FUN = readLines,
Expand Down
50 changes: 29 additions & 21 deletions tests/test-assert_package.R
Original file line number Diff line number Diff line change
@@ -1,44 +1,52 @@
expect_error <- function(x, e = "") {
invisible(
grepl(e, tryCatch(x, error = identity)[["message"]], fixed = TRUE) ||
stop("Error '", e, "' not generated")
stopifnot(
grepl(
"Invalid package file path",
r.releases.utils::assert_package(path = c(1L, 2L)),
fixed = TRUE
)
}

expect_error(
r.releases.utils::assert_package(path = c(1L, 2L)),
"Invalid package file path"
)

expect_error(
r.releases.utils::assert_package(path = tempfile()),
"does not exist"
stopifnot(
grepl(
"does not exist",
r.releases.utils::assert_package(path = tempfile()),
fixed = TRUE
)
)

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

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

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

Expand Down

0 comments on commit cce9f4f

Please sign in to comment.