Skip to content

Commit

Permalink
refactor: ♻️ ensure that if a dir only has .git/, continue with cre…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
lwjohnst86 committed Dec 13, 2024
1 parent 1c84af6 commit 978b85d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
22 changes: 16 additions & 6 deletions R/setup_project.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ setup_project <-
}

if (fs::dir_exists(proj_path)) {
cli::cli_abort(c(
"The {.val {proj_path}} folder already exists, so project creation is canceled.",
"i" = "Delete the folder or use another name (not {.val {proj_name}}) for your project."
))
if (has_only_git(proj_path)) {
cli::cli_alert_info("The project already has Git added, so we will skip setting up Git.")
} else {
cli::cli_abort(c(
"The {.val {proj_path}} folder already exists, so project creation is canceled.",
"i" = "Delete the folder or use another name (not {.val {proj_name}}) for your project."
))
}
}
proj_template <- find_template("projects", "basic-analysis")
fs::dir_copy(proj_template, new_path = proj_path)
fs::dir_copy(proj_template, new_path = proj_path, overwrite = TRUE)

withr::with_dir(
new = proj_path,
Expand All @@ -45,11 +49,17 @@ setup_project <-
fs::file_delete("template-Rproj")
fs::file_move("gitignore", ".gitignore")
update_template("README.md", data = list(ProjectName = proj_name))
gert::git_init()
if (!fs::dir_exists(".git")) {
gert::git_init()
}
}
)
}

has_only_git <- function(path) {
all(basename(fs::dir_ls(path, all = TRUE)) == ".git")
}

# Git setup functions -------------------------------------------

#' Setup Git to the project.
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-projects.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,16 @@ test_that("project checks work correctly", {
expect_true(fs::file_exists(sub("test new", "test-new", proj_with_space)))
fs::dir_delete(sub("test new", "test-new", proj_with_space))
})

test_that("project still creates if Git already exists", {
new_project <- fs::path_temp("test-with-git")
fs::dir_create(new_project)
withr::with_dir(new = new_project, {
gert::git_init()
})
expect_message(
setup_project(new_project),
regexp = ".*has [Gg]it.*"
)
expect_true(fs::dir_exists(new_project))
})

0 comments on commit 978b85d

Please sign in to comment.