diff --git a/R/scan-deps-dep-types.R b/R/scan-deps-dep-types.R index 67a177f4..e2ad9d40 100644 --- a/R/scan-deps-dep-types.R +++ b/R/scan-deps-dep-types.R @@ -5,7 +5,12 @@ get_dep_type_from_path <- function(paths, orig = NULL) { if (!is.null(orig)) { # for DESCRIPTION we detect the type from the file itself dsc <- basename(paths) == "DESCRIPTION" - tps[dsc] <- orig[dsc] + if (sum(dsc) > 0) { + withCallingHandlers( + tps[dsc] <- orig[dsc], + warning = function(w) browser() + ) + } } tps } diff --git a/R/scan-deps.R b/R/scan-deps.R index 2988ebf8..ab47fd71 100644 --- a/R/scan-deps.R +++ b/R/scan-deps.R @@ -87,6 +87,9 @@ scan_deps <- function(path = ".") { if (file.exists(file.path(path, "DESCRIPTION"))) { paths <- c(paths, "DESCRIPTION") } + if (file.exists(file.path(path, "NAMESPACE"))) { + paths <- c(paths, "NAMESPACE") + } full_paths <- normalizePath(file.path(path, paths)) deps_list <- lapply(full_paths, scan_path_deps) deps <- do.call("rbind", c(list(scan_deps_df()), deps_list)) @@ -149,7 +152,7 @@ scan_path_deps <- function(path) { deps <- readRDS(cache) if (!is.null(deps) && nrow(deps) > 0) { deps$path <- path - deps$type <- get_dep_type_from_path(path, deps$type) + deps$type <- get_dep_type_from_path(deps$path, deps$type) } return(deps) } @@ -204,6 +207,7 @@ scan_path_deps_do <- function(code, path) { ".qmd" = , ".rmd" = scan_path_deps_do_rmd(code, path), "DESCRIPTION" = scan_path_deps_do_dsc(code, path), + "NAMESPACE" = scan_path_deps_do_namespace(code, path), stop("Cannot parse ", ext, " file for dependencies, internal error") ) } @@ -896,3 +900,24 @@ scan_path_deps_do_dsc <- function(code, path) { code = deps$ref ) } + +# ------------------------------------------------------------------------- + +scan_path_deps_do_namespace <- function(code, path) { + tmp <- tempfile() + on.exit(unlink(tmp, recursive = TRUE), add = TRUE) + mkdirp(file.path(tmp, "pkg")) + if (is.raw(code)) { + writeBin(code, file.path(tmp, "pkg", "NAMESPACE")) + } else { + writeLines(code, file.path(tmp, "pkg", "NAMESPACE")) + } + info <- parseNamespaceFile(package = "pkg", package.lib = tmp) + pkg <- unique(vcapply(info$imports, "[[", 1)) + scan_deps_df( + path = path, + package = pkg, + type = "prod", + code = pkg + ) +} diff --git a/tests/testthat/_snaps/scan-deps.md b/tests/testthat/_snaps/scan-deps.md index 9c5ccea7..f797d989 100644 --- a/tests/testthat/_snaps/scan-deps.md +++ b/tests/testthat/_snaps/scan-deps.md @@ -322,20 +322,62 @@ # scan_path_deps_do_dsc Code - scan_path_deps_do_dsc(readLines(path), basename(path)) + print(scan_path_deps_do_dsc(readLines(path), basename(path)), n = Inf) Output # A data frame: 42 x 9 - path ref package version type code start_row start_column start_byte - - 1 DESCRIPTION callr callr >=3.3.1 prod callr 1 1 1 - 2 DESCRIPTION r-lib/cli cli >=3.6.0 prod r-lib/cli 1 1 1 - 3 DESCRIPTION curl curl * prod curl 1 1 1 - 4 DESCRIPTION desc desc >=1.4.3 prod desc 1 1 1 - 5 DESCRIPTION filelock filelock >=1.0.2 prod filelock 1 1 1 - 6 DESCRIPTION jsonlite jsonlite * prod jsonlite 1 1 1 - 7 DESCRIPTION lpSolve lpSolve * prod lpSolve 1 1 1 - 8 DESCRIPTION pkgbuild pkgbuild >=1.0.2 prod pkgbuild 1 1 1 - 9 DESCRIPTION pkgcache pkgcache >=2.2.0 prod pkgcache 1 1 1 - 10 DESCRIPTION processx processx >=3.4.2 prod processx 1 1 1 - # i 32 more rows + path ref package version type code start_row start_column start_byte + + 1 DESCRIPTION callr callr >=3.3.1 prod callr 1 1 1 + 2 DESCRIPTION r-lib/cli cli >=3.6.0 prod r-lib/cli 1 1 1 + 3 DESCRIPTION curl curl * prod curl 1 1 1 + 4 DESCRIPTION desc desc >=1.4.3 prod desc 1 1 1 + 5 DESCRIPTION filelock filelock >=1.0.2 prod filelock 1 1 1 + 6 DESCRIPTION jsonlite jsonlite * prod jsonlite 1 1 1 + 7 DESCRIPTION lpSolve lpSolve * prod lpSolve 1 1 1 + 8 DESCRIPTION pkgbuild pkgbuild >=1.0.2 prod pkgbuild 1 1 1 + 9 DESCRIPTION pkgcache pkgcache >=2.2.0 prod pkgcache 1 1 1 + 10 DESCRIPTION processx processx >=3.4.2 prod processx 1 1 1 + 11 DESCRIPTION ps ps * prod ps 1 1 1 + 12 DESCRIPTION R6 R6 * prod R6 1 1 1 + 13 DESCRIPTION zip zip >=2.3.0 prod zip 1 1 1 + 14 DESCRIPTION asciicast asciicast >=2.2.0.9000 test asciicast 1 1 1 + 15 DESCRIPTION codetools codetools * test codetools 1 1 1 + 16 DESCRIPTION covr covr * test covr 1 1 1 + 17 DESCRIPTION debugme debugme * test debugme 1 1 1 + 18 DESCRIPTION fansi fansi * test fansi 1 1 1 + 19 DESCRIPTION fs fs * test fs 1 1 1 + 20 DESCRIPTION gh gh * test gh 1 1 1 + 21 DESCRIPTION gitcreds gitcreds * test gitcreds 1 1 1 + 22 DESCRIPTION glue glue * test glue 1 1 1 + 23 DESCRIPTION htmlwidgets htmlwidgets * test htmlwidgets 1 1 1 + 24 DESCRIPTION mockery mockery * test mockery 1 1 1 + 25 DESCRIPTION pak pak * test pak 1 1 1 + 26 DESCRIPTION pingr pingr >=2.0.0 test pingr 1 1 1 + 27 DESCRIPTION rmarkdown rmarkdown * test rmarkdown 1 1 1 + 28 DESCRIPTION rstudioapi rstudioapi * test rstudioapi 1 1 1 + 29 DESCRIPTION spelling spelling * test spelling 1 1 1 + 30 DESCRIPTION svglite svglite * test svglite 1 1 1 + 31 DESCRIPTION testthat testthat >=3.2.0 test testthat 1 1 1 + 32 DESCRIPTION tibble tibble * test tibble 1 1 1 + 33 DESCRIPTION webfakes webfakes >=1.1.5.9000 test webfakes 1 1 1 + 34 DESCRIPTION withr withr >=2.1.1 test withr 1 1 1 + 35 DESCRIPTION gh gh * dev gh 1 1 1 + 36 DESCRIPTION pkgsearch pkgsearch * dev pkgsearch 1 1 1 + 37 DESCRIPTION withr withr >=2.1.1 dev withr 1 1 1 + 38 DESCRIPTION r-lib/asciicast asciicast * test r-lib/asciicast 1 1 1 + 39 DESCRIPTION covr covr * test covr 1 1 1 + 40 DESCRIPTION r-lib/asciicast asciicast * dev r-lib/asciicast 1 1 1 + 41 DESCRIPTION pkgdown pkgdown >=2.0.2 dev pkgdown 1 1 1 + 42 DESCRIPTION tidyverse/tidytemplate tidytemplate * dev tidyverse/tidytemplate 1 1 1 + +# scan_path_deps_do_namespace + + Code + print(scan_path_deps_do_namespace(readBin(path, "raw", 10000), path), n = Inf) + Output + # A data frame: 2 x 9 + path ref package version type code start_row start_column start_byte + + 1 fixtures/scan/NAMESPACE stats stats * prod stats 1 1 1 + 2 fixtures/scan/NAMESPACE utils utils * prod utils 1 1 1 diff --git a/tests/testthat/fixtures/scan/NAMESPACE b/tests/testthat/fixtures/scan/NAMESPACE new file mode 100644 index 00000000..5f50ef03 --- /dev/null +++ b/tests/testthat/fixtures/scan/NAMESPACE @@ -0,0 +1,73 @@ +# Generated by roxygen2: do not edit by hand + +S3method("[",pkg_resolution_result) +S3method("[",pkg_scan_deps) +S3method("[",pkg_solution_result) +S3method("[",pkg_sysreqs_check_result) +S3method("[",pkgplan_downloads) +S3method(format,package_build_error) +S3method(format,package_packaging_error) +S3method(format,package_uncompress_error) +S3method(format,pkg_name_check) +S3method(format,pkg_name_check_basics) +S3method(format,pkg_name_check_sentiment) +S3method(format,pkg_name_check_urban) +S3method(format,pkg_name_check_wikipedia) +S3method(format,pkg_name_check_wiktionary) +S3method(format,pkg_scan_deps) +S3method(format,pkg_solution_failures) +S3method(format,pkg_solution_result) +S3method(format,pkg_sysreqs_check_result) +S3method(format,pkgplan_lp_problem) +S3method(format,remote_ref) +S3method(print,package_build_error) +S3method(print,package_packaging_error) +S3method(print,package_uncompress_error) +S3method(print,pkg_name_check) +S3method(print,pkg_scan_deps) +S3method(print,pkg_solution_result) +S3method(print,pkg_sysreqs_check_result) +S3method(print,pkginstall_result) +S3method(print,pkgplan_lp_problem) +S3method(print,snapshot) +S3method(snapshot,pkg_resolution_result) +export(as_pkg_dependencies) +export(current_config) +export(current_r_platform) +export(default_platforms) +export(ghr) +export(ghrepo) +export(install_package_plan) +export(is_valid_package_name) +export(lib_status) +export(new_pkg_deps) +export(new_pkg_download_proposal) +export(new_pkg_installation_plan) +export(new_pkg_installation_proposal) +export(parse_pkg_ref) +export(parse_pkg_refs) +export(pkg_build) +export(pkg_dep_types) +export(pkg_dep_types_hard) +export(pkg_dep_types_soft) +export(pkg_deps) +export(pkg_download_proposal) +export(pkg_installation_plan) +export(pkg_installation_proposal) +export(pkg_name_check) +export(pkg_rx) +export(repo) +export(scan_deps) +export(sysreqs_check_installed) +export(sysreqs_db_list) +export(sysreqs_db_match) +export(sysreqs_db_update) +export(sysreqs_fix_installed) +export(sysreqs_install_plan) +export(sysreqs_is_supported) +export(sysreqs_list_system_packages) +export(sysreqs_platforms) +importFrom(stats,na.omit) +importFrom(utils,modifyList) +importFrom(utils,untar) +useDynLib(pkgdepends, .registration = TRUE, .fixes = "c_") diff --git a/tests/testthat/test-scan-deps.R b/tests/testthat/test-scan-deps.R index 32d21235..83cd0616 100644 --- a/tests/testthat/test-scan-deps.R +++ b/tests/testthat/test-scan-deps.R @@ -469,6 +469,17 @@ test_that("scan_path_deps_do_dsc", { local_reproducible_output(width = 500) path <- test_path("fixtures/scan/DESCRIPTION") expect_snapshot({ - scan_path_deps_do_dsc(readLines(path), basename(path)) + print(scan_path_deps_do_dsc(readLines(path), basename(path)), n = Inf) + }) +}) + +test_that("scan_path_deps_do_namespace", { + local_reproducible_output(width = 500) + path <- test_path("fixtures/scan/NAMESPACE") + expect_snapshot({ + print(scan_path_deps_do_namespace( + readBin(path, "raw", 10000), + path + ), n = Inf) }) })