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

Throw errors on invalid package name and invalid object name #125

Merged
merged 4 commits into from
Apr 20, 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
22 changes: 22 additions & 0 deletions R/build.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ package_build <- function(packageName = NULL,
}
)

# Check that directory name matches package name
validate_pkg_name(package_path)

# Return success if we've processed everything
success <-
DataPackageR(arg = package_path, deps = deps)
Expand Down Expand Up @@ -140,3 +143,22 @@ package_build <- function(packageName = NULL,
keepDataObjects <- function(...) {
.Defunct(msg = "keepDataObjects is defunct as of version 0.12.1 of DataPackageR. \nUse the config.yml file to control packaging.") # nolint
}

#' Check that pkg name inferred from pkg path is same as pkg name in DESCRIPTION
#'
#' @param package_path Package path
#'
#' @returns Package name (character) if validated
validate_pkg_name <- function(package_path){
desc_pkg_name <- desc::desc(
file = file.path(package_path, 'DESCRIPTION')
)$get("Package")
path_pkg_name <- basename(package_path)
if (desc_pkg_name != path_pkg_name){
err_msg <- paste("Data package name in DESCRIPTION does not match",
"name of the data package directory")
flog.fatal(err_msg, name = "console")
stop(err_msg, call. = FALSE)
}
desc_pkg_name
}
16 changes: 9 additions & 7 deletions R/processData.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@ DataPackageR <- function(arg = NULL, deps = TRUE) {
stop("exiting", call. = FALSE)
}
} else {
logpath <-
normalizePath(
file.path(pkg_dir, "inst/extdata"),
winslash = "/"
)
logpath <- file.path(logpath, "Logfiles")

logpath <- file.path(pkg_dir, "inst", "extdata", "Logfiles")
dir.create(logpath, recursive = TRUE, showWarnings = FALSE)
# open a log file
LOGFILE <- file.path(logpath, "processing.log")
Expand Down Expand Up @@ -116,6 +110,14 @@ DataPackageR <- function(arg = NULL, deps = TRUE) {
assert_that("files" %in% names(ymlconf[["configuration"]]))
assert_that(!is.null(names(ymlconf[["configuration"]][["files"]])))

# object with same name as package causes problems with
# overwriting documentation files
if (basename(pkg_dir) %in% ymlconf$configuration$objects){
err_msg <- "Data object not allowed to have same name as data package"
flog.fatal(err_msg, name = "console")
stop(err_msg, call. = FALSE)
}

r_files <- unique(names(
Filter(
x = ymlconf[["configuration"]][["files"]],
Expand Down
7 changes: 4 additions & 3 deletions R/skeleton.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,17 @@ datapackage_skeleton <-
"When you call package_build(), your datasets will",
"be automatically documented. Edit datapackager.yml to",
"add additional files / data objects to the package.",
"After building, you should edit dat-raw/documentation.R",
"After building, you should edit data-raw/documentation.R",
"to fill in dataset documentation details and rebuild.",
"",
"NOTES",
"If your code relies on other packages,",
"add those to the @import tag of the roxygen markup.",
"The R object names you wish to make available",
"(and document) in the package must match",
"the roxygen @name tags and must be listed",
"in the yml file."
"the roxygen @name tags, must be listed",
"in the yml file, and must not have the same name",
"as the name of your data package."
),
con
)
Expand Down
6 changes: 3 additions & 3 deletions R/use.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' @examples
#' if(rmarkdown::pandoc_available()){
#' myfile <- tempfile()
#' file <- system.file("extdata", "tests", "extra.rmd",
#' file <- system.file("extdata", "tests", "extra.Rmd",
#' package = "DataPackageR")
#' raw_data <- system.file("extdata", "tests", "raw_data",
#' package = "DataPackageR")
Expand Down Expand Up @@ -83,7 +83,7 @@ use_raw_dataset <- function(path = NULL, ignore = FALSE) {
#' @examples
#' if(rmarkdown::pandoc_available()){
#' myfile <- tempfile()
#' file <- system.file("extdata", "tests", "extra.rmd",
#' file <- system.file("extdata", "tests", "extra.Rmd",
#' package = "DataPackageR")
#' datapackage_skeleton(
#' name = "datatest",
Expand Down Expand Up @@ -175,7 +175,7 @@ use_processing_script <- function(file = NULL, title = NULL, author = NULL, over
#' @examples
#' if(rmarkdown::pandoc_available()){
#' myfile <- tempfile()
#' file <- system.file("extdata", "tests", "extra.rmd",
#' file <- system.file("extdata", "tests", "extra.Rmd",
#' package = "DataPackageR")
#' datapackage_skeleton(
#' name = "datatest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cars_over_20 = datapackager_object_read("cars_over_20")
print(cars_over_20)
```

This API reads from an environment named `ENVS`, containing `subsetCars` and any other previously built data set objects. It is passed into the render environment of `extra.rmd` by DataPackageR at the `render()` call.
This API reads from an environment named `ENVS`, containing `subsetCars` and any other previously built data set objects. It is passed into the render environment of `extra.Rmd` by DataPackageR at the `render()` call.

## Additional data objects

Expand Down
2 changes: 1 addition & 1 deletion inst/extdata/tests/subsetCars.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ When DataPackageR processes this file, it creates this `cars_over_20` object. Af

1. It compares the objects in the rmarkdown render environment of `subsetCars.Rmd` against the objects listed in the `config.yml` file `objects` property.
2. It finds `cars_over_20` is listed there, so it stores it in a new environment.
3. That environment is passed to subsequent R and Rmd files. Specifically when the `extra.rmd` file is processed, it has access to an environment object that holds all the `objects` (defined in the yaml config) that have already been created and processed. This environment is passed into subsequent scripts at the `render()` call.
3. That environment is passed to subsequent R and Rmd files. Specifically when the `extra.Rmd` file is processed, it has access to an environment object that holds all the `objects` (defined in the yaml config) that have already been created and processed. This environment is passed into subsequent scripts at the `render()` call.

All of the above is done automatically. The user only needs to list the objects to be stored and passed to other scripts in the `config.yml` file.

Expand Down
2 changes: 1 addition & 1 deletion inst/extdata/tests/subsetCars.html
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ <h1>Storing data set objects and making making accessible to other processing sc
<ol style="list-style-type: decimal">
<li>It compares the objects in the rmarkdown render environment of <code>subsetCars.Rmd</code> against the objects listed in the <code>config.yml</code> file <code>objects</code> property.</li>
<li>It finds <code>cars_over_20</code> is listed there, so it stores it in a new environment.</li>
<li>That environment is passed to subsequent R and Rmd files. Specifically when the <code>extra.rmd</code> file is processed, it has access to an environment object that holds all the <code>objects</code> (defined in the yaml config) that have already been created and processed. This environment is passed into subsequent scripts at the <code>render()</code> call.</li>
<li>That environment is passed to subsequent R and Rmd files. Specifically when the <code>extra.Rmd</code> file is processed, it has access to an environment object that holds all the <code>objects</code> (defined in the yaml config) that have already been created and processed. This environment is passed into subsequent scripts at the <code>render()</code> call.</li>
</ol>
<p>All of the above is done automatically. The user only needs to list the objects to be stored and passed to other scripts in the <code>config.yml</code> file.</p>
<p>The <code>datapackager_object_read()</code> API can be used to retrieve these objects from the environment.</p>
Expand Down
2 changes: 1 addition & 1 deletion man/use_data_object.Rd

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

2 changes: 1 addition & 1 deletion man/use_processing_script.Rd

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

2 changes: 1 addition & 1 deletion man/use_raw_dataset.Rd

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

17 changes: 17 additions & 0 deletions man/validate_pkg_name.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/test-DataPackageR.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test_that("Error on data object same name as data package", {
file <- system.file("extdata", "tests", "extra.Rmd", package = "DataPackageR")
td <- withr::local_tempdir()
pp <- 'pressure'
datapackage_skeleton(name = pp, path = td,
code_files = file, r_object_names = pp)
err_msg <- "Data object not allowed to have same name as data package"
expect_error(package_build(file.path(td, pp)), err_msg)
})
11 changes: 11 additions & 0 deletions tests/testthat/test-build-locations.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ test_that("package can be built from different locations", {
force = TRUE
)
})

test_that("Error on data pkg dirname different from data pkg name", {
td <- withr::local_tempdir()
sn <- 'skelname'
not_sn <- paste0('not_', sn)
datapackage_skeleton(name = sn, path = td)
file.rename(from = file.path(td, sn), to = file.path(td, not_sn))
err_msg <- paste("Data package name in DESCRIPTION does not match",
"name of the data package directory")
expect_error(package_build(file.path(td, not_sn)), err_msg)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-conditional-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("can add a data item", {
file <- system.file("extdata", "tests", "subsetCars.Rmd",
package = "DataPackageR"
)
file2 <- system.file("extdata", "tests", "extra.rmd",
file2 <- system.file("extdata", "tests", "extra.Rmd",
package = "DataPackageR"
)
expect_null(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-manual-version-bump.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("manual bump version when data unchanged", {
file <- system.file("extdata", "tests", "subsetCars.Rmd",
package = "DataPackageR"
)
file2 <- system.file("extdata", "tests", "extra.rmd",
file2 <- system.file("extdata", "tests", "extra.Rmd",
package = "DataPackageR"
)
expect_null(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-pkg_description.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("can_read_pkg_description, data_version", {
file <- system.file("extdata", "tests", "subsetCars.Rmd",
package = "DataPackageR"
)
file2 <- system.file("extdata", "tests", "extra.rmd",
file2 <- system.file("extdata", "tests", "extra.Rmd",
package = "DataPackageR"
)
datapackage_skeleton(
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-skeleton-data-dependencies.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
context("skeleton")
test_that("data, code, and dependencies are moved into place by skeleton", {
file <- system.file("extdata", "tests", "extra.rmd",
file <- system.file("extdata", "tests", "extra.Rmd",
package = "DataPackageR"
)
ancillary <- system.file("extdata", "tests", "rfileTest.R",
Expand Down Expand Up @@ -42,7 +42,7 @@ test_that("data, code, and dependencies are moved into place by skeleton", {
tempdir(),
"datatest",
"data-raw",
"extra.rmd"
"extra.Rmd"
),
winslash = "/"
)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-skeleton-edgecases.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test_that("datapackage_skeleton errors with no name arg", {
file <- system.file("extdata", "tests", "subsetCars.Rmd",
package = "DataPackageR"
)
file2 <- system.file("extdata", "tests", "extra.rmd",
file2 <- system.file("extdata", "tests", "extra.Rmd",
package = "DataPackageR"
)
expect_error(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-updating-datapackager-version.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("can update", {
file <- system.file("extdata", "tests", "subsetCars.Rmd",
package = "DataPackageR"
)
file2 <- system.file("extdata", "tests", "extra.rmd",
file2 <- system.file("extdata", "tests", "extra.Rmd",
package = "DataPackageR"
)
expect_null(
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-use_raw_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ context("test-use_raw_data")

test_that("use_raw_data works as expected", {
myfile <- tempfile()
file <- system.file("extdata", "tests", "extra.rmd",
file <- system.file("extdata", "tests", "extra.Rmd",
package = "DataPackageR"
)
ancillary <- system.file("extdata", "tests", "rfileTest.R",
Expand Down Expand Up @@ -55,7 +55,7 @@ test_that("use_raw_data works as expected", {

test_that("use_processing_script works as expected", {
myfile <- tempfile()
file <- system.file("extdata", "tests", "extra.rmd",
file <- system.file("extdata", "tests", "extra.Rmd",
package = "DataPackageR"
)
expect_null(
Expand Down Expand Up @@ -108,7 +108,7 @@ test_that("use_processing_script works as expected", {

test_that("use_data_object works as expected", {
myfile <- tempfile()
file <- system.file("extdata", "tests", "extra.rmd",
file <- system.file("extdata", "tests", "extra.Rmd",
package = "DataPackageR"
)
expect_null(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-version-bump.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test_that("auto bump version when data unchanged", {
file <- system.file("extdata", "tests", "subsetCars.Rmd",
package = "DataPackageR"
)
file2 <- system.file("extdata", "tests", "extra.rmd",
file2 <- system.file("extdata", "tests", "extra.Rmd",
package = "DataPackageR"
)
expect_null(
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-version-management-edge-cases.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("data changes but version out of sync", {
file <- system.file("extdata", "tests", "subsetCars.Rmd",
package = "DataPackageR"
)
file2 <- system.file("extdata", "tests", "extra.rmd",
file2 <- system.file("extdata", "tests", "extra.Rmd",
package = "DataPackageR"
)
expect_null(
Expand All @@ -18,7 +18,7 @@ test_that("data changes but version out of sync", {
)
package_build(file.path(tempdir(), "subsetCars"))
config <- yml_find(file.path(tempdir(), "subsetCars"))
config <- yml_add_files(config, "extra.rmd")
config <- yml_add_files(config, "extra.Rmd")
config <- yml_add_objects(config, "pressure")
file.copy(file2, file.path(tempdir(), "subsetCars", "data-raw"))
yml_write(config)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-yaml-config.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test_that("can add a file", {
file <- system.file("extdata", "tests", "subsetCars.Rmd",
package = "DataPackageR"
)
file2 <- system.file("extdata", "tests", "extra.rmd",
file2 <- system.file("extdata", "tests", "extra.Rmd",
package = "DataPackageR"
)
expect_null(
Expand All @@ -27,7 +27,7 @@ test_that("can add a file", {
))
))
config <- yml_find(file.path(tempdir(), "subsetCars"))
config <- yml_add_files(config, "extra.rmd")
config <- yml_add_files(config, "extra.Rmd")
yml_write(config)
file.copy(from = file2, file.path(tempdir(), "subsetCars", "data-raw"))
expect_equal(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-yaml-manipulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test_that("can remove a data item", {
file <- system.file("extdata", "tests", "subsetCars.Rmd",
package = "DataPackageR"
)
file2 <- system.file("extdata", "tests", "extra.rmd",
file2 <- system.file("extdata", "tests", "extra.Rmd",
package = "DataPackageR"
)
expect_null(
Expand Down
Loading