Debian Codegraph @

tests/run-test

raw
1 #!/bin/sh
2 # Run a paper test
3 #
4 # Copyright (c) 2021-2024 Reuben Thomas <[email protected]>
5 #
6 # This file is part of libpaper.
7 #
8 # This program is free software: you can redistribute it and/or modify it
9 # under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation; either version 2.1 of the License, or (at
11 # your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with this program. If not, see
20 # <https://www.gnu.org/licenses/lgpl-2.1.html>.
21
22 set -e
23
24 # Skip test if sysconfdir does not start with prefix, as then relocation won't
25 # work. Typical example: prefix=/usr, sysconfdir=/etc
26 case "$sysconfdir" in
27 "$prefix/"*)
28 ;;
29 *)
30 echo prefix: "$prefix", sysconfdir: "$sysconfdir"
31 echo "sysconfdir does not start with prefix, cannot run test!"
32 exit 0
33 ;;
34 esac
35
36 curr_dir="$(pwd)"
37 test_file="$curr_dir/$1"
38 name="${1%.sh}"
39 basename=$(basename "$name")
40 test_dir="$curr_dir/$basename.$$"
41 echo test_dir: "$test_dir"
42 expected_exit=0
43 expected_file="$abs_srcdir/$basename-expected.txt"
44
45 # Test runner.
46 # Arguments are command to run.
47 paper_test() {
48 exit_code=0
49 paper "$@" > "$basename-output.txt" 2>&1 || exit_code=$?
50 if [ $exit_code != $expected_exit ]; then
51 echo "Expected exit code $expected_exit but was $exit_code"
52 exit 1
53 fi
54 # Fix variation in output:
55 # 1. Convert backslashes to forward slashes
56 # 2. Compress double slashes (from relocated paths on some systems)
57 # 3. Remove prefix of name of 'paper' executable
58 sed -e 's|\\|/|g' -e 's|//|/|g' -e 's|^.*/paper:|paper:|g' < "$basename-output.txt" > "$basename-output-fixed.txt"
59 $DIFF -u "$expected_file" "$basename-output-fixed.txt"
60 }
61
62 # Test functions.
63 no_size() {
64 paper_test --no-size
65 }
66
67 no_PAPERSIZE () {
68 unset PAPERSIZE
69 no_size
70 }
71
72 no_user_papersize() {
73 rm -f "$XDG_CONFIG_HOME/papersize"
74 no_PAPERSIZE
75 }
76
77 no_locale() {
78 export LC_ALL=invalid_locale
79 no_user_papersize
80 }
81
82 no_system_papersize() {
83 rm -f "./$sysconfdir/papersize"
84 no_locale
85 }
86
87 no_system_paperspecs() {
88 rm -f "./$sysconfdir/paperspecs"
89 no_system_papersize
90 }
91
92 no_user_paperspecs() {
93 expected_exit=1
94 rm -f "$XDG_CONFIG_HOME/paperspecs"
95 no_system_paperspecs
96 }
97
98 no_home() {
99 unset XDG_CONFIG_HOME
100 unset HOME
101 no_user_paperspecs
102 }
103
104 # Make a test installation of paper
105 cd ..
106 ${MAKE} install DESTDIR="$test_dir"
107 export PATH="$test_dir/$bindir:$PATH"
108 export DYLD_LIBRARY_PATH="$test_dir/$libdir"
109 cd "$test_dir"
110
111 # Set up paper configuration
112 export PAPERSIZE=environment
113 export LC_PAPER=C
114 # LC_PAPER in C locale is A4 in glibc, and we rely on glibc to read the
115 # paper size, so the expected value when the locale is used is "A4".
116 export XDG_CONFIG_HOME="$test_dir/config"
117 mkdir "$XDG_CONFIG_HOME"
118 echo user_papersize > "$XDG_CONFIG_HOME/papersize"
119 TEST_PAPERSPECS=$abs_srcdir/test-paperspecs
120 cp "$TEST_PAPERSPECS" "$XDG_CONFIG_HOME/paperspecs"
121 echo system_papersize > "./$sysconfdir/papersize"
122 printf "first_system_paperspecs,55,550,mm\nA4,210,297,mm" > "./$sysconfdir/paperspecs"
123
124 # Remove the test directory on exit.
125 # Change directory back to $abs_srcdir first so we don't try to remove a
126 # directory above the cwd, which is forbidden on some systems.
127 trap 'cd "$abs_srcdir" && rm -rf "$test_dir"' EXIT
128
129 # Run the test script.
130 . "$test_file"