diff --git a/R/build.R b/R/build.R index 336aafe..d3348d1 100644 --- a/R/build.R +++ b/R/build.R @@ -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) @@ -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 +} diff --git a/R/processData.R b/R/processData.R index 37125cf..ec5b36c 100644 --- a/R/processData.R +++ b/R/processData.R @@ -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") @@ -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"]], diff --git a/R/skeleton.R b/R/skeleton.R index 215703c..f87cd2a 100644 --- a/R/skeleton.R +++ b/R/skeleton.R @@ -95,7 +95,7 @@ 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", @@ -103,8 +103,9 @@ datapackage_skeleton <- "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 ) diff --git a/R/use.R b/R/use.R index 53d2064..cbeaa65 100644 --- a/R/use.R +++ b/R/use.R @@ -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") @@ -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", @@ -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", diff --git a/inst/extdata/tests/extra.rmd b/inst/extdata/tests/extra.Rmd similarity index 93% rename from inst/extdata/tests/extra.rmd rename to inst/extdata/tests/extra.Rmd index 9aa49e8..b647b5e 100644 --- a/inst/extdata/tests/extra.rmd +++ b/inst/extdata/tests/extra.Rmd @@ -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 diff --git a/inst/extdata/tests/subsetCars.Rmd b/inst/extdata/tests/subsetCars.Rmd index ad72ff5..6b030db 100644 --- a/inst/extdata/tests/subsetCars.Rmd +++ b/inst/extdata/tests/subsetCars.Rmd @@ -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. diff --git a/inst/extdata/tests/subsetCars.html b/inst/extdata/tests/subsetCars.html index f84b4d4..9d12b9e 100644 --- a/inst/extdata/tests/subsetCars.html +++ b/inst/extdata/tests/subsetCars.html @@ -335,7 +335,7 @@

Storing data set objects and making making accessible to other processing sc
  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. -
  4. 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.
  5. +
  6. 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.

The datapackager_object_read() API can be used to retrieve these objects from the environment.

