forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add strict_env and unstrict_env (direnv#572)
Allows the gradual introduction of `set -euo pipefail` into envrc processing, as discussed in direnv#566, as well as an escape hatch for commands that must be run in the envrc context that do not meet `set -euo pipefail` requirements.
- Loading branch information
1 parent
08f9af4
commit f2f2b92
Showing
6 changed files
with
349 additions
and
2 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
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,67 @@ | ||
#! /usr/bin/env bash | ||
|
||
# Note: this is _explicitly_ not setting `set -euo pipefail` | ||
# because we are testing functions that configure that. | ||
|
||
declare base expected actual | ||
base="${TMPDIR:-/tmp}/$(basename "$0").$$" | ||
expected="$base".expected | ||
actual="$base".actual | ||
|
||
declare -i success | ||
success=0 | ||
|
||
# always execute relative to here | ||
cd "$(dirname "$0")" || exit 1 | ||
|
||
# add the built direnv to the path | ||
root=$(cd .. && pwd -P) | ||
export PATH=$root:$PATH | ||
|
||
load_stdlib() { | ||
# shellcheck disable=SC1090 | ||
source "$root/stdlib.sh" | ||
} | ||
|
||
test_fail() { | ||
echo "FAILED $*: expected is not actual" | ||
exit 1 | ||
} | ||
|
||
test_strictness() { | ||
local step | ||
step="$1" | ||
echo "$2" > "$expected" | ||
|
||
set -o | grep 'errexit\|nounset\|pipefail' > "$actual" | ||
diff -u "$expected" "$actual" || test_fail "$step" | ||
(( success += 1 )) | ||
} | ||
|
||
load_stdlib | ||
|
||
test_strictness 'after source' $'errexit off | ||
nounset off | ||
pipefail off' | ||
|
||
strict_env | ||
test_strictness 'after strict_env' $'errexit on | ||
nounset on | ||
pipefail on' | ||
|
||
unstrict_env echo HELLO > /dev/null | ||
test_strictness "after unstrict_env with command" $'errexit on | ||
nounset on | ||
pipefail on' | ||
|
||
strict_env echo HELLO > /dev/null | ||
test_strictness "after strict_env with command" $'errexit on | ||
nounset on | ||
pipefail on' | ||
|
||
unstrict_env | ||
test_strictness 'after unstrict_env' $'errexit off | ||
nounset off | ||
pipefail off' | ||
|
||
echo "OK ($success tests)" |