3
0

runtest 3.8 KB

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