3
0

runtest 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/sh
  2. # Usage:
  3. # runtest [applet1] [applet2...]
  4. . ./testing.sh
  5. total_failed=0
  6. # Run one old-style test.
  7. # Tests are stored in applet/testcase shell scripts.
  8. # They are run using "sh -x -e applet/testcase".
  9. # Option -e will make testcase stop on the first failed command.
  10. run_applet_testcase()
  11. {
  12. local applet="$1"
  13. local testcase="$2"
  14. local status=0
  15. local uc_applet=$(echo "$applet" | tr a-z A-Z)
  16. local testname="$testcase"
  17. testname="${testname##*/}" # take basename
  18. if grep "^# CONFIG_$uc_applet is not set$" "$bindir/.config" >/dev/null; then
  19. echo "UNTESTED: $testname"
  20. return 0
  21. fi
  22. if grep "^# FEATURE: " "$testcase" >/dev/null; then
  23. local feature=$(sed -ne 's/^# FEATURE: //p' "$testcase")
  24. for f in $feature; do
  25. if grep "^# $f is not set$" "$bindir/.config" >/dev/null; then
  26. echo "UNTESTED: $testname"
  27. return 0
  28. fi
  29. done
  30. fi
  31. rm -rf ".tmpdir.$applet"
  32. mkdir -p ".tmpdir.$applet"
  33. cd ".tmpdir.$applet" || return 1
  34. # echo "Running testcase $testcase"
  35. d="$tsdir" \
  36. sh -x -e "$testcase" >"$testname.stdout.txt" 2>&1 || status=$?
  37. if [ $status -ne 0 ]; then
  38. echo "FAIL: $testname"
  39. if [ x"$VERBOSE" != x ]; then
  40. cat "$testname.stdout.txt"
  41. fi
  42. else
  43. echo "PASS: $testname"
  44. fi
  45. cd ..
  46. rm -rf ".tmpdir.$applet"
  47. return $status
  48. }
  49. # Run all old-style tests for given applet
  50. run_oldstyle_applet_tests()
  51. {
  52. local applet="$1"
  53. local status=0
  54. for testcase in "$tsdir/$applet"/*; do
  55. # switch on basename of $testcase
  56. case "${testcase##*/}" in
  57. .*) continue ;; # .svn, .git etc
  58. *~) continue ;; # backup files
  59. "CVS") continue ;;
  60. \#*) continue ;; # CVS merge residues
  61. *.mine) continue ;; # svn-produced junk
  62. *.r[0-9]*) continue ;; # svn-produced junk
  63. esac
  64. run_applet_testcase "$applet" "$testcase" || status=1
  65. total_failed=$((total_failed + status))
  66. done
  67. return $status
  68. }
  69. lcwd=$(pwd)
  70. [ x"$tsdir" != x"" ] || tsdir="$lcwd"
  71. [ x"$bindir" != x"" ] || bindir="${lcwd%/*}" # one directory up from $lcwd
  72. PATH="$bindir:$PATH"
  73. export bindir # some tests need to look at $bindir/.config
  74. if [ x"$VERBOSE" = x ]; then
  75. export VERBOSE=
  76. fi
  77. if [ x"$1" = x"-v" ]; then
  78. export VERBOSE=1
  79. shift
  80. fi
  81. implemented=$(
  82. printf "busybox " # always implemented
  83. "$bindir/busybox" 2>&1 |
  84. while read line; do
  85. if [ x"$line" = x"Currently defined functions:" ]; then
  86. xargs | sed 's/,//g'
  87. break
  88. fi
  89. done
  90. )
  91. applets="$implemented"
  92. if [ $# -ne 0 ]; then
  93. applets="$@"
  94. fi
  95. # Populate a directory with links to all busybox applets
  96. LINKSDIR="$bindir/runtest-tempdir-links"
  97. # Comment this line out if you have put a different binary in $LINKSDIR
  98. # (say, a "standard" tool's binary) in order to run tests against it:
  99. rm -rf "$LINKSDIR" 2>/dev/null
  100. mkdir "$LINKSDIR" 2>/dev/null
  101. for i in $implemented; do
  102. # Note: if $LINKSDIR/applet exists, we do not overwrite it.
  103. # Useful if one wants to run tests against a standard utility,
  104. # not an applet.
  105. ln -s "$bindir/busybox" "$LINKSDIR/$i" 2>/dev/null
  106. done
  107. # Set up option flags so tests can be selective.
  108. export OPTIONFLAGS=:$(
  109. sed -nr 's/^CONFIG_//p' "$bindir/.config" |
  110. sed 's/=.*//' | xargs | sed 's/ /:/g'
  111. ):
  112. status=0
  113. for applet in $applets; do
  114. # Any old-style tests for this applet?
  115. if [ -d "$tsdir/$applet" ]; then
  116. run_oldstyle_applet_tests "$applet" || status=1
  117. fi
  118. # Is this a new-style test?
  119. if [ -f "$applet.tests" ]; then
  120. if [ ! -e "$LINKSDIR/$applet" ]; then
  121. # (avoiding bash'ism "${applet:0:4}")
  122. if ! echo "$applet" | grep "^all_" >/dev/null; then
  123. echo "SKIPPED: $applet (not built)"
  124. continue
  125. fi
  126. fi
  127. # echo "Running test $tsdir/$applet.tests"
  128. PATH="$LINKSDIR:$tsdir:$bindir:$PATH" \
  129. "$tsdir/$applet.tests"
  130. rc=$?
  131. total_failed=$((total_failed + rc))
  132. test $rc -ne 0 && status=1
  133. fi
  134. done
  135. # Leaving the dir makes it somewhat easier to run failed test by hand
  136. #rm -rf "$LINKSDIR"
  137. if [ $status -ne 0 ] && [ x"$VERBOSE" = x ]; then
  138. echo "$total_failed failure(s) detected; running with -v (verbose) will give more info"
  139. fi
  140. exit $status