File: run-test

package info (click to toggle)
libpaper 2.2.5-0.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,496 kB
  • sloc: sh: 8,516; ansic: 798; makefile: 137
file content (130 lines) | stat: -rwxr-xr-x 3,662 bytes parent folder | download
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
124
125
126
127
128
129
130
#!/bin/sh
# Run a paper test
#
# Copyright (c) 2021-2024 Reuben Thomas <rrt@sc3d.org>
#
# This file is part of libpaper.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see
# <https://www.gnu.org/licenses/lgpl-2.1.html>.

set -e

# Skip test if sysconfdir does not start with prefix, as then relocation won't
# work. Typical example: prefix=/usr, sysconfdir=/etc
case "$sysconfdir" in
    "$prefix/"*)
        ;;
    *)
        echo prefix: "$prefix", sysconfdir: "$sysconfdir"
        echo "sysconfdir does not start with prefix, cannot run test!"
        exit 0
        ;;
esac

curr_dir="$(pwd)"
test_file="$curr_dir/$1"
name="${1%.sh}"
basename=$(basename "$name")
test_dir="$curr_dir/$basename.$$"
echo test_dir: "$test_dir"
expected_exit=0
expected_file="$abs_srcdir/$basename-expected.txt"

# Test runner.
# Arguments are command to run.
paper_test() {
    exit_code=0
    paper "$@" > "$basename-output.txt" 2>&1 || exit_code=$?
    if [ $exit_code != $expected_exit ]; then
        echo "Expected exit code $expected_exit but was $exit_code"
        exit 1
    fi
    # Fix variation in output:
    # 1. Convert backslashes to forward slashes
    # 2. Compress double slashes (from relocated paths on some systems)
    # 3. Remove prefix of name of 'paper' executable
    sed -e 's|\\|/|g' -e 's|//|/|g' -e 's|^.*/paper:|paper:|g' < "$basename-output.txt" > "$basename-output-fixed.txt"
    $DIFF -u "$expected_file" "$basename-output-fixed.txt"
}

# Test functions.
no_size() {
    paper_test --no-size
}

no_PAPERSIZE () {
    unset PAPERSIZE
    no_size
}

no_user_papersize() {
    rm -f "$XDG_CONFIG_HOME/papersize"
    no_PAPERSIZE
}

no_locale() {
    export LC_ALL=invalid_locale
    no_user_papersize
}

no_system_papersize() {
    rm -f "./$sysconfdir/papersize"
    no_locale
}

no_system_paperspecs() {
    rm -f "./$sysconfdir/paperspecs"
    no_system_papersize
}

no_user_paperspecs() {
    expected_exit=1
    rm -f "$XDG_CONFIG_HOME/paperspecs"
    no_system_paperspecs
}

no_home() {
    unset XDG_CONFIG_HOME
    unset HOME
    no_user_paperspecs
}

# Make a test installation of paper
cd ..
${MAKE} install DESTDIR="$test_dir"
export PATH="$test_dir/$bindir:$PATH"
export DYLD_LIBRARY_PATH="$test_dir/$libdir"
cd "$test_dir"

# Set up paper configuration
export PAPERSIZE=environment
export LC_PAPER=C
# LC_PAPER in C locale is A4 in glibc, and we rely on glibc to read the
# paper size, so the expected value when the locale is used is "A4".
export XDG_CONFIG_HOME="$test_dir/config"
mkdir "$XDG_CONFIG_HOME"
echo user_papersize > "$XDG_CONFIG_HOME/papersize"
TEST_PAPERSPECS=$abs_srcdir/test-paperspecs
cp "$TEST_PAPERSPECS" "$XDG_CONFIG_HOME/paperspecs"
echo system_papersize > "./$sysconfdir/papersize"
printf "first_system_paperspecs,55,550,mm\nA4,210,297,mm" > "./$sysconfdir/paperspecs"

# Remove the test directory on exit.
# Change directory back to $abs_srcdir first so we don't try to remove a
# directory above the cwd, which is forbidden on some systems.
trap 'cd "$abs_srcdir" && rm -rf "$test_dir"' EXIT

# Run the test script.
. "$test_file"