stefan2904 / shunit2

Automatically exported from code.google.com/p/shunit2
0 stars 0 forks source link

Don't assume unbound variables (set -u) are in effect outside of shunit2 #3

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I don't think shunit2 (svn version of July 16 '08) should assume code
outside of itself has set -u (no unbound variables) set. Otherwise the
behavior of the code one is testing will be different inside the test
versus when it is run not by shunit2. And although I think it wonderful for
people to write code using set -u alas most of the shell code that is
written doesn't (yet).

Suppose my shell code is for example:

  while [[ -n $1 ]] ; do
    # Do something with $1
    shift
  done

If this code gets called via shunit2, you will get an error. If it isn't
and set +u is in effect, this code works fine. That said, there are ways
rewriting the code that work using set -u such as:

  while (( $# > 0 )) ; do
    # Do something with $1
    shift
  done

but I don't think shunit2 should be enforcing programming behavior.

A fix here would be to save $- inside shunit2, "set -u"  and restore it
before running. Or remove setting set -u altogether, and add it in as a
unit test of the shunit2 code.

Original issue reported on code.google.com by rocky.bernstein on 17 Jul 2008 at 1:39

GoogleCodeExporter commented 9 years ago
fixed in rev# 211. moved to the unit tests as shUnit2 does not need to require 
this as they will be caught with 
asserts anyway when failures arrise.

Original comment by kate.war...@gtempaccount.com on 17 Jul 2008 at 2:34