123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #!/bin/bash
-
- . "$TF_DIR/include/subtest.sh"
-
- tf_enum_subtests() {
- echo "import_fn"
- echo "import_var"
- echo "import_var_fn"
- echo "try_import_fn"
- echo "try_import_var"
- echo "try_import_var_fn"
- echo "import_var_fn_bash"
- echo "import_var_fn_bash_hint"
- }
-
- ignored() {
- #
- # Things generated by listing code we want to strip
- #
- # This is list of lines that we expect to appear as new after
- # running the import test since they are a known part of the
- # shellfu itself
- #
- echo declare -f shellfu
- echo SHELLFU_COMPAT
- echo SHELLFU_DEBUGINIT
- echo SHELLFU_EMBEDDED
- echo SHELLFU_EMBEDTMP
- echo SHELLFU_DIR
- echo SHELLFU_GIT_HASH
- echo SHELLFU_INCLUDE
- echo SHELLFU_PATH
- echo SHELLFU_PRETTY
- echo SHELLFU_VERSION
- echo __SHELLFU_IMPORTED
- }
-
- mksnippet() {
- #
- # Produce a snippet to store env info to a file $1
- #
- # File _ignored_ is used as list of tokens to always
- # remove from the info.
- #
- local target="$1"
- printf "
- {
- (set -o posix; set) \\
- | grep -e '^[a-zA-Z_]' \\
- | sed -e 's/=.*//'
- declare -F
- } | sort | grep -vxf _ignored_ > %s
- " "$target"
- }
-
- wrap_test() {
- #
- # Assemble and print test script code
- #
- # Assemble test script by including scanning snippet,
- # importing code from file $1.test, and snippet again. If
- # run, the code printed by this function will generate two
- # files: $1.before and $1.after.
- #
- local name="$1"
- if head -1 "test/$name.sh" | grep '^#!';
- then
- # shebang already "leaked"; add snippet and the rest
- mksnippet "$name.before"
- tail -n +2 "test/$name.sh"
- else
- mksnippet "$name.before"
- cat "test/$name.sh"
- fi \
- | sed -e "s|\\<sfpath\\>|SHELLFU_DIR=${shellfu_init%/*} $sfpath|"
- # ^^^ make `sfpath` work even if PATH is empty:
- mksnippet "$name.after"
- }
-
- tf_do_subtest() {
- #
- # Prepare, run and check results of test $1
- #
- # Contents of $1 file (which is supposed to be importing
- # code) are altered for testing and wrapped so that on
- # execution in clean environment, two files are produced to
- # hold sets of detected variable and function names--before
- # import and after it.
- #
- # The difference of the sets (i.e. what we have *really*
- # imported) must be same as oracle/$1.new
- #
- local name="$1"
- wrap_test "$name" > "$name.sh"
- mkdir -p result
- chmod +x "$name.sh"
- env -i ./"$name.sh"
- comm -13 "$name.before" "$name.after" > "result/$name.new"
- # i.e. lines introduced by import ^^
- diff -u "oracle/$name.new" "result/$name.new"
- }
-
- export LANG=C
- sfpath=$(which sfpath) || tf_exit_error "cannot find sfpath"
- shellfu_init=$(sfpath) || tf_exit_error "sfpath failed"
- ignored > _ignored_
-
- tf_do_subtests
|