Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for dependency scanning #395

Merged
merged 9 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ Meta
/man/_cache
/man/macros/eval2.Rd
/revdep
/src/*.gcda
*.gcda
*.gcno
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 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)
Expand All @@ -13,6 +14,7 @@ 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)
Expand All @@ -22,6 +24,7 @@ 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)
Expand Down
45 changes: 45 additions & 0 deletions R/scan-deps-print.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' @export

format.pkg_scan_deps <- function(x, ...) {
labels <- c(
prod = "Dependencies",
test = "Test dependencies",
dev = "Development dependencies",
# TODO: generic label for others
NULL
)
lns <- lapply(seq_along(labels), function(i) {
deps <- x[x$type == names(labels)[i], , drop = FALSE]
if (nrow(deps) == 0) return(NULL)
fls <- tapply(deps$path, deps$package, "c", simplify = FALSE)
fls[] <- lapply(fls, unique)
fls <- vcapply(fls, paste, collapse = ", ")
pkg <- format(names(fls))
flsw <- cli::console_width() - nchar(pkg[1]) - 5
c(
"", cli::col_yellow(paste0(labels[i], ":")),
paste0(
cli::col_grey("+ "),
cli::col_blue(pkg),
cli::col_grey(" @ "),
cli::col_silver(cli::ansi_strtrim(fls, flsw))
)
)
})

unlist(lns)
}

#' @export

print.pkg_scan_deps <- function(x, ...) {
writeLines(format(x, ...))
invisible(x)
}

#' @export

`[.pkg_scan_deps` <- function (x, i, j, drop = FALSE) {
class(x) <- setdiff(class(x), "pkg_scan_deps")
NextMethod("[")
}
23 changes: 15 additions & 8 deletions R/scan-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@

scan_deps <- function(path = ".") {
path <- tryCatch(find_project_root(path), error = function(...) path)
paths <- dir(path, pattern = "[.](R|r|Rmd|rmd)$", recursive = TRUE)
paths <- dir(path, pattern = "[.](R|r|Rmd|rmd|qmd)$", recursive = TRUE)
full_paths <- normalizePath(file.path(path, paths))
deps_list <- lapply(full_paths, scan_path_deps)
deps <- do.call("rbind", c(list(scan_path_deps_empty()), deps_list))
# write back the relative paths
deps$path <- paths[match(deps$path, full_paths)]
deps$type <- get_dep_type_from_path(deps$path)
class(deps) <- c("pkg_scan_deps", class(deps))
deps
}

Expand Down Expand Up @@ -331,6 +332,7 @@ scan_pat_deps_do_db_hits <- function(hits, path) {
)
}

# nocov start
prot_xfun_pkg_attach <- function(..., install, message) { }
prot_xfun_pkg_attach2 <- function(...) { }
prot_pacman_p_load <- function(
Expand Down Expand Up @@ -366,6 +368,7 @@ prot_testthat_test_dir <- function(
path, filter = NULL, reporter = NULL, ...) {
}
prot_testthat_test_file <- function(path, reporter = NULL, ...) { }
# nocov end

safe_parse_pkg_from_call <- function(ns, fn, code) {
tryCatch(
Expand All @@ -374,7 +377,7 @@ safe_parse_pkg_from_call <- function(ns, fn, code) {
)
}

parse_pkg_from_call <- function(ns, fn, code) {
parse_pkg_from_call_match <- function(fn, code) {
expr <- parse(text = code, keep.source = FALSE)
fun <- switch(fn,
"library" = base::library,
Expand All @@ -399,13 +402,17 @@ parse_pkg_from_call <- function(ns, fn, code) {
"test_dir" = prot_testthat_test_dir,
"test_file" = prot_testthat_test_file
)
matched <- match.call(fun, expr, expand.dots = FALSE)
match.call(fun, expr, expand.dots = FALSE)
}

parse_pkg_from_call <- function(ns, fn, code) {
matched <- parse_pkg_from_call_match(fn, code)
switch(fn,
"library" = , "require" =
parse_pkg_from_call_library(ns, fs, matched),
parse_pkg_from_call_library(ns, fn, matched),
"loadNamespace" = , "requireNamespace" =
parse_pkg_from_call_loadNamespace(ns, fn, matched),
"pkg_attache" = , "pkg_attach2" =
parse_pkg_from_call_loadnamespace(ns, fn, matched),
"pkg_attach" = , "pkg_attach2" =
parse_pkg_from_call_xfun(ns, fn, matched),
"p_load" =
parse_pkg_from_call_pacman(ns, fn, matched),
Expand Down Expand Up @@ -445,7 +452,7 @@ parse_pkg_from_call_library <- function(ns, fn, matched) {
NULL
}

parse_pkg_from_call_loadNamespace <- function(ns, fn, matched) {
parse_pkg_from_call_loadnamespace <- function(ns, fn, matched) {
if (!is.na(ns) && ns != "base") return(NULL)
pkg <- matched[["package"]]
if (is.character(pkg) && length(pkg) == 1) {
Expand All @@ -471,7 +478,7 @@ parse_pkg_from_call_pacman <- function(ns, fn, matched) {

# character vector or scalar
char <- matched[["char"]]
if (char[[1]] == quote(c)) {
if (length(char) > 0 && char[[1]] == quote(c)) {
pkgs <- c(pkgs, as.list(char[-1]))
} else if (is.character(char)) {
pkgs <- c(pkgs, as.list(char))
Expand Down
2 changes: 1 addition & 1 deletion src/tree-sitter/markdown-inline/parser.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "tree_sitter/parser.h"

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif

#ifdef _MSC_VER
Expand Down
6 changes: 3 additions & 3 deletions src/tree-sitter/markdown-inline/tree_sitter/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ extern "C" {
#ifdef _MSC_VER
#pragma warning(disable : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-variable"
#endif

#define Array(T) \
Expand Down Expand Up @@ -280,7 +280,7 @@ static inline void _array__splice(Array *self, size_t element_size,
#ifdef _MSC_VER
#pragma warning(default : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
# pragma GCC diagnostic pop
#endif

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion src/tree-sitter/markdown/parser.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "tree_sitter/parser.h"

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif

#ifdef _MSC_VER
Expand Down
6 changes: 3 additions & 3 deletions src/tree-sitter/markdown/tree_sitter/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ extern "C" {
#ifdef _MSC_VER
#pragma warning(disable : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-variable"
#endif

#define Array(T) \
Expand Down Expand Up @@ -280,7 +280,7 @@ static inline void _array__splice(Array *self, size_t element_size,
#ifdef _MSC_VER
#pragma warning(default : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
# pragma GCC diagnostic pop
#endif

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion src/tree-sitter/r/parser.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "tree_sitter/parser.h"

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif

#define LANGUAGE_VERSION 14
Expand Down
6 changes: 3 additions & 3 deletions src/tree-sitter/r/tree_sitter/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ extern "C" {
#ifdef _MSC_VER
#pragma warning(disable : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-variable"
#endif

#define Array(T) \
Expand Down Expand Up @@ -280,7 +280,7 @@ static inline void _array__splice(Array *self, size_t element_size,
#ifdef _MSC_VER
#pragma warning(default : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
# pragma GCC diagnostic pop
#endif

#ifdef __cplusplus
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/scan-deps-dep-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# get_dep_type_from_path

Code
get_dep_type_from_path(c("R/foo.R", "man/roxygen/meta.R", "tests/test-1.R",
"test/test-2.R"))
Output
[1] "prod" "dev" "test" "test"

Loading
Loading