Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
csgillespie committed Feb 26, 2021
1 parent d5d7a1c commit 252532f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ Imports:
glue,
httr,
jsonlite,
magrittr,
purrr,
rjson,
rlang,
Expand Down
7 changes: 4 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# oysteR 0.1.2 _2020-12-31_
* Adds `audit_req_txt()`, `audit_renv_lock()`, and `audit_conda()` functions
* Add Josiah Parry as an author
# oysteR 0.1.2 _2021-26-02_
* Feature: Add `audit_conda()` functions
* Feature: Add Josiah Parry as an author
* Feature: Handle missing versions in a nice way

# oysteR 0.1.1 _2021-01-08_
* Use `dontrun{}` in examples that may hit rate limits.
Expand Down
4 changes: 2 additions & 2 deletions R/expect_secure.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ expect_secure = function(pkg,
options(repos = c(CRAN = repo))

## Look up vulnerabilities
pkg_loc = system.file(package = pkg)
aud = audit_description(pkg_loc, verbose = verbose)
pkg_loc = system.file("DESCRIPTION", package = pkg)
aud = audit_description(dirname(pkg_loc), verbose = verbose)
no_of_vul = sum(aud$no_of_vulnerabilities)

## Report results
Expand Down
26 changes: 12 additions & 14 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,32 @@ generate_purls = function(pkg, version, type) {
}
# Make lower case to make caching better
type = tolower(type)
version = as.character(version)
# List format required for httr call
# The list translates to the body of the curl call
# Each purl must be it's own list element hence the use of as.list over list
# if version is missing, creates "@NA" this searches all. Alternatively could use @*
# it will check for all versions of packages in this case
no_missing_versions <- sum(is.na(version))
missing_pkgs <- pkg[is.na(version)]

# must have version for Sonatype
no_missing_versions = sum(is.na(version) | nchar(version) == 0L)
# create alert if missing versions
if (no_missing_versions > 0) {
missing_msg <- sprintf("%i packages missing versions: %s",
no_missing_versions, paste0(missing_pkgs, collapse = ", "))
cli::cli_h3("Missing pkg versions")
missing_pkgs = paste(pkg[is.na(version) | nchar(version) == 0L], collapse = ', ')
cli::cli_alert_warning("{no_missing_versions} package{?s} with missing versions: \\
{missing_pkgs}")

cli_alert_warning(missing_msg)
cli_alert_warning(glue::glue("oysteR will check {style_italic('all')} package verions."))
cli_alert_warning("This may result in false positives.")
cli::cli_alert_warning("oysteR will check all package versions.")
cli::cli_alert_warning("This may result in false positives.")
cli::cat_line()

version[is.na(version) | nchar(version) == 0L] = "*"
}


# generate purls
purls <- as.list(paste0("pkg:", type, "/", pkg, "@", version))
purls = as.list(paste0("pkg:", type, "/", pkg, "@", version))

# return purls
return(purls)
}


#' Get data frame of installed packages
#'
#' @importFrom tibble as_tibble tibble
Expand Down
8 changes: 5 additions & 3 deletions tests/testthat/test-audit.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ test_that("Test audit_deps", {
expect_equal(ncol(deps), 8)

# Pass explicit packages
deps = expect_message(audit(pkg = c("abind", "acepack"),
version = c("1.4-5", "1.4.1"), type = "cran"))
expect_equal(nrow(deps), 2)
# Also test missing versions, and mixing numbers and characters
deps = expect_message(audit(pkg = c("abind", "acepack", "abind", "abind", "abind"),
version = c("1.4-5", "1.4.1", NA, "", 1),
type = "cran"))
expect_equal(nrow(deps), 5)
expect_equal(ncol(deps), 8)

## Ensure that no cases have the same colnames
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-env-yml.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test_that("Test environment.yml file", {

# Sanity check to make sure the directory exists
expect_true(file.exists("environment.yml"))
audit = oysteR::audit_conda("tests/testthat/")
audit = oysteR::audit_conda()
expect_equal(ncol(audit), 8)
expect_equal(nrow(audit), 73)
expect_gte(sum(audit$no_of_vulnerabilities), 95) # This could increase
Expand Down

0 comments on commit 252532f

Please sign in to comment.