Skip to content

Commit

Permalink
Re-normalize path, after it is guaranteed to exist (#228)
Browse files Browse the repository at this point in the history
* Add failing test

* Re-normalize path, after it is guaranteed to exist

Fixes #227

* Extend fix and test to non-package project case

* NEWS bullet
  • Loading branch information
jennybc authored Jan 26, 2018
1 parent 367fe17 commit 606e5c8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# usethis 1.2.0.9000

* `create_package()` and `create_project()` return a normalized path, even if target directory does not pre-exist (#227, #228).

# usethis 1.2.0

## New functions
Expand Down
6 changes: 6 additions & 0 deletions R/create.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ create_package <- function(path,

create_directory(dirname(path), name)
cat_line(crayon::bold("Changing active project to", crayon::red(name)))
## the initial normalizePath() may not have returned an absolute path,
## if the path did not yet exist
path <- normalizePath(path, mustWork = TRUE)
proj_set(path, force = TRUE)

use_directory("R")
Expand Down Expand Up @@ -55,6 +58,9 @@ create_project <- function(path,

create_directory(dirname(path), name)
cat_line(crayon::bold("Changing active project to", crayon::red(name)))
## the initial normalizePath() may not have returned an absolute path,
## if the path did not yet exist
path <- normalizePath(path, mustWork = TRUE)
proj_set(path, force = TRUE)

use_directory("R")
Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/test-create.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,29 @@ test_that("nested project is disallowed, by default", {
subdir <- create_directory(dir, "subdir")
expect_error(scoped_temporary_project(subdir), "nested")
})

## https://github.com/r-lib/usethis/issues/227
test_that("proj is normalized when path does not pre-exist", {
## take care to provide a **non-absolute** path
path_package <- basename(tempfile())
withr::with_dir(
tempdir(), {
old_proj <- proj_get()
capture_output(create_package(path_package, rstudio = FALSE, open = FALSE))
new_proj <- proj_get()
proj_set(old_proj)
}
)
expect_true(dir.exists(new_proj))

path_project <- basename(tempfile())
withr::with_dir(
tempdir(), {
old_proj <- proj_get()
capture_output(create_project(path_project, rstudio = FALSE, open = FALSE))
new_proj <- proj_get()
proj_set(old_proj)
}
)
expect_true(dir.exists(new_proj))
})

0 comments on commit 606e5c8

Please # to comment.