-
Notifications
You must be signed in to change notification settings - Fork 206
Wrap the code in ( ) chars to execute it in a sub-shell (e.g. https://github.com/kward/shflags/blob/master/shflags_public_test.sh#L51).
(
FLAGS_HELP='this is a test'
FLAGS "${flag}" >"${stdoutF}" 2>"${stderrF}"
)
Possibly… it depends on your shell.
Different shells behave differently, so there is no one method that always works. The main documentation provides information on including line numbers in asserts using macros. This is the most portable solution if portability is what you need.
You are of course always welcome to use the ${LINENO}
variable in your assert messages. For example, this script…
#!/bin/sh
testMyThing() {
assertEquals "[${LINENO}] same, but different" 1 2
return 0
}
. ./shunit2
produces the following output when run.
$ sh blah_test.sh
testMyThing
ASSERT:[4] same, but different expected:<1> but was:<2>
Ran 1 test.
FAILED (failures=1)
No, hyphens are not supported.
shUnit2 was designed to be as portable as possible, which means it tries to support POSIX wherever possible. Hyphens are not part of the POSIX standard for shells for the Function Definition Command as a shell Name requires characters from the Portable Character Set.
These functions are created as executables in the temporary directory that is created for each shUnit2 execution. When a shell script makes a call to a function, but that function doesn't exist, the shell will look in the current PATH
for a command of the same name. This fact is taken advantage of by shUnit2 so that any provided function is called, but if they don't exist, no error is generated because a command of the same name also exists.
Detail
The _shunit_mktempFunc()
(L939) function generates stub functions that are written to disk as executables.
_shunit_mktempFunc() {
for _shunit_func_ in oneTimeSetUp oneTimeTearDown setUp tearDown suite noexec
do
_shunit_file_="${__shunit_tmpDir}/${_shunit_func_}"
command cat <<EOF >"${_shunit_file_}"
#! /bin/sh
exit ${SHUNIT_TRUE}
EOF
command chmod +x "${_shunit_file_}"
done
unset _shunit_file_
}
The function is called as part of the main of shUnit2 during startup (L1302) to create the stub executables.