Skip to content

Commit

Permalink
Attempt to fix errors I see on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolepore committed Aug 16, 2024
1 parent 3de7fd8 commit 999bf5a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 59 deletions.
50 changes: 25 additions & 25 deletions R/git-auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -302,26 +302,6 @@ gitcreds_env <- function() {
)
}

#' Check if `git` is installed and can run
#'
#' If not installed, a `gitcreds_nogit_error` is thrown.
#'
#' @noRd
#' @return Nothing

check_for_git <- function() {
# This is simpler than Sys.which(), and also less fragile
has_git <- tryCatch({
suppressWarnings(system2(
"git", "--version",
stdout = TRUE, stderr = null_file()
))
TRUE
}, error = function(e) FALSE)

if (!has_git) throw(new_error("gitcreds_nogit_error"))
}

new_gitcreds <- function(...) {
structure(list(...), class = "gitcreds")
}
Expand Down Expand Up @@ -371,16 +351,12 @@ has_no_newline <- function(url) {
! grepl("\n", url, fixed = TRUE)
}

null_file <- function() {
if (get_os() == "windows") "nul:" else "/dev/null"
}

`%||%` <- function(l, r) if (is.null(l)) r else l

environment()
})

# FIXME: Moving these functions outside of `local()` to avoid
# Moving these functions outside of `local()` to avoid
# Error in `local_mocked_bindings()`:
# ! Can't find binding for `SOME_FUNCTION`

Expand Down Expand Up @@ -835,3 +811,27 @@ gitcreds_set_replace <- function(url, current) {

invisible()
}

#' Check if `git` is installed and can run
#'
#' If not installed, a `gitcreds_nogit_error` is thrown.
#'
#' @noRd
#' @return Nothing

check_for_git <- function() {
# This is simpler than Sys.which(), and also less fragile
has_git <- tryCatch({
suppressWarnings(system2(
"git", "--version",
stdout = TRUE, stderr = null_file()
))
TRUE
}, error = function(e) FALSE)

if (!has_git) throw(new_error("gitcreds_nogit_error"))
}

