aboutsummaryrefslogtreecommitdiffstats
path: root/tests/integration.sh
blob: ea847e49d9315ef2588ae1f1ddef17d64cf61055 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/sh
# integration.sh - end-to-end test against a real LaTeX document
#
# Requires pdflatex and bibtex; skipped otherwise. Set BIBTEST_NET=1 to
# also exercise bib-fetch against doi.org (needs network access).

ROOT=$(cd "$(dirname "$0")/.." && pwd)
PATH=$ROOT:$PATH
LSKEYS="awk -f $ROOT/lib/bib-parse.awk -f $ROOT/lib/bib-lskeys.awk"

command -v pdflatex > /dev/null 2>&1 && command -v bibtex > /dev/null 2>&1 || {
  printf 'integration: pdflatex/bibtex not found, skipping\n' >&2
  exit 0
}

tmpd=$(mktemp -d) || exit 1
trap 'rm -rf "$tmpd"' EXIT INT TERM
cd "$tmpd" || exit 1

pass=0
fail=0
ok() { pass=$((pass + 1)); printf 'ok   - %s\n' "$1"; }
not_ok() { fail=$((fail + 1)); printf 'FAIL - %s\n' "$1"; }

# ---- build a database with bib-gen | bib-add ---------------------------
bib-gen -t article author='Donald E. Knuth' title='Literate Programming' \
  journal='The Computer Journal' year=1984 volume=27 number=2 \
  pages='97--111' | bib-add master.bib
bib-gen -t article author='Alan M. Turing' \
  title='Computing Machinery and Intelligence' journal='Mind' year=1950 \
  volume=59 pages='433--460' | bib-add master.bib
printf 'Claude E. Shannon\tA Mathematical Theory of Communication\tBell System Technical Journal\t1948
Edsger W. Dijkstra\tGo To Statement Considered Harmful\tCommunications of the ACM\t1968
' | bib-gen -F author,title,journal,year | bib-add master.bib

n=$($LSKEYS master.bib | wc -l)
[ "$n" -eq 4 ] && ok "database built with 4 entries" \
               || not_ok "database built with 4 entries (got $n)"

# ---- compile a document citing a subset --------------------------------
cat > paper.tex <<'EOF'
\documentclass{article}
\begin{document}
Machines may think~\cite{turing1950computing}; programs are
literature~\cite{knuth1984literate}.

DOI: 10.1093/comjnl/27.2.97
\bibliographystyle{plain}
\bibliography{master}
\end{document}
EOF
pdflatex -interaction=batchmode paper.tex > /dev/null 2>&1

grep -q 'citation{turing1950computing}' paper.aux \
  && ok "pdflatex produced citations in aux" \
  || not_ok "pdflatex produced citations in aux"

# ---- extract the cited subset and build against it ---------------------
bib-extract paper.aux master.bib > paper.bib
n=$($LSKEYS paper.bib | wc -l)
[ "$n" -eq 2 ] && ok "bib-extract kept the 2 cited entries" \
               || not_ok "bib-extract kept the 2 cited entries (got $n)"

sed 's/\\bibdata{master}/\\bibdata{paper}/' paper.aux > tmp.aux \
  && mv tmp.aux paper.aux
bibtex paper > bibtex.log 2>&1
grep -qi 'error\|warning' bibtex.log \
  && not_ok "bibtex accepts canonical output cleanly" \
  || ok "bibtex accepts canonical output cleanly"

pdflatex -interaction=batchmode paper.tex > /dev/null 2>&1
pdflatex -interaction=batchmode paper.tex > /dev/null 2>&1
if grep -qi 'undefined' paper.log; then
  not_ok "document resolves all citations"
else
  ok "document resolves all citations"
fi
[ -s paper.pdf ] && ok "pdf produced" || not_ok "pdf produced"

# ---- convert roundtrip --------------------------------------------------
bib-convert master.bib | bib-convert > roundtrip.bib
if [ "$($LSKEYS master.bib | sort)" = "$($LSKEYS roundtrip.bib | sort)" ]; then
  ok "bibtex -> refer -> bibtex preserves all keys"
else
  not_ok "bibtex -> refer -> bibtex preserves all keys"
fi

# ---- bib-fetch against the built pdf (network) --------------------------
if [ "$BIBTEST_NET" = 1 ]; then
  if bib-fetch paper.pdf > fetched.bib 2> /dev/null; then
    grep -q '^@article{knuth1984literate,' fetched.bib \
      && ok "bib-fetch resolves DOI from built pdf" \
      || not_ok "bib-fetch resolves DOI from built pdf"
    if bib-fetch paper.pdf 2> /dev/null | bib-add master.bib 2> /dev/null; then
      not_ok "fetched entry detected as duplicate"
    else
      ok "fetched entry detected as duplicate"
    fi
  else
    not_ok "bib-fetch resolves DOI from built pdf"
  fi
  bib-fetch -a 1706.03762 2> /dev/null \
      | grep -q '^@misc{vaswani[0-9]*attention,' \
    && ok "bib-fetch resolves arXiv id" \
    || not_ok "bib-fetch resolves arXiv id"
  cat > arx.tex <<'EOF'
\documentclass{article}
\begin{document}
A preprint without any DOI.

arXiv:1706.03762v7 [cs.CL] 2 Aug 2023
\end{document}
EOF
  pdflatex -interaction=batchmode arx.tex > /dev/null 2>&1
  bib-fetch arx.pdf 2> /dev/null | grep -q 'eprint = {1706.03762}' \
    && ok "bib-fetch extracts arXiv id from pdf" \
    || not_ok "bib-fetch extracts arXiv id from pdf"
else
  printf 'skip - bib-fetch network tests (set BIBTEST_NET=1 to enable)\n'
fi

printf '\n%d passed, %d failed\n' "$pass" "$fail"
[ "$fail" -eq 0 ]