Skip to content

Commit

Permalink
Make output matching more robust
Browse files Browse the repository at this point in the history
After struggling with what seem to be tty driver timing differences,
not to mention different people's shell output, I gave in and removed
the leading CRLF hack from matching.  This means that now no test can
have the same trailing input as the output it matches.

We're also more careful about signal names.
  • Loading branch information
tokenrove committed Oct 17, 2017
1 parent 4299c6d commit 77285ce
Show file tree
Hide file tree
Showing 24 changed files with 142 additions and 79 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# binaries
helpers/echo-signal
helpers/echo-exit
helpers/echo-rot13
helpers/echo-signal
helpers/exit-status-0
helpers/list-fds
helpers/fd-perms
helpers/list-fds
helpers/timeout
35 changes: 35 additions & 0 deletions helpers/echo-rot13.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#if 0
set -x "$(dirname $0)/$(basename $0 .c)"
exec ${CC:-cc} ${CFLAGS:--Wall -Wextra -g} $0 -o $1
#endif

/* Echo our arguments, rot13'd. */

#include <errno.h>
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

static void rot13(char *s)
{
while (*s) {
char c = *s|32;
if (c >= 'a' && c <= 'm') *s += 13;
else if (c >= 'n' && c <= 'z') *s -= 13;
++s;
}
}

int main(int argc, char **argv)
{
for (int i = 1; i < argc; ++i)
rot13(argv[i]);
if (argc > 1) printf("%s", argv[1]);
for (int i = 2; i < argc; ++i)
printf(" %s", argv[i]);
puts("");
return 0;
}
20 changes: 17 additions & 3 deletions helpers/echo-signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,33 @@ exec ${CC:-cc} ${CFLAGS:--Wall -Wextra -g} $0 -o $1
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static void echo(int n)
static char *awaited;

static int int_of_signal_name(char *name)
{
if (!strcmp(name, "INT")) return SIGINT;
if (!strcmp(name, "TSTP")) return SIGTSTP;
if (!strcmp(name, "CONT")) return SIGCONT;
abort();
}

static void echo(int _)
{
printf("%d\n", n); /* safe-ish because we don't do anything else. hah. */
(void)_;
puts(awaited); /* never do stuff like this in signal handlers. */
}

