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

testing.sh 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. ffoo import core
  3. ffoo import tmp
  4. FFOO_TESTING_ARTIFACTS=${FFOO_TESTING_ARTIFACTS:-+artifacts-%Y%m%d-%H%M%S}
  5. assert_linecount() {
  6. #
  7. # Assert that count of lines in stdin is $1
  8. #
  9. local ex num
  10. ex="$@"
  11. case $ex in
  12. [0-9]*) ex="-eq $ex" ;;
  13. -*) true ;;
  14. *) usage_is "FILE EXPR|NUM" ;;
  15. esac
  16. num=$(wc -l)
  17. test 0$num $ex
  18. }
  19. collect_artifacts() {
  20. #
  21. # Collect everything that is registered as artifact
  22. #
  23. # Usage: collect_artifacts [-s symlink] [[+]target]
  24. #
  25. # If *target* starts with plus sign, the value is first
  26. # passed to `date` utility and output is replaced. If
  27. # *target* is not given, `$FFOO_TESTING_ARTIFACTS`, or
  28. # a hard-coded value `+artifacts-%Y%m%d-%H%M%S` is used.
  29. #
  30. # If *symlink* is provided, symbolic link to current
  31. # artifact collection will be created at that path. If the
  32. # link already exists, it's removed first. This enables
  33. # easier access to "last artifacts".
  34. #
  35. local alink aroot
  36. while true; do case "$1" in
  37. -s|--symlink) alink="$2"; shift 2;;
  38. *) aroot="$1"; break;;
  39. esac done
  40. aroot=${aroot:-$FFOO_TESTING_ARTIFACTS}
  41. test "${aroot:0:1}" == "+" && aroot="$(date "$aroot")"
  42. debug -v aroot
  43. test -e $aroot && warn "target exists, not collecting anything: $aroot" && return 1
  44. test -f $FFOO_TMP/artifact_list || return 0
  45. local tgt
  46. think "collecting artifacts to $aroot"
  47. cat $FFOO_TMP/artifact_list \
  48. | while read path;
  49. do
  50. tgt=$aroot/$(dirname $path)
  51. mkdir -p $tgt
  52. debug -v path tgt
  53. cp -r "$path" "$tgt"
  54. done
  55. if test -n "$alink";
  56. then
  57. if test -x "$alink" && ! test -L "$alink";
  58. then
  59. warn "path exists but is not a symlink; not touching: $alink"
  60. return
  61. fi
  62. rm -f "$alink";
  63. ln -sr "$aroot" "$alink"
  64. fi
  65. }
  66. register_artifact() {
  67. #
  68. # Register artifact path for later collection
  69. #
  70. local list=$FFOO_TMP/artifact_list
  71. debug -v list
  72. local a
  73. for a in "$@";
  74. do
  75. local dn=$(cd $(dirname $a); pwd)
  76. test "$dn" = "/" && dn="" # hack: avoid "//" if path was abs
  77. local abspath="$dn/$(basename $a)"
  78. debug -v dn abspath
  79. think "registering artifact $abspath"
  80. append_if_missing "$abspath" $list
  81. done
  82. }