diff --git a/R/create.project.R b/R/create.project.R index c773d0d2..4357756e 100644 --- a/R/create.project.R +++ b/R/create.project.R @@ -10,6 +10,7 @@ #' #' @param project.name A character vector containing the name for this new #' project. Must be a valid directory name for your file system. +#' @param project.directory A character vector containing the full path of the project directory up to and excluding the project.name. #' @param template A character vector containing the name of the template to #' use for this project. By default a \code{full} and \code{minimal} template #' are provided, but custom templates can be created using @@ -41,24 +42,30 @@ #' library('ProjectTemplate') #' #' \dontrun{create.project('MyProject')} -create.project <- function(project.name = 'new-project', template = 'full', - dump = FALSE, merge.strategy = c("require.empty", "allow.non.conflict"), - rstudio.project = FALSE) +create.project <- function(project.name = 'new-project', + project.directory = getwd(), + template = 'full', + dump = FALSE, + merge.strategy = c("require.empty", "allow.non.conflict"), + rstudio.project = FALSE + ) { + project.path <- file.path(project.directory, project.name) + .stopifproject(c("Cannot create a new project inside an existing one", "Please change to another directory and re-run create.project()"), - path = normalizePath(dirname(project.name))) + path = normalizePath(dirname(project.path))) .stopifproject(c("Cannot create a new project inside an existing one", "Please change to another directory and re-run create.project()"), - path = dirname(normalizePath(dirname(project.name)))) + path = dirname(normalizePath(dirname(project.path)))) merge.strategy <- match.arg(merge.strategy) - if (.is.dir(project.name)) { - .create.project.existing(project.name, merge.strategy, template, rstudio.project) + if (.is.dir(project.path)) { + .create.project.existing(project.name, project.directory, merge.strategy, template, rstudio.project) } else - .create.project.new(project.name, template, rstudio.project) + .create.project.new(project.name, project.directory, template, rstudio.project) if (dump) { @@ -73,7 +80,7 @@ create.project <- function(project.name = 'new-project', template = 'full', for (item in pt.contents) { cat(deparse(get(item, envir = e, inherits = FALSE)), - file = file.path(project.name, paste(item, '.R', sep = ''))) + file = file.path(project.path, paste(item, '.R', sep = ''))) } } @@ -87,6 +94,7 @@ create.project <- function(project.name = 'new-project', template = 'full', #' an existing directory with the default files from a given template. #' #' @param project.name Character vector with the name of the project directory +#' @param project.directory Character vector with the full path of the project directory up to and excluding the project.name #' @param merge.strategy Character vector determining whether the directory #' should be empty or is allowed to contain non-conflicting files #' @param template Name of the template from which the project should be created @@ -99,11 +107,11 @@ create.project <- function(project.name = 'new-project', template = 'full', #' @keywords internal #' #' @rdname internal.create.project -.create.project.existing <- function(project.name, merge.strategy, template, rstudio.project) { +.create.project.existing <- function(project.name, project.directory, merge.strategy, template, rstudio.project) { template.path <- .get.template(template) template.files <- .list.files.and.dirs(path = template.path) - project.path <- file.path(project.name) + project.path <- file.path(project.directory, project.name) switch( merge.strategy, @@ -128,11 +136,11 @@ create.project <- function(project.name = 'new-project', template = 'full', # Add project name to header README.md <- file.path(project.path, "README.md") README <- readLines(README.md) - writeLines(c(sprintf("# %s\n", basename(normalizePath(project.name))), README), README.md) + writeLines(c(sprintf("# %s\n", project.name), README), README.md) # Add RProj file to the project directory if the user has requested an RStudio project if(rstudio.project){ - rstudiopath <- file.path(project.path, paste0(project.name,'.Rproj')) + rstudiopath <- paste0(project.path,'.Rproj') writeLines(.rstudioprojectfile(), rstudiopath) } } @@ -149,19 +157,22 @@ create.project <- function(project.name = 'new-project', template = 'full', #' @keywords internal #' #' @rdname internal.create.project -.create.project.new <- function(project.name, template, rstudio.project) { - if (file.exists(project.name)) { - stop(paste("Cannot run create.project() from a directory containing", project.name)) +.create.project.new <- function(project.name, project.directory, template, rstudio.project) { + project.path <- file.path(project.directory, project.name) + + if (file.exists(project.path)) { + stop(paste("Cannot run create.project() at a directory containing", project.name)) } - dir.create(project.name) + dir.create(project.path) tryCatch( .create.project.existing(project.name = project.name, + project.directory = project.directory, merge.strategy = "require.empty", template = template, rstudio.project = rstudio.project), error = function(e) { - unlink(project.name, recursive = TRUE) + unlink(project.path, recursive = TRUE) stop(e) } ) diff --git a/R/load.project.R b/R/load.project.R index 0ff2d0f3..0dcdc496 100644 --- a/R/load.project.R +++ b/R/load.project.R @@ -43,8 +43,6 @@ load.project <- function(...) assign('config', config, envir = .TargetEnv) my.project.info$config <- config - options(stringsAsFactors = config$as_factors) - if (config$load_libraries) { my.project.info <- .load.libraries(config, my.project.info) } diff --git a/tests/testthat/helper-functions_and_data.R b/tests/testthat/helper-functions_and_data.R index 51b893f1..cbc88413 100644 --- a/tests/testthat/helper-functions_and_data.R +++ b/tests/testthat/helper-functions_and_data.R @@ -4,14 +4,14 @@ #' #' @keywords tests tidy_up <- function() { - objs <- ls(envir = .TargetEnv) - rm(list = objs, envir = .TargetEnv) + objs <- ls(envir = .TargetEnv) + rm(list = objs, envir = .TargetEnv) } # Character vector holding the available cache file formats for testing cache_file_formats <- "RData" if (requireNamespace("qs", quietly = TRUE)) { - cache_file_formats <- c(cache_file_formats, "qs") + cache_file_formats <- c(cache_file_formats, "qs") } #' Set cache file format for testing @@ -22,11 +22,11 @@ if (requireNamespace("qs", quietly = TRUE)) { #' #' @keywords tests set_cache_file_format <- function(cache_file_format) { - if (cache_file_format != "RData") { - config <- .read.config() - config$cache_file_format <- cache_file_format - .save.config(config) - } + if (cache_file_format != "RData") { + config <- .read.config() + config$cache_file_format <- cache_file_format + .save.config(config) + } invisible(cache_file_format) } diff --git a/tests/testthat/test-add_config.R b/tests/testthat/test-add_config.R index d50c4e23..34687797 100644 --- a/tests/testthat/test-add_config.R +++ b/tests/testthat/test-add_config.R @@ -1,46 +1,49 @@ -context('Add custom configuration') +test_that("Custom configuration is added to config", { + test_project <- tempfile("test_project") -test_that('Custom configuration is added to config', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - on.exit(clear(), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + on.exit(clear(), add = TRUE) - expect_warning(load.project(), NA) - expect_error(add.config(new_config = 'a'), NA) - expect_equal(config$new_config, 'a') + expect_warning(load.project(), NA) + expect_error(add.config(new_config = "a"), NA) + expect_equal(config$new_config, "a") }) -test_that('Unnamed added configuration raises an error', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Unnamed added configuration raises an error", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - on.exit(clear(), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + on.exit(clear(), add = TRUE) - expect_warning(load.project(), NA) - expect_error(add.config('a'), 'All options should be named') + expect_warning(load.project(), NA) + expect_error(add.config("a"), "All options should be named") }) -test_that('Added configuration is displayed correctly by project.config()', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Added configuration is displayed correctly by project.config()", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - on.exit(clear(), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + on.exit(clear(), add = TRUE) - expect_warning(load.project(), NA) - expect_error(add.config(dummy = 999), NA) + expect_warning(load.project(), NA) + expect_error(add.config(dummy = 999), NA) - expect_message(project.config(), "Additional custom config present") - expect_message(project.config(), "dummy[ ]+999") + expect_message(project.config(), "Additional custom config present") + expect_message(project.config(), "dummy[ ]+999") }) - - diff --git a/tests/testthat/test-add_extension.R b/tests/testthat/test-add_extension.R index 91e20eff..f9726337 100644 --- a/tests/testthat/test-add_extension.R +++ b/tests/testthat/test-add_extension.R @@ -1,32 +1,36 @@ -context('AddExtension') - -test_that('Test 1: Add an extension', { - foo1.reader <- function() {} - - .add.extension('foo1', 'foo1.reader') - on.exit(rm(foo1.reader), add = TRUE) - expect_that(extensions.dispatch.table[['\\.foo1$']], equals('foo1.reader')) - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - file.copy(file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'foo.reader.R'), file.path('lib', 'foo.reader.R')) - file.copy(file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example.foo'), file.path('data', 'example.foo')) - - expect_true(file.exists(file.path('lib', 'foo.reader.R'))) - expect_true(file.exists(file.path('data', 'example.foo'))) - - load.project() - expect_that(extensions.dispatch.table[['\\.foo$']], equals('foo.reader')) - expect_that(get('example',envir = .GlobalEnv), equals("bar")) - - +test_that("Test 1: Add an extension", { + foo1.reader <- function() {} + + .add.extension("foo1", "foo1.reader") + on.exit(rm(foo1.reader), add = TRUE) + expect_equal(extensions.dispatch.table[["\\.foo1$"]], "foo1.reader") + + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + + file.copy(file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "foo.reader.R" + ), file.path("lib", "foo.reader.R")) + file.copy(file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example.foo" + ), file.path("data", "example.foo")) + + expect_true(file.exists(file.path("lib", "foo.reader.R"))) + expect_true(file.exists(file.path("data", "example.foo"))) + + load.project() + expect_equal(extensions.dispatch.table[["\\.foo$"]], "foo.reader") + expect_equal(get("example", envir = .GlobalEnv), "bar") }) diff --git a/tests/testthat/test-autogen.R b/tests/testthat/test-autogen.R index f28f682d..4ffb7f0d 100644 --- a/tests/testthat/test-autogen.R +++ b/tests/testthat/test-autogen.R @@ -1,18 +1,16 @@ -context('Testing Autogeneration') +test_that("stub.tests", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) -test_that('stub.tests', { + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - suppressMessages(stub.tests()) - - expect_true(file.exists(file.path('tests', 'autogenerated.R'))) - test.code <- scan(file.path('tests', 'autogenerated.R'), what = 'character', sep = '\n', quiet = TRUE) - expect_that(test.code, equals("expect_that(helper.function(), equals(NULL))")) + suppressMessages(stub.tests()) + expect_true(file.exists(file.path("tests", "autogenerated.R"))) + test.code <- scan(file.path("tests", "autogenerated.R"), what = "character", sep = "\n", quiet = TRUE) + expect_equal(test.code, "expect_that(helper.function(), equals(NULL))") }) diff --git a/tests/testthat/test-cache.R b/tests/testthat/test-cache.R index 5338363d..d7c4d038 100644 --- a/tests/testthat/test-cache.R +++ b/tests/testthat/test-cache.R @@ -1,264 +1,273 @@ -context('Caching') - -test_that('caching a variable that doesnt exist fails with correct message', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("caching a variable that doesnt exist fails with correct message", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - var_to_cache <- "xxxx" + var_to_cache <- "xxxx" - # make sure it doesn't exist - if (exists(var_to_cache, envir = .TargetEnv )) { - rm(list=var_to_cache, envir = .TargetEnv) - } - # try to cache it - expect_message(cache(var_to_cache, CODE = NULL, depends = NULL), - "Does not exist in global environment and no code to create it") - tidy_up() + # make sure it doesn't exist + if (exists(var_to_cache, envir = .TargetEnv)) { + rm(list = var_to_cache, envir = .TargetEnv) + } + # try to cache it + expect_message( + cache(var_to_cache, CODE = NULL, depends = NULL), + "Does not exist in global environment and no code to create it" + ) + tidy_up() }) -test_that('caching a variable not already in cache caches correctly', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) +test_that("caching a variable not already in cache caches correctly", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - var_to_cache <- "xxxx" - test_data <- data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40)) - assign(var_to_cache, test_data, envir = .TargetEnv) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # Create a new cached version - expect_message(cache(var_to_cache, CODE = NULL, depends = NULL), - "Creating cache entry from global environment") + var_to_cache <- "xxxx" + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + assign(var_to_cache, test_data, envir = .TargetEnv) - # Remove it from Global Environment - rm(list=var_to_cache, envir = .TargetEnv) + # Create a new cached version + expect_message( + cache(var_to_cache, CODE = NULL, depends = NULL), + "Creating cache entry from global environment" + ) - # Load up from cache and check it's the same as what was originally created - suppressMessages(load.project()) - expect_equal(get(var_to_cache, envir = .TargetEnv) , test_data) + # Remove it from Global Environment + rm(list = var_to_cache, envir = .TargetEnv) - tidy_up() + # Load up from cache and check it's the same as what was originally created + suppressMessages(load.project()) + expect_equal(get(var_to_cache, envir = .TargetEnv), test_data) + tidy_up() }) -test_that('caching a variable created from CODE caches correctly', { - - # CODE caching requires suggested package formatR +test_that("caching a variable created from CODE caches correctly", { + # CODE caching requires suggested package formatR - skip_if_not_installed("formatR") + skip_if_not_installed("formatR") - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - var_to_cache <- "xxxx" - test_data <- data.frame(Names=c("a", "b", "c"), - Ages=c(200,300,400)) - - - # Create a cached version created from CODE - expect_message(cache(var_to_cache, depends = NULL, CODE = { - data.frame(Names=c("a", "b", "c"), - Ages=c(200,300,400)) - }), - "Creating cache entry from CODE") - - # Remove it from Global Environment (rm will fail if it's not created from CODE) - expect_error(rm(list=var_to_cache, envir = .TargetEnv), NA) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - # Load up from cache and check it's the same as what was originally created - suppressMessages(load.project()) - expect_equal(get(var_to_cache, envir = .TargetEnv) , test_data) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - tidy_up() + var_to_cache <- "xxxx" + test_data <- data.frame( + Names = c("a", "b", "c"), + Ages = c(200, 300, 400) + ) + + + # Create a cached version created from CODE + expect_message( + cache(var_to_cache, depends = NULL, CODE = { + data.frame( + Names = c("a", "b", "c"), + Ages = c(200, 300, 400) + ) + }), + "Creating cache entry from CODE" + ) + + # Remove it from Global Environment (rm will fail if it's not created from CODE) + expect_error(rm(list = var_to_cache, envir = .TargetEnv), NA) + + # Load up from cache and check it's the same as what was originally created + suppressMessages(load.project()) + expect_equal(get(var_to_cache, envir = .TargetEnv), test_data) + tidy_up() }) test_that("caching a variable created from CODE using 'data.table' along with non-standard evaluation caches correctly", { - # The 'data.table' package has a mechanism to check if the calling - # environment is 'data.table' aware - # (https://github.com/Rdatatable/data.table/blob/master/R/cedta.R) - # and otherwise calls the corresponding 'data.frame' method for at least - # the '[' operator instead. This is why the parent environment of the - # environment where 'CODE' is evaluated in has to be the global - # environment ('.TargetEnv'). - - skip_if_not_installed("data.table") - # CODE caching requires suggested package formatR + # The 'data.table' package has a mechanism to check if the calling + # environment is 'data.table' aware + # (https://github.com/Rdatatable/data.table/blob/master/R/cedta.R) + # and otherwise calls the corresponding 'data.frame' method for at least + # the '[' operator instead. This is why the parent environment of the + # environment where 'CODE' is evaluated in has to be the global + # environment ('.TargetEnv'). - skip_if_not_installed("formatR") + skip_if_not_installed("data.table") + # CODE caching requires suggested package formatR - test_project <- tempfile("test_project") - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + skip_if_not_installed("formatR") - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project),project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - var_to_cache <- "foo" + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - expect_error( - { - suppressMessages(cache(var_to_cache, CODE = { - bar <- data.table::data.table(baz = 1:12) - bar[baz <= 6, ] - })) - }, - NA - ) + var_to_cache <- "foo" - expect_identical( - get(var_to_cache, envir = .TargetEnv), - data.table::data.table(baz = 1:6) - ) + expect_error( + { + suppressMessages(cache(var_to_cache, CODE = { + bar <- data.table::data.table(baz = 1:12) + bar[baz <= 6, ] + })) + }, + NA + ) - tidy_up() + expect_identical( + get(var_to_cache, envir = .TargetEnv), + data.table::data.table(baz = 1:6) + ) + + tidy_up() }) for (cache_file_format in cache_file_formats) { - test_that('re-caching is skipped when a cached variable hasnt changed', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + test_that("re-caching is skipped when a cached variable hasnt changed", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project),project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - set_cache_file_format(cache_file_format) + set_cache_file_format(cache_file_format) - var_to_cache <- "xxxx" - test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) - assign(var_to_cache, test_data, envir = .TargetEnv) + var_to_cache <- "xxxx" + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + assign(var_to_cache, test_data, envir = .TargetEnv) - # Create initial cached version - cache(var_to_cache, CODE = NULL, depends = NULL) - initial_mtime <- file.info(file.path('cache', paste(var_to_cache, cache_file_format, sep = ".")))$mtime + # Create initial cached version + cache(var_to_cache, CODE = NULL, depends = NULL) + initial_mtime <- file.info(file.path("cache", paste(var_to_cache, cache_file_format, sep = ".")))$mtime - # wait two seconds - Sys.sleep(2) + # wait two seconds + Sys.sleep(2) - # Remove it from Global Environment - rm(list=var_to_cache, envir = .TargetEnv) + # Remove it from Global Environment + rm(list = var_to_cache, envir = .TargetEnv) - # Load up from cache and attempt to re-cache - suppressMessages(load.project()) - expect_message(cache(var_to_cache, CODE = NULL, depends = NULL), - "Skipping cache update for") + # Load up from cache and attempt to re-cache + suppressMessages(load.project()) + expect_message( + cache(var_to_cache, CODE = NULL, depends = NULL), + "Skipping cache update for" + ) - # Check that modification time hasn't changed - new_mtime <- file.info(file.path('cache', paste(var_to_cache, cache_file_format, sep = ".")))$mtime + # Check that modification time hasn't changed + new_mtime <- file.info(file.path("cache", paste(var_to_cache, cache_file_format, sep = ".")))$mtime - expect_equal(initial_mtime, new_mtime) + expect_equal(initial_mtime, new_mtime) - tidy_up() - }) + tidy_up() + }) - test_that('re-caching is done again when a cached variable has changed', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + test_that("re-caching is done again when a cached variable has changed", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project),project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - set_cache_file_format(cache_file_format) + set_cache_file_format(cache_file_format) - var_to_cache <- "xxxx" - test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) - assign(var_to_cache, test_data, envir = .TargetEnv) + var_to_cache <- "xxxx" + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + assign(var_to_cache, test_data, envir = .TargetEnv) - # Create initial cached version - cache(var_to_cache, CODE = NULL, depends = NULL) - initial_mtime <- file.info(file.path('cache', paste(var_to_cache, cache_file_format, sep = ".")))$mtime + # Create initial cached version + cache(var_to_cache, CODE = NULL, depends = NULL) + initial_mtime <- file.info(file.path("cache", paste(var_to_cache, cache_file_format, sep = ".")))$mtime - # wait two seconds - Sys.sleep(2) + # wait two seconds + Sys.sleep(2) - # Remove it from Global Environment - rm(list=var_to_cache, envir = .TargetEnv) + # Remove it from Global Environment + rm(list = var_to_cache, envir = .TargetEnv) - # Load up from cache - suppressMessages(load.project()) + # Load up from cache + suppressMessages(load.project()) - # change the variable and attempt to re-cache - test_data2 <- data.frame(Names = c("aaa", "b", "c"), Ages = c(20, 30, 40)) - assign(var_to_cache, test_data2, envir = .TargetEnv) + # change the variable and attempt to re-cache + test_data2 <- data.frame(Names = c("aaa", "b", "c"), Ages = c(20, 30, 40)) + assign(var_to_cache, test_data2, envir = .TargetEnv) - expect_message(cache(var_to_cache, CODE = NULL, depends = NULL), - "Updating existing cache entry from global environment") + expect_message( + cache(var_to_cache, CODE = NULL, depends = NULL), + "Updating existing cache entry from global environment" + ) - # Check that modification time has changed - new_mtime <- file.info(file.path('cache', paste(var_to_cache, cache_file_format, sep = ".")))$mtime - expect_false(isTRUE(all.equal(initial_mtime, new_mtime))) + # Check that modification time has changed + new_mtime <- file.info(file.path("cache", paste(var_to_cache, cache_file_format, sep = ".")))$mtime + expect_false(isTRUE(all.equal(initial_mtime, new_mtime))) - # Reload and check that re-cached value is different - rm(list=var_to_cache, envir = .TargetEnv) - suppressMessages(load.project()) + # Reload and check that re-cached value is different + rm(list = var_to_cache, envir = .TargetEnv) + suppressMessages(load.project()) - expect_false(isTRUE(all.equal(get(var_to_cache, envir = .TargetEnv) , test_data))) + expect_false(isTRUE(all.equal(get(var_to_cache, envir = .TargetEnv), test_data))) - tidy_up() - }) + tidy_up() + }) - test_that('re-caching fails with correct message if cached variable is not in global env', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + test_that("re-caching fails with correct message if cached variable is not in global env", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - set_cache_file_format(cache_file_format) + set_cache_file_format(cache_file_format) - var_to_cache <- "xxxx" - test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) - assign(var_to_cache, test_data, envir = .TargetEnv) + var_to_cache <- "xxxx" + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + assign(var_to_cache, test_data, envir = .TargetEnv) - # Create initial cached version - cache(var_to_cache, CODE = NULL, depends = NULL) - initial_mtime <- file.info(file.path('cache', paste(var_to_cache, cache_file_format, sep = ".")))$mtime + # Create initial cached version + cache(var_to_cache, CODE = NULL, depends = NULL) + initial_mtime <- file.info(file.path("cache", paste(var_to_cache, cache_file_format, sep = ".")))$mtime - # wait two seconds - Sys.sleep(2) + # wait two seconds + Sys.sleep(2) - # Remove it from Global Environment - rm(list=var_to_cache, envir = .TargetEnv) + # Remove it from Global Environment + rm(list = var_to_cache, envir = .TargetEnv) - # Load up from cache - suppressMessages(load.project()) + # Load up from cache + suppressMessages(load.project()) - # And remove it from Global Environment again - rm(list=var_to_cache, envir = .TargetEnv) + # And remove it from Global Environment again + rm(list = var_to_cache, envir = .TargetEnv) - expect_message(cache(var_to_cache, CODE = NULL, depends = NULL), - "Unable to update cache for") + expect_message( + cache(var_to_cache, CODE = NULL, depends = NULL), + "Unable to update cache for" + ) - # Check that modification time hasn't changed - new_mtime <- file.info(file.path('cache', paste(var_to_cache, cache_file_format, sep = ".")))$mtime - expect_equal(initial_mtime, new_mtime) + # Check that modification time hasn't changed + new_mtime <- file.info(file.path("cache", paste(var_to_cache, cache_file_format, sep = ".")))$mtime + expect_equal(initial_mtime, new_mtime) - tidy_up() - }) -} + tidy_up() + }) + } test_that('re-caching a variable created from CODE only happens if code changes, not comments or white space', { # CODE caching requires suggested package formatR skip_if_not_installed("formatR") test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) on.exit(unlink(test_project, recursive = TRUE), add = TRUE) oldwd <- setwd(test_project) @@ -324,7 +333,7 @@ test_that('re-caching a variable created from CODE only happens if code changes, test_that("cached variable names are assessed correctly", { test_project <- tempfile("test_project") - suppressMessages(create.project(test_project)) + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) on.exit(unlink(test_project, recursive = TRUE), add = TRUE) oldwd <- setwd(test_project) @@ -345,207 +354,211 @@ test_that("cached variable names are assessed correctly", { }) test_that('caching a variable with an underscore is not unnecessarily loaded next load.project()', { + test_project <- tempfile('test_project') + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - var_to_cache <- "xx_xx" - test_data <- data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40)) - assign(var_to_cache, test_data, envir = .TargetEnv) + var_to_cache <- "xx_xx" + test_data <- data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40)) + assign(var_to_cache, test_data, envir = .TargetEnv) - # Create a new cached version - expect_message(cache(var_to_cache, CODE = NULL, depends = NULL), - "Creating cache entry from global environment") + # Create a new cached version + expect_message(cache(var_to_cache, CODE = NULL, depends = NULL), + "Creating cache entry from global environment") - # Load up from cache and check no message contains an x - expect_message(load.project(), "[^[^x]+$") + # Load up from cache and check no message contains an x + expect_message(load.project(), "[^[^x]+$") - tidy_up() + tidy_up() }) -test_that('cache and memory is cleared correctly', { - - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) +test_that("cache and memory is cleared correctly", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - var_to_cache <- "xxxx" + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - test_data <- data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40)) - assign(var_to_cache, test_data, envir = .TargetEnv) + var_to_cache <- "xxxx" - # Create a new cached version - expect_message(cache("xxxx"), - "Creating cache entry from global environment") + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + assign(var_to_cache, test_data, envir = .TargetEnv) - # clear from memory - clear("xxxx", force = TRUE) + # Create a new cached version + expect_message( + cache("xxxx"), + "Creating cache entry from global environment" + ) - # Read the config and set config$sticky_variables - cfg <- .read.config() - cfg$sticky_variables <- var_to_cache - .save.config(cfg) + # clear from memory + clear("xxxx", force = TRUE) - # variable is loaded into memory when load.project is run - expect_message(load.project(), "Loading cached data set: xxxx") + # Read the config and set config$sticky_variables + cfg <- .read.config() + cfg$sticky_variables <- var_to_cache + .save.config(cfg) - # variable exists in cache - expect_message(cache(), "Variable: xxxx") + # variable is loaded into memory when load.project is run + expect_message(load.project(), "Loading cached data set: xxxx") - # variable should still be in memory - expect_message(clear(), "not cleared: config xxxx") + # variable exists in cache + expect_message(cache(), "Variable: xxxx") - # variable exists in memory - expect_true(exists("xxxx")) + # variable should still be in memory + expect_message(clear(), "not cleared: config xxxx") - # delete variable from cache - expect_message(clear.cache(), "Removed successfully") + # variable exists in memory + expect_true(exists("xxxx")) - # variable does not exist in memory, should have been forced cleared - expect_true(!exists("xxxx")) + # delete variable from cache + expect_message(clear.cache(), "Removed successfully") - # shouldn't be anything in the cache - expect_message(cache(), "No variables in cache") + # variable does not exist in memory, should have been forced cleared + expect_true(!exists("xxxx")) - tidy_up() + # shouldn't be anything in the cache + expect_message(cache(), "No variables in cache") + tidy_up() }) -test_that('multiple items are cleared correctly from the cache', { - - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) +test_that("multiple items are cleared correctly from the cache", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - assign("xxx", 10, envir = .TargetEnv) - assign("yyy", 20, envir = .TargetEnv) - assign("zzz", 30, envir = .TargetEnv) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # Create cached version of each of these - expect_message(cache("xxx"), - "Creating cache entry from global environment") + assign("xxx", 10, envir = .TargetEnv) + assign("yyy", 20, envir = .TargetEnv) + assign("zzz", 30, envir = .TargetEnv) - expect_message(cache("yyy"), - "Creating cache entry from global environment") + # Create cached version of each of these + expect_message( + cache("xxx"), + "Creating cache entry from global environment" + ) - expect_message(cache("zzz"), - "Creating cache entry from global environment") + expect_message( + cache("yyy"), + "Creating cache entry from global environment" + ) + expect_message( + cache("zzz"), + "Creating cache entry from global environment" + ) - # clear two variables from cache - expect_message(clear.cache("yyy", "zzz"), "Removed successfully ") - # Check variables not in global env - expect_true(!exists("yyy")) - expect_true(!exists("zzz")) + # clear two variables from cache + expect_message(clear.cache("yyy", "zzz"), "Removed successfully ") - # clear everything and reload project - clear(force = TRUE) - # variable is loaded into memory when load.project is run - expect_message(load.project(), "Loading cached data set: xxx") + # Check variables not in global env + expect_true(!exists("yyy")) + expect_true(!exists("zzz")) - expect_equal(xxx, 10) + # clear everything and reload project + clear(force = TRUE) + # variable is loaded into memory when load.project is run + expect_message(load.project(), "Loading cached data set: xxx") - # variable exists in memory - expect_true(exists("xxx")) + expect_equal(xxx, 10) - # Check variables still not in global env - expect_true(!exists("yyy")) - expect_true(!exists("zzz")) + # variable exists in memory + expect_true(exists("xxx")) + # Check variables still not in global env + expect_true(!exists("yyy")) + expect_true(!exists("zzz")) - tidy_up() + tidy_up() }) -test_that('caching a variable using CODE doesnt leave variables in globalenv', { - # CODE caching requires suggested package formatR +test_that("caching a variable using CODE doesnt leave variables in globalenv", { + # CODE caching requires suggested package formatR - skip_if_not_installed("formatR") + skip_if_not_installed("formatR") - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - var_to_cache <- "xxxx" + var_to_cache <- "xxxx" - # make sure it doesn't exist - if (exists(var_to_cache, envir = .TargetEnv )) { - rm(list=var_to_cache, envir = .TargetEnv) - } + # make sure it doesn't exist + if (exists(var_to_cache, envir = .TargetEnv)) { + rm(list = var_to_cache, envir = .TargetEnv) + } - # set an environment variable in global env - assign("yyy", 10, envir = .TargetEnv) + # set an environment variable in global env + assign("yyy", 10, envir = .TargetEnv) - # create a cached variable - cache(var_to_cache, CODE = { - aaa <- 10 - bbb <- 10 - aaa*bbb*yyy - }) + # create a cached variable + cache(var_to_cache, CODE = { + aaa <- 10 + bbb <- 10 + aaa * bbb * yyy + }) - # check variable calculates correctly - expect_equal(get(var_to_cache), 10*10*10) + # check variable calculates correctly + expect_equal(get(var_to_cache), 10 * 10 * 10) - # Make sure local variables don't exist in global env - expect_true(!exists("aaa")) - expect_true(!exists("bbb")) + # Make sure local variables don't exist in global env + expect_true(!exists("aaa")) + expect_true(!exists("bbb")) - tidy_up() + tidy_up() }) -test_that('caching a variable already in cache with no hash file re-caches correctly', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) +test_that("caching a variable already in cache with no hash file re-caches correctly", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - var_to_cache <- "xxxx" - test_data <- data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40)) - assign(var_to_cache, test_data, envir = .TargetEnv) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # Create a new cached version - expect_message(cache(var_to_cache, CODE = NULL, depends = NULL), - "Creating cache entry from global environment") + var_to_cache <- "xxxx" + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + assign(var_to_cache, test_data, envir = .TargetEnv) - # delete the hash file - unlink(file.path("cache", paste0(var_to_cache, ".hash"))) + # Create a new cached version + expect_message( + cache(var_to_cache, CODE = NULL, depends = NULL), + "Creating cache entry from global environment" + ) - # Create another cached version: should be created from new again - expect_message(cache(var_to_cache, CODE = NULL, depends = NULL), - "Creating cache entry from global environment") + # delete the hash file + unlink(file.path("cache", paste0(var_to_cache, ".hash"))) - # Check that the hash file exists - expect_true(file.exists(file.path("cache", paste0(var_to_cache, ".hash")))) + # Create another cached version: should be created from new again + expect_message( + cache(var_to_cache, CODE = NULL, depends = NULL), + "Creating cache entry from global environment" + ) - # Load up from cache and check it's the same as what was originally created - suppressMessages(load.project()) - expect_equal(get(var_to_cache, envir = .TargetEnv) , test_data) + # Check that the hash file exists + expect_true(file.exists(file.path("cache", paste0(var_to_cache, ".hash")))) - tidy_up() + # Load up from cache and check it's the same as what was originally created + suppressMessages(load.project()) + expect_equal(get(var_to_cache, envir = .TargetEnv), test_data) + tidy_up() }) + diff --git a/tests/testthat/test-clear.R b/tests/testthat/test-clear.R index ce6492d6..de7bccfb 100644 --- a/tests/testthat/test-clear.R +++ b/tests/testthat/test-clear.R @@ -1,221 +1,227 @@ -context('Clearing from memory') +test_that("running clear() with default parameters removes everything except config", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) -test_that('running clear() with default parameters removes everything except config', { + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + suppressMessages(load.project()) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + # default objects are config helper.function project.info + # Because sticky_cvariables is not set, the config variable should be wiped also + # Using a regular expression as sometimes the list of cleared variables differs (notably .Random.seed) + expect_message(clear(), "clear from memory:.*?config.*?helper\\.function.*project.info.*") - suppressMessages(load.project()) + # check they don't exist + expect_true(!exists(c("project.info"), envir = .TargetEnv)) + expect_true(!exists(c("helper.function"), envir = .TargetEnv)) - # default objects are config helper.function project.info - # Because sticky_cvariables is not set, the config variable should be wiped also - # Using a regular expression as sometimes the list of cleared variables differs (notably .Random.seed) - expect_message(clear(), "clear from memory:.*?config.*?helper\\.function.*project.info.*") + # check config does not exist + expect_true(!exists("config", envir = .TargetEnv)) - # check they don't exist - expect_true(!exists(c("project.info"), envir = .TargetEnv)) - expect_true(!exists(c("helper.function"), envir = .TargetEnv)) - - # check config does not exist - expect_true(!exists("config", envir = .TargetEnv)) - - tidy_up() + tidy_up() }) -test_that('running clear(keep=helper.function) removes everything except the helper.function', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("running clear(keep=helper.function) removes everything except the helper.function", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + suppressMessages(load.project()) - # default objects are config helper.function project.info - expect_message(clear(keep="helper.function"), "not cleared: helper.function") + # default objects are config helper.function project.info + expect_message(clear(keep = "helper.function"), "not cleared: helper.function") - # check the others don't exist - expect_true(!exists("project.info", envir = .TargetEnv)) + # check the others don't exist + expect_true(!exists("project.info", envir = .TargetEnv)) - # check that config also does not exist - expect_true(!exists("config", envir = .TargetEnv)) + # check that config also does not exist + expect_true(!exists("config", envir = .TargetEnv)) - tidy_up() + tidy_up() }) -test_that('running clear() removes everything except the config$sticky_variables', { +test_that("running clear() removes everything except the config$sticky_variables", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + # Read the config and set config$sticky_variables + cfg <- .read.config() + cfg$sticky_variables <- "helper.function" + .save.config(cfg) - # Read the config and set config$sticky_variables - cfg <- .read.config() - cfg$sticky_variables <- "helper.function" - .save.config(cfg) + suppressMessages(load.project()) - suppressMessages(load.project()) + # default objects are config helper.function project.info + expect_message(clear(), "not cleared: config helper.function") - # default objects are config helper.function project.info - expect_message(clear(), "not cleared: config helper.function") + # check the others don't exist + expect_true(!exists("project.info", envir = .TargetEnv)) - # check the others don't exist - expect_true(!exists("project.info", envir = .TargetEnv)) + # check that config and helper.function does exist + expect_true(exists(c("config"), envir = .TargetEnv)) + expect_true(exists(c("helper.function"), envir = .TargetEnv)) - # check that config and helper.function does exist - expect_true(exists(c("config"), envir = .TargetEnv)) - expect_true(exists(c("helper.function"), envir = .TargetEnv)) - - tidy_up() + tidy_up() }) -test_that('running clear(force=TRUE) removes everything including the config$sticky_variables', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("running clear(force=TRUE) removes everything including the config$sticky_variables", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # Read the config and set config$sticky_variables - config <- .read.config() - config$sticky_variables <- "helper.function" - .save.config(config) + # Read the config and set config$sticky_variables + config <- .read.config() + config$sticky_variables <- "helper.function" + .save.config(config) - suppressMessages(load.project()) + suppressMessages(load.project()) - # default objects are config helper.function project.info - expect_message(clear(force=TRUE), "clear from memory: config helper.function project.info") + # default objects are config helper.function project.info + expect_message(clear(force = TRUE), "clear from memory: config helper.function project.info") - # check they don't exist - expect_true(!exists(c("project.info"), envir = .TargetEnv)) - expect_true(!exists(c("config"), envir = .TargetEnv)) - expect_true(!exists(c("helper.function"), envir = .TargetEnv)) + # check they don't exist + expect_true(!exists(c("project.info"), envir = .TargetEnv)) + expect_true(!exists(c("config"), envir = .TargetEnv)) + expect_true(!exists(c("helper.function"), envir = .TargetEnv)) - tidy_up() + tidy_up() }) -test_that('running in a project template directory loads the latest config.dcf', { +test_that("running in a project template directory loads the latest config.dcf", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + # Read the config and set config$sticky_variables + config <- .read.config() + config$sticky_variables <- "xxx" + .save.config(config) - # Read the config and set config$sticky_variables - config <- .read.config() - config$sticky_variables <- "xxx" - .save.config(config) + # set the sticky variable + assign("xxx", 1, envir = .TargetEnv) - # set the sticky variable - assign("xxx", 1, envir = .TargetEnv) + # set another variable + assign("yyy", 2, envir = .TargetEnv) - # set another variable - assign("yyy", 2, envir = .TargetEnv) + # check xxx is not cleared + expect_message(clear(), "not cleared: config xxx") - # check xxx is not cleared - expect_message(clear(), "not cleared: config xxx") + # check xxx is still exists + expect_true(exists("xxx", envir = .TargetEnv)) - # check xxx is still exists - expect_true(exists("xxx", envir = .TargetEnv)) + # check the others don't exist + expect_true(!exists("yyy", envir = .TargetEnv)) - # check the others don't exist - expect_true(!exists("yyy", envir = .TargetEnv)) + # check that config exists + expect_true(exists("config", envir = .TargetEnv)) - # check that config exists - expect_true(exists("config", envir = .TargetEnv)) - - tidy_up() + tidy_up() }) -test_that('running clear() with variables that dont exist fail cleanly', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("running clear() with variables that dont exist fail cleanly", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # nothing in memory - expect_message(clear("xxx"), "No objects to clear") + # nothing in memory + expect_message(clear("xxx"), "No objects to clear") - # Read the config and set config$sticky_variables - config <- .read.config() - config$sticky_variables <- "xxx" - .save.config(config) + # Read the config and set config$sticky_variables + config <- .read.config() + config$sticky_variables <- "xxx" + .save.config(config) - # should still be nothing, even though it's defined in config$sticky_variables - expect_message(clear("xxx"), "No objects to clear") + # should still be nothing, even though it's defined in config$sticky_variables + expect_message(clear("xxx"), "No objects to clear") - tidy_up() + tidy_up() }) -test_that('running clear() with an object not in memory fails cleanly', { +test_that("running clear() with an object not in memory fails cleanly", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + # Try to delete an item that doesn't exist - # Try to delete an item that doesn't exist + expect_message(clear("yyyy"), "objects not in memory: yyyy") - expect_message(clear("yyyy"), "objects not in memory: yyyy") - - tidy_up() + tidy_up() }) -test_that('running clear() removes variables beginning with a dot (except the config$sticky_variables)', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("running clear() removes variables beginning with a dot (except the config$sticky_variables)", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # Set some global variables beginning with a dot - assign(".xxx", 10, envir = .TargetEnv) - assign(".yyy", 20, envir = .TargetEnv) + # Set some global variables beginning with a dot + assign(".xxx", 10, envir = .TargetEnv) + assign(".yyy", 20, envir = .TargetEnv) - # Read the config and set config$sticky_variables - cfg <- .read.config() - cfg$sticky_variables <- ".xxx" - .save.config(cfg) + # Read the config and set config$sticky_variables + cfg <- .read.config() + cfg$sticky_variables <- ".xxx" + .save.config(cfg) - suppressMessages(load.project()) + suppressMessages(load.project()) - # should keep .xxx - expect_message(clear(), "not cleared: config .xxx") - expect_true(exists(".xxx")) - expect_equal(get(".xxx", envir = .TargetEnv), 10) + # should keep .xxx + expect_message(clear(), "not cleared: config .xxx") + expect_true(exists(".xxx")) + expect_equal(get(".xxx", envir = .TargetEnv), 10) - # should be no .yyy - expect_true(!exists(".yyy")) + # should be no .yyy + expect_true(!exists(".yyy")) - expect_message(clear(force=TRUE), "clear from memory: .xxx") + expect_message(clear(force = TRUE), "clear from memory: .xxx") - # should be no .xxx - expect_true(!exists(".xxx")) + # should be no .xxx + expect_true(!exists(".xxx")) - tidy_up() + tidy_up() }) diff --git a/tests/testthat/test-config.R b/tests/testthat/test-config.R index eb202386..9c8ccb42 100644 --- a/tests/testthat/test-config.R +++ b/tests/testthat/test-config.R @@ -1,85 +1,77 @@ -context('Configuration') - -test_that('Unknown fields give a warning, except if start with hash', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - config <- .new.config - config$dummy <- 'dummy' - write.dcf(config, 'config/global.dcf') - expect_that(load.project(), gives_warning("Your configuration contains the following unused entries")) - - config <- .new.config - write.dcf(config, 'config/global.dcf') - # write.dcf won't allow writing fields that start with a hash - config.dcf <- readLines('config/global.dcf') - config.dcf <- c(config.dcf, "# comment: A comment", - "#: Yet another comment") - writeLines(config.dcf, 'config/global.dcf') - expect_warning(load.project(), NA) - +test_that("Unknown fields give a warning, except if start with hash", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + + config <- .new.config + config$dummy <- "dummy" + write.dcf(config, "config/global.dcf") + expect_warning(load.project(), "Your configuration contains the following unused entries") + + config <- .new.config + write.dcf(config, "config/global.dcf") + # write.dcf won't allow writing fields that start with a hash + config.dcf <- readLines("config/global.dcf") + config.dcf <- c( + config.dcf, "# comment: A comment", + "#: Yet another comment" + ) + writeLines(config.dcf, "config/global.dcf") + expect_warning(load.project(), NA) }) -test_that('project.config() displays config correctly', { +test_that("project.config() displays config correctly", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - # Read the config and flip a value - config <- .read.config() - flipped_value <- !config$as_factors - config$as_factors <- flipped_value - .save.config(config) - - # check that the flipped value is displayed - expect_message(project.config(), paste0("as_factors[ ]+", as.character(flipped_value))) + # Read the config and flip a value + config <- .read.config() + flipped_value <- !config$as_factors + config$as_factors <- flipped_value + .save.config(config) + # check that the flipped value is displayed + expect_message(project.config(), paste0("as_factors[ ]+", as.character(flipped_value))) }) -test_that('R code in between back ticks is evaluated in config files', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("R code in between back ticks is evaluated in config files", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # Set an environment variable to an old version number - Sys.setenv(version="0.1") - on.exit(Sys.unsetenv("version")) + # Set an environment variable to an old version number + Sys.setenv(version = "0.1") + on.exit(Sys.unsetenv("version")) - config <- .load.config() - #change the version number field to be some R code to retrieve the environment variable - config$version <- '`Sys.getenv("version")`' - write.dcf(config, .project.config) - - expect_warning(load.project(), "compatible with version 0.1") + config <- .load.config() + # change the version number field to be some R code to retrieve the environment variable + config$version <- '`Sys.getenv("version")`' + write.dcf(config, .project.config) + expect_warning(load.project(), "compatible with version 0.1") }) -test_that('Replace data_tables with tables_type', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) +test_that("Replace data_tables with tables_type", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - config <- .new.config - config$tables_type <- 'tibble' - config$data_tables <- NULL - write.dcf(config, 'config/global.dcf') - expect_warning(load.project(), NA) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + config <- .new.config + config$tables_type <- "tibble" + config$data_tables <- NULL + write.dcf(config, "config/global.dcf") + expect_warning(load.project(), NA) }) diff --git a/tests/testthat/test-create.R b/tests/testthat/test-create.R index 9927f1e2..c6e3e0e1 100644 --- a/tests/testthat/test-create.R +++ b/tests/testthat/test-create.R @@ -1,262 +1,285 @@ -context('Create project') - expect_file <- function(...) { - x <- file.path(...) - expect_true(file.exists(x), x) + x <- file.path(...) + expect_true(file.exists(x), x) } expect_no_file <- function(...) { - x <- file.path(...) - expect_false(file.exists(x), x) + x <- file.path(...) + expect_false(file.exists(x), x) } expect_dir <- function(...) { - x <- file.path(...) - expect_file(x) - expect_true(.is.dir(x)) - expect_file(file.path(x, 'README.md')) + x <- file.path(...) + expect_file(x) + expect_true(.is.dir(x)) + expect_file(file.path(x, "README.md")) } expect_full <- function() { - expect_dir('.') - expect_dir('cache') - expect_dir('config') - expect_file(file.path('config', 'global.dcf')) - expect_dir('data') - expect_dir('diagnostics') - expect_file(file.path('diagnostics', '1.R')) - expect_dir('docs') - expect_dir('graphs') - expect_dir('lib') - expect_file(file.path('lib', 'helpers.R')) - expect_dir('logs') - expect_dir('munge') - expect_file(file.path('munge', '01-A.R')) - expect_dir('profiling') - expect_file(file.path('profiling', '1.R')) - expect_dir('reports') - expect_dir('src') - expect_file(file.path('src', 'eda.R')) - expect_dir('tests') - expect_file(file.path('tests', '1.R')) - expect_file(file.path('TODO')) + expect_dir(".") + expect_dir("cache") + expect_dir("config") + expect_file(file.path("config", "global.dcf")) + expect_dir("data") + expect_dir("diagnostics") + expect_file(file.path("diagnostics", "1.R")) + expect_dir("docs") + expect_dir("graphs") + expect_dir("lib") + expect_file(file.path("lib", "helpers.R")) + expect_dir("logs") + expect_dir("munge") + expect_file(file.path("munge", "01-A.R")) + expect_dir("profiling") + expect_file(file.path("profiling", "1.R")) + expect_dir("reports") + expect_dir("src") + expect_file(file.path("src", "eda.R")) + expect_dir("tests") + expect_file(file.path("tests", "1.R")) + expect_file(file.path("TODO")) } expect_minimal <- function() { - expect_dir('.') - expect_dir('cache') - expect_dir('config') - expect_file(file.path('config', 'global.dcf')) - expect_dir('data') - expect_dir('munge') - expect_file(file.path('munge', '01-A.R')) - expect_dir('src') - expect_file(file.path('src', 'eda.R')) - - expect_no_file('diagnostics') - expect_no_file('docs') - expect_no_file('graphs') - expect_no_file('lib') - expect_no_file('logs') - expect_no_file('profiling') - expect_no_file('reports') - expect_no_file('tests') - expect_no_file('TODO') + expect_dir(".") + expect_dir("cache") + expect_dir("config") + expect_file(file.path("config", "global.dcf")) + expect_dir("data") + expect_dir("munge") + expect_file(file.path("munge", "01-A.R")) + expect_dir("src") + expect_file(file.path("src", "eda.R")) + + expect_no_file("diagnostics") + expect_no_file("docs") + expect_no_file("graphs") + expect_no_file("lib") + expect_no_file("logs") + expect_no_file("profiling") + expect_no_file("reports") + expect_no_file("tests") + expect_no_file("TODO") } -test_that('Full project', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project, template = 'full')) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) +test_that("Full project", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project), + template = "full" + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - expect_full() + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - suppressMessages(test.project()) + expect_full() + suppressMessages(load.project()) + suppressMessages(test.project()) }) -test_that('Miminal project', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project, template = 'minimal')) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) +test_that("Miminal project", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project), + template = "minimal" + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - expect_minimal() + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + expect_minimal() + suppressMessages(load.project()) }) -test_that('Test full project into existing directory', { +test_that("Test full project into existing directory", { + test_project <- tempfile("test_project") + expect_false(file.exists(file.path(test_project))) + dir.create(test_project) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + expect_true(file.exists(file.path(test_project))) - test_project <- tempfile('test_project') - expect_false(file.exists(file.path(test_project))) - dir.create(test_project) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - expect_true(file.exists(file.path(test_project))) + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project), template = "full")) - suppressMessages(create.project(test_project, template = 'full')) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - expect_full() - - suppressMessages(load.project()) - suppressMessages(test.project()) + expect_full() + suppressMessages(load.project()) + suppressMessages(test.project()) }) -test_that('Test minimal project into existing directory with an unrelated entry', { - - test_project <- tempfile('test_project') - expect_false(file.exists(file.path(test_project))) - dir.create(test_project) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - expect_true(file.exists(file.path(test_project))) - - suppressMessages(create.project(test_project, template = 'minimal', merge.strategy = "allow.non.conflict")) +test_that("Test minimal project into existing directory with an unrelated entry", { + test_project <- tempfile("test_project") + expect_false(file.exists(file.path(test_project))) + dir.create(test_project) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + expect_true(file.exists(file.path(test_project))) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project), + template = "minimal", + merge.strategy = "allow.non.conflict" + )) - expect_minimal() + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + expect_minimal() + suppressMessages(load.project()) }) -test_that('Test failure creating project into existing directory with an unrelated entry if merge.existing is not set', { - - test_project <- tempfile('test_project') - expect_false(file.exists(file.path(test_project))) - dir.create(test_project) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - expect_true(file.exists(file.path(test_project))) - - file.create(file.path(test_project, '.dummy')) - expect_true(file.exists(file.path(test_project, '.dummy'))) - dir.create(file.path(test_project, 'dummy_dir')) - expect_true(file.exists(file.path(test_project, 'dummy_dir'))) - - expect_error( - suppressMessages( - create.project(test_project, template = 'minimal'), "not empty")) - +test_that("Test failure creating project into existing directory with an unrelated entry if merge.existing is not set", { + test_project <- tempfile("test_project") + expect_false(file.exists(file.path(test_project))) + dir.create(test_project) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + expect_true(file.exists(file.path(test_project))) + + file.create(file.path(test_project, ".dummy")) + expect_true(file.exists(file.path(test_project, ".dummy"))) + dir.create(file.path(test_project, "dummy_dir")) + expect_true(file.exists(file.path(test_project, "dummy_dir"))) + + expect_error( + suppressMessages( + create.project(basename(test_project), + project.directory = dirname(test_project), + template = "minimal"), "not empty" + ) + ) }) -test_that('Test failure creating project in directory with existing empty directory matching the name of a template directory', { - - test_project <- tempfile('test_project') - expect_false(file.exists(file.path(test_project))) - dir.create(test_project) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - expect_true(file.exists(file.path(test_project))) - - dir.create(file.path(test_project, 'munge')) - expect_true(file.exists(file.path(test_project, 'munge'))) - - expect_error( - suppressMessages( - create.project(test_project, template = 'minimal', - merge.strategy = "allow.non.conflict"), "overwrite")) - +test_that("Test failure creating project in directory with existing empty directory matching the name of a template directory", { + test_project <- tempfile("test_project") + expect_false(file.exists(file.path(test_project))) + dir.create(test_project) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + expect_true(file.exists(file.path(test_project))) + + dir.create(file.path(test_project, "munge")) + expect_true(file.exists(file.path(test_project, "munge"))) + + expect_error( + suppressMessages( + create.project(basename(test_project), + project.directory = dirname(test_project), + template = "minimal", + merge.strategy = "allow.non.conflict" + ), "overwrite" + ) + ) }) -test_that('Test failure creating project in directory with existing file matching the name of a template directory',{ - - test_project <- tempfile('test_project') - expect_false(file.exists(file.path(test_project))) - dir.create(test_project) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - expect_true(file.exists(file.path(test_project))) - - file.create(file.path(test_project, 'munge')) - expect_true(file.exists(file.path(test_project, 'munge'))) - - expect_error( - suppressMessages( - create.project(test_project, template = 'minimal', - merge.strategy = "allow.non.conflict"), "overwrite")) - +test_that("Test failure creating project in directory with existing file matching the name of a template directory", { + test_project <- tempfile("test_project") + expect_false(file.exists(file.path(test_project))) + dir.create(test_project) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + expect_true(file.exists(file.path(test_project))) + + file.create(file.path(test_project, "munge")) + expect_true(file.exists(file.path(test_project, "munge"))) + + expect_error( + suppressMessages( + create.project(basename(test_project), + project.directory = dirname(test_project), + template = "minimal", + merge.strategy = "allow.non.conflict" + ), "overwrite" + ) + ) }) -test_that('Test failure creating project in directory with existing empty directory matching the name of a template file', { - - test_project <- tempfile('test_project') - expect_false(file.exists(file.path(test_project))) - dir.create(test_project) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - expect_true(file.exists(file.path(test_project))) - - dir.create(file.path(test_project, 'README.md')) - expect_true(file.exists(file.path(test_project, 'README.md'))) - - expect_error( - suppressMessages( - create.project(test_project, template = 'minimal', - merge.strategy = "allow.non.conflict"), "overwrite")) - - +test_that("Test failure creating project in directory with existing empty directory matching the name of a template file", { + test_project <- tempfile("test_project") + expect_false(file.exists(file.path(test_project))) + dir.create(test_project) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + expect_true(file.exists(file.path(test_project))) + + dir.create(file.path(test_project, "README.md")) + expect_true(file.exists(file.path(test_project, "README.md"))) + + expect_error( + suppressMessages( + create.project(basename(test_project), + project.directory = dirname(test_project), + template = "minimal", + merge.strategy = "allow.non.conflict" + ), "overwrite" + ) + ) }) -test_that('Test failure creating project in directory with existing file matching the name of a template file', { - - test_project <- tempfile('test_project') - expect_false(file.exists(file.path(test_project))) - dir.create(test_project) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - expect_true(file.exists(file.path(test_project))) - - file.create(file.path(test_project, 'README.md')) - expect_true(file.exists(file.path(test_project, 'README.md'))) - - expect_error( - suppressMessages( - create.project(test_project, template = 'minimal', - merge.strategy = "allow.non.conflict"), "overwrite")) - +test_that("Test failure creating project in directory with existing file matching the name of a template file", { + test_project <- tempfile("test_project") + expect_false(file.exists(file.path(test_project))) + dir.create(test_project) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + expect_true(file.exists(file.path(test_project))) + + file.create(file.path(test_project, "README.md")) + expect_true(file.exists(file.path(test_project, "README.md"))) + + expect_error( + suppressMessages( + create.project(basename(test_project), + project.directory = dirname(test_project), + template = "minimal", + merge.strategy = "allow.non.conflict" + ), "overwrite" + ) + ) }) -test_that('Dont create projects inside other projects', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project, template = 'full')) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Dont create projects inside other projects", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project), template = "full")) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # shouldn't be able to create a new project inside this one - expect_error(create.project("new_project")) + # shouldn't be able to create a new project inside this one + expect_error(create.project("new_project")) - # Also shouldn't be able to create one inside a sub directory of an existing project - setwd(file.path(test_project, 'lib')) - expect_error(create.project("new_project")) + # Also shouldn't be able to create one inside a sub directory of an existing project + setwd(file.path(test_project, "lib")) + expect_error(create.project("new_project")) }) -test_that('Do create projects on an absolute path from inside project', { - test_project <- tempfile('test_project') - test_project2 <- tempfile('test_project2') - suppressMessages(create.project(test_project, template = 'full')) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - # should be able to create a new project outside this one - expect_error(create.project(test_project2), NA) - on.exit(unlink(test_project2, recursive = TRUE), add = TRUE) - - # But you shouldn't be able to create one inside a sub directory of an - # existing project, even on an absolute path outside the current project - setwd(file.path(test_project, 'lib')) - expect_error(create.project(file.path(test_project2, "munge"))) +test_that("Do create projects on an absolute path from inside project", { + test_project <- tempfile("test_project") + test_project2 <- tempfile("test_project2") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project), + template = "full" + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + + # should be able to create a new project outside this one + expect_error( + create.project(basename(test_project2), + project.directory = dirname(test_project2) + ), + NA + ) + on.exit(unlink(test_project2, recursive = TRUE), add = TRUE) + + # But you shouldn't be able to create one inside a sub directory of an + # existing project, even on an absolute path outside the current project + setwd(file.path(test_project, "lib")) + expect_error( + create.project(basename(test_project2)) + ) }) diff --git a/tests/testthat/test-list.R b/tests/testthat/test-list.R index ab886592..8a4b9e52 100644 --- a/tests/testthat/test-list.R +++ b/tests/testthat/test-list.R @@ -1,513 +1,533 @@ -context('List data') - -temp_csv_file <- function(dir = '') { - gsub('^[/\\\\]', '', tempfile(pattern = "file", tmpdir = dir, fileext = ".csv")) +temp_csv_file <- function(dir = "") { + gsub("^[/\\\\]", "", tempfile(pattern = "file", tmpdir = dir, fileext = ".csv")) } -test_that('available data is listed correctly with default configuration', { - # Create temporary project - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - # Set temporary project as working directory - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - # Clear the global environment - clear() - - # Create some test data so the file can be loaded if not ignored - test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) - - # Build two temporary filenames, strip leading / - test_file1 <- temp_csv_file() - test_file2 <- temp_csv_file(dir = 'test') - - # Write test data to files - write.csv(test_data, file = file.path('data', test_file1), row.names = FALSE) - dir.create('data/test') - write.csv(test_data, file = file.path('data', test_file2), row.names = FALSE) - - # Check if list.data can be called and returns a data.frame - expect_error(data.files <- list.data(), NA) - expect_true(is.data.frame(data.files)) - - # Check data.frame has a character variable "filename" - expect_true("filename" %in% names(data.files)) - expect_true(class(data.files$filename) == "character") - - # Check data.frame has a character variable "varname" - expect_true("varname" %in% names(data.files)) - expect_true(class(data.files$varname) == "character") - - # Check data.frame has a list variable "reader" - expect_true("reader" %in% names(data.files)) - expect_true(class(data.files$reader) == "list") - - # Check data.frame has a logical variable "is_ignored" - expect_true("is_ignored" %in% names(data.files)) - expect_true(class(data.files$is_ignored) == "logical") - - # Check data.frame has a logical variable "is_directory" - expect_true("is_directory" %in% names(data.files)) - expect_true(class(data.files$is_directory) == "logical") - - # Check data.frame has a logical variable "is_cached" - expect_true("is_cached" %in% names(data.files)) - expect_true(class(data.files$is_cached) == "logical") - - # Check data.frame has a logical variable "cache_only" - expect_true("cache_only" %in% names(data.files)) - expect_true(class(data.files$cache_only) == "logical") - - # Check if data.frame has three rows with default config: - # - fileXXXXX.csv (test_file1, not ignored, varname: fileXXXXX, reader: csv.reader) - # - README.md (not ignored, varname: "", reader: "") - # - test (subdirectory, not ignored, varname: "", reader: "") - expect_true(nrow(data.files) == 3) - test.df <- data.frame( - filename = c(test_file1, 'README.md', 'test'), - varname = c(gsub('.csv', '', test_file1), "", ""), - is_ignored = c(FALSE, FALSE, FALSE), - is_directory = c(FALSE, FALSE, TRUE), - is_cached = c(FALSE, FALSE, FALSE), - cache_only = c(FALSE, FALSE, FALSE), - stringsAsFactors = FALSE - ) - - test.df$reader <- list('csv.reader', '', '') - - # Sort data.frame according to collation of the testing process (for example - # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) - sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) - test.df <- test.df[sort.df,] - - expect_equal(data.files$filename, test.df$filename) - expect_equal(data.files$varname, test.df$varname) - expect_equal(data.files$reader, test.df$reader) - expect_equal(data.files$is_ignored, test.df$is_ignored) - expect_equal(data.files$is_directory, test.df$is_directory) - expect_equal(data.files$is_cached, test.df$is_cached) - expect_equal(data.files$cache_only, test.df$cache_only) +test_that("available data is listed correctly with default configuration", { + # Create temporary project + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + # Set temporary project as working directory + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + + # Clear the global environment + clear() + + # Create some test data so the file can be loaded if not ignored + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + + # Build two temporary filenames, strip leading / + test_file1 <- temp_csv_file() + test_file2 <- temp_csv_file(dir = "test") + + # Write test data to files + write.csv(test_data, file = file.path("data", test_file1), row.names = FALSE) + dir.create("data/test") + write.csv(test_data, file = file.path("data", test_file2), row.names = FALSE) + + # Check if list.data can be called and returns a data.frame + expect_error(data.files <- list.data(), NA) + expect_true(is.data.frame(data.files)) + + # Check data.frame has a character variable "filename" + expect_true("filename" %in% names(data.files)) + expect_true(class(data.files$filename) == "character") + + # Check data.frame has a character variable "varname" + expect_true("varname" %in% names(data.files)) + expect_true(class(data.files$varname) == "character") + + # Check data.frame has a list variable "reader" + expect_true("reader" %in% names(data.files)) + expect_true(class(data.files$reader) == "list") + + # Check data.frame has a logical variable "is_ignored" + expect_true("is_ignored" %in% names(data.files)) + expect_true(class(data.files$is_ignored) == "logical") + + # Check data.frame has a logical variable "is_directory" + expect_true("is_directory" %in% names(data.files)) + expect_true(class(data.files$is_directory) == "logical") + + # Check data.frame has a logical variable "is_cached" + expect_true("is_cached" %in% names(data.files)) + expect_true(class(data.files$is_cached) == "logical") + + # Check data.frame has a logical variable "cache_only" + expect_true("cache_only" %in% names(data.files)) + expect_true(class(data.files$cache_only) == "logical") + + # Check if data.frame has three rows with default config: + # - fileXXXXX.csv (test_file1, not ignored, varname: fileXXXXX, reader: csv.reader) + # - README.md (not ignored, varname: "", reader: "") + # - test (subdirectory, not ignored, varname: "", reader: "") + expect_true(nrow(data.files) == 3) + test.df <- data.frame( + filename = c(test_file1, "README.md", "test"), + varname = c(gsub(".csv", "", test_file1), "", ""), + is_ignored = c(FALSE, FALSE, FALSE), + is_directory = c(FALSE, FALSE, TRUE), + is_cached = c(FALSE, FALSE, FALSE), + cache_only = c(FALSE, FALSE, FALSE), + stringsAsFactors = FALSE + ) + + test.df$reader <- list("csv.reader", "", "") + + # Sort data.frame according to collation of the testing process (for example + # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) + sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) + test.df <- test.df[sort.df, ] + + expect_equal(data.files$filename, test.df$filename) + expect_equal(data.files$varname, test.df$varname) + expect_equal(data.files$reader, test.df$reader) + expect_equal(data.files$is_ignored, test.df$is_ignored) + expect_equal(data.files$is_directory, test.df$is_directory) + expect_equal(data.files$is_cached, test.df$is_cached) + expect_equal(data.files$cache_only, test.df$cache_only) }) -test_that('available data is listed correctly with recursive_loading = TRUE', { - # Create temporary project - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - # Set temporary project as working directory - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - # Read the config data and set cache_loaded_data to FALSE - config <- .read.config() - expect_error(config$recursive_loading <- TRUE, NA) - .save.config(config) - - # Create some test data so the file can be loaded if not ignored - test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) - - # Build two temporary filenames, strip leading / - test_file1 <- temp_csv_file() - test_file2 <- temp_csv_file(dir = 'test') - - # Write test data to files - write.csv(test_data, file = file.path('data', test_file1), row.names = FALSE) - dir.create('data/test') - write.csv(test_data, file = file.path('data', test_file2), row.names = FALSE) - - # Setup test data.frame - test.df <- data.frame( - filename = c(test_file1, 'README.md', gsub("\\\\", "/", test_file2)), - varname = c(gsub('.csv', '', test_file1), - "", - gsub('[/\\\\]', '.', gsub('.csv', '', test_file2))), - is_ignored = c(FALSE, FALSE, FALSE), - is_directory = c(FALSE, FALSE, FALSE), - is_cached = c(FALSE, FALSE, FALSE), - cache_only = c(FALSE, FALSE, FALSE), - stringsAsFactors = FALSE - ) - test.df$reader <- list("csv.reader", "", "csv.reader") - - # Sort data.frame according to collation of the testing process (for example - # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) - sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) - test.df <- test.df[sort.df,] - - # Check if list.data can be called and returns a data.frame - expect_error(data.files <- list.data(), NA) - - # Check if data.frame has three rows with default config: - # - fileXXXXX.csv (test_file1) - # - README.md - # - test/fileYYYYYY.csv (test_file2) - expect_true(nrow(data.files) == 3) - expect_equal(data.files$filename, test.df$filename) - expect_equal(data.files$varname, test.df$varname) - expect_equal(data.files$reader, test.df$reader) - expect_equal(data.files$is_directory, test.df$is_directory) +test_that("available data is listed correctly with recursive_loading = TRUE", { + # Create temporary project + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + # Set temporary project as working directory + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + + # Read the config data and set cache_loaded_data to FALSE + config <- .read.config() + expect_error(config$recursive_loading <- TRUE, NA) + .save.config(config) + + # Create some test data so the file can be loaded if not ignored + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + + # Build two temporary filenames, strip leading / + test_file1 <- temp_csv_file() + test_file2 <- temp_csv_file(dir = "test") + + # Write test data to files + write.csv(test_data, file = file.path("data", test_file1), row.names = FALSE) + dir.create("data/test") + write.csv(test_data, file = file.path("data", test_file2), row.names = FALSE) + + # Setup test data.frame + test.df <- data.frame( + filename = c(test_file1, "README.md", gsub("\\\\", "/", test_file2)), + varname = c( + gsub(".csv", "", test_file1), + "", + gsub("[/\\\\]", ".", gsub(".csv", "", test_file2)) + ), + is_ignored = c(FALSE, FALSE, FALSE), + is_directory = c(FALSE, FALSE, FALSE), + is_cached = c(FALSE, FALSE, FALSE), + cache_only = c(FALSE, FALSE, FALSE), + stringsAsFactors = FALSE + ) + test.df$reader <- list("csv.reader", "", "csv.reader") + + # Sort data.frame according to collation of the testing process (for example + # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) + sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) + test.df <- test.df[sort.df, ] + + # Check if list.data can be called and returns a data.frame + expect_error(data.files <- list.data(), NA) + + # Check if data.frame has three rows with default config: + # - fileXXXXX.csv (test_file1) + # - README.md + # - test/fileYYYYYY.csv (test_file2) + expect_true(nrow(data.files) == 3) + expect_equal(data.files$filename, test.df$filename) + expect_equal(data.files$varname, test.df$varname) + expect_equal(data.files$reader, test.df$reader) + expect_equal(data.files$is_directory, test.df$is_directory) }) -test_that('available data is listed correctly with data_ignore', { - # Create temporary project - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - # Set temporary project as working directory - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - # Create some test data so the file can be loaded if not ignored - test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) - - # Build two temporary filenames, strip leading / - test_file1 <- temp_csv_file() - test_file2 <- temp_csv_file(dir = 'test') - - # Write test data to files - write.csv(test_data, file = file.path('data', test_file1), row.names = FALSE) - dir.create('data/test') - write.csv(test_data, file = file.path('data', test_file2), row.names = FALSE) - # Write an empty Thumbs.db that cannot be loaded by RSQLite - writeLines('\n', file.path('data', 'Thumbs.db')) - - # Check if data.frame has four rows with default config: - # - fileXXXXX.csv (test_file1, not ignored) - # - README.md (not ignored) - # - test (subdirectory, not ignored) - # - Thumbs.db (ignored) - test.df <- data.frame( - filename = c(test_file1, 'README.md', 'test', 'Thumbs.db'), - varname = c(gsub('.csv', '', test_file1), "", "", ""), - reader = c("csv.reader", "", "", ""), - is_ignored = c(FALSE, FALSE, FALSE, FALSE), - is_directory = c(FALSE, FALSE, TRUE, FALSE), - is_cached = c(FALSE, FALSE, FALSE, FALSE), - cache_only = c(FALSE, FALSE, FALSE, FALSE), - stringsAsFactors = FALSE - ) - # Sort data.frame according to collation of the testing process (for example - # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) - sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) - test.df <- test.df[sort.df,] - - expect_error(data.files <- list.data(), NA) - expect_true(nrow(data.files) == 4) - expect_equal(data.files$filename, test.df$filename) - expect_equal(data.files$is_ignored, test.df$is_ignored) - - # Check empty data_ignore: - # - fileXXXXX.csv (test_file1, not ignored) - # - README.md (not ignored) - # - test/fileYYYYY.csv (test_file2, not ignored) - # - Thumbs.db (not ignored) - test.df <- data.frame( - filename = c(test_file1, 'README.md', gsub("\\\\", "/", test_file2), 'Thumbs.db'), - varname = c(gsub('.csv', '', test_file1), - "", - gsub('[/\\\\]', '.', gsub('.csv', '', test_file2)), - ""), - reader = c("csv.reader", "", "csv.reader", ""), - is_ignored = c(FALSE, FALSE, FALSE, FALSE), - is_directory = c(FALSE, FALSE, FALSE, FALSE), - is_cached = c(FALSE, FALSE, FALSE, FALSE), - cache_only = c(FALSE, FALSE, FALSE, FALSE), - stringsAsFactors = FALSE - ) - # Sort data.frame according to collation of the testing process (for example - # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) - sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) - test.df <- test.df[sort.df,] - - expect_error(data.files <- list.data(data_ignore = '', - recursive_loading = TRUE), NA) - expect_true(nrow(data.files) == 4) - expect_equal(data.files$filename, test.df$filename) - expect_equal(data.files$is_ignored, test.df$is_ignored) - - # Check wildcard ignore "*.csv": - # - fileXXXXX.csv (test_file1, ignored) - # - README.md (not ignored) - # - test (subdirectory, not ignored) - # - Thumbs.db (not ignored) - test.df <- data.frame( - filename = c(test_file1, 'README.md', 'test', 'Thumbs.db'), - varname = c(gsub('.csv', '', test_file1), "", "", ""), - reader = c("csv.reader", "", "", ""), - is_ignored = c(TRUE, FALSE, FALSE, FALSE), - is_directory = c(FALSE, FALSE, TRUE, FALSE), - is_cached = c(FALSE, FALSE, FALSE, FALSE), - cache_only = c(FALSE, FALSE, FALSE, FALSE), - stringsAsFactors = FALSE - ) - # Sort data.frame according to collation of the testing process (for example - # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) - sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) - test.df <- test.df[sort.df,] - - expect_error(data.files <- list.data(data_ignore = '*.csv'), NA) - expect_true(nrow(data.files) == 4) - expect_equal(data.files$filename, test.df$filename) - expect_equal(data.files$is_ignored, test.df$is_ignored) - - # Check wildcard ignore "*.csv" with recursive loading on: - # - fileXXXXX.csv (test_file1, ignored) - # - README.md (not ignored) - # - test/fileYYYYY.csv (test_file2, not ignored) - # - Thumbs.db (not ignored) - test.df <- data.frame( - filename = c(test_file1, 'README.md', gsub("\\\\", "/", test_file2), 'Thumbs.db'), - varname = c(gsub('.csv', '', test_file1), - "", - gsub('[/\\\\]', '.', gsub('.csv', '', test_file2)), - ""), - reader = c("csv.reader", "", "csv.reader", ""), - is_ignored = c(TRUE, FALSE, TRUE, FALSE), - is_directory = c(FALSE, FALSE, FALSE, FALSE), - is_cached = c(FALSE, FALSE, FALSE, FALSE), - cache_only = c(FALSE, FALSE, FALSE, FALSE), - stringsAsFactors = FALSE - ) - # Sort data.frame according to collation of the testing process (for example - # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) - sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) - test.df <- test.df[sort.df,] - - expect_error(data.files <- list.data(data_ignore = '*.csv', - recursive_loading = TRUE), NA) - expect_true(nrow(data.files) == 4) - expect_equal(data.files$filename, test.df$filename) - expect_equal(data.files$is_ignored, test.df$is_ignored) - - # Check regular expression ignore "^\w*.csv", only match csv in data, not in - # subdirectories: - # - fileXXXXX.csv (test_file1, ignored) - # - README.md (not ignored) - # - test/fileYYYYY.csv (test_file2, not ignored) - # - Thumbs.db (not ignored) - test.df <- data.frame( - filename = c(test_file1, 'README.md', gsub("\\\\", "/", test_file2), 'Thumbs.db'), - varname = c(gsub('.csv', '', test_file1), - "", - gsub('[/\\\\]', '.', gsub('.csv', '', test_file2)), - ""), - reader = c("csv.reader", "", "csv.reader", ""), - is_ignored = c(TRUE, FALSE, FALSE, FALSE), - is_directory = c(FALSE, FALSE, FALSE, FALSE), - is_cached = c(FALSE, FALSE, FALSE, FALSE), - cache_only = c(FALSE, FALSE, FALSE, FALSE), - stringsAsFactors = FALSE - ) - # Sort data.frame according to collation of the testing process (for example - # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) - sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) - test.df <- test.df[sort.df,] - - expect_error(data.files <- list.data(data_ignore = '/^\\w*\\.csv/', - recursive_loading = TRUE), NA) - expect_true(nrow(data.files) == 4) - expect_equal(data.files$filename, test.df$filename) - expect_equal(data.files$is_ignored, test.df$is_ignored) - - # Check combination literal filename "Thumbs.db" and regular expression - # ignore "^\w*.csv", only match csv in data, not in subdirectories: - # - fileXXXXX.csv (test_file1, ignored) - # - README.md (not ignored) - # - test/fileYYYYY.csv (test_file2, not ignored) - # - Thumbs.db (ignored) - test.df <- data.frame( - filename = c(test_file1, 'README.md', gsub("\\\\", "/", test_file2), 'Thumbs.db'), - varname = c(gsub('.csv', '', test_file1), - "", - gsub('[/\\\\]', '.', gsub('.csv', '', test_file2)), - ""), - reader = c("csv.reader", "", "csv.reader", ""), - is_ignored = c(TRUE, FALSE, FALSE, TRUE), - is_directory = c(FALSE, FALSE, FALSE, FALSE), - is_cached = c(FALSE, FALSE, FALSE, FALSE), - cache_only = c(FALSE, FALSE, FALSE, FALSE), - stringsAsFactors = FALSE - ) - # Sort data.frame according to collation of the testing process (for example - # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) - sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) - test.df <- test.df[sort.df,] - - expect_error(data.files <- list.data(data_ignore = 'Thumbs.db, /^\\w*\\.csv/', - recursive_loading = TRUE), NA) - expect_true(nrow(data.files) == 4) - expect_equal(data.files$filename, test.df$filename) - expect_equal(data.files$is_ignored, test.df$is_ignored) - - # Check extra comma in combination literal filename "Thumbs.db" and regular - # expression ignore "^\w*\.csv", only match csv in data, not in - # subdirectories: - # - fileXXXXX.csv (test_file1, ignored) - # - README.md (not ignored) - # - test/fileYYYYY.csv (test_file2, not ignored) - # - Thumbs.db (ignored) - test.df <- data.frame( - filename = c(test_file1, 'README.md', gsub("\\\\", "/", test_file2), 'Thumbs.db'), - varname = c(gsub('.csv', '', test_file1), - "", - gsub('[/\\\\]', '.', gsub('.csv', '', test_file2)), - ""), - reader = c("csv.reader", "", "csv.reader", ""), - is_ignored = c(TRUE, FALSE, FALSE, TRUE), - is_directory = c(FALSE, FALSE, FALSE, FALSE), - is_cached = c(FALSE, FALSE, FALSE, FALSE), - cache_only = c(FALSE, FALSE, FALSE, FALSE), - stringsAsFactors = FALSE - ) - # Sort data.frame according to collation of the testing process (for example - # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) - sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) - test.df <- test.df[sort.df,] - - expect_error(data.files <- list.data(data_ignore = 'Thumbs.db,,/^\\w*\\.csv/', - recursive_loading = TRUE), NA) - expect_true(nrow(data.files) == 4) - expect_equal(data.files$filename, test.df$filename) - expect_equal(data.files$is_ignored, test.df$is_ignored) +test_that("available data is listed correctly with data_ignore", { + # Create temporary project + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + # Set temporary project as working directory + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + + # Create some test data so the file can be loaded if not ignored + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + + # Build two temporary filenames, strip leading / + test_file1 <- temp_csv_file() + test_file2 <- temp_csv_file(dir = "test") + + # Write test data to files + write.csv(test_data, file = file.path("data", test_file1), row.names = FALSE) + dir.create("data/test") + write.csv(test_data, file = file.path("data", test_file2), row.names = FALSE) + # Write an empty Thumbs.db that cannot be loaded by RSQLite + writeLines("\n", file.path("data", "Thumbs.db")) + + # Check if data.frame has four rows with default config: + # - fileXXXXX.csv (test_file1, not ignored) + # - README.md (not ignored) + # - test (subdirectory, not ignored) + # - Thumbs.db (ignored) + test.df <- data.frame( + filename = c(test_file1, "README.md", "test", "Thumbs.db"), + varname = c(gsub(".csv", "", test_file1), "", "", ""), + reader = c("csv.reader", "", "", ""), + is_ignored = c(FALSE, FALSE, FALSE, FALSE), + is_directory = c(FALSE, FALSE, TRUE, FALSE), + is_cached = c(FALSE, FALSE, FALSE, FALSE), + cache_only = c(FALSE, FALSE, FALSE, FALSE), + stringsAsFactors = FALSE + ) + # Sort data.frame according to collation of the testing process (for example + # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) + sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) + test.df <- test.df[sort.df, ] + + expect_error(data.files <- list.data(), NA) + expect_true(nrow(data.files) == 4) + expect_equal(data.files$filename, test.df$filename) + expect_equal(data.files$is_ignored, test.df$is_ignored) + + # Check empty data_ignore: + # - fileXXXXX.csv (test_file1, not ignored) + # - README.md (not ignored) + # - test/fileYYYYY.csv (test_file2, not ignored) + # - Thumbs.db (not ignored) + test.df <- data.frame( + filename = c(test_file1, "README.md", gsub("\\\\", "/", test_file2), "Thumbs.db"), + varname = c( + gsub(".csv", "", test_file1), + "", + gsub("[/\\\\]", ".", gsub(".csv", "", test_file2)), + "" + ), + reader = c("csv.reader", "", "csv.reader", ""), + is_ignored = c(FALSE, FALSE, FALSE, FALSE), + is_directory = c(FALSE, FALSE, FALSE, FALSE), + is_cached = c(FALSE, FALSE, FALSE, FALSE), + cache_only = c(FALSE, FALSE, FALSE, FALSE), + stringsAsFactors = FALSE + ) + # Sort data.frame according to collation of the testing process (for example + # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) + sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) + test.df <- test.df[sort.df, ] + + expect_error(data.files <- list.data( + data_ignore = "", + recursive_loading = TRUE + ), NA) + expect_true(nrow(data.files) == 4) + expect_equal(data.files$filename, test.df$filename) + expect_equal(data.files$is_ignored, test.df$is_ignored) + + # Check wildcard ignore "*.csv": + # - fileXXXXX.csv (test_file1, ignored) + # - README.md (not ignored) + # - test (subdirectory, not ignored) + # - Thumbs.db (not ignored) + test.df <- data.frame( + filename = c(test_file1, "README.md", "test", "Thumbs.db"), + varname = c(gsub(".csv", "", test_file1), "", "", ""), + reader = c("csv.reader", "", "", ""), + is_ignored = c(TRUE, FALSE, FALSE, FALSE), + is_directory = c(FALSE, FALSE, TRUE, FALSE), + is_cached = c(FALSE, FALSE, FALSE, FALSE), + cache_only = c(FALSE, FALSE, FALSE, FALSE), + stringsAsFactors = FALSE + ) + # Sort data.frame according to collation of the testing process (for example + # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) + sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) + test.df <- test.df[sort.df, ] + + expect_error(data.files <- list.data(data_ignore = "*.csv"), NA) + expect_true(nrow(data.files) == 4) + expect_equal(data.files$filename, test.df$filename) + expect_equal(data.files$is_ignored, test.df$is_ignored) + + # Check wildcard ignore "*.csv" with recursive loading on: + # - fileXXXXX.csv (test_file1, ignored) + # - README.md (not ignored) + # - test/fileYYYYY.csv (test_file2, not ignored) + # - Thumbs.db (not ignored) + test.df <- data.frame( + filename = c(test_file1, "README.md", gsub("\\\\", "/", test_file2), "Thumbs.db"), + varname = c( + gsub(".csv", "", test_file1), + "", + gsub("[/\\\\]", ".", gsub(".csv", "", test_file2)), + "" + ), + reader = c("csv.reader", "", "csv.reader", ""), + is_ignored = c(TRUE, FALSE, TRUE, FALSE), + is_directory = c(FALSE, FALSE, FALSE, FALSE), + is_cached = c(FALSE, FALSE, FALSE, FALSE), + cache_only = c(FALSE, FALSE, FALSE, FALSE), + stringsAsFactors = FALSE + ) + # Sort data.frame according to collation of the testing process (for example + # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) + sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) + test.df <- test.df[sort.df, ] + + expect_error(data.files <- list.data( + data_ignore = "*.csv", + recursive_loading = TRUE + ), NA) + expect_true(nrow(data.files) == 4) + expect_equal(data.files$filename, test.df$filename) + expect_equal(data.files$is_ignored, test.df$is_ignored) + + # Check regular expression ignore "^\w*.csv", only match csv in data, not in + # subdirectories: + # - fileXXXXX.csv (test_file1, ignored) + # - README.md (not ignored) + # - test/fileYYYYY.csv (test_file2, not ignored) + # - Thumbs.db (not ignored) + test.df <- data.frame( + filename = c(test_file1, "README.md", gsub("\\\\", "/", test_file2), "Thumbs.db"), + varname = c( + gsub(".csv", "", test_file1), + "", + gsub("[/\\\\]", ".", gsub(".csv", "", test_file2)), + "" + ), + reader = c("csv.reader", "", "csv.reader", ""), + is_ignored = c(TRUE, FALSE, FALSE, FALSE), + is_directory = c(FALSE, FALSE, FALSE, FALSE), + is_cached = c(FALSE, FALSE, FALSE, FALSE), + cache_only = c(FALSE, FALSE, FALSE, FALSE), + stringsAsFactors = FALSE + ) + # Sort data.frame according to collation of the testing process (for example + # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) + sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) + test.df <- test.df[sort.df, ] + + expect_error(data.files <- list.data( + data_ignore = "/^\\w*\\.csv/", + recursive_loading = TRUE + ), NA) + expect_true(nrow(data.files) == 4) + expect_equal(data.files$filename, test.df$filename) + expect_equal(data.files$is_ignored, test.df$is_ignored) + + # Check combination literal filename "Thumbs.db" and regular expression + # ignore "^\w*.csv", only match csv in data, not in subdirectories: + # - fileXXXXX.csv (test_file1, ignored) + # - README.md (not ignored) + # - test/fileYYYYY.csv (test_file2, not ignored) + # - Thumbs.db (ignored) + test.df <- data.frame( + filename = c(test_file1, "README.md", gsub("\\\\", "/", test_file2), "Thumbs.db"), + varname = c( + gsub(".csv", "", test_file1), + "", + gsub("[/\\\\]", ".", gsub(".csv", "", test_file2)), + "" + ), + reader = c("csv.reader", "", "csv.reader", ""), + is_ignored = c(TRUE, FALSE, FALSE, TRUE), + is_directory = c(FALSE, FALSE, FALSE, FALSE), + is_cached = c(FALSE, FALSE, FALSE, FALSE), + cache_only = c(FALSE, FALSE, FALSE, FALSE), + stringsAsFactors = FALSE + ) + # Sort data.frame according to collation of the testing process (for example + # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) + sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) + test.df <- test.df[sort.df, ] + + expect_error(data.files <- list.data( + data_ignore = "Thumbs.db, /^\\w*\\.csv/", + recursive_loading = TRUE + ), NA) + expect_true(nrow(data.files) == 4) + expect_equal(data.files$filename, test.df$filename) + expect_equal(data.files$is_ignored, test.df$is_ignored) + + # Check extra comma in combination literal filename "Thumbs.db" and regular + # expression ignore "^\w*\.csv", only match csv in data, not in + # subdirectories: + # - fileXXXXX.csv (test_file1, ignored) + # - README.md (not ignored) + # - test/fileYYYYY.csv (test_file2, not ignored) + # - Thumbs.db (ignored) + test.df <- data.frame( + filename = c(test_file1, "README.md", gsub("\\\\", "/", test_file2), "Thumbs.db"), + varname = c( + gsub(".csv", "", test_file1), + "", + gsub("[/\\\\]", ".", gsub(".csv", "", test_file2)), + "" + ), + reader = c("csv.reader", "", "csv.reader", ""), + is_ignored = c(TRUE, FALSE, FALSE, TRUE), + is_directory = c(FALSE, FALSE, FALSE, FALSE), + is_cached = c(FALSE, FALSE, FALSE, FALSE), + cache_only = c(FALSE, FALSE, FALSE, FALSE), + stringsAsFactors = FALSE + ) + # Sort data.frame according to collation of the testing process (for example + # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) + sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) + test.df <- test.df[sort.df, ] + + expect_error(data.files <- list.data( + data_ignore = "Thumbs.db,,/^\\w*\\.csv/", + recursive_loading = TRUE + ), NA) + expect_true(nrow(data.files) == 4) + expect_equal(data.files$filename, test.df$filename) + expect_equal(data.files$is_ignored, test.df$is_ignored) }) -test_that('cached data is listed correctly as already cached', { - # Create temporary project - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - # Set temporary project as working directory - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - # Clear the global environment - clear() - - # Create some test data so the file can be loaded if not ignored - test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) - - # Build two temporary filenames - test_file1 <- temp_csv_file() - test_file2 <- temp_csv_file(dir = 'test') - - # Write test data to files - write.csv(test_data, file = file.path('data', test_file1), row.names = FALSE) - dir.create('data/test') - write.csv(test_data, file = file.path('data', test_file2), row.names = FALSE) - - expect_error(data.files <- list.data(), NA) - - # Check if data.frame has three rows with default config: - # - fileXXXXX.csv (test_file1, not cached) - # - README.md (not ignored, not cached) - # - test (subdirectory, not cached) - test.df <- data.frame( - filename = c(test_file1, 'README.md', 'test'), - varname = c(gsub('.csv', '', test_file1), '', ''), - reader = c("csv.reader", "", ""), - is_ignored = c(FALSE, FALSE, FALSE), - is_directory = c(FALSE, FALSE, TRUE), - is_cached = c(FALSE, FALSE, FALSE), - cache_only = c(FALSE, FALSE, FALSE), - stringsAsFactors = FALSE - ) - # Sort data.frame according to collation of the testing process (for example - # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) - sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) - test.df <- test.df[sort.df,] - - expect_equal(data.files$filename, test.df$filename) - expect_equal(data.files$is_cached, test.df$is_cached) - - # Load the project so variables are cached, then list data again - suppressMessages(load.project()) - expect_error(data.files <- list.data(), NA) - - # Check if data.frame has three rows with default config: - # - fileXXXXX.csv (test_file1, cached) - # - README.md (not ignored, not cached) - # - test (subdirectory, not cached) - test.df <- data.frame( - filename = c(test_file1, 'README.md', 'test'), - varname = c(gsub('.csv', '', test_file1), '', ''), - reader = c("csv.reader", "", ""), - is_ignored = c(FALSE, FALSE, FALSE), - is_directory = c(FALSE, FALSE, TRUE), - is_cached = c(TRUE, FALSE, FALSE), - cache_only = c(FALSE, FALSE, FALSE), - stringsAsFactors = FALSE - ) - # Sort data.frame according to collation of the testing process (for example - # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) - sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) - test.df <- test.df[sort.df,] - - expect_equal(data.files$filename, test.df$filename) - expect_equal(data.files$is_cached, test.df$is_cached) +test_that("cached data is listed correctly as already cached", { + # Create temporary project + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + # Set temporary project as working directory + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + + # Clear the global environment + clear() + + # Create some test data so the file can be loaded if not ignored + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + + # Build two temporary filenames + test_file1 <- temp_csv_file() + test_file2 <- temp_csv_file(dir = "test") + + # Write test data to files + write.csv(test_data, file = file.path("data", test_file1), row.names = FALSE) + dir.create("data/test") + write.csv(test_data, file = file.path("data", test_file2), row.names = FALSE) + + expect_error(data.files <- list.data(), NA) + + # Check if data.frame has three rows with default config: + # - fileXXXXX.csv (test_file1, not cached) + # - README.md (not ignored, not cached) + # - test (subdirectory, not cached) + test.df <- data.frame( + filename = c(test_file1, "README.md", "test"), + varname = c(gsub(".csv", "", test_file1), "", ""), + reader = c("csv.reader", "", ""), + is_ignored = c(FALSE, FALSE, FALSE), + is_directory = c(FALSE, FALSE, TRUE), + is_cached = c(FALSE, FALSE, FALSE), + cache_only = c(FALSE, FALSE, FALSE), + stringsAsFactors = FALSE + ) + # Sort data.frame according to collation of the testing process (for example + # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) + sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) + test.df <- test.df[sort.df, ] + + expect_equal(data.files$filename, test.df$filename) + expect_equal(data.files$is_cached, test.df$is_cached) + + # Load the project so variables are cached, then list data again + suppressMessages(load.project()) + expect_error(data.files <- list.data(), NA) + + # Check if data.frame has three rows with default config: + # - fileXXXXX.csv (test_file1, cached) + # - README.md (not ignored, not cached) + # - test (subdirectory, not cached) + test.df <- data.frame( + filename = c(test_file1, "README.md", "test"), + varname = c(gsub(".csv", "", test_file1), "", ""), + reader = c("csv.reader", "", ""), + is_ignored = c(FALSE, FALSE, FALSE), + is_directory = c(FALSE, FALSE, TRUE), + is_cached = c(TRUE, FALSE, FALSE), + cache_only = c(FALSE, FALSE, FALSE), + stringsAsFactors = FALSE + ) + # Sort data.frame according to collation of the testing process (for example + # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) + sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) + test.df <- test.df[sort.df, ] + + expect_equal(data.files$filename, test.df$filename) + expect_equal(data.files$is_cached, test.df$is_cached) }) -test_that('cached data created during munging listed as cached only', { - skip_if_not_installed("formatR") - - # Create temporary project - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - # Set temporary project as working directory - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - # Clear the global environment - clear() - - # Create some test data so the file can be loaded if not ignored - test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) - - # Build two temporary filenames - test_file1 <- temp_csv_file() - test_file2 <- temp_csv_file(dir = 'test') - - # Write test data to files - write.csv(test_data, file = file.path('data', test_file1), row.names = FALSE) - dir.create('data/test') - write.csv(test_data, file = file.path('data', test_file2), row.names = FALSE) - - # Cache a code fragment - cache(variable = "test_data", CODE = { - data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) - }) - - expect_error(data.files <- list.data(), NA) - - # Check if data.frame has four rows with default config: - # - fileXXXXX.csv (test_file1, not cached) - # - README.md (not ignored, not cached) - # - test (subdirectory, not cached) - # - test_data (cached variable only) - test.df <- data.frame( - filename = c(test_file1, 'README.md', 'test', ''), - varname = c(gsub('.csv', '', test_file1), '', '', 'test_data'), - reader = c("csv.reader", "", "", ""), - is_ignored = c(FALSE, FALSE, FALSE, FALSE), - is_directory = c(FALSE, FALSE, TRUE, FALSE), - is_cached = c(FALSE, FALSE, FALSE, TRUE), - cache_only = c(FALSE, FALSE, FALSE, TRUE), - stringsAsFactors = FALSE - ) - # Sort data.frame according to collation of the testing process (for example - # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) - sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) - test.df <- test.df[sort.df,] - - expect_equal(data.files$filename, test.df$filename) - expect_equal(data.files$varname, test.df$varname) - expect_equal(data.files$is_cached, test.df$is_cached) - expect_equal(data.files$cache_only, test.df$cache_only) +test_that("cached data created during munging listed as cached only", { + skip_if_not_installed("formatR") + + # Create temporary project + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + # Set temporary project as working directory + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + + # Clear the global environment + clear() + + # Create some test data so the file can be loaded if not ignored + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + + # Build two temporary filenames + test_file1 <- temp_csv_file() + test_file2 <- temp_csv_file(dir = "test") + + # Write test data to files + write.csv(test_data, file = file.path("data", test_file1), row.names = FALSE) + dir.create("data/test") + write.csv(test_data, file = file.path("data", test_file2), row.names = FALSE) + + # Cache a code fragment + cache(variable = "test_data", CODE = { + data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + }) + + expect_error(data.files <- list.data(), NA) + + # Check if data.frame has four rows with default config: + # - fileXXXXX.csv (test_file1, not cached) + # - README.md (not ignored, not cached) + # - test (subdirectory, not cached) + # - test_data (cached variable only) + test.df <- data.frame( + filename = c(test_file1, "README.md", "test", ""), + varname = c(gsub(".csv", "", test_file1), "", "", "test_data"), + reader = c("csv.reader", "", "", ""), + is_ignored = c(FALSE, FALSE, FALSE, FALSE), + is_directory = c(FALSE, FALSE, TRUE, FALSE), + is_cached = c(FALSE, FALSE, FALSE, TRUE), + cache_only = c(FALSE, FALSE, FALSE, TRUE), + stringsAsFactors = FALSE + ) + # Sort data.frame according to collation of the testing process (for example + # LC_COLLATE=C and LC_COLLATE=en_US.UTF-8 return different order) + sort.df <- order(test.df$cache_only, test.df$filename, test.df$varname) + test.df <- test.df[sort.df, ] + + expect_equal(data.files$filename, test.df$filename) + expect_equal(data.files$varname, test.df$varname) + expect_equal(data.files$is_cached, test.df$is_cached) + expect_equal(data.files$cache_only, test.df$cache_only) }) diff --git a/tests/testthat/test-load.R b/tests/testthat/test-load.R index c3393b8b..1e0d89fe 100644 --- a/tests/testthat/test-load.R +++ b/tests/testthat/test-load.R @@ -1,496 +1,494 @@ -context('Load project') +test_that("All elements have length 1", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + + suppressMessages(load.project()) + expect_equal(unname(vapply(config, length, integer(1))), rep(1L, length(config))) +}) + +test_that("user commands fail when not in ProjectTemplate directory", { + test_project <- tempfile("test_project") + dir.create(test_project) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + + # Check load.project() + expect_error(load.project()) + + # Check clear.cache() + expect_error(clear.cache()) + + # Check cache() + expect_error(cache()) + + # Check reload.project() + expect_error(reload.project()) -test_that('All elements have length 1', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + # Check reload.project() + expect_error(test.project()) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + # Check stub.tests() + expect_error(stub.tests()) - suppressMessages(load.project()) - expect_equal(unname(vapply(config, length, integer(1))), rep(1L, length(config))) + # Check project.config() + expect_error(project.config()) }) -test_that('user commands fail when not in ProjectTemplate directory', { - test_project <- tempfile('test_project') - dir.create(test_project) +for (cache_file_format in cache_file_formats) { + test_that("auto loaded data is cached by default", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) on.exit(unlink(test_project, recursive = TRUE), add = TRUE) oldwd <- setwd(test_project) on.exit(setwd(oldwd), add = TRUE) - # Check load.project() - expect_error(load.project()) + set_cache_file_format(cache_file_format) - # Check clear.cache() - expect_error(clear.cache()) + # clear the global environment + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) - # Check cache() - expect_error(cache()) + test_data <- tibble::as_tibble(data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40))) - # Check reload.project() - expect_error(reload.project()) + # save test data as a csv in the data directory + write.csv(test_data, file = "data/test.csv", row.names = FALSE) - # Check reload.project() - expect_error(test.project()) + suppressMessages(load.project()) - # Check stub.tests() - expect_error(stub.tests()) + # check that the cached file loads without error + switch(cache_file_format, + RData = expect_no_error(load("cache/test.RData", envir = environment())), + qs = expect_no_error(qs::qload("cache/test.qs", env = environment())) + ) - # Check project.config() - expect_error(project.config()) + # and check that the loaded data from the cache is what we saved + expect_equal(test, test_data) + }) -}) + test_that("auto loaded data is not cached when cached_loaded_data is FALSE", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) -for (cache_file_format in cache_file_formats) { - test_that('auto loaded data is cached by default', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + set_cache_file_format(cache_file_format) - set_cache_file_format(cache_file_format) + # clear the global environment + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) - # clear the global environment - rm(list=ls(envir = .TargetEnv), envir = .TargetEnv) + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) - test_data <- tibble::as_tibble(data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40))) + # save test data as a csv in the data directory + write.csv(test_data, file = "data/test.csv", row.names = FALSE) - # save test data as a csv in the data directory - write.csv(test_data, file="data/test.csv", row.names = FALSE) + # Read the config data and set cache_loaded_data to FALSE + config <- .read.config() + expect_error(config$cache_loaded_data <- FALSE, NA) + .save.config(config) - suppressMessages(load.project()) + suppressMessages(load.project()) - # check that the cached file loads without error - switch( - cache_file_format, - RData = expect_no_error(load("cache/test.RData", envir = environment())), - qs = expect_no_error(qs::qload("cache/test.qs", env = environment())) - ) + # check that the the test variable has not been cached + switch(cache_file_format, + RData = expect_error(suppressWarnings(load("cache/test.RData", envir = environment())), "cannot open the connection"), + qs = expect_error(qs::qload("cache/test.qs", env = environment()), "Failed to open for reading") + ) + }) - # and check that the loaded data from the cache is what we saved - expect_equal(test, test_data) - }) + test_that("auto loaded data from an R script is cached correctly", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + + set_cache_file_format(cache_file_format) + + # clear the global environment + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) + + # create some variables in the global env that shouldn't be cached + test_data11 <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + test_data21 <- data.frame(Names = c("a1", "b1", "c1"), Ages = c(20, 30, 40)) + + # Create some R code and put in data directory + CODE <- paste0(deparse(substitute({ + test_data12 <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) + test_data22 <- data.frame(Names = c("a1", "b1", "c1"), Ages = c(20, 30, 40)) + })), collapse = "\n") - test_that('auto loaded data is not cached when cached_loaded_data is FALSE', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) + # save R code in the data directory + writeLines(CODE, "data/test.R") + + # load the project and R code + suppressMessages(load.project()) + + # check that the test variables have been cached correctly + switch(cache_file_format, + RData = { + expect_no_error(load("cache/test_data12.RData", envir = environment())) + expect_no_error(load("cache/test_data22.RData", envir = environment())) + }, + qs = { + expect_no_error(qs::qload("cache/test_data12.qs", env = environment())) + expect_no_error(qs::qload("cache/test_data22.qs", env = environment())) + } + ) + + # check that the other test variables have not been cached + switch(cache_file_format, + RData = { + expect_error(suppressWarnings(load("cache/test_data11.RData", envir = environment())), "cannot open the connection") + expect_error(suppressWarnings(load("cache/test_data21.RData", envir = environment())), "cannot open the connection") + }, + qs = { + expect_error(qs::qload("cache/test_data11.qs", env = environment()), "Failed to open for reading") + expect_error(qs::qload("cache/test_data21.qs", env = environment()), "Failed to open for reading") + } + ) + }) +} + +test_that("ignored data files are not loaded", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) on.exit(unlink(test_project, recursive = TRUE), add = TRUE) oldwd <- setwd(test_project) on.exit(setwd(oldwd), add = TRUE) - set_cache_file_format(cache_file_format) - # clear the global environment - rm(list=ls(envir = .TargetEnv), envir = .TargetEnv) - - test_data <- data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40)) - - # save test data as a csv in the data directory - write.csv(test_data, file="data/test.csv", row.names = FALSE) + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) # Read the config data and set cache_loaded_data to FALSE + config <- .read.config() + expect_error(config$cache_loaded_data <- FALSE, NA) .save.config(config) + # create some test data so the file can be loaded if not ignored + test_data <- tibble::as_tibble(data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40))) + + # write test data to files + write.csv(test_data, file = "data/test.csv", row.names = FALSE) + dir.create("data/test") + write.csv(test_data, file = "data/test/test.csv", row.names = FALSE) + + # load the project and data with default settings, + # check that data/test.csv is loaded suppressMessages(load.project()) + expect_equal(test, test_data) - # check that the the test variable has not been cached - switch( - cache_file_format, - RData = expect_error(suppressWarnings(load("cache/test.RData", envir = environment())), "cannot open the connection"), - qs = expect_error(qs::qload("cache/test.qs", env = environment()), "Failed to open for reading") - ) - }) + # reload the project, now with recursive_loading + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) + suppressMessages(load.project(recursive_loading = TRUE)) + expect_equal(test, test_data) + expect_equal(test.test, test_data) + + writeLines("\n", "data/Thumbs.db") # Should trigger error + # reload the project, now with an illegal Thumbs.db + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) + # The Thumbs.db is not a valid SQLite database so should raise an error + expect_error(suppressWarnings(load.project(override.config = list(data_ignore = "")))) + + # reload the project, ignore *.csv + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) + suppressMessages(load.project(data_ignore = "Thumbs.db, *.csv")) + expect_false(exists("test", envir = .TargetEnv)) + expect_false(exists("test.test", envir = .TargetEnv)) + + # reload the project, ignore *.csv with recursive loading + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) + suppressMessages(load.project( + data_ignore = "Thumbs.db, *.csv", + recursive_loading = TRUE + )) + expect_false(exists("test", envir = .TargetEnv)) + expect_false(exists("test.test", envir = .TargetEnv)) + + # reload the project, ignore test/*.csv with recursive loading + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) + suppressMessages(load.project( + data_ignore = "Thumbs.db, test/*.csv", + recursive_loading = TRUE + )) + expect_equal(test, test_data) + expect_false(exists("test.test", envir = .TargetEnv)) + + # reload the project, ignore test/ with recursive loading + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) + suppressMessages(load.project( + data_ignore = "Thumbs.db, test/", + recursive_loading = TRUE + )) + expect_equal(test, test_data) + expect_false(exists("test.test", envir = .TargetEnv)) + + + # reload the project, ignore test/*.csv as a regular expression with recursive loading + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) + suppressMessages(load.project( + data_ignore = "Thumbs.db, /test/.*\\.csv/", + recursive_loading = TRUE + )) + expect_equal(test, test_data) + expect_false(exists("test.test", envir = .TargetEnv)) + + # reload the project, ignore cached var_to_cache + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) + assign("var_to_cache", test_data, envir = .TargetEnv) + cache("var_to_cache") + rm(var_to_cache, envir = .TargetEnv) + suppressMessages(load.project(data_ignore = "Thumbs.db, var_to_cache")) + expect_false(exists("var_to_cache", envir = .TargetEnv)) +}) - test_that('auto loaded data from an R script is cached correctly', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) +test_that("data is loaded as data_frame", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) on.exit(unlink(test_project, recursive = TRUE), add = TRUE) oldwd <- setwd(test_project) on.exit(setwd(oldwd), add = TRUE) - set_cache_file_format(cache_file_format) - # clear the global environment - rm(list=ls(envir = .TargetEnv), envir = .TargetEnv) + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) - # create some variables in the global env that shouldn't be cached - test_data11 <- data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40)) - test_data21 <- data.frame(Names=c("a1", "b1", "c1"), Ages=c(20,30,40)) + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) - # Create some R code and put in data directory - CODE <- paste0(deparse(substitute({ - test_data12 <- data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40)) - test_data22 <- data.frame(Names=c("a1", "b1", "c1"), Ages=c(20,30,40)) - })), collapse ="\n") + # save test data as a csv in the data directory + write.csv(test_data, file = "data/test.csv", row.names = FALSE) - # save R code in the data directory - writeLines(CODE, "data/test.R") + config <- .new.config + config$tables_type <- "data_frame" + write.dcf(config, "config/global.dcf") - # load the project and R code suppressMessages(load.project()) - # check that the test variables have been cached correctly - switch( - cache_file_format, - RData = { - expect_no_error(load("cache/test_data12.RData", envir = environment())) - expect_no_error(load("cache/test_data22.RData", envir = environment())) - }, - qs = { - expect_no_error(qs::qload("cache/test_data12.qs", env = environment())) - expect_no_error(qs::qload("cache/test_data22.qs", env = environment())) - } - ) - - # check that the other test variables have not been cached - switch( - cache_file_format, - RData = { - expect_error(suppressWarnings(load("cache/test_data11.RData", envir = environment())), "cannot open the connection") - expect_error(suppressWarnings(load("cache/test_data21.RData", envir = environment())), "cannot open the connection") - }, - qs = { - expect_error(qs::qload("cache/test_data11.qs", env = environment()), "Failed to open for reading") - expect_error(qs::qload("cache/test_data21.qs", env = environment()), "Failed to open for reading") - } - ) - }) -} - -test_that('ignored data files are not loaded', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - # clear the global environment - rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) - - # Read the config data and set cache_loaded_data to FALSE - - config <- .read.config() - - expect_error(config$cache_loaded_data <- FALSE, NA) - .save.config(config) - - # create some test data so the file can be loaded if not ignored - test_data <- tibble::as_tibble(data.frame(Names = c("a", "b", "c"), Ages = c(20,30,40))) - - # write test data to files - write.csv(test_data, file = 'data/test.csv', row.names = FALSE) - dir.create('data/test') - write.csv(test_data, file = 'data/test/test.csv', row.names = FALSE) - - # load the project and data with default settings, - # check that data/test.csv is loaded - suppressMessages(load.project()) - expect_equal(test, test_data) - - # reload the project, now with recursive_loading - rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) - suppressMessages(load.project(recursive_loading = TRUE)) - expect_equal(test, test_data) - expect_equal(test.test, test_data) - - writeLines('\n', 'data/Thumbs.db') # Should trigger error - # reload the project, now with an illegal Thumbs.db - rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) - # The Thumbs.db is not a valid SQLite database so should raise an error - expect_error(suppressWarnings(load.project(override.config = list(data_ignore = '')))) - - # reload the project, ignore *.csv - rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) - suppressMessages(load.project(data_ignore = 'Thumbs.db, *.csv')) - expect_false(exists("test", envir = .TargetEnv)) - expect_false(exists("test.test", envir = .TargetEnv)) - - # reload the project, ignore *.csv with recursive loading - rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) - suppressMessages(load.project(data_ignore = 'Thumbs.db, *.csv', - recursive_loading = TRUE)) - expect_false(exists("test", envir = .TargetEnv)) - expect_false(exists("test.test", envir = .TargetEnv)) - - # reload the project, ignore test/*.csv with recursive loading - rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) - suppressMessages(load.project(data_ignore = 'Thumbs.db, test/*.csv', - recursive_loading = TRUE)) - expect_equal(test, test_data) - expect_false(exists("test.test", envir = .TargetEnv)) - - # reload the project, ignore test/ with recursive loading - rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) - suppressMessages(load.project(data_ignore = 'Thumbs.db, test/', - recursive_loading = TRUE)) - expect_equal(test, test_data) - expect_false(exists("test.test", envir = .TargetEnv)) - - - # reload the project, ignore test/*.csv as a regular expression with recursive loading - rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) - suppressMessages(load.project(data_ignore = 'Thumbs.db, /test/.*\\.csv/', - recursive_loading = TRUE)) - expect_equal(test, test_data) - expect_false(exists("test.test", envir = .TargetEnv)) - - # reload the project, ignore cached var_to_cache - rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) - assign("var_to_cache", test_data, envir = .TargetEnv) - cache("var_to_cache") - rm(var_to_cache, envir = .TargetEnv) - suppressMessages(load.project(data_ignore = 'Thumbs.db, var_to_cache')) - expect_false(exists("var_to_cache", envir = .TargetEnv)) - + # and check that the loaded data from the cache is what we saved + expect_equal(test, test_data) }) -test_that('data is loaded as data_frame', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("data is loaded as data_table", { + skip_if_not_installed("data.table") - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - # clear the global environment - rm(list=ls(envir = .TargetEnv), envir = .TargetEnv) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - test_data <- data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40)) + # clear the global environment + rm(list = ls(envir = .TargetEnv), envir = .TargetEnv) - # save test data as a csv in the data directory - write.csv(test_data, file="data/test.csv", row.names = FALSE) + require("data.table") + test_data <- data.table::data.table(data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40))) - config <- .new.config - config$tables_type <- "data_frame" - write.dcf(config, 'config/global.dcf') + # save test data as a csv in the data directory + write.csv(test_data, file = "data/test.csv", row.names = FALSE) - suppressMessages(load.project()) + config <- .new.config + config$tables_type <- "data_table" + write.dcf(config, "config/global.dcf") - # and check that the loaded data from the cache is what we saved - expect_equal(test, test_data) -}) + suppressMessages(load.project()) -test_that('data is loaded as data_table', { - skip_if_not_installed("data.table") + # and check that the loaded data from the cache is what we saved + expect_equal(test, test_data) +}) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("logs are written to a logs subdirectory", { + skip_if_not_installed("log4r") - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - # clear the global environment - rm(list=ls(envir = .TargetEnv), envir = .TargetEnv) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - require('data.table') - test_data <- data.table::data.table(data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40))) + config <- .new.config + config$logging <- TRUE + write.dcf(config, "config/global.dcf") + dir.create("logs/test_logs") # Don't want to trigger warning - causes problems with CRAN - # save test data as a csv in the data directory - write.csv(test_data, file="data/test.csv", row.names = FALSE) + # Create some R code and put in data directory + CODE <- paste0(deparse(substitute({ + require.package("log4r") + info(logger, "this is a test file") + })), collapse = "\n") - config <- .new.config - config$tables_type <- "data_table" - write.dcf(config, 'config/global.dcf') + # save R code in the munge directory + writeLines(CODE, "munge/test.R") - suppressMessages(load.project()) + # load the project and R code + suppressMessages(load.project(logs_sub_dir = "test_logs")) - # and check that the loaded data from the cache is what we saved - expect_equal(test, test_data) + expect_false(file.exists(file.path("logs", "project.log"))) + expect_true(file.exists(file.path("logs", "test_logs", "project.log"))) }) -test_that('logs are written to a logs subdirectory',{ - skip_if_not_installed("log4r") - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - config <- .new.config - config$logging <- TRUE - write.dcf(config, 'config/global.dcf') - dir.create("logs/test_logs") # Don't want to trigger warning - causes problems with CRAN +test_that("read from munge subdirectory", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - # Create some R code and put in data directory - CODE <- paste0(deparse(substitute({ - require.package('log4r') - info(logger,"this is a test file") - })), collapse ="\n") + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # save R code in the munge directory - writeLines(CODE, "munge/test.R") + # Create some R code and put in munge subdirectory directory + CODE <- paste0(deparse(substitute({ + test_data <- tibble::as_tibble(data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40))) + })), collapse = "\n") - #load the project and R code - suppressMessages(load.project(logs_sub_dir="test_logs")) + dir.create(file.path("munge", "test_munge")) + writeLines(CODE, file.path("munge", "test_munge", "02-test_data.R")) - expect_false(file.exists(file.path("logs","project.log"))) - expect_true(file.exists(file.path("logs","test_logs","project.log"))) + # load the project and R code + suppressMessages(load.project(munge_sub_dir = "test_munge")) + expect_true(exists("test_data")) }) -test_that('read from munge subdirectory',{ - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("pass munge files to run", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # Create some R code and put in munge subdirectory directory - CODE <- paste0(deparse(substitute({ - test_data <- tibble::as_tibble(data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40))) - })), collapse ="\n") + # Create some R code and put in munge subdirectory directory + CODE <- paste0(deparse(substitute({ + test_data_1 <- tibble::as_tibble(data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40))) + })), collapse = "\n") - dir.create(file.path("munge","test_munge")) - writeLines(CODE, file.path("munge","test_munge","02-test_data.R") ) + writeLines(CODE, file.path("munge", "01-test_data.R")) - #load the project and R code - suppressMessages(load.project(munge_sub_dir="test_munge")) + CODE <- paste0(deparse(substitute({ + test_data_2 <- tibble::as_tibble(data.frame(Names = c("d", "e", "f"), Ages = c(50, 60, 70))) + })), collapse = "\n") - expect_true(exists("test_data")) + writeLines(CODE, file.path("munge", "02-test_data.R")) -}) + CODE <- paste0(deparse(substitute({ + test_data_3 <- tibble::as_tibble(data.frame(Names = c("d", "e", "f"), Ages = c(50, 60, 70))) + })), collapse = "\n") -test_that('pass munge files to run',{ - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - # Create some R code and put in munge subdirectory directory - CODE <- paste0(deparse(substitute({ - test_data_1 <- tibble::as_tibble(data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40))) - })), collapse ="\n") - - writeLines(CODE, file.path("munge","01-test_data.R")) - - CODE <- paste0(deparse(substitute({ - test_data_2 <- tibble::as_tibble(data.frame(Names=c("d", "e", "f"), Ages=c(50,60,70))) - })), collapse ="\n") - - writeLines(CODE, file.path("munge","02-test_data.R")) - - CODE <- paste0(deparse(substitute({ - test_data_3 <- tibble::as_tibble(data.frame(Names=c("d", "e", "f"), Ages=c(50,60,70))) - })), collapse ="\n") - - writeLines(CODE, file.path("munge","03-test_data.R")) - - #load the project and R code - suppressMessages(load.project(munge_files=c("02-test_data.R"))) - - # expect_false(exists("test_data_1")) - expect_true(exists("test_data_2")) - # expect_false(exists("test_data_3")) - - suppressMessages(load.project(munge_files=c("03-test_data.R" ,"02-test_data.R"))) - # expect_false(exists("test_data_1")) - expect_true(exists("test_data_2")) - expect_true(exists("test_data_3")) - -# ------------------------------------------------------------------------------ -# Define a Python script and put in munge subdirectory directory -# ------------------------------------------------------------------------------ - python_code <- c( - "print(' ')", - "print('01-test_data.py start')", - "", # Empty line for readability - "import csv", - "import os", - "# create a dictionary with the base package collections", - "data = {'Names': [1, 2, 3], 'Ages': [20, 30, 40]}", - "# Write data to CSV file in munge directory (adjust path if needed)", - "with open('munge/test_data_py.csv', 'w', newline='') as f:", - " writer = csv.writer(f)", - " writer.writerows(zip(data['Names'], data['Ages']))", - "", # Empty line for readability - "# Print data sum for testing purposes", - "# print(data.sum())", - "print('01-test_data.py finish')" - ) -# ------------------------------------------------------------------------------ -# Write the Python code to a .py file -# ------------------------------------------------------------------------------ - writeLines(python_code, file.path("munge","01-test_data.py")) -# ------------------------------------------------------------------------------ -# Check if python dataframe exists -# ------------------------------------------------------------------------------ - check_py_data <- c( - "print(' ')", - "print('02-test_data.py start')", - "import collections", - "import csv", - "import os", - "import sys", -# ------------------------------------------------------------------------------ - "with open('munge/test_data_py.csv', 'r') as f:", - " df_csv = csv.reader(f)", - " for row in df_csv:", - " print(row)", -# ------------------------------------------------------------------------------ - "with open('munge/write_test_data_py.csv', 'w', newline='') as f:", - " writer = csv.writer(f)", - " writer.writerows(zip(data['Names'], data['Ages']))", -# ------------------------------------------------------------------------------ - "", - "subdirectory = 'munge'", -# ------------------------------------------------------------------------------ - "", - "if 'subdirectory' in globals():", - " data = 'y'", - " print('Python data exists in the environment')", - "else:", - " data = 'n'", - " print('Python data NOT in the environment')", -# ------------------------------------------------------------------------------ - "", - "print(data)", - "", - "full_file_path = os.path.join(subdirectory, f'{data}.csv')", - "open(full_file_path, 'w', newline='')", -# ------------------------------------------------------------------------------ - "", - "print('02-test_data.py finish')" -# ------------------------------------------------------------------------------ - ) -# ------------------------------------------------------------------------------ - writeLines(check_py_data, file.path( "munge", "02-test_data.py" )) -# ------------------------------------------------------------------------------ - suppressMessages(load.project()) -# ------------------------------------------------------------------------------ -# Check if python and R source file exists -# ------------------------------------------------------------------------------ - expect_true(file.exists(file.path("munge", "01-test_data.py"))) - expect_true(file.exists(file.path("munge", "01-test_data.R"))) - expect_true(file.exists(file.path("munge", "02-test_data.py"))) - expect_true(file.exists(file.path("munge", "02-test_data.R"))) -# ------------------------------------------------------------------------------ -# Check if CSV file exists (created by 01-test_data.py) -# ------------------------------------------------------------------------------ - expect_true(file.exists(file.path("munge", "test_data_py.csv"))) - expect_true(file.exists(file.path("munge", "write_test_data_py.csv"))) -# ------------------------------------------------------------------------------ -# validate if entry defined in python environment (created by 02-test_data.py) -# ------------------------------------------------------------------------------ - expect_true(file.exists(file.path( "munge", "y.csv"))) - expect_false(file.exists(file.path("munge", "n.csv"))) -# ------------------------------------------------------------------------------ -# Check if py_data is present in the current R environment -# ------------------------------------------------------------------------------ - expect_false("data" %in% ls(), "Python dataframe exists in the R environment") -# ------------------------------------------------------------------------------ + writeLines(CODE, file.path("munge", "03-test_data.R")) + + # load the project and R code + suppressMessages(load.project(munge_files = c("02-test_data.R"))) + + # expect_false(exists("test_data_1")) + expect_true(exists("test_data_2")) + # expect_false(exists("test_data_3")) + + suppressMessages(load.project(munge_files = c("03-test_data.R", "02-test_data.R"))) + # expect_false(exists("test_data_1")) + expect_true(exists("test_data_2")) + expect_true(exists("test_data_3")) + + # ------------------------------------------------------------------------------ + # Define a Python script and put in munge subdirectory directory + # ------------------------------------------------------------------------------ + python_code <- c( + "print(' ')", + "print('01-test_data.py start')", + "", # Empty line for readability + "import csv", + "import os", + "# create a dictionary with the base package collections", + "data = {'Names': [1, 2, 3], 'Ages': [20, 30, 40]}", + "# Write data to CSV file in munge directory (adjust path if needed)", + "with open('munge/test_data_py.csv', 'w', newline='') as f:", + " writer = csv.writer(f)", + " writer.writerows(zip(data['Names'], data['Ages']))", + "", # Empty line for readability + "# Print data sum for testing purposes", + "# print(data.sum())", + "print('01-test_data.py finish')" + ) + # ------------------------------------------------------------------------------ + # Write the Python code to a .py file + # ------------------------------------------------------------------------------ + writeLines(python_code, file.path("munge", "01-test_data.py")) + # ------------------------------------------------------------------------------ + # Check if python dataframe exists + # ------------------------------------------------------------------------------ + check_py_data <- c( + "print(' ')", + "print('02-test_data.py start')", + "import collections", + "import csv", + "import os", + "import sys", + # ------------------------------------------------------------------------------ + "with open('munge/test_data_py.csv', 'r') as f:", + " df_csv = csv.reader(f)", + " for row in df_csv:", + " print(row)", + # ------------------------------------------------------------------------------ + "with open('munge/write_test_data_py.csv', 'w', newline='') as f:", + " writer = csv.writer(f)", + " writer.writerows(zip(data['Names'], data['Ages']))", + # ------------------------------------------------------------------------------ + "", + "subdirectory = 'munge'", + # ------------------------------------------------------------------------------ + "", + "if 'subdirectory' in globals():", + " data = 'y'", + " print('Python data exists in the environment')", + "else:", + " data = 'n'", + " print('Python data NOT in the environment')", + # ------------------------------------------------------------------------------ + "", + "print(data)", + "", + "full_file_path = os.path.join(subdirectory, f'{data}.csv')", + "open(full_file_path, 'w', newline='')", + # ------------------------------------------------------------------------------ + "", + "print('02-test_data.py finish')" + # ------------------------------------------------------------------------------ + ) + # ------------------------------------------------------------------------------ + writeLines(check_py_data, file.path("munge", "02-test_data.py")) + # ------------------------------------------------------------------------------ + suppressMessages(load.project()) + # ------------------------------------------------------------------------------ + # Check if python and R source file exists + # ------------------------------------------------------------------------------ + expect_true(file.exists(file.path("munge", "01-test_data.py"))) + expect_true(file.exists(file.path("munge", "01-test_data.R"))) + expect_true(file.exists(file.path("munge", "02-test_data.py"))) + expect_true(file.exists(file.path("munge", "02-test_data.R"))) + # ------------------------------------------------------------------------------ + # Check if CSV file exists (created by 01-test_data.py) + # ------------------------------------------------------------------------------ + expect_true(file.exists(file.path("munge", "test_data_py.csv"))) + expect_true(file.exists(file.path("munge", "write_test_data_py.csv"))) + # ------------------------------------------------------------------------------ + # validate if entry defined in python environment (created by 02-test_data.py) + # ------------------------------------------------------------------------------ + expect_true(file.exists(file.path("munge", "y.csv"))) + expect_false(file.exists(file.path("munge", "n.csv"))) + # ------------------------------------------------------------------------------ + # Check if py_data is present in the current R environment + # ------------------------------------------------------------------------------ + expect_false("data" %in% ls(), "Python dataframe exists in the R environment") + # ------------------------------------------------------------------------------ }) diff --git a/tests/testthat/test-load.project.R b/tests/testthat/test-load.project.R index 8849056e..94b301e7 100644 --- a/tests/testthat/test-load.project.R +++ b/tests/testthat/test-load.project.R @@ -1,3 +1,3 @@ test_that("multiplication works", { - expect_equal(2 * 2, 4) + expect_equal(2 * 2, 4) }) diff --git a/tests/testthat/test-migration.R b/tests/testthat/test-migration.R index 3696f9dd..42a0cc99 100644 --- a/tests/testthat/test-migration.R +++ b/tests/testthat/test-migration.R @@ -1,286 +1,279 @@ -context('Migration') - expect_defaults <- function(config) { - expect_true(is.character(config$version)) - expect_true(config$attach_internal_libraries) + expect_true(is.character(config$version)) + expect_true(config$attach_internal_libraries) } lapply( - c("0.5", "0.5-2"), - function(version) { - test_that(paste('Version', version), { - projdir <- tempfile("PT-test-") - dir.create(projdir) - file.copy(dir(file.path('migration', version), full.names = T), projdir, recursive = TRUE) - oldwd <- setwd(projdir) - on.exit(setwd(oldwd), add = TRUE) - - expect_that(suppressMessages(load.project()), gives_warning("migrate.project")) - on.exit(.unload.project(), add = TRUE) - - expect_message(migrate.project(), "file was missing entries") - expect_warning(suppressMessages(load.project()), NA) - expect_defaults(get.project()$config) - }) - } + c("0.5", "0.5-2"), + function(version) { + test_that(paste("Version", version), { + projdir <- tempfile("PT-test-") + dir.create(projdir) + + file.copy(dir(file.path("migration", version), full.names = T), projdir, recursive = TRUE) + oldwd <- setwd(projdir) + on.exit(setwd(oldwd), add = TRUE) + + expect_warning(suppressMessages(load.project()), "migrate.project") %>% + expect_warning("configuration file is missing the following entries") %>% + expect_warning("configuration contains the following unused entries") + + on.exit(.unload.project(), add = TRUE) + + expect_message(migrate.project(), "file was missing entries") + expect_no_warning(suppressMessages(load.project())) + expect_defaults(get.project()$config) + }) + } ) -test_that('migrating a project which doesnt need config update results in an Up to date message', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("migrating a project which doesnt need config update results in an Up to date message", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # should be nothing - expect_message(migrate.project(), "Already up to date") - tidy_up() + # should be nothing + expect_message(migrate.project(), "Already up to date") + tidy_up() }) for (cache_file_format in cache_file_formats) { - test_that('projects without the cached_loaded_data config have their migrated config set to FALSE', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + test_that("projects without the cached_loaded_data config have their migrated config set to FALSE", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - set_cache_file_format(cache_file_format) + set_cache_file_format(cache_file_format) - # Read the config data and remove the cache_loaded_data flag - config <- .read.config() - expect_error(config$cache_loaded_data <- NULL, NA) - .save.config(config) + # Read the config data and remove the cache_loaded_data flag + config <- .read.config() + expect_error(config$cache_loaded_data <- NULL, NA) + .save.config(config) - # should get a warning because of the missing cache_loaded_data - expect_warning(suppressMessages(load.project()), "missing the following entries") + # should get a warning because of the missing cache_loaded_data + expect_warning(suppressMessages(load.project()), "missing the following entries") - test_data <- data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40)) + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) - # save test data as a csv in the data directory - write.csv(test_data, file="data/test.csv", row.names = FALSE) - - # run load.project again and check that the the test variable has not been cached - # because the default should be FALSE if the cache_loaded_data is missing before migrate.project - # is called - expect_warning(suppressMessages(load.project()), "missing the following entries: cache_loaded_data") - switch( - cache_file_format, - RData = expect_error(suppressWarnings(load("cache/test.RData", envir = environment())), "cannot open the connection"), - qs = expect_error(qs::qload("cache/test.qs", env = environment()), "Failed to open for reading") - ) + # save test data as a csv in the data directory + write.csv(test_data, file = "data/test.csv", row.names = FALSE) - # Migrate the project - expect_message(migrate.project(), "new config item called cache_loaded_data") + # run load.project again and check that the the test variable has not been cached + # because the default should be FALSE if the cache_loaded_data is missing before migrate.project + # is called + expect_warning(suppressMessages(load.project()), "missing the following entries: cache_loaded_data") + switch(cache_file_format, + RData = expect_error(suppressWarnings(load("cache/test.RData", envir = environment())), "cannot open the connection"), + qs = expect_error(qs::qload("cache/test.qs", env = environment()), "Failed to open for reading") + ) - # Read the config data and check cached_loaded_data is FALSE - config <- .read.config() - expect_equal(config$cache_loaded_data, FALSE) + # Migrate the project + expect_message(migrate.project(), "new config item called cache_loaded_data") - # Should be a clean load.project - expect_warning(suppressMessages(load.project()), NA) + # Read the config data and check cached_loaded_data is FALSE + config <- .read.config() + expect_equal(config$cache_loaded_data, FALSE) - # check that the the test variable has not been cached - switch( - cache_file_format, - RData = expect_error(suppressWarnings(load("cache/test.RData", envir = environment())), "cannot open the connection"), - qs = expect_error(qs::qload("cache/test.qs", env = environment()), "Failed to open for reading") - ) - }) -} + # Should be a clean load.project + expect_warning(suppressMessages(load.project()), NA) -test_that('migrating a project with a missing config file results in a message to user', { + # check that the the test variable has not been cached + switch(cache_file_format, + RData = expect_error(suppressWarnings(load("cache/test.RData", envir = environment())), "cannot open the connection"), + qs = expect_error(qs::qload("cache/test.qs", env = environment()), "Failed to open for reading") + ) + }) +} - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("migrating a project with a missing config file results in a message to user", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # remove the config file - unlink('config/global.dcf') + # remove the config file + unlink("config/global.dcf") - # should be a message to say no config file - expect_message(migrate.project(), "didn't have a config\\.dcf file") + # should be a message to say no config file + expect_message(migrate.project(), "didn't have a config\\.dcf file") - suppressMessages(load.project()) + suppressMessages(load.project()) - # Get the default config - default_config <- .read.config(.default.config.file()) - default_config$version <- .package.version() + # Get the default config + default_config <- .read.config(.default.config.file()) + default_config$version <- .package.version() - # check the config is all the default - expect_equal(get.project()$config, default_config) + # check the config is all the default + expect_equal(get.project()$config, default_config) - tidy_up() + tidy_up() }) -test_that('migrating a project with a missing config item results in a message to user', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("migrating a project with a missing config item results in a message to user", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + suppressMessages(load.project()) - # remove the config item - config$data_loading <- NULL - .save.config(config) + # remove the config item + config$data_loading <- NULL + .save.config(config) - # should be a message to say no config item - expect_message(migrate.project(), "data_loading") + # should be a message to say no config item + expect_message(migrate.project(), "data_loading") - suppressMessages(load.project()) + suppressMessages(load.project()) - # check the missing config item is the default value - default_config <- .read.config(.default.config.file()) - expect_equal(get.project()$config$data_loading, default_config$data_loading) + # check the missing config item is the default value + default_config <- .read.config(.default.config.file()) + expect_equal(get.project()$config$data_loading, default_config$data_loading) - tidy_up() + tidy_up() }) -test_that('migrating a project with a dummy config item results in a message to user', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("migrating a project with a dummy config item results in a message to user", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + suppressMessages(load.project()) - # add the dummy config item - config$dummy <- TRUE - .save.config(config) + # add the dummy config item + config$dummy <- TRUE + .save.config(config) - # should be a message to say no config item - expect_message(migrate.project(), "dummy") + # should be a message to say no config item + expect_message(migrate.project(), "dummy") - suppressMessages(load.project()) + suppressMessages(load.project()) - # check that the dummy config item is not in the config - expect_null(get.project()$config$dummy) + # check that the dummy config item is not in the config + expect_null(get.project()$config$dummy) - tidy_up() + tidy_up() }) -test_that('migrating a project with a data/*.csv2 file results in a message to user', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("migrating a project with a data/*.csv2 file results in a message to user", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - test_data <- data.frame(Names=c("a", "b", "c"), Ages=c(20,30,40)) + test_data <- data.frame(Names = c("a", "b", "c"), Ages = c(20, 30, 40)) - # save test data as a csv in the data directory - write.csv2(test_data, file="data/test.csv2", row.names = FALSE) + # save test data as a csv in the data directory + write.csv2(test_data, file = "data/test.csv2", row.names = FALSE) - expect_warning(suppressMessages(load.project()), "csv2") + expect_warning(suppressMessages(load.project()), "csv2") - # should be a message to say check code and data for .csv2 files - expect_message(migrate.project(), 'csv2') + # should be a message to say check code and data for .csv2 files + expect_message(migrate.project(), "csv2") - suppressMessages(load.project()) + suppressMessages(load.project()) - # remove .csv2 file - unlink("data/test.csv2") + # remove .csv2 file + unlink("data/test.csv2") - tidy_up() + tidy_up() }) -test_that('migrating a project without a cache directory has a cache directory created', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("migrating a project without a cache directory has a cache directory created", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - cache_dir <- file.path(test_project, 'cache') + cache_dir <- file.path(test_project, "cache") - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - ## Delete the cache dir - unlink(cache_dir, recursive = TRUE) + ## Delete the cache dir + unlink(cache_dir, recursive = TRUE) - # should be a message to say adding cache directory - expect_message(migrate.project(), 'cache') + # should be a message to say adding cache directory + expect_message(migrate.project(), "cache") - # should be a cache directory - expect_true(.is.dir(cache_dir)) + # should be a cache directory + expect_true(.is.dir(cache_dir)) }) -test_that('projects without the tables_type config have their migrated config set to data_frame', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("projects without the tables_type config have their migrated config set to data_frame", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # Read the config data and remove the tables_type config - config <- .read.config() - expect_error(config$tables_type <- NULL, NA) - .save.config(config) + # Read the config data and remove the tables_type config + config <- .read.config() + expect_error(config$tables_type <- NULL, NA) + .save.config(config) - # should get a warning because of the missing tables_type - expect_warning(suppressMessages(load.project()), "missing the following entries: tables_type") + # should get a warning because of the missing tables_type + expect_warning(suppressMessages(load.project()), "missing the following entries: tables_type") - # Migrate the project - expect_message(migrate.project(), "data_tables has been renamed tables_type") + # Migrate the project + expect_message(migrate.project(), "data_tables has been renamed tables_type") - # Read the config data and check tables_type is 'tibble' - config <- .read.config() - expect_equal(config$tables_type, 'data_frame') + # Read the config data and check tables_type is 'tibble' + config <- .read.config() + expect_equal(config$tables_type, "data_frame") - # Should be a clean load.project - expect_warning(suppressMessages(load.project()), NA) + # Should be a clean load.project + expect_warning(suppressMessages(load.project()), NA) - tidy_up() + tidy_up() }) -test_that('projects without the tables_type config have their migrated config set to data_table', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("projects without the tables_type config have their migrated config set to data_table", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # Read the config data and remove the tables_type config and set data_tables to TRUE - config <- .read.config() - expect_error(config$tables_type <- NULL, NA) - expect_error(config$data_tables <- TRUE, NA) - .save.config(config) + # Read the config data and remove the tables_type config and set data_tables to TRUE + config <- .read.config() + expect_no_error(config$tables_type <- NULL) + expect_no_error(config$data_tables <- TRUE) + .save.config(config) - # should get a warning because of the missing tables_type - expect_warning(suppressMessages(load.project()), "missing the following entries: tables_type") - expect_warning(suppressMessages(load.project()), "contains the following unused entries: data_tables") + # should get a warning because of the missing tables_type + expect_warning(suppressMessages(load.project()), "missing the following entries: tables_type") %>% + expect_warning("contains the following unused entries: data_tables") - # Migrate the project - expect_message(migrate.project(), "data_tables has been renamed tables_type") + # Migrate the project + expect_message(migrate.project(), "data_tables has been renamed tables_type") - # Read the config data and check tables_type is 'data_table' - config <- .read.config() - expect_equal(config$tables_type, 'data_table') + # Read the config data and check tables_type is 'data_table' + config <- .read.config() + expect_equal(config$tables_type, "data_table") - # Should be a clean load.project - expect_warning(suppressMessages(load.project()), NA) + # Should be a clean load.project + expect_warning(suppressMessages(load.project()), NA) - tidy_up() + tidy_up() }) diff --git a/tests/testthat/test-override_config.R b/tests/testthat/test-override_config.R index 4e7a034c..aa8327e9 100644 --- a/tests/testthat/test-override_config.R +++ b/tests/testthat/test-override_config.R @@ -1,184 +1,175 @@ -context('Override configuration') +test_that("Overridden configuration is stored in config", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) -test_that('Overridden configuration is stored in config', { + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + on.exit(clear(), add = TRUE) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - on.exit(clear(), add = TRUE) - - expect_warning(load.project(new_config_setting = TRUE), NA) - expect_equal(config$.override.config, list(new_config_setting = TRUE)) + expect_warning(load.project(new_config_setting = TRUE), NA) + expect_equal(config$.override.config, list(new_config_setting = TRUE)) }) -test_that('Old-style named argument override.config to load.project is parsed correctly', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Old-style named argument override.config to load.project is parsed correctly", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - on.exit(clear(), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + on.exit(clear(), add = TRUE) - override.config <- list(new_config_setting = TRUE) - expect_warning(load.project(override.config = override.config), NA) - expect_equal(config$.override.config, override.config) + override.config <- list(new_config_setting = TRUE) + expect_warning(load.project(override.config = override.config), NA) + expect_equal(config$.override.config, override.config) - override.config <- list(new_config_setting = TRUE, second_custom_config = 'a') - expect_warning(load.project(override.config = override.config), NA) - expect_equal(config$.override.config, override.config) + override.config <- list(new_config_setting = TRUE, second_custom_config = "a") + expect_warning(load.project(override.config = override.config), NA) + expect_equal(config$.override.config, override.config) }) -test_that('Old-style unnamed argument override.config to load.project is parsed correctly', { +test_that("Old-style unnamed argument override.config to load.project is parsed correctly", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + on.exit(clear(), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - on.exit(clear(), add = TRUE) + override.config <- list(new_config_setting = TRUE) + expect_warning(load.project(override.config), NA) + expect_equal(config$.override.config, override.config) - override.config <- list(new_config_setting = TRUE) - expect_warning(load.project(override.config), NA) - expect_equal(config$.override.config, override.config) - - override.config <- list(new_config_setting = TRUE, second_custom_config = 'a') - expect_warning(load.project(override.config), NA) - expect_equal(config$.override.config, override.config) + override.config <- list(new_config_setting = TRUE, second_custom_config = "a") + expect_warning(load.project(override.config), NA) + expect_equal(config$.override.config, override.config) }) -test_that('New-style ... argument to load.project is parsed correctly', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("New-style ... argument to load.project is parsed correctly", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - on.exit(clear(), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + on.exit(clear(), add = TRUE) - expect_warning(load.project(new_config_setting = TRUE), NA) - expect_equal(config$.override.config, list(new_config_setting = TRUE)) + expect_warning(load.project(new_config_setting = TRUE), NA) + expect_equal(config$.override.config, list(new_config_setting = TRUE)) - expect_warning(load.project(new_config_setting = TRUE, second_custom_config = 'a'), NA) - expect_equal(config$.override.config, list(new_config_setting = TRUE, second_custom_config = 'a')) + expect_warning(load.project(new_config_setting = TRUE, second_custom_config = "a"), NA) + expect_equal(config$.override.config, list(new_config_setting = TRUE, second_custom_config = "a")) }) -test_that('New-style ... argument to load.project is parsed correctly when passing a single list', { +test_that("New-style ... argument to load.project is parsed correctly when passing a single list", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + on.exit(clear(), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - on.exit(clear(), add = TRUE) - - expect_warning(load.project(new_config_setting = list(a = 1)), NA) - expect_equal(config$.override.config, list(new_config_setting = list(a = 1))) + expect_warning(load.project(new_config_setting = list(a = 1)), NA) + expect_equal(config$.override.config, list(new_config_setting = list(a = 1))) }) -test_that('Override configuration to built in options is applied correctly', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Override configuration to built in options is applied correctly", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - on.exit(clear(), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + on.exit(clear(), add = TRUE) - config <- .new.config - config$load_libraries <- TRUE - write.dcf(config, 'config/global.dcf') - rm(config) + config <- .new.config + config$load_libraries <- TRUE + write.dcf(config, "config/global.dcf") + rm(config) - expect_warning(load.project(load_libraries = FALSE), NA) - expect_equal(config$.override.config, list(load_libraries = FALSE)) - expect_equal(config$load_libraries, FALSE) + expect_warning(load.project(load_libraries = FALSE), NA) + expect_equal(config$.override.config, list(load_libraries = FALSE)) + expect_equal(config$load_libraries, FALSE) - expect_warning(load.project(load_libraries = FALSE, munging = FALSE), NA) - expect_equal(config$.override.config, list(load_libraries = FALSE, munging = FALSE)) - expect_equal(config$load_libraries, FALSE) - expect_equal(config$munging, FALSE) + expect_warning(load.project(load_libraries = FALSE, munging = FALSE), NA) + expect_equal(config$.override.config, list(load_libraries = FALSE, munging = FALSE)) + expect_equal(config$load_libraries, FALSE) + expect_equal(config$munging, FALSE) }) -test_that('Override configuration to custom options is applied correctly', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Override configuration to custom options is applied correctly", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - on.exit(clear(), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + on.exit(clear(), add = TRUE) - writeLines('add.config(new_option = TRUE, apply.override = TRUE)', - file.path('lib', 'globals.R')) + writeLines( + "add.config(new_option = TRUE, apply.override = TRUE)", + file.path("lib", "globals.R") + ) - expect_warning(load.project(), NA) - expect_equal(config$new_option, TRUE) + expect_warning(load.project(), NA) + expect_equal(config$new_option, TRUE) - expect_warning(load.project(new_option = FALSE), NA) - expect_equal(config$.override.config, list(new_option = FALSE)) - expect_equal(config$new_option, FALSE) + expect_warning(load.project(new_option = FALSE), NA) + expect_equal(config$.override.config, list(new_option = FALSE)) + expect_equal(config$new_option, FALSE) }) -test_that('Override configuration to built in and custom options is applied correctly', { +test_that("Override configuration to built in and custom options is applied correctly", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + on.exit(clear(), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - on.exit(clear(), add = TRUE) + writeLines("add.config(new_option = TRUE, apply.override = TRUE)", file.path("lib", "globals.R")) - writeLines('add.config(new_option = TRUE, apply.override = TRUE)', file.path('lib', 'globals.R')) + expect_warning(load.project(), NA) + expect_equal(config$new_option, TRUE) - expect_warning(load.project(), NA) - expect_equal(config$new_option, TRUE) - - expect_warning(load.project(munging = FALSE, new_option = FALSE), NA) - expect_equal(config$.override.config, list(munging = FALSE, new_option = FALSE)) - expect_equal(config$munging, FALSE) - expect_equal(config$new_option, FALSE) + expect_warning(load.project(munging = FALSE, new_option = FALSE), NA) + expect_equal(config$.override.config, list(munging = FALSE, new_option = FALSE)) + expect_equal(config$munging, FALSE) + expect_equal(config$new_option, FALSE) }) -test_that('Unnamed override (not being a list) raises an error', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Unnamed override (not being a list) raises an error", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - on.exit(clear(), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + on.exit(clear(), add = TRUE) - expect_warning(load.project(), NA) - expect_error(load.project('a'), 'All options should be named') + expect_warning(load.project(), NA) + expect_error(load.project("a"), "All options should be named") }) -test_that('Override configuration to custom options is not applied when using apply.override = FALSE', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Override configuration to custom options is not applied when using apply.override = FALSE", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - on.exit(clear(), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + on.exit(clear(), add = TRUE) - writeLines('add.config(new_option = TRUE, apply.override = FALSE)', file.path('lib', 'globals.R')) + writeLines("add.config(new_option = TRUE, apply.override = FALSE)", file.path("lib", "globals.R")) - expect_warning(load.project(), NA) - expect_equal(config$new_option, TRUE) + expect_warning(load.project(), NA) + expect_equal(config$new_option, TRUE) - expect_warning(load.project(new_option = FALSE), NA) - expect_equal(config$.override.config, list(new_option = FALSE)) - expect_equal(config$new_option, TRUE) + expect_warning(load.project(new_option = FALSE), NA) + expect_equal(config$.override.config, list(new_option = FALSE)) + expect_equal(config$new_option, TRUE) }) diff --git a/tests/testthat/test-readers.R b/tests/testthat/test-readers.R index d0abe7c6..2af105d0 100644 --- a/tests/testthat/test-readers.R +++ b/tests/testthat/test-readers.R @@ -1,378 +1,406 @@ -context('Readers') - -test_that('Test 1: CSV Data file', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_01.csv' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_01.csv') - variable.name <- clean.variable.name('example_01') - - csv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_01, envir = .TargetEnv) +test_that("Test 1: CSV Data file", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_01.csv" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_01.csv" + ) + variable.name <- clean.variable.name("example_01") + + csv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_01, envir = .TargetEnv) }) -test_that('Test 2: .csv.bz2', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_02.csv.bz2' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_02.csv.bz2') - variable.name <- clean.variable.name('example_02') - - csv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_02, envir = .TargetEnv) - +test_that("Test 2: .csv.bz2", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_02.csv.bz2" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_02.csv.bz2" + ) + variable.name <- clean.variable.name("example_02") + + csv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_02, envir = .TargetEnv) }) -test_that('Test 3: csv.zip data', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_03.csv.zip' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_03.csv.zip') - variable.name <- clean.variable.name('example_03') - - csv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_03, envir = .TargetEnv) - +test_that("Test 3: csv.zip data", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_03.csv.zip" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_03.csv.zip" + ) + variable.name <- clean.variable.name("example_03") + + csv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_03, envir = .TargetEnv) }) -test_that('Example 04: CSV Data File with GZip Compression', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_04.csv.gz' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_04.csv.gz') - variable.name <- clean.variable.name('example_04') - - csv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_04, envir = .TargetEnv) - +test_that("Example 04: CSV Data File with GZip Compression", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_04.csv.gz" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_04.csv.gz" + ) + variable.name <- clean.variable.name("example_04") + + csv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_04, envir = .TargetEnv) }) -test_that('Example 05: TSV Data File', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_05.tsv' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_05.tsv') - variable.name <- clean.variable.name('example_05') - - tsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_05, envir = .TargetEnv) - +test_that("Example 05: TSV Data File", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_05.tsv" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_05.tsv" + ) + variable.name <- clean.variable.name("example_05") + + tsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_05, envir = .TargetEnv) }) -test_that('Example 06: TSV Data File with BZip2 Compression', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_06.tsv.bz2' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_06.tsv.bz2') - variable.name <- clean.variable.name('example_06') - - tsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_06, envir = .TargetEnv) - +test_that("Example 06: TSV Data File with BZip2 Compression", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_06.tsv.bz2" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_06.tsv.bz2" + ) + variable.name <- clean.variable.name("example_06") + + tsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_06, envir = .TargetEnv) }) -test_that('Example 07: TSV Data File with Zip Compression', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_07.tsv.zip' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_07.tsv.zip') - variable.name <- clean.variable.name('example_07') - - tsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_07, envir = .TargetEnv) - +test_that("Example 07: TSV Data File with Zip Compression", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_07.tsv.zip" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_07.tsv.zip" + ) + variable.name <- clean.variable.name("example_07") + + tsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_07, envir = .TargetEnv) }) -test_that('Example 08: TSV Data File with GZip Compression', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_08.tsv.gz' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_08.tsv.gz') - variable.name <- clean.variable.name('example_08') - - tsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_08, envir = .TargetEnv) - +test_that("Example 08: TSV Data File with GZip Compression", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_08.tsv.gz" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_08.tsv.gz" + ) + variable.name <- clean.variable.name("example_08") + + tsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_08, envir = .TargetEnv) }) -test_that('Example 09: WSV Data File', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_09.wsv' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_09.wsv') - variable.name <- clean.variable.name('example_09') - - wsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - expect_false(any(is.na(as.matrix(get(variable.name))))) - rm(example_09, envir = .TargetEnv) - +test_that("Example 09: WSV Data File", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_09.wsv" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_09.wsv" + ) + variable.name <- clean.variable.name("example_09") + + wsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + expect_false(any(is.na(as.matrix(get(variable.name))))) + rm(example_09, envir = .TargetEnv) }) -test_that('Example 10: WSV Data File with BZip2 Compression', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_10.wsv.bz2' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_10.wsv.bz2') - variable.name <- clean.variable.name('example_10') - - wsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_10, envir = .TargetEnv) - +test_that("Example 10: WSV Data File with BZip2 Compression", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_10.wsv.bz2" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_10.wsv.bz2" + ) + variable.name <- clean.variable.name("example_10") + + wsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_10, envir = .TargetEnv) }) -test_that('Example 11: WSV Data File with Zip Compression', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_11.wsv.zip' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_11.wsv.zip') - variable.name <- clean.variable.name('example_11') - - wsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_11, envir = .TargetEnv) +test_that("Example 11: WSV Data File with Zip Compression", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_11.wsv.zip" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_11.wsv.zip" + ) + variable.name <- clean.variable.name("example_11") + + wsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_11, envir = .TargetEnv) }) -test_that('Example 12: WSV Data File with GZip Compression', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_12.wsv.gz' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_12.wsv.gz') - variable.name <- clean.variable.name('example_12') - - wsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_12, envir = .TargetEnv) - +test_that("Example 12: WSV Data File with GZip Compression", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_12.wsv.gz" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_12.wsv.gz" + ) + variable.name <- clean.variable.name("example_12") + + wsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_12, envir = .TargetEnv) }) -test_that('Example 13: RData Data File with .RData Extension', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_13.RData' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_13.RData') - variable.name <- clean.variable.name('example_13') - - rdata.reader(data.file, filename, variable.name) - - expect_true(exists('m')) - expect_that(names(get('m')), equals(c('N', 'Prime'))) - expect_that(nrow(get('m')), equals(5)) - expect_that(ncol(get('m')), equals(2)) - expect_that(get('m')[5, 2], equals(11)) - rm('m', envir = .TargetEnv) - +test_that("Example 13: RData Data File with .RData Extension", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_13.RData" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_13.RData" + ) + variable.name <- clean.variable.name("example_13") + + rdata.reader(data.file, filename, variable.name) + + expect_true(exists("m")) + expect_equal(names(get("m")), c("N", "Prime")) + expect_equal(nrow(get("m")), 5) + expect_equal(ncol(get("m")), 2) + expect_equal(get("m")[5, 2], 11) + rm("m", envir = .TargetEnv) }) -test_that('Example 14: RData Data File with .rda Extension', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_14.rda' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_14.rda') - variable.name <- clean.variable.name('example_14') - - rdata.reader(data.file, filename, variable.name) - - expect_true(exists('n')) - expect_that(names(get('n')), equals(c('N', 'Prime'))) - expect_that(nrow(get('n')), equals(5)) - expect_that(ncol(get('n')), equals(2)) - expect_that(get('n')[5, 2], equals(11)) - rm('n', envir = .TargetEnv) - +test_that("Example 14: RData Data File with .rda Extension", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_14.rda" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_14.rda" + ) + variable.name <- clean.variable.name("example_14") + + rdata.reader(data.file, filename, variable.name) + + expect_true(exists("n")) + expect_equal(names(get("n")), c("N", "Prime")) + expect_equal(nrow(get("n")), 5) + expect_equal(ncol(get("n")), 2) + expect_equal(get("n")[5, 2], 11) + rm("n", envir = .TargetEnv) }) @@ -381,537 +409,582 @@ test_that('Example 14: RData Data File with .rda Extension', { ## }) -test_that('Example 16: TSV File with .tab Extension', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_16.tab' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_16.tab') - variable.name <- clean.variable.name('example_16') - - tsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_16, envir = .TargetEnv) +test_that("Example 16: TSV File with .tab Extension", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_16.tab" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_16.tab" + ) + variable.name <- clean.variable.name("example_16") + + tsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_16, envir = .TargetEnv) }) -test_that('Example 17: TSV File with .tab Extension and BZip2 Compression', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_17.tab.bz2' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_17.tab.bz2') - variable.name <- clean.variable.name('example_17') - - tsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_17, envir = .TargetEnv) +test_that("Example 17: TSV File with .tab Extension and BZip2 Compression", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_17.tab.bz2" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_17.tab.bz2" + ) + variable.name <- clean.variable.name("example_17") + + tsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_17, envir = .TargetEnv) }) -test_that('Example 18: TSV File with .tab Extension and Zip Compression', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_18.tab.zip' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_18.tab.zip') - variable.name <- clean.variable.name('example_18') - - tsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_18, envir = .TargetEnv) +test_that("Example 18: TSV File with .tab Extension and Zip Compression", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_18.tab.zip" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_18.tab.zip" + ) + variable.name <- clean.variable.name("example_18") + + tsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_18, envir = .TargetEnv) }) -test_that('Example 19: TSV File with .tab Extension and GZip Compression', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_19.tab.gz' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_19.tab.gz') - variable.name <- clean.variable.name('example_19') - - tsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_19, envir = .TargetEnv) - +test_that("Example 19: TSV File with .tab Extension and GZip Compression", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_19.tab.gz" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_19.tab.gz" + ) + variable.name <- clean.variable.name("example_19") + + tsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_19, envir = .TargetEnv) }) -test_that('Example 20: WSV File with .txt Extension', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_20.txt' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_20.txt') - variable.name <- clean.variable.name('example_20') - - wsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_20, envir = .TargetEnv) - +test_that("Example 20: WSV File with .txt Extension", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_20.txt" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_20.txt" + ) + variable.name <- clean.variable.name("example_20") + + wsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_20, envir = .TargetEnv) }) -test_that('Example 21: WSV File with .txt Extension and BZip2 Compression', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_21.txt.bz2' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_21.txt.bz2') - variable.name <- clean.variable.name('example_21') - - wsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_21, envir = .TargetEnv) - +test_that("Example 21: WSV File with .txt Extension and BZip2 Compression", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_21.txt.bz2" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_21.txt.bz2" + ) + variable.name <- clean.variable.name("example_21") + + wsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_21, envir = .TargetEnv) }) -test_that('Example 22: WSV File with .txt Extension and Zip Compression', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_22.txt.zip' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_22.txt.zip') - variable.name <- clean.variable.name('example_22') - - wsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_22, envir = .TargetEnv) - +test_that("Example 22: WSV File with .txt Extension and Zip Compression", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_22.txt.zip" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_22.txt.zip" + ) + variable.name <- clean.variable.name("example_22") + + wsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_22, envir = .TargetEnv) }) -test_that('Example 23: WSV File with .txt Extension and GZip Compression', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_23.txt.gz' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_23.txt.gz') - variable.name <- clean.variable.name('example_23') - - wsv.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_23, envir = .TargetEnv) - +test_that("Example 23: WSV File with .txt Extension and GZip Compression", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_23.txt.gz" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_23.txt.gz" + ) + variable.name <- clean.variable.name("example_23") + + wsv.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_23, envir = .TargetEnv) }) -test_that('Example 24: R File with .R Extension', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_24.R' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_24.R') - variable.name <- clean.variable.name('example_24') - - r.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_24, envir = .TargetEnv) - +test_that("Example 24: R File with .R Extension", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_24.R" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_24.R" + ) + variable.name <- clean.variable.name("example_24") + + r.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_24, envir = .TargetEnv) }) -test_that('Example 25: R File with .r Extension', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_25.r' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_25.r') - variable.name <- clean.variable.name('example_25') - - r.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_25, envir = .TargetEnv) - +test_that("Example 25: R File with .r Extension", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_25.r" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_25.r" + ) + variable.name <- clean.variable.name("example_25") + + r.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_25, envir = .TargetEnv) }) -test_that('Example 26: Excel 2007 File with .xls Extension', { - skip_if_not_installed("readxl") - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Example 26: Excel 2007 File with .xls Extension", { + skip_if_not_installed("readxl") - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - data.file <- 'example_26.xls' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_26.xls') - variable.name <- clean.variable.name('example_26') + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) - xls.reader(data.file, filename, variable.name) + data.file <- "example_26.xls" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_26.xls" + ) + variable.name <- clean.variable.name("example_26") - variable.name <- paste(variable.name, '.Sheet1', sep = '') + xls.reader(data.file, filename, variable.name) - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_26.Sheet1, envir = .TargetEnv) + variable.name <- paste(variable.name, ".Sheet1", sep = "") + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_26.Sheet1, envir = .TargetEnv) }) ## test_that('Example 27: Excel 2011 File with .xlsx Extension', { - #data.file <- 'example_27.xlsx' - #filename <- file.path(system.file('example_data', - # package = 'ProjectTemplate'), - # 'example_27.xlsx') - #variable.name <- clean.variable.name('example_27') - # - #xlsx.reader(data.file, filename, variable.name) - # - #variable.name <- paste(variable.name, '.Sheet1', sep = '') - # - #expect_that(exists(variable.name), is_true()) - #expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - #expect_that(nrow(get(variable.name)), equals(5)) - #expect_that(ncol(get(variable.name)), equals(2)) - #expect_that(get(variable.name)[5, 2], equals(11)) - #rm(example.27.Sheet1, envir = .TargetEnv) +# data.file <- 'example_27.xlsx' +# filename <- file.path(system.file('example_data', +# package = 'ProjectTemplate'), +# 'example_27.xlsx') +# variable.name <- clean.variable.name('example_27') +# +# xlsx.reader(data.file, filename, variable.name) +# +# variable.name <- paste(variable.name, '.Sheet1', sep = '') +# +# expect_equal(exists(variable.name), is_true()) +# expect_equal(names(get(variable.name)), c('N', 'Prime'))) +# expect_equal(nrow(get(variable.name)), 5) +# expect_equal(ncol(get(variable.name)), 2) +# expect_equal(get(variable.name)[5, 2], 11) +# rm(example.27.Sheet1, envir = .TargetEnv) ## }) test_that('Example 28: SQLite3 Support with .sql Extension with table = "..."', { - skip_if_not_installed("RSQLite") - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - sql.file <- data.frame(type = 'sqlite', - dbname = file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_28.db'), - table = 'example_28') - write.dcf(sql.file, file = 'example_28.sql', width = 1000) - - data.file <- 'example_28.sql' - filename <- 'example_28.sql' - variable.name <- clean.variable.name('example_28') - - sql.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_28, envir = .TargetEnv) - unlink('example_28.sql') - + skip_if_not_installed("RSQLite") + + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + sql.file <- data.frame( + type = "sqlite", + dbname = file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_28.db" + ), + table = "example_28" + ) + write.dcf(sql.file, file = "example_28.sql", width = 1000) + + data.file <- "example_28.sql" + filename <- "example_28.sql" + variable.name <- clean.variable.name("example_28") + + sql.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_28, envir = .TargetEnv) + unlink("example_28.sql") }) test_that('Example 29: SQLite3 Support with .sql Extension with query = "SELECT * FROM ..."', { - skip_if_not_installed("RSQLite") - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - sql.file <- data.frame(type = 'sqlite', - dbname = file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_29.db'), - query = 'SELECT * FROM example_29') - write.dcf(sql.file, file = 'example_29.sql', width = 1000) - - data.file <- 'example_29.sql' - filename <- 'example_29.sql' - variable.name <- clean.variable.name('example_29') - - sql.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_29, envir = .TargetEnv) - unlink('example_29.sql') - + skip_if_not_installed("RSQLite") + + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + sql.file <- data.frame( + type = "sqlite", + dbname = file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_29.db" + ), + query = "SELECT * FROM example_29" + ) + write.dcf(sql.file, file = "example_29.sql", width = 1000) + + data.file <- "example_29.sql" + filename <- "example_29.sql" + variable.name <- clean.variable.name("example_29") + + sql.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_29, envir = .TargetEnv) + unlink("example_29.sql") }) test_that('Example 30: SQLite3 Support with .sql Extension and table = "*"', { - skip_if_not_installed("RSQLite") - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - sql.file <- data.frame(type = 'sqlite', - dbname = file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_30.db'), - table = '*') - write.dcf(sql.file, file = 'example_30.sql', width = 1000) - - data.file <- 'example_30.sql' - filename <- 'example_30.sql' - variable.name <- clean.variable.name('example_30') - - sql.reader(data.file, filename, variable.name) - - variable1.name <- clean.variable.name('example_30a') - variable2.name <- clean.variable.name('example_30b') - expect_true(exists(variable1.name)) - expect_that(names(get(variable1.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable1.name)), equals(5)) - expect_that(ncol(get(variable1.name)), equals(2)) - expect_that(get(variable1.name)[5, 2], equals(11)) - rm(example_30a, envir = .TargetEnv) - expect_true(exists(variable2.name)) - expect_that(names(get(variable2.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable2.name)), equals(5)) - expect_that(ncol(get(variable2.name)), equals(2)) - expect_that(get(variable2.name)[5, 2], equals(11)) - rm(example_30b, envir = .TargetEnv) - rm(example_30, envir = .TargetEnv) - unlink('example_30.sql') - + skip_if_not_installed("RSQLite") + + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + sql.file <- data.frame( + type = "sqlite", + dbname = file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_30.db" + ), + table = "*" + ) + write.dcf(sql.file, file = "example_30.sql", width = 1000) + + data.file <- "example_30.sql" + filename <- "example_30.sql" + variable.name <- clean.variable.name("example_30") + + sql.reader(data.file, filename, variable.name) + + variable1.name <- clean.variable.name("example_30a") + variable2.name <- clean.variable.name("example_30b") + expect_true(exists(variable1.name)) + expect_equal(names(get(variable1.name)), c("N", "Prime")) + expect_equal(nrow(get(variable1.name)), 5) + expect_equal(ncol(get(variable1.name)), 2) + expect_equal(get(variable1.name)[5, 2], 11) + rm(example_30a, envir = .TargetEnv) + expect_true(exists(variable2.name)) + expect_equal(names(get(variable2.name)), c("N", "Prime")) + expect_equal(nrow(get(variable2.name)), 5) + expect_equal(ncol(get(variable2.name)), 2) + expect_equal(get(variable2.name)[5, 2], 11) + rm(example_30b, envir = .TargetEnv) + rm(example_30, envir = .TargetEnv) + unlink("example_30.sql") }) -test_that('Example 31: SQLite3 Support with .db Extension', { - skip_if_not_installed("RSQLite") - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_31.db' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_31.db') - variable.name <- clean.variable.name('example_31') - - db.reader(data.file, filename, variable.name) - - variable1.name <- clean.variable.name('example_31a') - variable2.name <- clean.variable.name('example_31b') - expect_true(exists(variable1.name)) - expect_that(names(get(variable1.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable1.name)), equals(5)) - expect_that(ncol(get(variable1.name)), equals(2)) - expect_that(get(variable1.name)[5, 2], equals(11)) - rm(example_31a, envir = .TargetEnv) - expect_true(exists(variable2.name)) - expect_that(names(get(variable2.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable2.name)), equals(5)) - expect_that(ncol(get(variable2.name)), equals(2)) - expect_that(get(variable2.name)[5, 2], equals(11)) - rm(example_31b, envir = .TargetEnv) +test_that("Example 31: SQLite3 Support with .db Extension", { + skip_if_not_installed("RSQLite") + + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_31.db" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_31.db" + ) + variable.name <- clean.variable.name("example_31") + + db.reader(data.file, filename, variable.name) + + variable1.name <- clean.variable.name("example_31a") + variable2.name <- clean.variable.name("example_31b") + expect_true(exists(variable1.name)) + expect_equal(names(get(variable1.name)), c("N", "Prime")) + expect_equal(nrow(get(variable1.name)), 5) + expect_equal(ncol(get(variable1.name)), 2) + expect_equal(get(variable1.name)[5, 2], 11) + rm(example_31a, envir = .TargetEnv) + expect_true(exists(variable2.name)) + expect_equal(names(get(variable2.name)), c("N", "Prime")) + expect_equal(nrow(get(variable2.name)), 5) + expect_equal(ncol(get(variable2.name)), 2) + expect_equal(get(variable2.name)[5, 2], 11) + rm(example_31b, envir = .TargetEnv) }) -test_that('Example 32: Weka Support with .arff Extension', { +test_that("Example 32: Weka Support with .arff Extension", { + skip_if_not_installed("foreign") - skip_if_not_installed("foreign") - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) - data.file <- 'example_32.arff' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_32.arff') - variable.name <- clean.variable.name('example_32') + data.file <- "example_32.arff" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_32.arff" + ) + variable.name <- clean.variable.name("example_32") - arff.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_32, envir = .TargetEnv) + arff.reader(data.file, filename, variable.name) + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_32, envir = .TargetEnv) }) -test_that('Example 33: Arbitary File Support with .file File Pointing to .db File', { - skip_if_not_installed("RSQLite") - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - info.file <- data.frame(path = file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_28.db'), - extension = 'db') - write.dcf(info.file, file = 'example_33.file', width = 1000) - - data.file <- 'example_33.file' - filename <- 'example_33.file' - variable.name <- clean.variable.name('example_28') - - file.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_28, envir = .TargetEnv) - unlink('example_33.file') - +test_that("Example 33: Arbitary File Support with .file File Pointing to .db File", { + skip_if_not_installed("RSQLite") + + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + info.file <- data.frame( + path = file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_28.db" + ), + extension = "db" + ) + write.dcf(info.file, file = "example_33.file", width = 1000) + + data.file <- "example_33.file" + filename <- "example_33.file" + variable.name <- clean.variable.name("example_28") + + file.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_28, envir = .TargetEnv) + unlink("example_33.file") }) @@ -920,235 +993,250 @@ test_that('Example 33: Arbitary File Support with .file File Pointing to .db Fil ## }) -test_that('Example 35: PPM Support with .ppm Extension', { - skip_if_not_installed("pixmap") - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Example 35: PPM Support with .ppm Extension", { + skip_if_not_installed("pixmap") - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - data.file <- 'example_35.ppm' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_35.ppm') - variable.name <- clean.variable.name('example_35') + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) - expect_warning( - ppm.reader(data.file, filename, variable.name), - " is NULL so the result will be NULL") + data.file <- "example_35.ppm" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_35.ppm" + ) + variable.name <- clean.variable.name("example_35") - expect_true(exists(variable.name)) - expect_that(as.character(class(get(variable.name))), equals('pixmapRGB')) - rm(example_35, envir = .TargetEnv) + expect_warning( + ppm.reader(data.file, filename, variable.name), + " is NULL so the result will be NULL" + ) + expect_true(exists(variable.name)) + expect_equal(as.character(class(get(variable.name))), "pixmapRGB") + rm(example_35, envir = .TargetEnv) }) -test_that('Example 36: dBase Support with .dbf Extension', { - - skip_if_not_installed("foreign") - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_36.dbf' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_36.dbf') - variable.name <- clean.variable.name('example_36') - - dbf.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_36, envir = .TargetEnv) - +test_that("Example 36: dBase Support with .dbf Extension", { + skip_if_not_installed("foreign") + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_36.dbf" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_36.dbf" + ) + variable.name <- clean.variable.name("example_36") + + dbf.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_36, envir = .TargetEnv) }) -test_that('Example 37: SPSS Support with .sav Extension', { - skip_if_not_installed("foreign") +test_that("Example 37: SPSS Support with .sav Extension", { + skip_if_not_installed("foreign") - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) - data.file <- 'example_37.sav' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_37.sav') - variable.name <- clean.variable.name('example_37') + data.file <- "example_37.sav" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_37.sav" + ) + variable.name <- clean.variable.name("example_37") - spss.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_37, envir = .TargetEnv) + spss.reader(data.file, filename, variable.name) + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_37, envir = .TargetEnv) }) -test_that('Example 38: SPSS Support with .sav Extension / Alternative Generation', { - - skip_if_not_installed("foreign") - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Example 38: SPSS Support with .sav Extension / Alternative Generation", { + skip_if_not_installed("foreign") - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - data.file <- 'example_38.sav' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_38.sav') - variable.name <- clean.variable.name('example_38') + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) - spss.reader(data.file, filename, variable.name) + data.file <- "example_38.sav" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_38.sav" + ) + variable.name <- clean.variable.name("example_38") - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_38, envir = .TargetEnv) + spss.reader(data.file, filename, variable.name) + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_38, envir = .TargetEnv) }) -test_that('Example 39: Stata Support with .dta Extension', { - skip_if_not_installed("foreign") +test_that("Example 39: Stata Support with .dta Extension", { + skip_if_not_installed("foreign") - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) - data.file <- 'example_39.dta' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_39.dta') - variable.name <- clean.variable.name('example_39') + data.file <- "example_39.dta" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_39.dta" + ) + variable.name <- clean.variable.name("example_39") - stata.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_39, envir = .TargetEnv) + stata.reader(data.file, filename, variable.name) + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_39, envir = .TargetEnv) }) -test_that('Example 40: Stata Support with .dta Extension / Alternative Generation', { - skip_if_not_installed("foreign") - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Example 40: Stata Support with .dta Extension / Alternative Generation", { + skip_if_not_installed("foreign") - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - data.file <- 'example_40.dta' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_40.dta') - variable.name <- clean.variable.name('example_40') + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) - stata.reader(data.file, filename, variable.name) + data.file <- "example_40.dta" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_40.dta" + ) + variable.name <- clean.variable.name("example_40") - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_40, envir = .TargetEnv) + stata.reader(data.file, filename, variable.name) + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_40, envir = .TargetEnv) }) -test_that('Example 41: SAS Support with .xport Extension', { - skip_if_not_installed("foreign") +test_that("Example 41: SAS Support with .xport Extension", { + skip_if_not_installed("foreign") - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) - data.file <- 'example_41.xport' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_41.xport') - variable.name <- clean.variable.name('example_41') + data.file <- "example_41.xport" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_41.xport" + ) + variable.name <- clean.variable.name("example_41") - xport.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'PRIME'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_41, envir = .TargetEnv) + xport.reader(data.file, filename, variable.name) + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "PRIME")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_41, envir = .TargetEnv) }) -test_that('Example 42: SAS Support with .xpt Extension', { - skip_if_not_installed("foreign") - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Example 42: SAS Support with .xpt Extension", { + skip_if_not_installed("foreign") - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - data.file <- 'example_42.xpt' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_42.xpt') - variable.name <- clean.variable.name('example_42') + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) - xport.reader(data.file, filename, variable.name) + data.file <- "example_42.xpt" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_42.xpt" + ) + variable.name <- clean.variable.name("example_42") - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'PRIME'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(example_42, envir = .TargetEnv) + xport.reader(data.file, filename, variable.name) + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "PRIME")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(example_42, envir = .TargetEnv) }) @@ -1156,54 +1244,60 @@ test_that('Example 42: SAS Support with .xpt Extension', { ## }) -test_that('Example 44: Single Object Serialization Support with .rds Extension', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_44.rds' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_44.rds') - variable.name <- clean.variable.name('example_44') - - rds.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_that(names(get(variable.name)), equals(c('N', 'Prime'))) - expect_that(nrow(get(variable.name)), equals(5)) - expect_that(ncol(get(variable.name)), equals(2)) - expect_that(get(variable.name)[5, 2], equals(11)) - rm(list = variable.name, envir = .TargetEnv) +test_that("Example 44: Single Object Serialization Support with .rds Extension", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_44.rds" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_44.rds" + ) + variable.name <- clean.variable.name("example_44") + + rds.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[5, 2], 11) + rm(list = variable.name, envir = .TargetEnv) }) -test_that('Example 45: Feather support with .feather extension', { - skip_if_not_installed("feather") - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - suppressMessages(load.project()) - - data.file <- 'example_45.feather' - filename <- file.path(system.file('example_data', - package = 'ProjectTemplate'), - 'example_45.feather') - variable.name <- clean.variable.name('example_45') - - feather.reader(data.file, filename, variable.name) - - expect_true(exists(variable.name)) - expect_equal(names(get(variable.name)), c('N', 'Prime')) - expect_equal(nrow(get(variable.name)), 5) - expect_equal(ncol(get(variable.name)), 2) - expect_equal(get(variable.name)[[5, 2]], 11) - rm(list = variable.name, envir = .TargetEnv) +test_that("Example 45: Feather support with .feather extension", { + skip_if_not_installed("feather") + + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + suppressMessages(load.project()) + + data.file <- "example_45.feather" + filename <- file.path( + system.file("example_data", + package = "ProjectTemplate" + ), + "example_45.feather" + ) + variable.name <- clean.variable.name("example_45") + + feather.reader(data.file, filename, variable.name) + + expect_true(exists(variable.name)) + expect_equal(names(get(variable.name)), c("N", "Prime")) + expect_equal(nrow(get(variable.name)), 5) + expect_equal(ncol(get(variable.name)), 2) + expect_equal(get(variable.name)[[5, 2]], 11) + rm(list = variable.name, envir = .TargetEnv) }) diff --git a/tests/testthat/test-reload.R b/tests/testthat/test-reload.R index fe0a63b6..9966d250 100644 --- a/tests/testthat/test-reload.R +++ b/tests/testthat/test-reload.R @@ -1,155 +1,141 @@ -context('Reload project') +test_that("Options are passed through correctly to load.project", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) -test_that('Options are passed through correctly to load.project', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + clear(force = TRUE) - clear(force=TRUE) + # Create some R code and put in data directory + CODE <- paste0(deparse(substitute({ + x <- 10 + })), collapse = "\n") - # Create some R code and put in data directory - CODE <- paste0(deparse(substitute({ - x <- 10 + # save R code in the data directory + writeLines(CODE, "data/x.R") - })), collapse ="\n") + CODE <- paste0(deparse(substitute({ + y <- 20 + })), collapse = "\n") - # save R code in the data directory - writeLines(CODE, "data/x.R") + # save R code in the data directory + writeLines(CODE, "data/y.R") - CODE <- paste0(deparse(substitute({ - y <- 20 + CODE <- paste0(deparse(substitute({ + z <- 10 + })), collapse = "\n") - })), collapse ="\n") + # save R code in the data directory + writeLines(CODE, "data/z.R") - # save R code in the data directory - writeLines(CODE, "data/y.R") - - CODE <- paste0(deparse(substitute({ - z <- 10 - - })), collapse ="\n") - - # save R code in the data directory - writeLines(CODE, "data/z.R") - - # reload the project but switch off data loading - suppressMessages(reload.project(data_loading = FALSE)) - - # x should not exist in Global Env or the cache - expect_true(!exists("x")) - expect_true(!.read.cache.info("x")$in.cache) + # reload the project but switch off data loading + suppressMessages(reload.project(data_loading = FALSE)) + # x should not exist in Global Env or the cache + expect_true(!exists("x")) + expect_true(!.read.cache.info("x")$in.cache) }) -test_that('reload.project ignores sticky_variables', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - clear(force=TRUE) - - # Create some R code and put in data directory - CODE <- paste0(deparse(substitute({ - x <- 10 +test_that("reload.project ignores sticky_variables", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - })), collapse ="\n") + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - # save R code in the data directory - writeLines(CODE, "data/x.R") + clear(force = TRUE) - CODE <- paste0(deparse(substitute({ - y <- 20 + # Create some R code and put in data directory + CODE <- paste0(deparse(substitute({ + x <- 10 + })), collapse = "\n") - })), collapse ="\n") + # save R code in the data directory + writeLines(CODE, "data/x.R") - # save R code in the data directory - writeLines(CODE, "data/y.R") + CODE <- paste0(deparse(substitute({ + y <- 20 + })), collapse = "\n") - CODE <- paste0(deparse(substitute({ - z <- 10 + # save R code in the data directory + writeLines(CODE, "data/y.R") - })), collapse ="\n") + CODE <- paste0(deparse(substitute({ + z <- 10 + })), collapse = "\n") - # save R code in the data directory - writeLines(CODE, "data/z.R") + # save R code in the data directory + writeLines(CODE, "data/z.R") - # Read the config data and set sticky_variables to x,y - config <- translate.dcf("config/global.dcf") - expect_error(config$sticky_variables <- "x,y", NA) - write.dcf(config, "config/global.dcf" ) + # Read the config data and set sticky_variables to x,y + config <- translate.dcf("config/global.dcf") + expect_error(config$sticky_variables <- "x,y", NA) + write.dcf(config, "config/global.dcf") - # Load the project and check that x exists and is in the cache - suppressMessages(load.project()) - expect_true(exists("x")) - expect_true(.read.cache.info("x")$in.cache) - - # reload the project and config, x, y should not be cleared - expect_message(reload.project(), "not cleared: config x y") - - # Also, z should be reloaded from data - expect_true(exists("z")) + # Load the project and check that x exists and is in the cache + suppressMessages(load.project()) + expect_true(exists("x")) + expect_true(.read.cache.info("x")$in.cache) + # reload the project and config, x, y should not be cleared + expect_message(reload.project(), "not cleared: config x y") + # Also, z should be reloaded from data + expect_true(exists("z")) }) -test_that('reload.project with reset clears everything', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - clear(force=TRUE) - - # Create some R code and put in data directory - CODE <- paste0(deparse(substitute({ - x <- 10 - - })), collapse ="\n") - - # save R code in the data directory - writeLines(CODE, "data/x.R") +test_that("reload.project with reset clears everything", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - CODE <- paste0(deparse(substitute({ - y <- 20 + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - })), collapse ="\n") + clear(force = TRUE) - # save R code in the data directory - writeLines(CODE, "data/y.R") + # Create some R code and put in data directory + CODE <- paste0(deparse(substitute({ + x <- 10 + })), collapse = "\n") - CODE <- paste0(deparse(substitute({ - z <- 10 + # save R code in the data directory + writeLines(CODE, "data/x.R") - })), collapse ="\n") + CODE <- paste0(deparse(substitute({ + y <- 20 + })), collapse = "\n") - # save R code in the data directory - writeLines(CODE, "data/z.R") + # save R code in the data directory + writeLines(CODE, "data/y.R") - # Read the config data and set sticky_variables to x,y - config <- translate.dcf("config/global.dcf") - expect_error(config$sticky_variables <- "x,y", NA) - write.dcf(config, "config/global.dcf" ) + CODE <- paste0(deparse(substitute({ + z <- 10 + })), collapse = "\n") + # save R code in the data directory + writeLines(CODE, "data/z.R") - # cache should have items in it after a load.project - suppressMessages(reload.project()) - expect_true(exists("x")) - expect_true(.read.cache.info("x")$in.cache) + # Read the config data and set sticky_variables to x,y + config <- translate.dcf("config/global.dcf") + expect_error(config$sticky_variables <- "x,y", NA) + write.dcf(config, "config/global.dcf") - # reload the project with reset and switch off data loading - suppressMessages(reload.project(data_loading = FALSE, - reset = TRUE)) - # Should find the cache empty, even though x is in sticky_variables - expect_true(!.read.cache.info("x")$in.cache) + # cache should have items in it after a load.project + suppressMessages(reload.project()) + expect_true(exists("x")) + expect_true(.read.cache.info("x")$in.cache) + # reload the project with reset and switch off data loading + suppressMessages(reload.project( + data_loading = FALSE, + reset = TRUE + )) + # Should find the cache empty, even though x is in sticky_variables + expect_true(!.read.cache.info("x")$in.cache) }) diff --git a/tests/testthat/test-require.R b/tests/testthat/test-require.R index 0da0d0e2..2ca8e65c 100644 --- a/tests/testthat/test-require.R +++ b/tests/testthat/test-require.R @@ -1,24 +1,23 @@ -context('Require package') +test_that(".require.package warns if compat setting is set", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), + project.directory = dirname(test_project) + )) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) -test_that('.require.package warns if compat setting is set', { + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + suppressMessages(.unload.project()) + expect_false(.has.project()) + suppressMessages(load.project()) + expect_true(.has.project()) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + expect_false(project.info$config$attach_internal_libraries) + expect_warning(.require.package("ProjectTemplate"), NA) - suppressMessages(.unload.project()) - expect_false(.has.project()) - suppressMessages(load.project()) - expect_true(.has.project()) - - expect_false(project.info$config$attach_internal_libraries) - expect_warning(.require.package('ProjectTemplate'), NA) - - project.info$config[['attach_internal_libraries']] <<- TRUE - expect_true(project.info$config[['attach_internal_libraries']]) - expect_true(get.project()$config$attach_internal_libraries) - expect_that(.require.package('ProjectTemplate'), gives_warning('attach_internal_libraries')) + project.info$config[["attach_internal_libraries"]] <<- TRUE + expect_true(project.info$config[["attach_internal_libraries"]]) + expect_true(get.project()$config$attach_internal_libraries) + expect_warning(.require.package("ProjectTemplate"), "attach_internal_libraries") }) diff --git a/tests/testthat/test-templates.R b/tests/testthat/test-templates.R index ee28cd85..4ed96285 100644 --- a/tests/testthat/test-templates.R +++ b/tests/testthat/test-templates.R @@ -1,127 +1,125 @@ -context('Templates') - expect_file <- function(...) { - x <- file.path(...) - expect_true(file.exists(x), x) + x <- file.path(...) + expect_true(file.exists(x), x) } expect_no_file <- function(...) { - x <- file.path(...) - expect_false(file.exists(x), x) + x <- file.path(...) + expect_false(file.exists(x), x) } expect_dir <- function(...) { - x <- file.path(...) - expect_file(x) - expect_true(.is.dir(x)) - expect_file(file.path(x, 'README.md')) + x <- file.path(...) + expect_file(x) + expect_true(.is.dir(x)) + expect_file(file.path(x, "README.md")) } expect_full <- function() { - expect_dir('.') - expect_dir('cache') - expect_dir('config') - expect_file(file.path('config', 'global.dcf')) - expect_dir('data') - expect_dir('diagnostics') - expect_file(file.path('diagnostics', '1.R')) - expect_dir('docs') - expect_dir('graphs') - expect_dir('lib') - expect_file(file.path('lib', 'helpers.R')) - expect_dir('logs') - expect_dir('munge') - expect_file(file.path('munge', '01-A.R')) - expect_dir('profiling') - expect_file(file.path('profiling', '1.R')) - expect_dir('reports') - expect_dir('src') - expect_file(file.path('src', 'eda.R')) - expect_dir('tests') - expect_file(file.path('tests', '1.R')) - expect_file(file.path('TODO')) + expect_dir(".") + expect_dir("cache") + expect_dir("config") + expect_file(file.path("config", "global.dcf")) + expect_dir("data") + expect_dir("diagnostics") + expect_file(file.path("diagnostics", "1.R")) + expect_dir("docs") + expect_dir("graphs") + expect_dir("lib") + expect_file(file.path("lib", "helpers.R")) + expect_dir("logs") + expect_dir("munge") + expect_file(file.path("munge", "01-A.R")) + expect_dir("profiling") + expect_file(file.path("profiling", "1.R")) + expect_dir("reports") + expect_dir("src") + expect_file(file.path("src", "eda.R")) + expect_dir("tests") + expect_file(file.path("tests", "1.R")) + expect_file(file.path("TODO")) } expect_minimal <- function() { - expect_dir('.') - expect_dir('cache') - expect_dir('config') - expect_file(file.path('config', 'global.dcf')) - expect_dir('data') - expect_dir('munge') - expect_file(file.path('munge', '01-A.R')) - expect_dir('src') - expect_file(file.path('src', 'eda.R')) - - expect_no_file('diagnostics') - expect_no_file('docs') - expect_no_file('graphs') - expect_no_file('lib') - expect_no_file('logs') - expect_no_file('profiling') - expect_no_file('reports') - expect_no_file('tests') - expect_no_file('TODO') + expect_dir(".") + expect_dir("cache") + expect_dir("config") + expect_file(file.path("config", "global.dcf")) + expect_dir("data") + expect_dir("munge") + expect_file(file.path("munge", "01-A.R")) + expect_dir("src") + expect_file(file.path("src", "eda.R")) + + expect_no_file("diagnostics") + expect_no_file("docs") + expect_no_file("graphs") + expect_no_file("lib") + expect_no_file("logs") + expect_no_file("profiling") + expect_no_file("reports") + expect_no_file("tests") + expect_no_file("TODO") } -test_that('creating a template produces at least a minimal project skeleton', { - test_project <- basename(tempfile('test_project')) - # Set temporary template as working directory - expect_error(oldwd <- setwd(tempdir()), NA) - on.exit(setwd(oldwd), add = TRUE) +test_that("creating a template produces at least a minimal project skeleton", { + test_project <- basename(tempfile("test_project")) + # Set temporary template as working directory + expect_error(oldwd <- setwd(tempdir()), NA) + on.exit(setwd(oldwd), add = TRUE) - # Create temporary template - suppressMessages(create.template(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + # Create temporary template + suppressMessages(create.template(test_project)) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - expect_error(oldwd <- setwd(test_project), NA) - on.exit(setwd(oldwd), add = TRUE) - expect_error(load.project(), NA) + expect_error(oldwd <- setwd(test_project), NA) + on.exit(setwd(oldwd), add = TRUE) + expect_error(load.project(), NA) - expect_minimal() + expect_minimal() }) test_that('creating a template from "full" produces a full project skeleton', { - test_project <- basename(tempfile('test_project')) - # Set temporary template as working directory - expect_error(oldwd <- setwd(tempdir()), NA) - on.exit(setwd(oldwd), add = TRUE) + test_project <- basename(tempfile("test_project")) + # Set temporary template as working directory + expect_error(oldwd <- setwd(tempdir()), NA) + on.exit(setwd(oldwd), add = TRUE) - # Create temporary template - suppressMessages(create.template(test_project, source = 'full')) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + # Create temporary template + suppressMessages(create.template(test_project, source = "full")) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - expect_error(oldwd <- setwd(test_project), NA) - on.exit(setwd(oldwd), add = TRUE) + expect_error(oldwd <- setwd(test_project), NA) + on.exit(setwd(oldwd), add = TRUE) - expect_full() - expect_error(load.project(), NA) + expect_full() + expect_error(load.project(), NA) }) -test_that('creating a template from an existing template produces the same project skeleton', { - test_project <- basename(tempfile('test_project')) - test_project2 <- basename(tempfile('test_project')) - # Set temporary template as working directory - expect_error(oldwd <- setwd(tempdir()), NA) - on.exit(setwd(oldwd), add = TRUE) +test_that("creating a template from an existing template produces the same project skeleton", { + test_project <- basename(tempfile("test_project")) + test_project2 <- basename(tempfile("test_project")) + # Set temporary template as working directory + expect_error(oldwd <- setwd(tempdir()), NA) + on.exit(setwd(oldwd), add = TRUE) - # Create temporary template - suppressMessages(create.template(test_project, source = 'full')) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + # Create temporary template + suppressMessages(create.template(test_project, source = "full")) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - # Add custom file to template to verify it is copied - temp_file <- file.path(test_project, 'data', 'file_not_in_full_template.csv') - write.csv(data.frame(x = 1:4), file = temp_file) + # Add custom file to template to verify it is copied + temp_file <- file.path(test_project, "data", "file_not_in_full_template.csv") + write.csv(data.frame(x = 1:4), file = temp_file) - # Create temporary template from previous template - suppressMessages(create.template(test_project2, source = test_project)) - on.exit(unlink(test_project2, recursive = TRUE), add = TRUE) + # Create temporary template from previous template + suppressMessages(create.template(test_project2, source = test_project)) + on.exit(unlink(test_project2, recursive = TRUE), add = TRUE) - expect_error(setwd(test_project2), NA) + expect_error(setwd(test_project2), NA) - expect_full() - expect_true(file.exists(file.path('data', 'file_not_in_full_template.csv'))) - expect_error(load.project(), NA) + expect_full() + expect_true(file.exists(file.path("data", "file_not_in_full_template.csv"))) + expect_error(load.project(), NA) }) diff --git a/tests/testthat/test-test-load.R b/tests/testthat/test-test-load.R index 8849056e..94b301e7 100644 --- a/tests/testthat/test-test-load.R +++ b/tests/testthat/test-test-load.R @@ -1,3 +1,3 @@ test_that("multiplication works", { - expect_equal(2 * 2, 4) + expect_equal(2 * 2, 4) }) diff --git a/tests/testthat/test-varnames.R b/tests/testthat/test-varnames.R index 109f3607..003d9b6e 100644 --- a/tests/testthat/test-varnames.R +++ b/tests/testthat/test-varnames.R @@ -1,45 +1,39 @@ -context('Clean variable names') +test_that("Cleans variable names with underscore when underscore_variables = TRUE", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) -test_that('Cleans variable names with underscore when underscore_variables = TRUE', { + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + config$underscore_variables <- TRUE + .save.config(config) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - config$underscore_variables <- TRUE - .save.config(config) - - suppressMessages(load.project()) - - expect_that(clean.variable.name('test_me'), equals('test_me')) - expect_that(clean.variable.name('test-me'), equals('test.me')) - expect_that(clean.variable.name('test..me'), equals('test.me')) - expect_that(clean.variable.name('test me'), equals('test.me')) - expect_that(clean.variable.name('1990'), equals('X1990')) + suppressMessages(load.project()) + expect_equal(clean.variable.name("test_me"), "test_me") + expect_equal(clean.variable.name("test-me"), "test.me") + expect_equal(clean.variable.name("test..me"), "test.me") + expect_equal(clean.variable.name("test me"), "test.me") + expect_equal(clean.variable.name("1990"), "X1990") }) -test_that('Cleans variable names without underscore when underscore_variables = FALSE', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Cleans variable names without underscore when underscore_variables = FALSE", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - config$underscore_variables <- FALSE - .save.config(config) + config$underscore_variables <- FALSE + .save.config(config) - suppressMessages(load.project(underscore_variables = FALSE)) - - expect_that(clean.variable.name('test_me'), equals('test.me')) - expect_that(clean.variable.name('test-me'), equals('test.me')) - expect_that(clean.variable.name('test..me'), equals('test.me')) - expect_that(clean.variable.name('test me'), equals('test.me')) - expect_that(clean.variable.name('1990'), equals('X1990')) + suppressMessages(load.project(underscore_variables = FALSE)) + expect_equal(clean.variable.name("test_me"), "test.me") + expect_equal(clean.variable.name("test-me"), "test.me") + expect_equal(clean.variable.name("test..me"), "test.me") + expect_equal(clean.variable.name("test me"), "test.me") + expect_equal(clean.variable.name("1990"), "X1990") }) diff --git a/tests/testthat/test-version.R b/tests/testthat/test-version.R index 36c274b3..ba5f98af 100644 --- a/tests/testthat/test-version.R +++ b/tests/testthat/test-version.R @@ -1,68 +1,62 @@ -context('Version field') +test_that("Test matching version field", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) -test_that('Test matching version field', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) - - expect_warning(suppressMessages(load.project()), NA) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) + expect_warning(suppressMessages(load.project()), NA) }) -test_that('Test too old version of ProjectTemplate', { - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Test too old version of ProjectTemplate", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - config <- .new.config - config$version <- paste0('1', config$version) - write.dcf(config, 'config/global.dcf') - - expect_that(suppressMessages(load.project()), throws_error("Please upgrade ProjectTemplate")) + config <- .new.config + config$version <- paste0("1", config$version) + write.dcf(config, "config/global.dcf") + expect_error(suppressMessages(load.project()), "Please upgrade ProjectTemplate") }) -test_that('Test new version of ProjectTemplate', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) +test_that("Test new version of ProjectTemplate", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - config <- .new.config - config$version <- '0.4' - write.dcf(config, 'config/global.dcf') - - expect_that(suppressMessages(load.project()), - gives_warning("ProjectTemplate::migrate.project()")) + config <- .new.config + config$version <- "0.4" + write.dcf(config, "config/global.dcf") + expect_warning( + suppressMessages(load.project()), + "ProjectTemplate::migrate.project()" + ) }) -test_that('Test migration', { - - test_project <- tempfile('test_project') - suppressMessages(create.project(test_project)) - on.exit(unlink(test_project, recursive = TRUE), add = TRUE) - - oldwd <- setwd(test_project) - on.exit(setwd(oldwd), add = TRUE) +test_that("Test migration", { + test_project <- tempfile("test_project") + suppressMessages(create.project(basename(test_project), project.directory = dirname(test_project))) + on.exit(unlink(test_project, recursive = TRUE), add = TRUE) + oldwd <- setwd(test_project) + on.exit(setwd(oldwd), add = TRUE) - config <- .new.config - expect_true("version" %in% names(config)) - config$version <- '0.4' - write.dcf(config, 'config/global.dcf') - suppressMessages(migrate.project()) - expect_equal(sum(grepl("^version: ", readLines('config/global.dcf'))), 1) - expect_warning(suppressMessages(load.project()), NA) + config <- .new.config + expect_true("version" %in% names(config)) + config$version <- "0.4" + write.dcf(config, "config/global.dcf") + suppressMessages(migrate.project()) + expect_equal(sum(grepl("^version: ", readLines("config/global.dcf"))), 1) + expect_warning(suppressMessages(load.project()), NA) })