Skip to content

Commit bb024ee

Browse files
authored
Merge pull request #49 from ThinkR-open/renv_en_folie
Add pkg_ignore to create_renv()
2 parents 576bfa4 + d8e445e commit bb024ee

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# attachment 0.2.4.900x
22
## Major changes
33

4-
* add `create_renv_for_dev()` and `create_renv_for_prod()` function to create `renv.lock` file based on development project (@VincentGuyader).
4+
* add `create_renv_for_dev()` and `create_renv_for_prod()` function to create `renv.lock` file based on development project (@VincentGuyader and @statnmap).
55

66
## Minor changes
77

R/create_renv.R

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extra_dev_pkg <- c(
1818
#'
1919
#'
2020
#' @param path Path to your current package source folder
21-
#' @param dev_pkg Package development toolbox you need. Use `_default`
21+
#' @param dev_pkg Vector of packages you need for development. Use `_default`
2222
#' (with underscore before to avoid confusing with a package name), to
2323
#' use the default list. Use `NULL` for no extra package.
2424
#' Use `attachment:::extra_dev_pkg` for the list.
@@ -27,6 +27,8 @@ extra_dev_pkg <- c(
2727
#' @param install_if_missing Logical. Install missing packages. `TRUE` by default
2828
#' @param document Logical. Whether to run [att_amend_desc()] before
2929
#' detecting packages in DESCRIPTION.
30+
#' @param pkg_ignore Vector of packages to ignore from being discovered in your files.
31+
#' This does not prevent them to be in "renv.lock" if they are recursive dependencies.
3032
#' @param ... Other arguments to pass to [renv::snapshot()]
3133
#'
3234
#' @return a renv.lock file
@@ -47,6 +49,7 @@ create_renv_for_dev <- function(path = ".",
4749
output = "renv.lock",
4850
install_if_missing = TRUE,
4951
document = TRUE,
52+
pkg_ignore = NULL,
5053
...) {
5154

5255
if (!requireNamespace("renv")) {
@@ -66,11 +69,12 @@ create_renv_for_dev <- function(path = ".",
6669
att_amend_desc(path)
6770
}
6871

69-
pkg_list <-
72+
pkg_list <- unique(
7073
c(
7174
att_from_description(path = file.path(path, "DESCRIPTION")),
7275
dev_pkg
7376
)
77+
)
7478

7579
# Extra folders
7680
folder_to_include_relative <- folder_to_include
@@ -98,6 +102,11 @@ create_renv_for_dev <- function(path = ".",
98102
pkg_list <- unique(c(pkg_list, from_r_script, from_rmd))
99103
}
100104

105+
# Ignore
106+
if (!is.null(pkg_ignore)) {
107+
pkg_list <- pkg_list[!pkg_list %in% pkg_ignore]
108+
}
109+
101110
# Install
102111
if (install_if_missing) {
103112
install_if_missing(pkg_list)

man/create_renv_for_dev.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-renv_create.R

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ test_that("lockfile are renv files", {
100100
pkg_extra <- names(local_renv_extra$data()$Packages)
101101
pkg_blank <- names(local_renv_blank$data()$Packages)
102102

103-
test_that("extrapackage is present thank to dev_pkg", {
103+
test_that("extrapackage is present thanks to dev_pkg", {
104104
expect_true("extrapackage" %in% pkg_extra)
105105
expect_false("extrapackage" %in% pkg_blank)
106106
# all blank are in extra
@@ -115,7 +115,7 @@ test_that("extrapackage is present thank to dev_pkg", {
115115
# expect_equal_to_reference(names(pkg_blank),"my_renv_blank.test")
116116
# })
117117

118-
# Test for extra and "_default" in interactive
118+
# Test for extra and "_default" in interactive ----
119119
test_that("_default works", {
120120
skip_if_not(interactive())
121121

@@ -143,7 +143,7 @@ test_that("_default works", {
143143
expect_true(all(c("devtools", "fusen") %in% pkg_extra_default))
144144
})
145145

146-
# Test for "folder_to_include" in dummypackage
146+
# Test for "folder_to_include" in dummypackage ----
147147
dir.create(file.path(dummypackage, "dev"))
148148
cat("library(glue)", file = file.path(dummypackage, "dev", "my_r.R"))
149149
cat("```{r}\nlibrary(\"extrapackage\")\n```", file = file.path(dummypackage, "dev", "my_rmd.Rmd"))
@@ -157,7 +157,7 @@ test_that("folder_to_include works", {
157157
install_if_missing = FALSE,
158158
output = lock_includes_devdir,
159159
force = TRUE)}
160-
# "There is no directory named: dev, data-raw" # cli
160+
# "There is no directory named: data-raw" # cli
161161
)
162162

163163
expect_true(file.exists(lock_includes_devdir))
@@ -171,5 +171,32 @@ test_that("folder_to_include works", {
171171
expect_true(all(c("glue", "extrapackage") %in% pkg_devdir))
172172
})
173173

174+
# Test pkg_ignore works ----
175+
# extrapackage is in "dev/" but I want it to be ignored
176+
test_that("create_renv_(pkg_ignore) works", {
177+
178+
lock_includes_ignore <- file.path(tmpdir, "for_ignore.lock")
179+
expect_message({my_renv_ignore <-
180+
create_renv_for_dev(
181+
path = dummypackage,
182+
install_if_missing = FALSE,
183+
output = lock_includes_ignore,
184+
pkg_ignore = "extrapackage",
185+
force = TRUE)}
186+
)
187+
188+
expect_true(file.exists(lock_includes_ignore))
189+
expect_true(file.exists(my_renv_ignore))
190+
191+
local_renv_ignore <- getFromNamespace("lockfile", "renv")(my_renv_ignore)
192+
expect_s3_class(local_renv_ignore, "renv_lockfile_api")
193+
194+
pkg_ignore <- names(local_renv_ignore$data()$Packages)
195+
# glue and in dev/ are there
196+
expect_true(all(c("glue") %in% pkg_ignore))
197+
# extrapackage is ignored in dev/
198+
expect_false(all(c("extrapackage") %in% pkg_ignore))
199+
})
200+
174201
remove.packages("extrapackage")
175202
unlink(tmpdir, recursive = TRUE)

0 commit comments

Comments
 (0)