Skip to content

Commit

Permalink
scan_deps() parses NAMESPACE as well (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi authored Dec 14, 2024
1 parent b68be9d commit db8be2c
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 17 deletions.
7 changes: 6 additions & 1 deletion R/scan-deps-dep-types.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
27 changes: 26 additions & 1 deletion R/scan-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
)
}
Expand Down Expand Up @@ -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
)
}
70 changes: 56 additions & 14 deletions tests/testthat/_snaps/scan-deps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<chr> <chr> <chr> <chr> <chr> <chr> <int> <int> <int>
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
<chr> <chr> <chr> <chr> <chr> <chr> <int> <int> <int>
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
<chr> <chr> <chr> <chr> <chr> <chr> <int> <int> <int>
1 fixtures/scan/NAMESPACE stats stats * prod stats 1 1 1
2 fixtures/scan/NAMESPACE utils utils * prod utils 1 1 1

73 changes: 73 additions & 0 deletions tests/testthat/fixtures/scan/NAMESPACE
Original file line number Diff line number Diff line change
@@ -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_")
13 changes: 12 additions & 1 deletion tests/testthat/test-scan-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

0 comments on commit db8be2c

Please sign in to comment.