From 9b0478fe28f1f883dfdd62e0a6f6633ddd816998 Mon Sep 17 00:00:00 2001 From: Colin Gillespie Date: Mon, 24 Jun 2024 19:32:03 +0100 Subject: [PATCH] chore: Use jrStyle (#32) --- DESCRIPTION | 2 +- NEWS.md | 3 ++ R/check.R | 2 +- R/check_server_headers.R | 10 ++++--- R/check_sys_deps.R | 2 +- R/config.R | 21 ++++++++------ R/create_software_tibble.R | 12 +++++--- R/get_pkg_requirements.R | 9 +++--- R/posit_versions.R | 11 +++++--- R/quarto-helpers.R | 24 ++++++++++------ R/r6-base_check.R | 14 ++++++--- R/r6-logger.R | 14 +++++---- R/software-versions.R | 33 ++++++++++++++-------- R/suppress.R | 7 +++-- tests/testthat/test-check_sys_deps.R | 6 ++-- tests/testthat/test-get_pkg_requirements.R | 6 ++-- tests/testthat/test-posit_versions.R | 22 +++++++++------ tests/testthat/test-software-versions.R | 20 ++++++++----- 18 files changed, 138 insertions(+), 80 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 21b1a0c..7afbf6c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", , "info@jumpingrivers.com", role = c("aut", "cre")) Description: Base package for sharing classes between posit audit diff --git a/NEWS.md b/NEWS.md index 13fb2d4..7c7a1cd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/check.R b/R/check.R index b99f452..1e75dc4 100644 --- a/R/check.R +++ b/R/check.R @@ -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 diff --git a/R/check_server_headers.R b/R/check_server_headers.R index 37b2cef..9dd7cb2 100644 --- a/R/check_server_headers.R +++ b/R/check_server_headers.R @@ -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 { diff --git a/R/check_sys_deps.R b/R/check_sys_deps.R index 69066c8..5e8188f 100644 --- a/R/check_sys_deps.R +++ b/R/check_sys_deps.R @@ -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") } diff --git a/R/config.R b/R/config.R index 2218341..3a058b7 100644 --- a/R/config.R +++ b/R/config.R @@ -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 @@ -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]] } } diff --git a/R/create_software_tibble.R b/R/create_software_tibble.R index fd0898c..41fe0b1 100644 --- a/R/create_software_tibble.R +++ b/R/create_software_tibble.R @@ -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)) %>% @@ -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) } diff --git a/R/get_pkg_requirements.R b/R/get_pkg_requirements.R index ac0d192..8a89906 100644 --- a/R/get_pkg_requirements.R +++ b/R/get_pkg_requirements.R @@ -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)) + ) } diff --git a/R/posit_versions.R b/R/posit_versions.R index a24cdaf..cafc20f 100644 --- a/R/posit_versions.R +++ b/R/posit_versions.R @@ -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) @@ -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") @@ -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 = "-")) } diff --git a/R/quarto-helpers.R b/R/quarto-helpers.R index 2335c02..6b570e4 100644 --- a/R/quarto-helpers.R +++ b/R/quarto-helpers.R @@ -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 @@ -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 @@ -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 } @@ -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]}." diff --git a/R/r6-base_check.R b/R/r6-base_check.R index 9fb929d..d2db902 100644 --- a/R/r6-base_check.R +++ b/R/r6-base_check.R @@ -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( @@ -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]])) }, diff --git a/R/r6-logger.R b/R/r6-logger.R index c735346..5fdc7cc 100644 --- a/R/r6-logger.R +++ b/R/r6-logger.R @@ -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) diff --git a/R/software-versions.R b/R/software-versions.R index 4f27444..bfdfd78 100644 --- a/R/software-versions.R +++ b/R/software-versions.R @@ -48,25 +48,29 @@ 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") } @@ -74,15 +78,19 @@ 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)) @@ -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) diff --git a/R/suppress.R b/R/suppress.R index f92faaf..d04a8aa 100644 --- a/R/suppress.R +++ b/R/suppress.R @@ -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 diff --git a/tests/testthat/test-check_sys_deps.R b/tests/testthat/test-check_sys_deps.R index d9f4447..1985407 100644 --- a/tests/testthat/test-check_sys_deps.R +++ b/tests/testthat/test-check_sys_deps.R @@ -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")) }) diff --git a/tests/testthat/test-get_pkg_requirements.R b/tests/testthat/test-get_pkg_requirements.R index 32e3fe9..b691d03 100644 --- a/tests/testthat/test-get_pkg_requirements.R +++ b/tests/testthat/test-get_pkg_requirements.R @@ -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) diff --git a/tests/testthat/test-posit_versions.R b/tests/testthat/test-posit_versions.R index 6d713b6..5a7650c 100644 --- a/tests/testthat/test-posit_versions.R +++ b/tests/testthat/test-posit_versions.R @@ -1,5 +1,4 @@ test_that("Testing check server", { - types = c("workbench", "connect", "drivers") for (type in types) { versions = get_posit_versions(type) @@ -12,24 +11,31 @@ test_that("Testing check server", { latest_version = versions[1, ]$version expect_message(audit_posit_version(latest_version, type), - regexp = "up to date") + regexp = "up to date" + ) expect_message(audit_posit_version(paste0(latest_version, ".pro1"), type), - regexp = "up to date") + regexp = "up to date" + ) # Version not in DB expect_equal(lookup_version(posit_version = "2029.01.01", type), 1) # Really old version not in the DB - expect_equal(lookup_version(posit_version = "2010.01.01", type), - NA_integer_) + expect_equal( + lookup_version(posit_version = "2010.01.01", type), + NA_integer_ + ) - expect_equal(lookup_version(posit_version = "Not in DB", type), - NA_integer_) + expect_equal( + lookup_version(posit_version = "Not in DB", type), + NA_integer_ + ) # No version in DB v = lookup_version(posit_version = "2022.10.31", type) expect_true(is.na(v)) expect_message(audit_posit_version("2022.10.0", type), - regexp = "out of date") + regexp = "out of date" + ) }) diff --git a/tests/testthat/test-software-versions.R b/tests/testthat/test-software-versions.R index 52db9fc..7f87b48 100644 --- a/tests/testthat/test-software-versions.R +++ b/tests/testthat/test-software-versions.R @@ -4,12 +4,16 @@ test_that("Testing software versions", { expect_equal(colnames(versions), v_colnames) expect_gte(nrow(versions), 16) - installed = tibble::tibble(software = c("r", "r", "python"), - installed_version = c("3.4.3", "3.5.3", "3.7.1")) + installed = tibble::tibble( + software = c("r", "r", "python"), + installed_version = c("3.4.3", "3.5.3", "3.7.1") + ) augmented = augment_installed(installed) - expect_equal(colnames(augmented), c(v_colnames[1:4], - paste0("installed_", c("version", "patch")), - "upgrade")) + expect_equal(colnames(augmented), c( + v_colnames[1:4], + paste0("installed_", c("version", "patch")), + "upgrade" + )) expect_equal(sum(!augmented$upgrade), 1) # Check specific packages @@ -28,8 +32,10 @@ test_that("Testing software versions", { }) test_that("Testing software versions quarto output", { - installed = tibble::tibble(software = c("r", "r", "python"), - installed_version = c("3.4.3", "3.5.3", "3.7.1")) + installed = tibble::tibble( + software = c("r", "r", "python"), + installed_version = c("3.4.3", "3.5.3", "3.7.1") + ) augmented = suppressMessages(augment_installed(installed)) out = list(versions = augmented) q = get_quarto_software_versions(out)