null_file <- function() {
if (get_os() == "windows") "nul:" else "/dev/null"
}
12 changes: 6 additions & 6 deletions R/list-creds.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,16 @@ gitcreds_list <- function(url = "https://github.com",

credential_helper <- credential_helper %||% gitcreds_list_helpers()
if (length(credential_helper) == 0) {
gitcreds$throw(gitcreds$new_error("gitcreds_no_helper"))
throw(new_error("gitcreds_no_helper"))
}
if (length(credential_helper) > 1) {
gitcreds$throw(gitcreds$new_warning("gitcreds_multiple_helpers"))
throw(gitcreds$new_warning("gitcreds_multiple_helpers"))
credential_helper <- credential_helper[[1]]
}

host <- NULL
if (!is.null(url)) {
purl <- gitcreds$parse_url(url)
purl <- parse_url(url)
if (!is.na(purl$host)) host <- purl$host
if (!is.na(purl$protocol)) protocol <- purl$protocol
}
Expand All @@ -142,7 +142,7 @@ gitcreds_list <- function(url = "https://github.com",
"osxkeychain" = gitcreds_list_osxkeychain(url, host, protocol),
"manager" = gitcreds_list_manager(url, host, protocol),
"manager-core" = gitcreds_list_manager_core(url, host, protocol),
gitcreds$throw(gitcreds$new_error(
throw(new_error(
"gitcreds_unknown_helper",
credential_helper = credential_helper,
message = sprintf(
Expand Down Expand Up @@ -222,7 +222,7 @@ is_manager_core_macos_item <- function(it, protocol, host) {
if (!grepl("^git:", it$attributes$service)) return(FALSE)
if (is.null(host)) return(TRUE)
iturl <- sub("^git:", "", it$attributes$service)
piturl <- gitcreds$parse_url(iturl)
piturl <- parse_url(iturl)
!is.na(piturl$host) && piturl$host == host &&
!is.na(piturl$protocol) && piturl$protocol == protocol
}
Expand All @@ -247,7 +247,7 @@ is_manager_core_win_item <- function(it, protocol, host) {
if (!grepl("^git:", it$target_name)) return(FALSE)
iturl <- sub("^git:", "", it$target_name)
if (is.null(host)) return(TRUE)
piturl <- gitcreds$parse_url(iturl)
piturl <- parse_url(iturl)
!is.na(piturl$host) && piturl$host == host &&
!is.na(piturl$protocol) && piturl$protocol == protocol
}
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ is_ci <- function() {
gc_test_that <- function(desc, code, os = NULL, helpers = NULL) {
if (!is_ci()) return()
if (!is.null(os)) {
if (! gitcreds$get_os() %in% os) return()
if (! get_os() %in% os) return()
}

if (is.null(helpers)) {
os <- gitcreds$get_os()
os <- get_os()
if (os == "windows") {
helpers <- c("manager-core")
} else if (os == "macos") {
Expand Down Expand Up @@ -43,11 +43,11 @@ gc_test_that_run <- function(desc, code) {
options(gitcreds_test_consent = TRUE)
}

if (gitcreds$get_os() == "windows") {
if (get_os() == "windows") {
cleanup_windows()
on.exit(cleanup_windows(), add = TRUE)
}
if (gitcreds$get_os() == "macos") {
if (get_os() == "macos") {
cleanup_macos()
on.exit(cleanup_macos(), add = TRUE)
}
Expand Down Expand Up @@ -139,8 +139,8 @@ clear_helpers <- function() {
local_helpers <- function(helpers, .local_envir = parent.frame()) {
withr::defer(clear_helpers(), envir = .local_envir)
clear_helpers()
gitcreds$git_run(c("config", "--global", "--add", "credential.helper", "\"\""))
git_run(c("config", "--global", "--add", "credential.helper", "\"\""))
for (helper in helpers) {
gitcreds$git_run(c("config", "--global", "--add", "credential.helper", helper))
git_run(c("config", "--global", "--add", "credential.helper", helper))
}
}
6 changes: 0 additions & 6 deletions tests/testthat/test-gitcreds-list.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,6 @@ gc_test_that("gitcreds_list_manager_core_macos", os = "macos",
"needs the `oskeyring` package"
)

# FIXME: Testing interactively I get
# Error in gitcreds$throw(gitcreds$new_error("gitcreds_unknown_helper", :
# Unknown credential helper: `foo`, cannot list credentials
# In addition: Warning message:
# In gitcreds$throw(gitcreds$new_warning("gitcreds_multiple_helpers")) :
# Multiple credential helpers, only using the first no credentials just yet
expect_equal(gitcreds_list(), list())

cred <- list(
Expand Down
22 changes: 11 additions & 11 deletions tests/testthat/test-username.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
gc_test_that("gitcreds_username_for_url", {
tmp <- withr::local_tempdir()
withr::local_dir(tmp)
gitcreds$git_run(c("init", "."))
gitcreds$git_run(c("config", "credential.username", "global"))
gitcreds$git_run(c("config", "credential.https://example.com.username", "spec"))
git_run(c("init", "."))
git_run(c("config", "credential.username", "global"))
git_run(c("config", "credential.https://example.com.username", "spec"))
expect_equal(gitcreds_username_for_url("https://example.com"), "spec")
expect_equal(gitcreds_username_for_url("https://foo.com"), "global")
expect_equal(gitcreds$gitcreds_username(), "global")
expect_equal(gitcreds$gitcreds_username("https://foo.com"), "global")
expect_equal(gitcreds$gitcreds_username("https://example.com"), "spec")
expect_equal(gitcreds_username(), "global")
expect_equal(gitcreds_username("https://foo.com"), "global")
expect_equal(gitcreds_username("https://example.com"), "spec")
})

gc_test_that("gitcreds$gitcreds_username_generic", {
gc_test_that("gitcreds_username_generic", {
tmp <- withr::local_tempdir()
withr::local_dir(tmp)
gitcreds$git_run(c("init", "."))
gitcreds$git_run(c("config", "credential.username", "global"))
gitcreds$git_run(c("config", "credential.https://example.com.username", "spec"))
expect_equal(gitcreds$gitcreds_username_generic(), "global")
git_run(c("init", "."))
git_run(c("config", "credential.username", "global"))
git_run(c("config", "credential.https://example.com.username", "spec"))
expect_equal(gitcreds_username_generic(), "global")
})

gc_test_that("errors", {
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ test_that("ack", {
})

gc_test_that("check_for_git", {
expect_silent(gitcreds$check_for_git())
expect_silent(check_for_git())

local_mocked_bindings(system2 = function(command, ...) {
system2(basename(tempfile()), ...)
})
expect_error(
gitcreds$check_for_git(),
check_for_git(),
class = "gitcreds_nogit_error"
)
})
Expand Down Expand Up @@ -104,8 +104,8 @@ test_that("throw", {
})

test_that("null_file", {
expect_silent(writeLines(letters, gitcreds$null_file()))
expect_equal(readLines(gitcreds$null_file()), character())
expect_silent(writeLines(letters, null_file()))
expect_equal(readLines(null_file()), character())
})

test_that("msg", {
Expand Down Expand Up @@ -206,7 +206,7 @@ test_that("is_interactive", {
})

gc_test_that("git_run", {
err <- tryCatch(gitcreds$git_run("qwertyzxcvbn"), error = function(x) x)
err <- tryCatch(git_run("qwertyzxcvbn"), error = function(x) x)
expect_s3_class(err, "git_error")
expect_s3_class(err, "gitcreds_error")
expect_match(conditionMessage(err), "System git failed:.*qwertyzxcvbn")
Expand Down

0 comments on commit 999bf5a

Please sign in to comment.