Step implementations ==================== This chapter contains implementations of each step. See the [yarn] manual for details and examples. Running distix -------------- We need a way for the steps to run distix with various command line arguments. Instead of having a different IMPLEMENTS step for each command that needs to be run, we'll have one fairly generic one. It'll allow the step to provide the command line arguments, but then also adds in options and things to make distix be isolated from, say, global configuration etc. User needs to change directory. Since each step is run in a new shell, we do this by keeping track ourselves of where the scenario wants to be, and changing there when running various commands. IMPLEMENTS WHEN user changes working directory to (\S+) printf "%s" "$MATCH_1" > "$DATADIR/cwd" IMPLEMENTS WHEN user attempts to run distix ([^$]*) # Yarn sets $HOME to point at an empty temporary directory. # Set up a simple git config so tests can use git. git config --global user.email 'Tomjon ' git config --global user.name 'Tomjon Tester' cd "$DATADIR" rm -f attempt.exit attempt.stdout attempt.stderr cd "$(cat cwd || echo .)" if "$SRCDIR/distix" --no-default-config --quiet \ --log "$DATADIR/distix.log" \ $MATCH_1 \ > "$DATADIR/attempt.stdout" 2> "$DATADIR/attempt.stderr" then echo 0 > "$DATADIR/attempt.exit" else echo "$?" > "$DATADIR/attempt.exit" fi IMPLEMENTS WHEN user attempts to run distix show \$PREFIX # Yarn sets $HOME to point at an empty temporary directory. # Set up a simple git config so tests can use git. git config --global user.email 'Tomjon ' git config --global user.name 'Tomjon Tester' cd "$DATADIR" rm -f attempt.exit attempt.stdout attempt.stderr cd "$(cat cwd || echo .)" if "$SRCDIR/distix" --no-default-config --quiet \ --log "$DATADIR/distix.log" \ show "$(cat "$DATADIR/tid-prefix")"\ > "$DATADIR/attempt.stdout" 2> "$DATADIR/attempt.stderr" then echo 0 > "$DATADIR/attempt.exit" else echo "$?" > "$DATADIR/attempt.exit" fi IMPLEMENTS WHEN user sets all tickets in (\S+) to (\S+) cd "$DATADIR/$MATCH_1" "$SRCDIR/distix" --no-default-config --quiet \ --log "$DATADIR/distix.log" list | awk '/^ / { print $2 }' > "$DATADIR/tickets.list" cat "$DATADIR/tickets.list" | while read ticket do "$SRCDIR/distix" --no-default-config --quiet \ --log "$DATADIR/distix.log" \ set "$MATCH_2" "$ticket" done We also need steps for inspecting the results of attempting to run distix. IMPLEMENTS THEN attempt succeeded cat "$DATADIR/attempt.exit" cat "$DATADIR/attempt.stderr" if ! grep -Fx 0 "$DATADIR/attempt.exit" then echo "Attempt should have succeeded, but didn't" 1>&2 exit 1 fi IMPLEMENTS THEN output is "(.*)" cat "$DATADIR/attempt.stdout" printf "$MATCH_1" > "$DATADIR/temp-stdout" diff -u "$DATADIR/temp-stdout" "$DATADIR/attempt.stdout" IMPLEMENTS THEN output matches "(.*)" cat "$DATADIR/attempt.stdout" grep -P -e "$MATCH_1" "$DATADIR/attempt.stdout" IMPLEMENTS THEN output doesn't match "(.*)" cat "$DATADIR/attempt.stdout" if grep -P -e "$MATCH_1" "$DATADIR/attempt.stdout" then echo "$DATADIR/attempt.stdout unexpected matches $MATCH_1" 1>&2 exit 1 fi IMPLEMENTS THEN first ticket id is captured as \$TID # We assume previous command run was distix list. # Each line starts with a full ticket id. awk 'NR == 2 { print $2; exit }' "$DATADIR/attempt.stdout" \ > "$DATADIR/ticket-id-1" IMPLEMENTS THEN attempt failed cat "$DATADIR/attempt.exit" # helps debugging scenarios if grep -Fx 0 "$DATADIR/attempt.exit" then echo "Attempt should have failed, but didn't" 1>&2 exit 1 fi IMPLEMENTS THEN error message matches (.*) cat "$DATADIR/attempt.stderr" grep -e "$MATCH_1" "$DATADIR/attempt.stderr" File creation ------------- We need to provide a way for scenarios to create files with known content. IMPLEMENTS GIVEN file (\S+) containing "(.*)" printf "$MATCH_2" > "$DATADIR/$MATCH_1" Maildir creation ---------------- We need to create a maildir with mails in it. IMPLEMENTS GIVEN maildir (\S+) containing a mail with subject "(.+)" dirname="$DATADIR/$MATCH_1" mkdir -p "$dirname/new" "$dirname/cur" "$dirname/tmp" cat < "$dirname/new/message" From: user@example.com Subject: $MATCH_2 Message body goes here. EOF File tests ----------- Does a file or directory exist? IMPLEMENTS THEN (\S+) exists test -e "$DATADIR/$MATCH_1" Repository/ticket tests ----------------------- How many tickets does a repository have? IMPLEMENTS THEN repository (\S+) has (\d+) tickets? n="$(ls "$DATADIR/$MATCH_1/tickets" | wc -l)" [ "$n" = "$MATCH_2" ] How many copies of a given message does a repository have? In any tickets? IMPLEMENTS THEN repository (\S+) has one copy of message in (\S+) find "$DATADIR/$MATCH_1/tickets" -type f -exec md5sum '{}' + \ > "$DATADIR/md5sums" msgsum="$(md5sum < "$DATADIR/$MATCH_2" | awk '{ print $1 }')" n="$(grep -c -e "$msgsum" "$DATADIR/md5sums" | wc -l)" [ "$n" = 1 ] Git operations -------------- Clone a repository. IMPLEMENTS WHEN user clones (\S+) to (\S+) cd "$DATADIR" git clone "$MATCH_1" "$MATCH_2" Check that everything is in git. IMPLEMENTS THEN everything in (\S+) is committed to git cd "$DATADIR/$MATCH_1" if git status --short | grep . then echo "Not everything is in git" 1>&2 exit 1 fi Ticket id manipulation ---------------------- IMPLEMENTS THEN ticket id \$TID 7-nybble prefix is \$PREFIX cut -c1-7 "$DATADIR/ticket-id-1" > "$DATADIR/tid-prefix" IMPLEMENTS THEN output contains ticket id \$TID grep -f "$DATADIR/ticket-id-1" "$DATADIR/attempt.stdout"