-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97b07bb
commit 7e826ea
Showing
6 changed files
with
93 additions
and
0 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
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,36 @@ | ||
#' @rdname nix | ||
#' @title Detect `Nix` Shell | ||
#' | ||
#' @description `using_nix_shell()` checks whether code is running within an | ||
# nolint start | ||
#' environment defined by a [Nix expression](https://nixos.org/manual/nix/unstable/command-ref/nix-shell.html). | ||
# nolint end | ||
#' | ||
#' @param pure Whether or not the environment is pure, meaning most environment | ||
#' variables have been cleared before the shell started. | ||
#' | ||
#' @return A logical value | ||
#' @export | ||
#' | ||
#' @examples | ||
#' # Check for Nix | ||
#' using_nix_shell() | ||
#' | ||
#' # Check for Nix in a pure environment | ||
#' using_nix_shell(pure = TRUE) | ||
using_nix_shell <- function(pure = NULL) { | ||
if (!rlang::is_logical(pure, n = 1) && !rlang::is_null(pure)) { | ||
rlang::abort( | ||
message = "`pure` should be a logical scalar", | ||
class = "arg_not_scalar_logical" | ||
) | ||
} | ||
|
||
if (isTRUE(pure)) { | ||
using_envvar("IN_NIX_SHELL", "pure") | ||
} else if (isFALSE(pure)) { | ||
using_envvar("IN_NIX_SHELL", "impure") | ||
} else { | ||
using_envvar("IN_NIX_SHELL") | ||
} | ||
} |
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 |
---|---|---|
|
@@ -73,4 +73,5 @@ reference: | |
desc: Additional checks | ||
contents: | ||
- using_databricks | ||
- using_nix_shell | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,27 @@ | ||
test_that("using_nix_shell validates input properly", { | ||
expect_error(using_nix_shell(pure = NA_character_)) | ||
expect_error(using_nix_shell(pure = 1)) | ||
expect_error(using_nux_shell(pure = "pure")) | ||
expect_error(using_nix_shell(pure = c(TRUE, TRUE))) | ||
}) | ||
|
||
test_that("using_nix_shell works as expected when `IN_NIX_SHELL` not set", { | ||
withr::local_envvar(list("IN_NIX_SHELL" = NA)) | ||
expect_false(using_nix_shell()) | ||
expect_false(using_nix_shell(pure = TRUE)) | ||
expect_false(using_nix_shell(pure = FALSE)) | ||
}) | ||
|
||
test_that("using_nix_shell works as expected in pure environment", { | ||
withr::local_envvar(list("IN_NIX_SHELL" = "pure")) | ||
expect_true(using_nix_shell()) | ||
expect_true(using_nix_shell(pure = TRUE)) | ||
expect_false(using_nix_shell(pure = FALSE)) | ||
}) | ||
|
||
test_that("using_nix_shell works as expected in impure environment", { | ||
withr::local_envvar(list("IN_NIX_SHELL" = "impure")) | ||
expect_true(using_nix_shell()) | ||
expect_false(using_nix_shell(pure = TRUE)) | ||
expect_true(using_nix_shell(pure = FALSE)) | ||
}) |