diff --git a/bash-env-json b/bash-env-json index 8f2f5fe..c8c19ef 100755 --- a/bash-env-json +++ b/bash-env-json @@ -40,12 +40,16 @@ function capture() { local -n _capture_env="$1" local -n _capture_shellvars="$2" local _name _value - # some shell variables leak into our environment, so inhibit that: - local -A _inhibit_shellvars=([_value]=X [BASH_LINENO]=X [COLUMNS]=X [LINES]=X) + # inhibit env vars which are side-effects of cd + local -A _inhibit_envvars=([PWD]=X [OLDPWD]=X) + # some shell variables leak into our environment, so inhibit those, along with side-effects of cd + local -A _inhibit_shellvars=([_value]=X [BASH_LINENO]=X [COLUMNS]=X [LINES]=X [DIRSTACK]=X) # environment variables while IFS='=' read -r -d '' _name _value; do - _capture_env["$_name"]="${_value}" + if test ! "${_inhibit_envvars[$_name]+EXISTS}"; then + _capture_env["$_name"]="${_value}" + fi done < <(_env -0) # shellvars @@ -54,7 +58,7 @@ function capture() { set | _sed -n -e '/^[a-zA-Z_][a-zA-Z_0-9]*=/s/=.*$//p' set +o posix ); do - if test -v "$_name" -a ! "${_capture_env[$_name]+EXISTS}" -a ! "${_inhibit_shellvars[$_name]+EXISTS}"; then + if test -v "$_name" -a ! "${_capture_env[$_name]+EXISTS}" -a ! "${_inhibit_envvars[$_name]+EXISTS}" -a ! "${_inhibit_shellvars[$_name]+EXISTS}"; then _capture_shellvars["$_name"]="${!_name}" fi done diff --git a/tests.bats b/tests.bats index 243e390..530247a 100755 --- a/tests.bats +++ b/tests.bats @@ -51,3 +51,7 @@ function test_case() { @test "shell-function-error" { test_case shell-function-error --shellfns f } + +@test "cd-does-nothing" { + test_case cd-does-nothing +} diff --git a/tests/cd-does-nothing.env b/tests/cd-does-nothing.env new file mode 100644 index 0000000..d4f0f11 --- /dev/null +++ b/tests/cd-does-nothing.env @@ -0,0 +1 @@ +cd ${TMPDIR:-/tmp} diff --git a/tests/cd-does-nothing.json b/tests/cd-does-nothing.json new file mode 100644 index 0000000..f029e12 --- /dev/null +++ b/tests/cd-does-nothing.json @@ -0,0 +1,4 @@ +{ + "env": {}, + "shellvars": {} +} diff --git a/tests/cd-does-nothing.setup.env b/tests/cd-does-nothing.setup.env new file mode 100644 index 0000000..e69de29