shell dot on steroids https://pagure.io/shellfu

TF_RUN 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/bash
  2. . "$TF_DIR/include/subtest.sh"
  3. tf_enum_subtests() {
  4. echo "import_fn"
  5. echo "import_var"
  6. echo "import_var_fn"
  7. echo "try_import_fn"
  8. echo "try_import_var"
  9. echo "try_import_var_fn"
  10. echo "import_var_fn_bash"
  11. echo "import_var_fn_bash_hint"
  12. }
  13. ignored() {
  14. #
  15. # Things generated by listing code we want to strip
  16. #
  17. # This is list of lines that we expect to appear as new after
  18. # running the import test since they are a known part of the
  19. # shellfu itself
  20. #
  21. echo declare -f shellfu
  22. echo SHELLFU_COMPAT
  23. echo SHELLFU_DEBUGINIT
  24. echo SHELLFU_EMBEDDED
  25. echo SHELLFU_EMBEDTMP
  26. echo SHELLFU_DIR
  27. echo SHELLFU_GIT_HASH
  28. echo SHELLFU_INCLUDE
  29. echo SHELLFU_PATH
  30. echo SHELLFU_PRETTY
  31. echo SHELLFU_VERSION
  32. echo __SHELLFU_IMPORTED
  33. }
  34. mksnippet() {
  35. #
  36. # Produce a snippet to store env info to a file $1
  37. #
  38. # File _ignored_ is used as list of tokens to always
  39. # remove from the info.
  40. #
  41. local target="$1"
  42. printf "
  43. {
  44. (set -o posix; set) \\
  45. | grep -e '^[a-zA-Z_]' \\
  46. | sed -e 's/=.*//'
  47. declare -F
  48. } | sort | grep -vxf _ignored_ > %s
  49. " "$target"
  50. }
  51. wrap_test() {
  52. #
  53. # Assemble and print test script code
  54. #
  55. # Assemble test script by including scanning snippet,
  56. # importing code from file $1.test, and snippet again. If
  57. # run, the code printed by this function will generate two
  58. # files: $1.before and $1.after.
  59. #
  60. local name="$1"
  61. if head -1 "test/$name.sh" | grep '^#!';
  62. then
  63. # shebang already "leaked"; add snippet and the rest
  64. mksnippet "$name.before"
  65. tail -n +2 "test/$name.sh"
  66. else
  67. mksnippet "$name.before"
  68. cat "test/$name.sh"
  69. fi \
  70. | sed -e "s|\\<sfpath\\>|SHELLFU_DIR=${shellfu_init%/*} $sfpath|"
  71. # ^^^ make `sfpath` work even if PATH is empty:
  72. mksnippet "$name.after"
  73. }
  74. tf_do_subtest() {
  75. #
  76. # Prepare, run and check results of test $1
  77. #
  78. # Contents of $1 file (which is supposed to be importing
  79. # code) are altered for testing and wrapped so that on
  80. # execution in clean environment, two files are produced to
  81. # hold sets of detected variable and function names--before
  82. # import and after it.
  83. #
  84. # The difference of the sets (i.e. what we have *really*
  85. # imported) must be same as oracle/$1.new
  86. #
  87. local name="$1"
  88. wrap_test "$name" > "$name.sh"
  89. mkdir -p result
  90. chmod +x "$name.sh"
  91. env -i ./"$name.sh"
  92. comm -13 "$name.before" "$name.after" > "result/$name.new"
  93. # i.e. lines introduced by import ^^
  94. diff -u "oracle/$name.new" "result/$name.new"
  95. }
  96. export LANG=C
  97. sfpath=$(which sfpath) || tf_exit_error "cannot find sfpath"
  98. shellfu_init=$(sfpath) || tf_exit_error "sfpath failed"
  99. ignored > _ignored_
  100. tf_do_subtests