diff --git a/man/use_data_object.Rd b/man/use_data_object.Rd index 89d7ca9..e90c7c0 100644 --- a/man/use_data_object.Rd +++ b/man/use_data_object.Rd @@ -18,7 +18,7 @@ The data object will be added to the yml configuration file. \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", diff --git a/man/use_processing_script.Rd b/man/use_processing_script.Rd index d21d055..813b130 100644 --- a/man/use_processing_script.Rd +++ b/man/use_processing_script.Rd @@ -31,7 +31,7 @@ Any existing file by that name will be overwritten when overwrite is set to TRUE \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", diff --git a/man/use_raw_dataset.Rd b/man/use_raw_dataset.Rd index 76b291b..5f05265 100644 --- a/man/use_raw_dataset.Rd +++ b/man/use_raw_dataset.Rd @@ -21,7 +21,7 @@ the inst/extdata directory. \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") diff --git a/man/validate_pkg_name.Rd b/man/validate_pkg_name.Rd new file mode 100644 index 0000000..93e067b --- /dev/null +++ b/man/validate_pkg_name.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/build.R +\name{validate_pkg_name} +\alias{validate_pkg_name} +\title{Check that pkg name inferred from pkg path is same as pkg name in DESCRIPTION} +\usage{ +validate_pkg_name(package_path) +} +\arguments{ +\item{package_path}{Package path} +} +\value{ +Package name (character) if validated +} +\description{ +Check that pkg name inferred from pkg path is same as pkg name in DESCRIPTION +} diff --git a/tests/testthat/test-DataPackageR.R b/tests/testthat/test-DataPackageR.R new file mode 100644 index 0000000..2b7825d --- /dev/null +++ b/tests/testthat/test-DataPackageR.R @@ -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) +}) diff --git a/tests/testthat/test-build-locations.R b/tests/testthat/test-build-locations.R index 2269489..12d5306 100644 --- a/tests/testthat/test-build-locations.R +++ b/tests/testthat/test-build-locations.R @@ -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) +}) diff --git a/tests/testthat/test-conditional-build.R b/tests/testthat/test-conditional-build.R index b7228fb..7e1a48d 100644 --- a/tests/testthat/test-conditional-build.R +++ b/tests/testthat/test-conditional-build.R @@ -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( diff --git a/tests/testthat/test-manual-version-bump.R b/tests/testthat/test-manual-version-bump.R index e8ea1fb..6062f1d 100644 --- a/tests/testthat/test-manual-version-bump.R +++ b/tests/testthat/test-manual-version-bump.R @@ -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( diff --git a/tests/testthat/test-pkg_description.R b/tests/testthat/test-pkg_description.R index 00db501..ea8d3c5 100644 --- a/tests/testthat/test-pkg_description.R +++ b/tests/testthat/test-pkg_description.R @@ -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( diff --git a/tests/testthat/test-skeleton-data-dependencies.R b/tests/testthat/test-skeleton-data-dependencies.R index cab908a..bb1e385 100644 --- a/tests/testthat/test-skeleton-data-dependencies.R +++ b/tests/testthat/test-skeleton-data-dependencies.R @@ -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", @@ -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 = "/" ) diff --git a/tests/testthat/test-skeleton-edgecases.R b/tests/testthat/test-skeleton-edgecases.R index d45b263..aaa98ba 100644 --- a/tests/testthat/test-skeleton-edgecases.R +++ b/tests/testthat/test-skeleton-edgecases.R @@ -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( diff --git a/tests/testthat/test-updating-datapackager-version.R b/tests/testthat/test-updating-datapackager-version.R index d02d2b7..8be11ed 100644 --- a/tests/testthat/test-updating-datapackager-version.R +++ b/tests/testthat/test-updating-datapackager-version.R @@ -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( diff --git a/tests/testthat/test-use_raw_data.R b/tests/testthat/test-use_raw_data.R index f4ffcc0..819238d 100644 --- a/tests/testthat/test-use_raw_data.R +++ b/tests/testthat/test-use_raw_data.R @@ -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", @@ -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( @@ -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( diff --git a/tests/testthat/test-version-bump.R b/tests/testthat/test-version-bump.R index 697c1c2..1cd31ee 100644 --- a/tests/testthat/test-version-bump.R +++ b/tests/testthat/test-version-bump.R @@ -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( diff --git a/tests/testthat/test-version-management-edge-cases.R b/tests/testthat/test-version-management-edge-cases.R index b736efd..0efac75 100644 --- a/tests/testthat/test-version-management-edge-cases.R +++ b/tests/testthat/test-version-management-edge-cases.R @@ -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( @@ -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) diff --git a/tests/testthat/test-yaml-config.R b/tests/testthat/test-yaml-config.R index f3179b1..69e3090 100644 --- a/tests/testthat/test-yaml-config.R +++ b/tests/testthat/test-yaml-config.R @@ -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( @@ -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( diff --git a/tests/testthat/test-yaml-manipulation.R b/tests/testthat/test-yaml-manipulation.R index 2ba831d..68f397e 100644 --- a/tests/testthat/test-yaml-manipulation.R +++ b/tests/testthat/test-yaml-manipulation.R @@ -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( diff --git a/tests/testthat/test-yaml.R b/tests/testthat/test-yaml.R index ce4e8d4..3e0fc2e 100644 --- a/tests/testthat/test-yaml.R +++ b/tests/testthat/test-yaml.R @@ -29,13 +29,13 @@ test_that("yaml reading, adding, removing, listing, and writing", { attr(test_config, "path") <- attr(config, "path") expect_identical(config, test_config) - config <- yml_add_files(config, "extra.rmd") + config <- yml_add_files(config, "extra.Rmd") test_config <- structure(list( configuration = list( files = list( subsetCars.Rmd = list(enabled = TRUE), - extra.rmd = list(enabled = TRUE) + extra.Rmd = list(enabled = TRUE) ), objects = "cars_over_20", render_root = "dummy" @@ -52,7 +52,7 @@ test_that("yaml reading, adding, removing, listing, and writing", { configuration = list( files = list( subsetCars.Rmd = list(enabled = TRUE), - extra.rmd = list(enabled = TRUE) + extra.Rmd = list(enabled = TRUE) ), objects = "cars_over_20", render_root = "dummy" @@ -69,7 +69,7 @@ test_that("yaml reading, adding, removing, listing, and writing", { configuration = list( files = list( subsetCars.Rmd = list(enabled = TRUE), - extra.rmd = list(enabled = TRUE) + extra.Rmd = list(enabled = TRUE) ), objects = c( "cars_over_20", @@ -90,7 +90,7 @@ test_that("yaml reading, adding, removing, listing, and writing", { configuration = list( files = list( subsetCars.Rmd = list(enabled = TRUE), - extra.rmd = list(enabled = TRUE) + extra.Rmd = list(enabled = TRUE) ), objects = "cars_over_20", render_root = "dummy" @@ -104,7 +104,7 @@ test_that("yaml reading, adding, removing, listing, and writing", { list <- yml_list_files(config) expect_identical( list, - c("subsetCars.Rmd", "extra.rmd") + c("subsetCars.Rmd", "extra.Rmd") ) @@ -119,7 +119,7 @@ test_that("yaml reading, adding, removing, listing, and writing", { configuration = list( files = list( subsetCars.Rmd = list(enabled = TRUE), - extra.rmd = list(enabled = TRUE) + extra.Rmd = list(enabled = TRUE) ), objects = "cars_over_20", render_root = "dummy"