Skip to content

Commit

Permalink
Automated regression test for output.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregordick committed Oct 3, 2016
1 parent 7b63c05 commit 299d5da
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ language: "perl"
perl:
- "5.10"

script: 'perl Build.PL && ./Build test --test_files "$(find t -name *.t -type f)" verbose=1'
script: 'regress/scripts/travis.sh'

branches:
only:
- calcalc
- master

notifications:
email:
Expand Down
5 changes: 5 additions & 0 deletions regress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Regression tests

This directory contains tests for identifying regressions in the generated
offices between two commits. Contrast this to the unit tests in `t/`, which
test the correctness of the implementation.
39 changes: 39 additions & 0 deletions regress/scripts/expand-dates.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/perl

use strict;
use warnings;

use DateTime;
use List::MoreUtils qw(zip);

while (<>)
{
# Strip comments and ignore blank lines.
s/#.*//;
next unless $_;

# Expand date ranges.
s/^(\d+-\d+-\d+)$/$1:$1/;
s/(\d+)-(\d+)-(\d+):(\d+)-(\d+)-(\d+)/expand_dates($1, $2, $3, $4, $5, $6)/e;

print;
}

sub expand_dates
{
my @keys = ('month', 'day', 'year');
my @start_mdy = @_[0..2];
my @end_mdy = @_[3..5];
my $rover = DateTime->new(zip(@keys, @start_mdy));
my $end = DateTime->new(zip(@keys, @end_mdy));

my @dates;

while ($rover <= $end)
{
push @dates, $rover->mdy('-');
$rover->add(days => 1);
}

return join("\n", @dates);
}
137 changes: 137 additions & 0 deletions regress/scripts/generate-diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/bin/sh

basedir="$(dirname $0)"
source "${basedir}/util.sh"

usage() {
err 'Usage:'
err " $scriptname <base_ref> <test_ref> <testspec>"
exit 1
}

###############################################################################

export_tree() {
local ref="$1"

git archive --prefix="${treedir}/${ref}/" "${ref}": | tar x -C "${tempdir}"
}

# Path to office script from root of tree.
office_script_path() {
local hour="$1"
echo -n 'web/cgi-bin/'
if [ "${hour}" = 'SanctaMissa' ]; then
echo 'missa/missa.pl'
else
echo 'horas/officium.pl'
fi
}

long_version() {
local short_version="$1"
case "${short_version}" in
Monastic) echo 'pre-Trident Monastic';;
1570) echo 'Trident 1570';;
1910) echo 'Trident 1910';;
Divino) echo 'Divino Afflatu';;
1955) echo 'Reduced 1955';;
1960) echo 'Rubrics 1960';;
*) die "Invalid short version: ${short_version}"
esac
}

hour_command() {
local hour="$1"
echo "pray${hour}"
}

run_single_test() {
local test_tree="$1"
local output_tree="$2"
local short_version="$3"
local date="$4"
local hour="$5"

local output_dir="${output_tree}/${short_version}/${date}"
mkdir -p "${output_dir}"

# Run the script and store its output, stripping off the cookie.
"${test_tree}/$(office_script_path "${hour}")" \
"version=$(long_version "${short_version}")" \
"command=$(hour_command "${hour}")" \
"date=${date}" | \
grep -Pv '^Set-Cookie:' > \
"${output_dir}/${hour}.out"
}

expand_dates() {
local testspec="$1"
"${scriptdir}/expand-dates.pl" "${testspec}"
}

gen_output_tree() {
local ref="$1"
echo "${tempdir}/${outputdir}/${ref}"
}

run_test() {
local ref="$1"
local testspec="$2"
local test_tree="${tempdir}/${treedir}/${ref}"
local output_tree="$(gen_output_tree "${ref}")"

for date in $(expand_dates "${testspec}"); do
for hour in Matutinum Vespera SanctaMissa; do
for short_version in Divino 1960; do
run_single_test \
"${test_tree}" \
"${output_tree}" \
"${short_version}" \
"${date}" \
"${hour}"
done
done
done
}

###############################################################################

main() {
# Globals.
treedir=trees
outputdir=output
scriptname=$(basename "$0")
scriptdir=$(dirname "$0")

[ $# -eq 3 ] || usage

local reporoot=$(git rev-parse --show-toplevel)
local base_ref=$(git rev-parse --verify "$1")
local test_ref=$(git rev-parse --verify "$2")
local testspec="$3"

# Export the two test trees.
echo 'Exporting...'
try export_tree "${base_ref}"
try export_tree "${test_ref}"

# Run test against each tree.
echo 'Testing...'
run_test "${base_ref}" "${testspec}"
run_test "${test_ref}" "${testspec}"

# Generate diff.
echo 'Diffing...'
local diff_output="${tempdir}/output.diff"
diff -ur \
"$(gen_output_tree "${base_ref}")" \
"$(gen_output_tree "${test_ref}")" > \
"${diff_output}" || true
diffstat -p5 "${diff_output}"
cat "${diff_output}"
}

tempdir=$(mktemp -d) || die 'Failed to create temporary directory.'
(set -e; main "$@")
try rm -rf "${tempdir}"
19 changes: 19 additions & 0 deletions regress/scripts/travis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

set -e

basedir="$(dirname $0)"
source "${basedir}/util.sh"

parent_rev="$(echo ${TRAVIS_COMMIT_RANGE} |
perl -ne '/(.*)\.\.\./ && print $1')"
test_rev="${TRAVIS_COMMIT}"

# Check validity of commits.
git rev-parse --verify "${parent_rev}"
git rev-parse --verify "${test_rev}"

"${basedir}/generate-diff.sh" \
"${parent_rev}" \
"${test_rev}" \
"${basedir}/../tests/travis.testspec"
12 changes: 12 additions & 0 deletions regress/scripts/util.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
err() {
echo "$@" >&2
}

die() {
err "$@"
exit 1
}

try() {
"$@" || die "ERROR: failed $@"
}
12 changes: 12 additions & 0 deletions regress/tests/travis.testspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Circumcision to St Hilary
1-1-2016:1-14-2016
# Quinquagesima to Saturday of the first week of Lent
2-7-2016:2-20-2016
# Sitientes to Low Monday
3-12-2016:4-4-2016
# Ascension to Monday of first week after the octave of Pentecost
5-5-2016:5-23-2016
# XVII. and XVIII. weeks after the octave of Pentecost; latter is ember week
9-11-2016:9-24-2016
# III. week of Advent
12-11-2016:12-17-2016

0 comments on commit 299d5da

Please sign in to comment.