commit db018202b88b529616efd0651f71681906c9036a from: Sven M. Hallberg date: Thu Jul 17 10:07:06 2025 UTC allow embedding test arguments after a colon commit - 15f1a49a5f1b34c6d571fe2d4561b3005c418592 commit + db018202b88b529616efd0651f71681906c9036a blob - ebd39c4f42ec24c30364eb06cbd987c5e20c9f57 blob + 8f4b89b2cb0c37849715682fd01092e41739bcca --- exercise +++ exercise @@ -29,7 +29,14 @@ set opts(-q) 0 ;# quiet mode # the coroutine ends, the channel is closed and any event handler vanishes. proc execute_test {job basedir path} { global opts counters + set args {} + # Handle test arguments embedded in path after a colon. + if {[set i [string first : $path]] != -1} { + set args [string range $path [expr $i + 1] end] + set path [string range $path 0 [expr $i - 1]] + } + # Should the test result be integrated as a nested test suite? set suite [string match $opts(-ps) [file tail $path]] @@ -48,7 +55,7 @@ proc execute_test {job basedir path} { set scounters {} if {[catch { # Execute the test, attaching pipes to stdin and stdout. - set pipe [open "|$exe 2>$errfile" r+] + set pipe [open "|$exe 2>$errfile $args" r+] yield $pipe # Consume the test's stdout. Ignore it for normal tests. blob - 0a3258df7bafd9b42ec1f410698aca7f953052ab blob + 2c8a581d832e54901f45054573dd4418110320eb --- exercise.1 +++ exercise.1 @@ -19,19 +19,34 @@ .Sh DESCRIPTION The .Nm -utility searches for and executes software (unit) tests in or below the -current directory. +utility searches for and executes software (unit) tests. It displays a running status and passes any error messages to standard error. .Pp -If given a directory argument, +Any +.Ar path +argument that refers to a directory +is recursively searched for tests. +Other arguments are executed as individual files. +Executables should match one of the patterns +.Pa *.t +or +.Pa *.ts +(for test suites). +.Pp +If called without arguments, .Nm -searches for tests therein. -If given a file argument, it executes it as a single test (or test suite). -To be recognized as tests, files must match the pattern -.Pa *.t . -To be run as test suites (see below), files must match the pattern -.Pa *.ts . +searches the current directory. .Pp +Arguments can be passed to a test or test suite +by embedding them in +.Ar path +after a colon +.Sq ":" . +Anything after the (first) colon is passed to the test command, +split into arguments by white space. +To avoid splitting an argument that contains spaces, +surrounding double quotes must be embedded with it. +.Pp The options are as follows: .Bl -tag -width Ds .It Fl B Ar dir @@ -196,6 +211,16 @@ The helper runs .Nm with some test (shell) script to execute and compares the outcome to expectations. +.Pp +Calling a test suite with three arguments, +the last of which contains a space character: +.Bd -literal -offset Ds +.Sy $ \c +exercise foo/bar.ts:'a b "x y"' +.Ed +.Pp +Note the use of (shell) single quotes to embed the necessary spaces and +double quotes. . .Sh SEE ALSO .Xr sh 1 blob - /dev/null blob + f93352d27d024fdd90fb36d7e6ba2d951280154a (mode 755) --- /dev/null +++ tests/exercise/test/args.t @@ -0,0 +1,30 @@ +#!/bin/sh + +export pth=$0.tst:foobar +./assert-exercise $0 'run 1 ok 1 fail 0 error 0\n' '' <<-"EOF" + #!/bin/sh + if ! [ "$1" = "foobar" ] + then + echo "/$1/" >&2 + fi +EOF + +# test argument split on whitespace +export pth=$0.tst:"foo bar" +./assert-exercise $0 'run 1 ok 1 fail 0 error 0\n' '' <<-"EOF" + #!/bin/sh + if ! [ "$1" = "foo" -a "$2" = "bar" ] + then + echo "/$1/$2/" >&2 + fi +EOF + +# test argument containing whitespace in double quotes +export pth=$0.tst:'"foo bar"' +./assert-exercise $0 'run 1 ok 1 fail 0 error 0\n' '' <<-"EOF" + #!/bin/sh + if ! [ "$1" = "foo bar" ] + then + echo "/$1/" >&2 + fi +EOF blob - 663923e2c024331d2519d136e27dd878eafc9597 blob + 6bb2d48c8644b823b71bddc72cc52af2181624b3 --- tests/helpers/assert-exercise +++ tests/helpers/assert-exercise @@ -31,7 +31,7 @@ chmod +x $tst # execute & compare shift 3 unset EXERCISEDIR # clean environment -env $env $exe "$@" $pth 1>$out 2>$err +env $env $exe "$@" "$pth" 1>$out 2>$err test "$?" = "127" && exit 127 diff -u $experr $err >&2 && \ diff -u $expout $out >&2