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

TF_RUN 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. mkdelim() {
  35. #
  36. # Print visual delimiter
  37. #
  38. echo
  39. echo "######"
  40. echo
  41. }
  42. mksnippet() {
  43. #
  44. # Produce a snippet to store env info to a file $1
  45. #
  46. # File _ignored_ is used as list of tokens to always
  47. # remove from the info.
  48. #
  49. local target="$1"
  50. printf "
  51. {
  52. (set -o posix; set) \\
  53. | grep -e '^[a-zA-Z_]' \\
  54. | sed -e 's/=.*//'
  55. declare -F
  56. } | sort | grep -vxf _ignored_ > %s
  57. " "$target"
  58. }
  59. mktscript_header() {
  60. #
  61. # Print part before .before snippet
  62. #
  63. case $name in
  64. *_bash) echo '#!/bin/bash' ;;
  65. *_bash_hint) echo '#!/bin/bash' ;;
  66. *) echo '#!/bin/sh' ;;
  67. esac
  68. }
  69. mktscript_body() {
  70. #
  71. # Print main script body
  72. #
  73. echo 'SHELLFU_INCLUDE=test/include'
  74. echo ". \$(sfpath)"
  75. case $name in
  76. import_fn) echo 'shellfu import mod_f' ;;
  77. import_var) echo 'shellfu import mod_v' ;;
  78. import_var_fn) echo 'shellfu import mod_v_f' ;;
  79. import_var_fn_bash) echo 'shellfu import mod_v_f_bash' ;;
  80. import_var_fn_bash_hint) echo 'SHELLFU_COMPAT=bash; shellfu import mod_v_f_bash' ;;
  81. try_import_fn) echo 'shellfu try_import mod_f' ;;
  82. try_import_var) echo 'shellfu try_import mod_v' ;;
  83. try_import_var_fn) echo 'shellfu try_import mod_v_f' ;;
  84. esac
  85. }
  86. mktscript() {
  87. #
  88. # Compose test script
  89. #
  90. mktscript_header
  91. mkdelim
  92. mksnippet "$name.before"
  93. mkdelim
  94. mktscript_body
  95. mkdelim
  96. mksnippet "$name.after"
  97. }
  98. tf_do_subtest() {
  99. #
  100. # Prepare, run and check results of test $1
  101. #
  102. # Contents of $1 file (which is supposed to be importing
  103. # code) are altered for testing and wrapped so that on
  104. # execution in clean environment, two files are produced to
  105. # hold sets of detected variable and function names--before
  106. # import and after it.
  107. #
  108. # The difference of the sets (i.e. what we have *really*
  109. # imported) must be same as oracle/$1.new
  110. #
  111. local name="$1"
  112. mktscript "$name" > "$name.sh"
  113. mkdir -p result
  114. chmod +x "$name.sh"
  115. env -i PATH="$PATH" SHELLFU_DIR="$SHELLFU_DIR" ./"$name.sh"
  116. comm -13 "$name.before" "$name.after" > "result/$name.new"
  117. # i.e. lines introduced by import ^^
  118. diff -u "oracle/$name.new" "result/$name.new"
  119. }
  120. export LANG=C
  121. ignored > _ignored_
  122. tf_do_subtests