File: strict_env_test.bash

package info (click to toggle)
direnv 2.37.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 872 kB
  • sloc: sh: 1,499; csh: 83; makefile: 7
file content (67 lines) | stat: -rwxr-xr-x 1,438 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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)"