Bash · 1625 bytes Raw Blame History
1 #!/bin/sh
2 TEST_PREFIX="[export-unset]"
3 . "$(cd "$(dirname "$0")" && pwd)/test_harness.sh"
4
5 section "1. export"
6 compare_output "export VAR=value" 'export MYVAR=hello; echo $MYVAR'
7 compare_output "export preserves in subshell" 'export MYVAR=hello; '"$BASH_REF"' -c "echo \$MYVAR"'
8 compare_output "export without value marks for export" 'MYVAR=test; export MYVAR; '"$BASH_REF"' -c "echo \$MYVAR"'
9 compare_output "export multiple vars" 'export A=1 B=2 C=3; echo $A $B $C'
10 compare_exit "export -p succeeds" 'export -p'
11 compare_output "export overwrites existing" 'export V=old; export V=new; echo $V'
12 compare_output "unexported var not in subshell" 'MYVAR=local; '"$BASH_REF"' -c "echo \${MYVAR:-empty}"'
13 compare_output "export -n unexports variable" 'export MYVAR=hello; export -n MYVAR; '"$BASH_REF"' -c "echo \${MYVAR:-gone}"'
14
15 section "2. unset"
16 compare_output "unset removes variable" 'MYVAR=hello; unset MYVAR; echo ">${MYVAR}<"'
17 compare_exit "unset nonexistent var succeeds" 'unset NONEXISTENT_VAR_XYZ_99'
18 compare_output "unset -v removes variable" 'MYVAR=hello; unset -v MYVAR; echo ">${MYVAR}<"'
19 compare_output "unset -f removes function" 'f() { echo hi; }; unset -f f; f 2>/dev/null; echo $?'
20 compare_output "unset multiple vars" 'A=1; B=2; C=3; unset A C; echo ">${A}< ${B} >${C}<"'
21 compare_output "unset array" 'arr=(a b c); unset arr; echo ">${arr[@]}<"'
22
23 section "3. printenv"
24 compare_output "printenv specific var" 'export MYVAR=hello; printenv MYVAR'
25 compare_exit "printenv nonexistent var fails" 'printenv NONEXISTENT_VAR_XYZ_99'
26 compare_exit "printenv with no args succeeds" 'printenv >/dev/null'
27
28 print_summary