Skip to content

Commit

Permalink
fix tests + add test for error if both filenames present
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Kusumgar committed Jan 24, 2024
1 parent e9871a7 commit 01649f4
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions tests/testthat/test-orderly.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ test_that("can create empty orderly report", {
path <- test_prepare_orderly_example(character())
expect_message(
orderly_new("foo", root = path),
"Created 'src/foo/orderly.R'")
path_orderly <- file.path(path, "src", "foo", "orderly.R")
"Created 'src/foo/foo.R'")
path_orderly <- file.path(path, "src", "foo", "foo.R")
expect_true(file.exists(path_orderly))
txt <- readLines(path_orderly)
expect_match(txt[[1]], "This is an orderly script")
Expand All @@ -47,19 +47,31 @@ test_that("can create a totally blank orderly report", {
path <- test_prepare_orderly_example(character())
expect_message(
orderly_new("foo", template = FALSE, root = path),
"Created 'src/foo/orderly.R'")
path_orderly <- file.path(path, "src", "foo", "orderly.R")
"Created 'src/foo/foo.R'")
path_orderly <- file.path(path, "src", "foo", "foo.R")
expect_true(file.exists(path_orderly))
expect_equal(readLines(path_orderly), character())
})


test_that("error if orderly.R exists already", {
path <- test_prepare_orderly_example("data")
expect_error(orderly_new("data", root = path),
"'src/data/orderly.R' already exists")
expect_error(orderly_new("data", force = TRUE, root = path),
"'src/data/orderly.R' already exists")
test_that("error if <reportname>.R exists already", {
path <- test_prepare_orderly_example("reportname-orderly-file")
expect_error(orderly_new("reportname-orderly-file", root = path),
paste("'src/reportname-orderly-file'",
"already contains one of the following files:",
"reportname-orderly-file.R, orderly.R"))
expect_error(orderly_new("reportname-orderly-file", force = TRUE, root = path),
paste("'src/reportname-orderly-file'",
"already contains one of the following files:",
"reportname-orderly-file.R, orderly.R"))
})


test_that("error if <reportname>.R and orderly.R both exist", {
path <- test_prepare_orderly_example("bad-ex-two-orderly-files")
expect_error(orderly_run("bad-ex-two-orderly-files", root = path),
paste("Please create only ONE of bad-ex-two-orderly-files.R,",
"orderly.R files"))
})


Expand All @@ -75,7 +87,7 @@ test_that("error if a non-directory file is found in the src dir", {
})


test_that("allow creation of orderly.R in existing dir if force is given", {
test_that("allow creation of <reportname>.R in existing dir if force is given", {
path <- test_prepare_orderly_example(character())
fs::dir_create(file.path(path, "src", "foo"))
file.create(file.path(path, "src", "foo", "bar"))
Expand All @@ -84,22 +96,22 @@ test_that("allow creation of orderly.R in existing dir if force is given", {
"'src/foo/' already exists and contains files")
expect_equal(
err$body,
c(i = paste("If you want to add an orderly.R to this directory,",
c(i = paste("If you want to add a foo.R to this directory,",
"rerun `orderly_new()` with `force = TRUE`")))
expect_message(
orderly_new("foo", force = TRUE, root = path),
"Created 'src/foo/orderly.R'")
expect_true(file.exists(file.path(path, "src/foo/orderly.R")))
"Created 'src/foo/foo.R'")
expect_true(file.exists(file.path(path, "src/foo/foo.R")))
})


test_that("allow creation of orderly.R in existing empty dir", {
test_that("allow creation of <reportname>.R in existing empty dir", {
path <- test_prepare_orderly_example(character())
fs::dir_create(file.path(path, "src", "foo"))
expect_message(
orderly_new("foo", root = path),
"Created 'src/foo/orderly.R'")
expect_true(file.exists(file.path(path, "src/foo/orderly.R")))
"Created 'src/foo/foo.R'")
expect_true(file.exists(file.path(path, "src/foo/foo.R")))
})


Expand Down

0 comments on commit 01649f4

Please sign in to comment.