summaryrefslogtreecommitdiff
path: root/macros/generic/markdown/tests/test.sh
blob: c32b8274a268de1e53314ded9cf58aa392fdec97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
# Run a test for each testfile passed as an argument. If a testfile does not
# contain the expected test result, generate one.
set -e
BUILDDIR=`mktemp -d`
trap 'rm -rf $BUILDDIR' INT TERM
for TESTFILE; do
  printf 'Testfile %s\n' $TESTFILE
  for FORMAT in templates/*/; do
    printf '  Format %s\n' $FORMAT
    for TEMPLATE in ${FORMAT}*.tex.m4; do
      printf '    Template %s\n' $TEMPLATE
      m4 -DTEST_FILENAME=test.tex <$FORMAT/COMMANDS.m4 |
      (while read COMMAND; do
        printf '      Command %s\n' "$COMMAND"

        # Set up the testing directory.
        cp support/* $TESTFILE $BUILDDIR
        cd $BUILDDIR
        sed -r '/^\s*<<<\s*$/{x;q}' \
          <${TESTFILE##*/} >test-setup.tex
        sed -rn '/^\s*<<<\s*$/,/^\s*>>>\s*$/{/^\s*(<<<|>>>)\s*$/!p}' \
          <${TESTFILE##*/} >test-input.md
        sed -n '/^\s*>>>\s*$/,${/^\s*>>>\s*$/!p}' \
          <${TESTFILE##*/} >test-expected.log
        m4 -DTEST_SETUP_FILENAME=test-setup.tex \
           -DTEST_INPUT_FILENAME=test-input.md <"$OLDPWD"/$TEMPLATE >test.tex

        # Run the test, filter the output and concatenate adjacent lines.
        eval $COMMAND >/dev/null 2>&1 ||
          { cat test.markdown.err 1>&2 2>/dev/null; exit 1; }
        sed -nr '/^\s*TEST INPUT BEGIN\s*$/,/^\s*TEST INPUT END\s*$/{
          /^\s*TEST INPUT (BEGIN|END)\s*$/!H
          /^\s*TEST INPUT END\s*$/{s/.*//;x;s/\n//g;p}
        }' <test.log >test-actual.log

        # If the testfile does not contain an expected outcome, use the current
        # outcome and update the testfile.
        if ! grep -q '^\s*>>>\s*$' <${TESTFILE##*/}; then
          cp test-actual.log test-expected.log
          (cat ${TESTFILE##*/} && printf '>>>\n'
            cat test-expected.log) >"$OLDPWD"/$TESTFILE
          printf '      Added the expected test outcome to the testfile.\n'
        fi

        # Compare the expected outcome against the actual outcome.
        diff -c $BUILDDIR/test-expected.log $BUILDDIR/test-actual.log # ||
#          (sed -n '1,/^\s*>>>\s*$/p' <${TESTFILE##*/} && 
#            cat test-actual.log) >"$OLDPWD"/$TESTFILE

        # Clean up the testing directory.
        cd "$OLDPWD"
        find $BUILDDIR -mindepth 1 -exec rm -rf {} + # && break
      done) # && break
    done # && break
  done
done
rm -rf $BUILDDIR