Skip to content

Commit

Permalink
feat: Improve att_amend_desc with Suggest from examples
Browse files Browse the repository at this point in the history
- Used att_from_examples
- Complete dummypackage with an example
- Update test with the new dummypackage

Issue #103
  • Loading branch information
MurielleDelmotte committed Aug 1, 2024
1 parent de9aa0a commit 85297a2
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 10 deletions.
12 changes: 11 additions & 1 deletion R/att_to_description.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Amend DESCRIPTION with dependencies read from package code parsing
#'
#' Amend package DESCRIPTION file with the list of dependencies extracted from
#' R, tests, vignettes files.
#' R, examples, tests, vignettes files.
#' att_to_desc_from_pkg() is an alias of att_amend_desc(),
#' for the correspondence with [att_to_desc_from_is()].
#'
Expand All @@ -19,6 +19,7 @@
#' @inheritParams att_from_namespace
#' @inheritParams att_to_desc_from_is
#' @inheritParams att_from_rmds
#' @inheritParams att_from_examples
#'
#' @importFrom desc description
#'
Expand Down Expand Up @@ -88,6 +89,8 @@ att_amend_desc <- function(path = ".",
on.exit(setwd(old))
}



path <- normalizePath(path)

# decide whether to use or update config file ----
Expand Down Expand Up @@ -196,6 +199,13 @@ att_amend_desc <- function(path = ".",

# Suggests ----
suggests <- NULL

# Get suggests in examples and remove if already in imports
if (dir.r != "") {
ex <- att_from_examples(dir.r = dir.r)
suggests <- c(suggests, ex[!ex %in% imports])
}

# Get suggests in vignettes and remove if already in imports
if (!grepl("^$|^\\s+$$", dir.v)) {
vg <- att_from_rmds(dir.v, inside_rmd = inside_rmd)
Expand Down
3 changes: 3 additions & 0 deletions inst/dummypackage/R/my_mean.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#'
#' @export
#' @importFrom magrittr %>%
#' @examples
#' # example code
#' library(utils)
my_mean <- function(x){
x <- x %>% stats::na.omit()
1+1
Expand Down
2 changes: 1 addition & 1 deletion man/att_amend_desc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/attachment-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 100 additions & 6 deletions tests/testthat/test-amend-description.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ test_that("att_amend_desc updates description", {
expect_equal(desc_file[w.depends + 6], " glue,")
expect_equal(desc_file[w.depends + 7], " knitr,")
expect_equal(desc_file[w.depends + 8], " rmarkdown,")
expect_equal(desc_file[w.depends + 9], " testthat")
expect_equal(desc_file[w.depends + 10], "LinkingTo:" )
expect_equal(desc_file[w.depends + 11], " Rcpp")
expect_equal(desc_file[w.depends + 9], " testthat,")
expect_equal(desc_file[w.depends + 10], " utils")
expect_equal(desc_file[w.depends + 11], "LinkingTo:" )
expect_equal(desc_file[w.depends + 12], " Rcpp")
# base does not appear
expect_false(all(grepl("base", desc_file)))
# utils is removed
Expand Down Expand Up @@ -485,12 +486,105 @@ library(ggplot3)
expect_equal(desc_file[w.depends + 7], " glue,")
expect_equal(desc_file[w.depends + 8], " knitr,")
expect_equal(desc_file[w.depends + 9], " rmarkdown,")
expect_equal(desc_file[w.depends + 10], " testthat")
expect_equal(desc_file[w.depends + 11], "LinkingTo:" )
expect_equal(desc_file[w.depends + 12], " Rcpp")
expect_equal(desc_file[w.depends + 10], " testthat,")
expect_equal(desc_file[w.depends + 11], " utils")
expect_equal(desc_file[w.depends + 12], "LinkingTo:" )
expect_equal(desc_file[w.depends + 13], " Rcpp")


# Clean after
unlink(dummypackage, recursive = TRUE)
})


# Test update desc when packages in examples ----
test_that("if a package is used in an example, it is added to Suggests packages", {
# Copy package in a temporary directory
tmpdir <- tempfile("dummysuggestexamples")
dir.create(tmpdir)
file.copy(system.file("dummypackage",package = "attachment"), tmpdir, recursive = TRUE)
dummypackage <- file.path(tmpdir, "dummypackage")

r_file <- file.path(dummypackage, "R", "fun_manual.R")
file.create(r_file)
#> [1] TRUE
writeLines(
text = "#' @importFrom magrittr %>%
#' @examples
#' library(pkgfake)
#' fakepkg::fun()
#' @export
my_length <- function(x) {
x %>% length()
}",
con = r_file
)

attachment::att_amend_desc(path = dummypackage, check_if_suggests_is_installed = FALSE)


desc_file <- readLines(file.path(dummypackage, "DESCRIPTION"))

w.depends <- grep("Depends:", desc_file)
expect_length(w.depends, 1)
expect_equal(desc_file[w.depends + 5], "Suggests: ")
expect_equal(desc_file[w.depends + 6], " fakepkg,")
expect_equal(desc_file[w.depends + 7], " glue,")
expect_equal(desc_file[w.depends + 8], " knitr,")
expect_equal(desc_file[w.depends + 9], " pkgfake,")
expect_equal(desc_file[w.depends + 10], " rmarkdown,")
expect_equal(desc_file[w.depends + 11], " testthat,")
expect_equal(desc_file[w.depends + 12], " utils")
# Clean after
unlink(dummypackage, recursive = TRUE)

})


test_that("if a package is used in an example has already been added to IMPORTS, it is not added to the suggests, it is added to Suggests packages", {
# Copy package in a temporary directory
tmpdir <- tempfile("dummysuggestexamples")
dir.create(tmpdir)
file.copy(system.file("dummypackage",package = "attachment"), tmpdir, recursive = TRUE)
dummypackage <- file.path(tmpdir, "dummypackage")

r_file <- file.path(dummypackage, "R", "fun_manual.R")
file.create(r_file)
#> [1] TRUE
writeLines(
text = "#' @importFrom magrittr %>%
#' @examples
#' library(magrittr)
#' fakepkg::fun()
#' @export
my_length <- function(x) {
x %>% length()
}",
con = r_file
)

attachment::att_amend_desc(path = dummypackage, check_if_suggests_is_installed = FALSE)


desc_file <- readLines(file.path(dummypackage, "DESCRIPTION"))

w.depends <- grep("Depends:", desc_file)
expect_length(w.depends, 1)
expect_equal(desc_file[w.depends + 1], " R (>= 3.5.0)")
expect_equal(desc_file[w.depends + 2], "Imports: ")
expect_equal(desc_file[w.depends + 3], " magrittr,")
expect_equal(desc_file[w.depends + 4], " stats")
expect_equal(desc_file[w.depends + 5], "Suggests: ")
expect_equal(desc_file[w.depends + 6], " fakepkg,")
expect_equal(desc_file[w.depends + 7], " glue,")
expect_equal(desc_file[w.depends + 8], " knitr,")
expect_equal(desc_file[w.depends + 9], " rmarkdown,")
expect_equal(desc_file[w.depends + 10], " testthat,")
expect_equal(desc_file[w.depends + 11], " utils")
expect_equal(desc_file[w.depends + 12], "LinkingTo:" )
expect_equal(desc_file[w.depends + 13], " Rcpp")

# Clean after
unlink(dummypackage, recursive = TRUE)

})

0 comments on commit 85297a2

Please sign in to comment.