Comments (3)
@CyberShadow -- looks like this is due to the fact that Bashcov exports SHELLOPTS=xtrace
into Bash's environment:
cat > child.sh <<'EOF'
#!/usr/bin/env bash
printf 1>&2 '%s: SHELLOPTS=%s\n' "${BASH_SOURCE[0]}" "$SHELLOPTS"
echo $unset
echo "Survived!"
EOF
cat > parent.sh <<'EOF'
#!/usr/bin/env bash
set -eu
printf 1>&2 '%s: SHELLOPTS=%s\n' "${BASH_SOURCE[0]}" "$SHELLOPTS"
./child.sh
EOF
chmod +x parent.sh child.sh
Works as expected when run without touching SHELLOPTS
:
$ ./parent.sh
./parent.sh: SHELLOPTS=braceexpand:errexit:hashall:interactive-comments:nounset
./child.sh: SHELLOPTS=braceexpand:hashall:interactive-comments
Survived!
Also works as expected when run with bash -x
:
$ bash -x ./parent.sh
+ set -eu
+ printf '%s: SHELLOPTS=%s\n' ./parent.sh braceexpand:errexit:hashall:interactive-comments:nounset:xtrace
./parent.sh: SHELLOPTS=braceexpand:errexit:hashall:interactive-comments:nounset:xtrace
+ ./child.sh
./child.sh: SHELLOPTS=braceexpand:hashall:interactive-comments
Survived!
But, when called with called with env SHELLOPTS=xtrace
:
$ env SHELLOPTS=xtrace ./parent.sh
+ set -eu
+ printf '%s: SHELLOPTS=%s\n' ./parent.sh braceexpand:errexit:hashall:interactive-comments:nounset:xtrace
./parent.sh: SHELLOPTS=braceexpand:errexit:hashall:interactive-comments:nounset:xtrace
+ ./child.sh
+ printf '%s: SHELLOPTS=%s\n' ./child.sh braceexpand:errexit:hashall:interactive-comments:nounset:xtrace
./child.sh: SHELLOPTS=braceexpand:errexit:hashall:interactive-comments:nounset:xtrace
./child.sh: line 3: unset: unbound variable
Interestingly, it looks like the issue crops up whenever the parent explicitly defines SHELLOPTS
, not just when it enables xtrace
:
$ env SHELLOPTS=emacs ./parent.sh
./parent.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: line 3: unset: unbound variable
This behavior goes back a while:
matt@ratmaster /tmp/tmp.LXRydMzQPe :( $ for bv in 3.2 4.0 4.1 4.2 4.3 4.4 5.0; do printf 1>&2 -- '# %s\n' "$bv"; docker run -it --rm -v "${PWD}:/shellopts" --workdir /shellopts -u "$(id -u):$(id -g)" "bash:${bv}" env SHELLOPTS=emacs bash /shellopts/parent.sh; done
# 3.2
/shellopts/parent.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: line 3: unset: unbound variable
# 4.0
/shellopts/parent.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: line 3: unset: unbound variable
# 4.1
/shellopts/parent.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: line 3: unset: unbound variable
# 4.2
/shellopts/parent.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: line 3: unset: unbound variable
# 4.3
/shellopts/parent.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: line 3: unset: unbound variable
# 4.4
/shellopts/parent.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: line 3: unset: unbound variable
# 5.0
/shellopts/parent.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: SHELLOPTS=braceexpand:emacs:errexit:hashall:interactive-comments:nounset
./child.sh: line 3: unset: unbound variable
Hypothesis: using env SHELLOPTS=xtrace
results in the equivalent of set -x; export SHELLOPTS
, causing child processes of Bash to inherit SHELLOPTS
. Further adjustments of SHELLOPTS
(e.g., set -eu
) are now inherited by child processes, too. So, revising ./parent.sh
to run export SHELLOPTS
:
cat > child.sh <<'EOF'
#!/usr/bin/env bash
printf 1>&2 '%s: SHELLOPTS=%s\n' "${BASH_SOURCE[0]}" "$SHELLOPTS"
echo $unset
echo "Survived!"
EOF
cat > parent.sh <<'EOF'
#!/usr/bin/env bash
set -eu
printf 1>&2 '%s: SHELLOPTS=%s\n' "${BASH_SOURCE[0]}" "$SHELLOPTS"
export SHELLOPTS
./child.sh
EOF
chmod +x parent.sh child.sh
Results in:
$ ./parent.sh
./parent.sh: SHELLOPTS=braceexpand:errexit:hashall:interactive-comments:nounset
./child.sh: SHELLOPTS=braceexpand:errexit:hashall:interactive-comments:nounset
./child.sh: line 3: unset: unbound variable
At present, there is not much Bashcov can do about this issue -- setting SHELLOPTS=xtrace
is essential to how it gathers coverage data for all shell scripts under test. A while back, I started working on a branch that globally enables executing tracing by setting BASH_ENV
to the path of a script that runs (more or less) set -x
, rather than by setting SHELLOPTS=xtrace
. I've left this effort on the back burner for the last several months, but may be able to find some time in the coming weeks to complete it.
from bashcov.
Thanks. Would it make sense to make bashcov do the equivalent of bash -c "set -x ; source $script"
instead of env SHELLOPTS=xtrace $script
?
from bashcov.
Would it make sense to make bashcov do the equivalent of bash -c "set -x ; source $script" instead of env SHELLOPTS=xtrace $script ?
I think this could be implemented as an optional execution mode, with a conspicuous note that using this mode may limit the number of scripts Bashcov is able to gather coverage data for, since (unlike env SHELLOPTS=xtrace
) set -x
doesn't globally enable execution tracing.
from bashcov.
Related Issues (20)
- Option to suppress all bashcov output HOT 5
- Error when used with bats HOT 3
- parse_hit fails
- 0% coverage with BATS on Mac HOT 1
- Is bashcov concurrency safe? HOT 1
- bashcov output should be silent by default HOT 3
- Using PS4 in tested script breaks coverage results HOT 1
- Capturing coverage on sourced script functions executed by bashunit
- `--bash-path` is not respected for `BASH_VERSION` detection HOT 1
- [question] bash 'compiler' HOT 1
- bashcov HOT 1
- Bashcov failing with LINENO related error. Bash is indeed above 4.4 (that was the recommendation till now to this error) HOT 1
- Are you planning a new release? HOT 3
- please rewrite this in bash HOT 2
- How to install on RHEL UBI8? HOT 1
- How to ignore files and folders in bashcov in GitLab-CI yaml? HOT 1
- Use of mutation testing in bashcov - Help needed
- Function definition line missed HOT 4
- Latest release April 2023 No longer has any coverage in report HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bashcov.