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

TF_RUN 3.8KB

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