-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create helper for getting key & checking for existance (#34)
Mostly prospective change to better support incoming gemini and claude wrappers.
- Loading branch information
Showing
6 changed files
with
39 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
is_testing <- function() { | ||
identical(Sys.getenv("TESTTHAT"), "true") | ||
} | ||
|
||
key_get <- function(name, error_call = caller_env()) { | ||
val <- Sys.getenv(name) | ||
if (!identical(val, "")) { | ||
val | ||
} else { | ||
if (is_testing()) { | ||
testthat::skip(sprintf("%s env var is not configured", name)) | ||
} else { | ||
cli::cli_abort("Can't find env var {.code {name}}.", call = error_call) | ||
} | ||
} | ||
} | ||
|
||
key_exists <- function(name) { | ||
!identical(Sys.getenv(name), "") | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# informative error if no key | ||
|
||
Code | ||
key_get("FOO") | ||
Condition | ||
Error: | ||
! Can't find env var `FOO`. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
test_that("finds key if set", { | ||
withr::local_envvar(FOO = "abc123") | ||
expect_true(key_exists("FOO")) | ||
expect_equal(key_get("FOO"), "abc123") | ||
}) | ||
|
||
|
||
test_that("informative error if no key", { | ||
withr::local_envvar(FOO = NULL, TESTTHAT = "false") | ||
expect_false(key_exists("FOO")) | ||
expect_snapshot(key_get("FOO"), error = TRUE) | ||
}) |