int main(int argc, char **argv)
{
struct sigaction action = { .sa_handler = echo };
if (2 != argc) abort();
int sig = atoi(argv[1]);
int sig = int_of_signal_name(argv[1]);
awaited = argv[1];
sigaction(sig, &action, NULL);
puts("ready");
pause();
return 0;
}
13 changes: 7 additions & 6 deletions helpers/harness.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ while {![eof $test_file]} {
seek $test_file 0 start

log_user 0
set send_slow { 10 .001 }
set send_slow { 1 .01 }
setup_execution_environment
if [catch {spawn $shell} err] {error $err}
# waiting for first prompt can help
Expand Down Expand Up @@ -125,12 +125,13 @@ proc not_ok {} {
proc is {x} { uplevel 1 [list if $x {ok} {not_ok}]}

proc expect_line {line} {
# NB: the \r\n at the start of the line here breaks fish, and
# makes this somewhat unreliable, but it's tricky to find an
# alternative that is as simple and stops cat from passing the
# tests.
# NB: this is fairly unreliable, and we should have some lint
# where we reject tests whose expected output would match the tail
# of their input. There doesn't seem to be a better way to deal
# with the output and timing differences between shells, short of
# going full ptrace as mentioned previously.
set rv [expect {
"\r\n$line\r\n" {return 1}
"$line\r\n" {return 1}
default {return 0}
}]
expect *
Expand Down
2 changes: 2 additions & 0 deletions stage_1/01-fork-exec.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
← foo bar
→ tr a-z n-za-m⏎foo bar baz⏎^D
← sbb one onm
./helpers/echo-rot13 foo bar⏎
← sbb one
./helpers/echo-exit ./helpers/successful-exit-status⏎
0
3 changes: 2 additions & 1 deletion stage_1/03-cd-changes-directory.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/bin/pwd⏎
/tmp
→ cd /tmp⏎
→ cd ..⏎
/bin/pwd⏎
/tmp
/
4 changes: 2 additions & 2 deletions stage_1/04-semicolon-separates-commands.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
← foobarbaz
→ cd /tmp; pwd⏎
← /tmp
→ false; echo foo⏎
foo
→ false; echo-rot13 foo⏎
sbb
26 changes: 14 additions & 12 deletions stage_1/05-and-or-chaining.t
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# POSIX calls these AND Lists and OR Lists
→ true && echo foo⏎
← foo
→ false && echo foo⏎
≠ foo
→ true && false && echo foo⏎
≠ foo
→ false || echo foo⏎
← foo
→ true || false || echo foo⏎
≠ foo
→ false || true && echo foo⏎
← foo
→ true && echo-rot13 foo⏎
← sbb
→ false && echo-rot13 foo⏎
≠ sbb
→ true && false && echo-rot13 foo⏎
≠ sbb
→ false || echo-rot13 foo⏎
← sbb
→ true || false || echo-rot13 foo⏎
≠ sbb
→ false || true && echo-rot13 foo⏎
← sbb
→ nonexistent-command || echo-rot13 zim⏎
← mvz
8 changes: 4 additions & 4 deletions stage_1/07-subshell.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/tmp
→ pwd⏎
/tmp
→ (exit 1) && echo foo⏎
foo
→ (exit 0 && exit 1) && echo foo⏎
foo
→ (exit 1) && echo-rot13 foo⏎
sbb
→ (exit 0 && exit 1) && echo-rot13 foo⏎
sbb
12 changes: 6 additions & 6 deletions stage_1/08-bang-negates-exit-code.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
! false && echo foo⏎
foo
! true && echo foo⏎
foo
! true || echo foo⏎
foo
! false && echo-rot13 foo⏎
sbb
! true && echo-rot13 foo⏎
sbb
! true || echo-rot13 foo⏎
sbb
4 changes: 2 additions & 2 deletions stage_1/10-multiline-list.t
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
→ true &&⏎false ||⏎echo foo ||⏎echo bar⏎
foo
→ true &&⏎false ||⏎echo-rot13 foo ||⏎echo-rot13 bar⏎
sbb
4 changes: 2 additions & 2 deletions stage_2/02-redirections.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# NB echo -n isn't portable...
→ echo -n a > foo ; echo b >> foo ; cat foo⏎
← ab
cat < /non-existent-file || echo foo⏎
foo
cat < /non-existent-file || echo-rot13 foo⏎
sbb
cat /non-existent-file 2>foo || grep -c . <foo⏎
1
→ echo quux > foo && cat < foo⏎
Expand Down
4 changes: 2 additions & 2 deletions stage_2/04-ensure-pipes-are-closed.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This will hang in the case of a very common bug: you forget to close
# the other ends of the pipe.
cat </etc/passwd | cat | cat | cat >/dev/null | echo foo⏎
foo
cat </etc/passwd | cat | cat | cat >/dev/null | echo-rot13 foo⏎
sbb
4 changes: 2 additions & 2 deletions stage_2/07-bang-only-at-start-of-pipelines.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Would prefer to test this but mksh doesn't agree
#→ echo zim | ! tr z b | grep -qc bim || echo foo⏎
#≠ foo
! echo zim | tr z b | grep -qc bim || echo foo⏎
foo
! echo zim | tr z b | grep -qc bim || echo-rot13 foo⏎
sbb
4 changes: 2 additions & 2 deletions stage_2/08-builtin-in-pipeline.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
→ echo foo | cd /tmp | pwd⏎
/tmp
→ echo foo | cd /tmp | echo bar⏎
bar
→ echo foo | cd /tmp | echo-rot13 bar⏎
one
13 changes: 6 additions & 7 deletions stage_3/01-executed-command-receives-signals.t
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# this would be nicer if we already had $@...
→ echo-signal 2
→ echo-signal INT⏎
← ready
^C⏎
2
cat
^\⏎echo foo⏎
← foo
→ echo-signal 20
← INT
→ echo-signal TSTP⏎
← ready
^Z
20
TSTP
18 changes: 10 additions & 8 deletions stage_3/02-job-control-with-fg-bg.t
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
→ echo-signal 18
→ echo-signal CONT⏎
← ready
^Z
→ echo foo⏎
foo
→ echo-rot13 foo⏎
sbb
→ fg⏎
18
→ echo-signal 2 &⏎
→ echo foo⏎
← foo
← CONT
→ echo-signal INT &⏎
← ready
→ echo-rot13 foo⏎
← sbb
→ fg⏎
^C⏎
2
INT
7 changes: 4 additions & 3 deletions stage_3/03-sequencing-with-suspend.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
→ echo-signal 2 & echo foo⏎
← foo
→ echo-signal INT & sleep 1; echo-rot13 foo⏎
← ready
← sbb
→ fg⏎
→ ^C⏎
2
INT
6 changes: 4 additions & 2 deletions stage_3/05-sigquit.t
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
cat^\⏎echo foo⏎
← foo
cat^\⏎echo-rot13 foo⏎
← sbb
^\^\echo-rot13 blah⏎
← oynu
4 changes: 3 additions & 1 deletion stage_4/05-commands-can-be-quoted.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
→ e'ch'o foo⏎
← foo
''''""/"b"i'n/e'"c"ho''"" yup⏎
''''""/"b"i'n/e'"c"ho''"" yup; true
← yup
""'echo'"-"rot1'3' yup⏎
← lhc
2 changes: 1 addition & 1 deletion stage_4/10-tilde-expansion.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
→ echo ~
→ echo ~; true
~
# XXX this will vary by system; use magic.
→ echo ~root⏎
Expand Down
4 changes: 2 additions & 2 deletions stage_5/01-tab-completion.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# not POSIX
→ touch foo bar⏎
→ ech^I f^I b^I⏎
foo bar
→ ech^I-rot^I f^I b^I⏎
sbb one
9 changes: 5 additions & 4 deletions stage_5/02-basic-history.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
→ echo foo⏎
← foo
→ echo bar⏎
→ echo-rot13 foo⏎
← sbb
→ echo-rot13 bar⏎
one
→ ⇑⇑⏎
foo
sbb
10 changes: 5 additions & 5 deletions stage_5/03-search-with-control-r.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
→ true⏎
→ echo foo⏎
foo
→ echo bar⏎
bar
→ echo-rot13 foo⏎
sbb
→ echo-rot13 bar⏎
one
^Rf⏎
foo
sbb

0 comments on commit 77285ce

Please sign in to comment.