diff --git a/DESCRIPTION b/DESCRIPTION index 4e21fbd..f86ef89 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: box.linters Title: Linters for 'box' Modules -Version: 0.9.1 +Version: 0.9.1.9001 Authors@R: c( person("Ricardo Rodrigo", "Basa", role = c("aut", "cre"), email = "opensource+rodrigo@appsilon.com"), diff --git a/NEWS.md b/NEWS.md index bd438a4..6d08564 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# box.linters (development version) + +* [Bug fix] Allow multiple `box::use(pkg)` calls (#111) + # box.linters 0.9.1 * Handle `box` recommended method of testing private methods. diff --git a/R/box_module_usage_helper_functions.R b/R/box_module_usage_helper_functions.R index 815b3a4..c65e915 100644 --- a/R/box_module_usage_helper_functions.R +++ b/R/box_module_usage_helper_functions.R @@ -55,7 +55,11 @@ get_attached_modules <- function(xml) { xpath_whole_modules <- paste(box_module_base_path(), whole_module_imports) xml_whole_modules <- xml2::xml_find_all(xml, xpath_whole_modules) - aliased_whole_modules <- paste0(xml2::xml_text(xml_whole_modules), collapse = "") + xml_whole_modules_text <- xml2::xml_text(xml_whole_modules) + + hacky_comma_fix <- hacky_comma_fix(xml_whole_modules_text) + + aliased_whole_modules <- paste0(hacky_comma_fix, collapse = "") mods <- strsplit(gsub("`", "", aliased_whole_modules), ",")[[1]] output <- do.call(rbind, strsplit(mods, "=")) diff --git a/R/box_package_usage_helper_functions.R b/R/box_package_usage_helper_functions.R index fc17e3f..9e56672 100644 --- a/R/box_package_usage_helper_functions.R +++ b/R/box_package_usage_helper_functions.R @@ -47,7 +47,11 @@ get_attached_packages <- function(xml) { xpath_whole_packages <- paste(box_package_base_path(), whole_package_imports) xml_whole_packages <- xml2::xml_find_all(xml, xpath_whole_packages) - aliased_whole_packages <- paste(xml2::xml_text(xml_whole_packages), collapse = "") + xml_whole_packages_text <- xml2::xml_text(xml_whole_packages) + + hacky_comma_fix <- hacky_comma_fix(xml_whole_packages_text) + + aliased_whole_packages <- paste(hacky_comma_fix, collapse = "") pkgs <- strsplit(gsub("`", "", aliased_whole_packages), ",")[[1]] output <- do.call(rbind, strsplit(pkgs, "=")) @@ -183,3 +187,28 @@ get_attached_pkg_functions <- function(xml) { text = attached_functions$text ) } + +#' @keywords internal +hacky_comma_fix <- function(pkg_imports) { + punc <- c("=", ",") + + if (rlang::is_empty(pkg_imports)) { + return(pkg_imports) + } + + hacky_comma_fix <- pkg_imports[1] + if (length(pkg_imports) == 1) { + return(hacky_comma_fix) + } + for (i in seq(2, length(pkg_imports))) { + curr <- pkg_imports[i] + prev <- pkg_imports[i - 1] + + if (!curr %in% punc && !prev %in% punc) { + hacky_comma_fix <- c(hacky_comma_fix, ",", curr) + } else { + hacky_comma_fix <- c(hacky_comma_fix, curr) + } + } + hacky_comma_fix +} diff --git a/tests/testthat/test-box_module_usage_halper_functions.R b/tests/testthat/test-box_module_usage_halper_functions.R index 3457af3..5aa6291 100644 --- a/tests/testthat/test-box_module_usage_halper_functions.R +++ b/tests/testthat/test-box_module_usage_halper_functions.R @@ -29,6 +29,28 @@ test_that("get_attached_modules returns correct list of imported whole modules", expect_setequal(results$nested$module_b, module_b_objects) }) +test_that("get_attached_modules returns correct list of imported whole modules in separate calls", { + whole_imported_modules <- " + box::use(path/to/module_a) + box::use(alias = path/to/module_b, path/to/module_c) + " + + xml_whole_imported_modules <- code_to_xml_expr(whole_imported_modules) + results <- get_attached_modules(xml_whole_imported_modules) + expected_results <- c("module_a", "alias", "module_c") + + expect_equal(names(results$nested), expected_results) + + module_a_objects <- c("a_fun_a", "a_fun_b") + expect_setequal(results$nested$module_a, module_a_objects) + + module_b_objects <- c("b_fun_a", "b_fun_b", "b_obj_a") + expect_setequal(results$nested$alias, module_b_objects) + + module_c_objects <- c("c_fun_a", "c_fun_b", "c_obj_a") + expect_setequal(results$nested$module_c, module_c_objects) +}) + test_that("get_attached_modules does not return modules imported with '...'", { whole_imported_modules <- " box::use( @@ -247,6 +269,26 @@ test_that("get_attached_mod_three_dots returns correct list of imported function expect_setequal(results$text, c(module_a_objects, module_b_objects)) }) +test_that( + "get_attached_mod_three_dots returns correct list of imported functions in separate calls", { + three_dots_import <- " + box::use(path/to/module_a[...]) + box::use(path/to/module_b[...]) + " + + xml_three_dots_modules <- code_to_xml_expr(three_dots_import) + results <- get_attached_mod_three_dots(xml_three_dots_modules) + + module_a_objects <- c("a_fun_a", "a_fun_b") + module_b_objects <- c("b_fun_a", "b_fun_b", "b_obj_a") + + expect_setequal(results$nested$module_a, module_a_objects) + expect_setequal(results$nested$module_b, module_b_objects) + + expect_setequal(results$text, c(module_a_objects, module_b_objects)) + } +) + test_that("get_attached_mod_three_dots does not return whole imported modules", { three_dots_import <- " box::use( diff --git a/tests/testthat/test-box_package_usage_helper_functions.R b/tests/testthat/test-box_package_usage_helper_functions.R index 0841902..eb2c9e2 100644 --- a/tests/testthat/test-box_package_usage_helper_functions.R +++ b/tests/testthat/test-box_package_usage_helper_functions.R @@ -27,6 +27,36 @@ test_that("get_attached_packages returns correct list of imported whole packages expect_setequal(results$nested$stringr, stringr_functions) }) +test_that( + "get_attached_packages returns correct list of imported whole packages in +separate box::use calls", { + whole_imported_packages <- " + box::use( + dplyr, + stringr, + ) + box::use( + fs + ) + " + + xml_whole_imported_packages <- code_to_xml_expr(whole_imported_packages) + results <- get_attached_packages(xml_whole_imported_packages) + expected_results <- c("dplyr", "stringr", "fs") + + expect_equal(names(results$nested), expected_results) + + dplyr_functions <- getNamespaceExports("dplyr") + expect_setequal(results$nested$dplyr, dplyr_functions) + + stringr_functions <- getNamespaceExports("stringr") + expect_setequal(results$nested$stringr, stringr_functions) + + fs_functions <- getNamespaceExports("fs") + expect_setequal(results$nested$fs, fs_functions) + } +) + test_that("get_attached_packages does not return packages imported with '...'", { whole_imported_packages <- " box::use( @@ -133,6 +163,26 @@ test_that("get_attached_pkg_three_dots returns correct list of imported function expect_setequal(results$text, c(dplyr_results, stringr_results)) }) +test_that( + "get_attached_pkg_three_dots separate box::use returns correct list of imported functions", { + three_dots_import <- " + box::use(dplyr[...]) + box::use(stringr[...]) + " + + xml_three_dots_packages <- code_to_xml_expr(three_dots_import) + results <- get_attached_pkg_three_dots(xml_three_dots_packages) + + dplyr_results <- getNamespaceExports("dplyr") + stringr_results <- getNamespaceExports("stringr") + + expect_setequal(results$nested$dplyr, dplyr_results) + expect_setequal(results$nested$stringr, stringr_results) + + expect_setequal(results$text, c(dplyr_results, stringr_results)) + } +) + test_that("get_attached_pkg_three_dots does not return whole imported packages", { three_dots_import <- " box::use(