Skip to content

Commit

Permalink
Handle errors getting Git global user name and email
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau-lilly committed Sep 6, 2022
1 parent a614c0d commit a4c74b7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# gittargets 0.0.5

* Use `processx::run()` instead of `system2()` in `tar_git_ok()` and set `HOME` to `USERPROFILE` on Windows (#12, @psychelzh).
* Handle errors invoking Git to get global user name and email.

# gittargets 0.0.4

Expand Down
17 changes: 15 additions & 2 deletions R/tar_git_ok.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@
tar_git_ok <- function(verbose = TRUE) {
binary <- tar_git_binary()
cli_success("Git binary: {.file ", binary, "}", verbose = verbose)
user_name <- tar_git_config_global_user_name()
success <- tryCatch({
user_name <- tar_git_config_global_user_name()
user_email <- tar_git_config_global_user_email()
TRUE
}, error = function(condition) {
cli_danger(
"Error getting Git global user name and email:",
verbose = verbose
)
cli_danger(conditionMessage(condition), verbose = verbose)
FALSE
})
if (!success) {
return(FALSE)
}
if_any(
nzchar(user_name),
cli_success(
Expand All @@ -30,7 +44,6 @@ tar_git_ok <- function(verbose = TRUE) {
verbose = verbose
)
)
user_email <- tar_git_config_global_user_email()
if_any(
nzchar(user_email),
cli_success(
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-tar_git_ok.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@ targets::tar_test("tar_git_ok()", {
expect_message(tar_git_ok(verbose = TRUE))
expect_silent(tar_git_ok(verbose = FALSE))
})

targets::tar_test("tar_git_ok()", {
skip_os_git()
old <- Sys.getenv("TAR_GIT", unset = Sys.which("git"))
on.exit(Sys.setenv(TAR_GIT = old))
tmp <- tempfile()
file.create(tmp)
Sys.setenv(TAR_GIT = tmp)
expect_false(tar_git_ok())
expect_message(tar_git_ok(verbose = TRUE))
expect_silent(tar_git_ok(verbose = FALSE))
})

0 comments on commit a4c74b7

Please sign in to comment.