runtest 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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" || {
  65. status=1
  66. total_failed=$((total_failed + 1))
  67. }
  68. done
  69. return $status
  70. }
  71. lcwd=$(pwd)
  72. [ x"$tsdir" != x"" ] || tsdir="$lcwd"
  73. [ x"$bindir" != x"" ] || bindir="${lcwd%/*}" # one directory up from $lcwd
  74. PATH="$bindir:$PATH"
  75. export bindir # some tests need to look at $bindir/.config
  76. if [ x"$VERBOSE" = x ]; then
  77. export VERBOSE=
  78. fi
  79. if [ x"$1" = x"-v" ]; then
  80. export VERBOSE=1
  81. shift
  82. fi
  83. implemented=$(
  84. printf "busybox " # always implemented
  85. "$bindir/busybox" 2>&1 |
  86. while read line; do
  87. if [ x"$line" = x"Currently defined functions:" ]; then
  88. xargs | sed 's/,//g'
  89. break
  90. fi
  91. done
  92. )
  93. applets="$implemented"
  94. if [ $# -ne 0 ]; then
  95. applets="$@"
  96. fi
  97. # Populate a directory with links to all busybox applets
  98. LINKSDIR="$bindir/runtest-tempdir-links"
  99. # Comment this line out if you have put a different binary in $LINKSDIR
  100. # (say, a "standard" tool's binary) in order to run tests against it:
  101. rm -rf "$LINKSDIR" 2>/dev/null
  102. mkdir "$LINKSDIR" 2>/dev/null
  103. for i in $implemented; do
  104. # Note: if $LINKSDIR/applet exists, we do not overwrite it.
  105. # Useful if one wants to run tests against a standard utility,
  106. # not an applet.
  107. ln -s "$bindir/busybox" "$LINKSDIR/$i" 2>/dev/null
  108. done
  109. # Set up option flags so tests can be selective.
  110. export OPTIONFLAGS=:$(
  111. sed -nr 's/^CONFIG_//p' "$bindir/.config" |
  112. sed 's/=.*//' | xargs | sed 's/ /:/g'
  113. ):
  114. status=0
  115. for applet in $applets; do
  116. # Any old-style tests for this applet?
  117. if [ -d "$tsdir/$applet" ]; then
  118. run_oldstyle_applet_tests "$applet" || status=1
  119. fi
  120. # Is this a new-style test?
  121. if [ -f "$applet.tests" ]; then
  122. if [ ! -e "$LINKSDIR/$applet" ]; then
  123. # (avoiding bash'ism "${applet:0:4}")
  124. if ! echo "$applet" | grep "^all_" >/dev/null; then
  125. echo "SKIPPED: $applet (not built)"
  126. continue
  127. fi
  128. fi
  129. # echo "Running test $tsdir/$applet.tests"
  130. PATH="$LINKSDIR:$tsdir:$bindir:$PATH" \
  131. "$tsdir/$applet.tests"
  132. rc=$?
  133. total_failed=$((total_failed + rc))
  134. test $rc -ne 0 && status=1
  135. fi
  136. done
  137. # Leaving the dir makes it somewhat easier to run failed test by hand
  138. #rm -rf "$LINKSDIR"
  139. if [ $status -ne 0 ] && [ x"$VERBOSE" = x ]; then
  140. echo "$total_failed failure(s) detected; running with -v (verbose) will give more info"
  141. fi
  142. exit $status