Skip to content

Commit

Permalink
chore: Use jrStyle (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
csgillespie authored Jun 24, 2024
1 parent 1bfed8c commit 9b0478f
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 80 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: audit.base
Title: Base package for Posit Checks
Version: 0.6.15
Version: 0.6.16
Authors@R:
person("Jumping", "Rivers", , "[email protected]", role = c("aut", "cre"))
Description: Base package for sharing classes between posit audit
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# audit.base 0.6.16 _2024-06-24_
- chore: Use jrStyling

# audit.base 0.6.15 _2024-06-06_
- feat: Add support for Centos
- fix: Update software version numbers
Expand Down
2 changes: 1 addition & 1 deletion R/check.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ init_r6_checks = function(dir, file, pkg_name) {
exports = getNamespaceExports(pkg_name)
check_exports = sort(exports[stringr::str_starts(exports, "check_")])
r6_inits = purrr::map(check_exports, init_r6_check, dir = dir, file = file, pkg_name)
purrr::discard(r6_inits, ~is.null(.x))
purrr::discard(r6_inits, ~ is.null(.x))
}

#' @rdname init_r6_checks
Expand Down
10 changes: 6 additions & 4 deletions R/check_server_headers.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ check_server_headers = function(server) {
get_posit_headers = function(headers) {
posit_header = headers %>%
dplyr::filter(.data$header == "server" &
stringr::str_detect(.data$message, "[p|P]osit")) %>%
dplyr::mutate(documentation = "https://developer.mozilla.org/docs/Web/HTTP/Headers/Server",
primary_header = TRUE,
status = "WARN")
stringr::str_detect(.data$message, "[p|P]osit")) %>%
dplyr::mutate(
documentation = "https://developer.mozilla.org/docs/Web/HTTP/Headers/Server",
primary_header = TRUE,
status = "WARN"
)
if (nrow(posit_header) == 0) {
cli::cli_alert_success("{cli::col_green('server')}: Does not leak information")
} else {
Expand Down
2 changes: 1 addition & 1 deletion R/check_sys_deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ check_sys_deps = function(os_release, installed_libs, debug_level = 0:2) {
cli::cli_alert_info("Unable to install {nrow(missing_libs)} CRAN packages")

if (nrow(missing_libs) > 0) {
sys_libs = unique(missing_libs$sys_libs) #nolint
sys_libs = unique(missing_libs$sys_libs) # nolint
cli::cli_alert_info("Missing sys_libs: {sys_libs}")
cli::cli_alert_info("Note: this is not necessarily a bad thing")
}
Expand Down
21 changes: 12 additions & 9 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ create_config = function(file, pkg_name) {
create_config_list = function(dir, file, default, pkg_name) {
obj_info = get_check_info(dir, file, pkg_name)
groups = unique(obj_info$group)
shorts = purrr::map(groups, ~obj_info[obj_info$group == .x, ]$short)
shorts = purrr::map(groups, ~ obj_info[obj_info$group == .x, ]$short)
group_shorts = purrr::map(shorts, create_group_short, default = default)

names(group_shorts) = groups
Expand All @@ -45,24 +45,27 @@ create_config_list = function(dir, file, default, pkg_name) {

get_check_info = function(dir, file, pkg_name) {
r6_inits = init_r6_checks(dir, file, pkg_name)
if (length(r6_inits) > 0L)
if (length(r6_inits) > 0L) {
purrr::map_dfr(r6_inits, function(r6) c("class" = class(r6)[1], r6$info()))
else
tibble::tibble(class = character(0), group = character(0),
short = character(0), context = character(0),
long = character(0))
} else {
tibble::tibble(
class = character(0), group = character(0),
short = character(0), context = character(0),
long = character(0)
)
}
}

merge_configs = function(new, existing) {
xnames = names(existing)
for (v in names(new)) {
if (is.list(new[[v]])) {
if (is.list(new[[v]])) {
new[[v]] = merge_configs(new[[v]], existing[[v]])
# Ensure that existing list is the same "type"
# If not, new gets precedent
} else if (v %in% xnames &&
!is.null(existing[[v]]) &&
!is.list(existing[[v]])) {
!is.null(existing[[v]]) &&
!is.list(existing[[v]])) {
new[[v]] = existing[[v]]
}
}
Expand Down
12 changes: 8 additions & 4 deletions R/create_software_tibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ create_software_tibble = function() {
q = jsonlite::read_json("https://api.github.com/repos/quarto-dev/quarto-cli/releases/latest")
quarto = c("1.0.38", "1.1.189", "1.2.475", stringr::str_remove(q$name, "^v"))

software_tibble = tibble::tibble(software = rep(c("r", "python", "quarto"),
c(length(r), length(py), length(quarto))),
version = c(r, py, quarto))
software_tibble = tibble::tibble(
software = rep(
c("r", "python", "quarto"),
c(length(r), length(py), length(quarto))
),
version = c(r, py, quarto)
)
# Use package_version to get better sorting
software_tibble %>%
dplyr::mutate(tmp_version = package_version(.data$version)) %>%
Expand All @@ -40,7 +44,7 @@ get_latest_versions_from_posit = function(type = c("r", "python")) {
dplyr::filter(!is.na(.data$patch)) %>%
dplyr::arrange(.data$major, -.data$patch) %>%
dplyr::group_by(.data$major) %>%
dplyr::mutate(patch = max(.data$patch)) %>%
dplyr::mutate(patch = max(.data$patch)) %>%
dplyr::slice(1) %>%
dplyr::pull(versions)
}
9 changes: 5 additions & 4 deletions R/get_pkg_requirements.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ get_pkg_requirements = function(distribution = c("ubuntu", "redhat", "centos"),
repo_id = 2) {
distribution = match.arg(distribution)
release = match.arg(release)
config_url = glue::glue("{repo_id}/sysreqs?all=true&distribution={distribution}&release={release}") #nolint
config_url = glue::glue("{repo_id}/sysreqs?all=true&distribution={distribution}&release={release}") # nolint
url = glue::glue("{base_url}{config_url}")
res = httr::GET(url)
r = httr::content(res)

purrr::map_df(r$requirements,
~tibble::tibble(pkg = .x$name, sys_libs = unlist(.x$requirements$packages)))

purrr::map_df(
r$requirements,
~ tibble::tibble(pkg = .x$name, sys_libs = unlist(.x$requirements$packages))
)
}
11 changes: 7 additions & 4 deletions R/posit_versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
get_posit_versions = function(type = c("connect", "workbench", "drivers")) {
type = match.arg(type)
fname = system.file("extdata", "versions", paste0(type, ".csv"),
mustWork = TRUE, package = "audit.base")
mustWork = TRUE, package = "audit.base"
)
versions = readr::read_csv(fname, comment = "#", col_types = c("c", "D", "c"))
versions = dplyr::arrange(versions, dplyr::desc(date))
return(versions)
Expand All @@ -33,8 +34,8 @@ audit_posit_version = function(posit_version, type = c("connect", "workbench", "
cli::cli_alert_info("The version {posit_version}, of Posit {type} isn't in the database")
} else if (row_number > 1L) {
newer_versions = versions[seq_len(row_number - 1), ]
no_of_versions = length(unique(newer_versions$version)) #nolint
no_of_cves = sum(!is.na(newer_versions$cve)) #nolint
no_of_versions = length(unique(newer_versions$version)) # nolint
no_of_cves = sum(!is.na(newer_versions$cve)) # nolint
cli::cli_alert_info("Posit {type} is {cli::col_red('out of date')}")
cli::cli_alert_info("There are {cli::col_red(no_of_versions)} newer versions that fix \\
{cli::col_red(no_of_cves)} CVEs")
Expand Down Expand Up @@ -65,7 +66,9 @@ lookup_version = function(posit_version, type) {

version_to_date = function(version) {
# Old style version
if (!is_new_version(version)) return(NA)
if (!is_new_version(version)) {
return(NA)
}
as_date = stringr::str_match_all(version, "^(202[0-9])\\.([01][0-9])")[[1]]
as.Date(paste(as_date[1, 2], as_date[1, 3], "01", sep = "-"))
}
Expand Down
24 changes: 15 additions & 9 deletions R/quarto-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ get_quarto_server_header = function(out) {
headers = dplyr::filter(headers, .data$primary_header)
headers = dplyr::arrange(headers, dplyr::desc(.data$status)) %>%
dplyr::mutate(
header_docs = purrr::map(.data$documentation, ~htmltools::a(href = .x, "(docs)")),
message = purrr::map2(message, .data$header_docs,
~ gt::html(paste(.x, as.character(.y))))) %>%
header_docs = purrr::map(.data$documentation, ~ htmltools::a(href = .x, "(docs)")),
message = purrr::map2(
message, .data$header_docs,
~ gt::html(paste(.x, as.character(.y)))
)
) %>%
dplyr::mutate(value = ifelse(is.na(.data$value), "-", .data$value)) |>
dplyr::distinct()
dplyr::select(headers, -"documentation", -"header_docs", -"primary_header")
dplyr::select(headers, -"documentation", -"header_docs", -"primary_header")
}

#' @rdname get_quarto_server_header
Expand All @@ -23,8 +26,10 @@ get_quarto_sys_deps = function(out) {
sys_deps = out$sys_deps
sys_deps %>%
dplyr::group_by(.data$sys_libs) %>%
dplyr::reframe(pkg = paste(sort(.data$pkg), collapse = ", "),
n = length(.data$sys_libs))
dplyr::reframe(
pkg = paste(sort(.data$pkg), collapse = ", "),
n = length(.data$sys_libs)
)
}

#' @rdname get_quarto_server_header
Expand All @@ -33,7 +38,8 @@ get_quarto_software_versions = function(out) {
software = out$versions
software = dplyr::select(software, "software", "version", "installed_version", "upgrade")
software$installed_version = ifelse(is.na(software$installed_version),
"Not installed", software$installed_version)
"Not installed", software$installed_version
)
software
}

Expand All @@ -57,8 +63,8 @@ get_quarto_posit_version_msg = function(out, type = c("connect", "workbench", "d
This could be because we've missed it or it's really old."
} else if (row_number > 1L) {
versions = get_posit_versions(type = type)
newer_versions = versions[seq_len(row_number - 1), ] #nolint
no_of_versions = length(unique(versions$version)) #nolint
newer_versions = versions[seq_len(row_number - 1), ] # nolint
no_of_versions = length(unique(versions$version)) # nolint
msg = "Posit {type} is out of date (v{posit_version}).
There are {no_of_versions} newer versions that fix {nrow(newer_versions)} CVEs.
The latest version is v{versions[1, 1]}."
Expand Down
14 changes: 10 additions & 4 deletions R/r6-base_check.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ base_check = R6::R6Class(
if (is.na(private$short)) stop("Missing short")
if (is.na(private$long)) stop("Missing long description")

c("group" = private$group,
c(
"group" = private$group,
"short" = private$short,
"context" = private$context,
"long" = private$long)
"long" = private$long
)
}
),
private = list(
Expand All @@ -47,10 +49,14 @@ base_check = R6::R6Class(
# Assume TRUE, unless explicitly FALSE
skip_test = function() {
config_path = file.path(private$dir, private$file)
if (!file.exists(config_path)) return(FALSE)
if (!file.exists(config_path)) {
return(FALSE)
}
config = yaml::read_yaml(config_path)

if (!(private$group %in% names(config))) return(FALSE)
if (!(private$group %in% names(config))) {
return(FALSE)
}
group = config[[private$group]]
return(isFALSE(group[[private$short]]))
},
Expand Down
14 changes: 8 additions & 6 deletions R/r6-logger.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ logger = R6::R6Class(
#' @param passed Logical, if skipped, the NA
stop_logger = function(passed) {
time_taken = Sys.time() - private$start_time
private$log = dplyr::tibble(group = private$group,
short = private$short,
context = private$context,
long = private$long,
passed = passed,
time_taken = round(time_taken, 2))
private$log = dplyr::tibble(
group = private$group,
short = private$short,
context = private$context,
long = private$long,
passed = passed,
time_taken = round(time_taken, 2)
)

msg_function(passed)

Expand Down
33 changes: 21 additions & 12 deletions R/software-versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,41 +48,49 @@ get_patch = function(v) as.numeric(unlist(stringr::str_match(v, "\\.([0-9]*)$")[
# Checks if versions are in the DB
in_db = function(installed) {
# latest/earliest versions stored in DB
software_range = get_latest_versions() %>%
software_range = get_latest_versions() %>%
dplyr::group_by(.data$software) %>%
dplyr::summarise(latest = max(.data$major), earliest = min(.data$major))

# Check installed packages are in software range
installed %>%
dplyr::left_join(software_range, by = c("software" = "software")) %>%
dplyr::mutate(to_old = .data$installed_major < .data$earliest,
to_new = .data$installed_major > .data$latest) %>%
dplyr::mutate(
to_old = .data$installed_major < .data$earliest,
to_new = .data$installed_major > .data$latest
) %>%
dplyr::select(-"latest", -"earliest")
}

add_upgrade_column = function(installed) {
versions = versions_to_display(installed)
versions = versions %>%
dplyr::mutate(upgrade = .data$patch > .data$installed_patch | .data$to_old) %>%
dplyr::mutate(upgrade = .data$patch > .data$installed_patch | .data$to_old) %>%
dplyr::mutate(upgrade = is.na(.data$upgrade) | .data$upgrade) %>%
dplyr::mutate(upgrade =
dplyr::if_else(!is.na(.data$to_new) & .data$to_new, FALSE, .data$upgrade))
dplyr::mutate(
upgrade =
dplyr::if_else(!is.na(.data$to_new) & .data$to_new, FALSE, .data$upgrade)
)
dplyr::select(versions, -"to_old", -"to_new")
}

versions_to_display = function(installed) {
latest = get_latest_versions()

min_installed = latest %>%
dplyr::full_join(installed, by = c("software" = "software",
"major" = "installed_major")) %>%
dplyr::full_join(installed, by = c(
"software" = "software",
"major" = "installed_major"
)) %>%
dplyr::group_by(.data$software, .drop = FALSE) %>%
dplyr::filter(!is.na(.data$installed_version)) %>%
dplyr::summarise(installed_version_num = max(.data$version_num, 3, na.rm = TRUE))

l = get_latest_versions() %>%
dplyr::full_join(installed, by = c("software" = "software",
"major" = "installed_major")) %>%
dplyr::full_join(installed, by = c(
"software" = "software",
"major" = "installed_major"
)) %>%
dplyr::full_join(min_installed, by = c("software" = "software")) %>%
dplyr::group_by(.data$software) %>%
dplyr::filter(.data$version_num <= .data$installed_version_num | is.na(.data$version_num))
Expand All @@ -91,8 +99,9 @@ versions_to_display = function(installed) {

get_latest_versions = function() {
versions_fname = system.file("extdata", "versions", "software.csv",
package = "audit.base",
mustWork = TRUE)
package = "audit.base",
mustWork = TRUE
)
versions = readr::read_csv(versions_fname, comment = "#", col_types = "fc")
versions$major = get_major(versions$version)
versions$patch = get_patch(versions$version)
Expand Down
7 changes: 5 additions & 2 deletions R/suppress.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
#' @param debug_level An integer, 0, 1, 2.
#' @export
get_suppress = function(debug_level) {
if (debug_level == 0) suppressMessages
else function(expr) expr
if (debug_level == 0) {
suppressMessages
} else {
function(expr) expr
}
}

#' @rdname get_suppress
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test-check_sys_deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ test_that("Testing sys deps", {
expect_true(all(is.integer(q$n)))

# Empty state
out = list(sys_deps = tibble::tibble(pkg = character(0),
sys_libs = character(0)))
out = list(sys_deps = tibble::tibble(
pkg = character(0),
sys_libs = character(0)
))
q = get_quarto_sys_deps(out)
expect_equal(colnames(q), c("sys_libs", "pkg", "n"))
})
6 changes: 4 additions & 2 deletions tests/testthat/test-get_pkg_requirements.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
test_that("Test pkg requirements", {
pkgs = get_pkg_requirements(distribution = "ubuntu",
release = "22.04")
pkgs = get_pkg_requirements(
distribution = "ubuntu",
release = "22.04"
)
expect_true(ncol(pkgs) == 2)
expect_true(nrow(pkgs) > 750)
expect_true("fftw" %in% pkgs$pkg)
Expand Down
Loading

0 comments on commit 9b0478f

Please sign in to comment.