Git Product home page Git Product logo

Comments (3)

tomeon avatar tomeon commented on May 18, 2024

@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.

CyberShadow avatar CyberShadow commented on May 18, 2024

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.

tomeon avatar tomeon commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.