aboutsummaryrefslogtreecommitdiff
path: root/tests/libtest.sh
blob: 74f5ee6fb0f2c7b07be0a67fa13aab86dcce7a39 (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
# Shell function library for test cases.
#
# Written by Russ Allbery <rra@stanford.edu>
# Copyright 2006, 2007, 2008 Board of Trustees, Leland Stanford Jr. University
#
# See LICENSE for licensing terms.

# The count starts at 1 and is updated each time ok is printed.  printcount
# takes "ok" or "not ok".
count=1
printcount () {
    echo "$1 $count $2"
    count=`expr $count + 1`
}

# Run a program expected to succeed, and print ok if it does and produces
# the correct output.  Takes the output as the first argument, the command to
# run as the second argument, and then all subsequent arguments are arguments
# to the command.
runsuccess () {
    w_output="$1"
    shift
    output=`"$@" 2>&1`
    status=$?
    if [ $status = 0 ] && [ x"$output" = x"$w_output" ] ; then
        printcount 'ok'
    else
        printcount 'not ok'
        echo "  saw: $output"
        echo "  not: $w_output"
    fi
}

# Run a program expected to fail and make sure it fails with the correct exit
# status and the correct failure message.  Takes the expected status, the
# expected output, and then everything else is the command and arguments.
# Strip the second colon and everything after it off the error message since
# it's system-specific.
runfailure () {
    w_status="$1"
    shift
    w_output="$1"
    shift
    output=`"$@" 2>&1`
    status=$?
    output=`echo "$output" | sed 's/\(:[^:]*\):.*/\1/'`
    if [ $status = $w_status ] && [ x"$output" = x"$w_output" ] ; then
        printcount 'ok'
    else
        printcount 'not ok'
        echo "  saw: ($status) $output"
        echo "  not: ($w_status) $w_output"
    fi
}

# Skip tests from $1 to $2 inclusive with reason $3.
skip () {
    n="$1"
    while [ "$n" -le "$2" ] ; do
        echo ok "$n # skip $3"
        n=`expr "$n" + 1`
    done
}

# Given a file name or relative file path, try to cd to the correct directory
# so that the relative file path is valid.  Exits with an error if that isn't
# possible.
chdir_data () {
    if [ -f "../$1" ] ; then
        cd ..
    else
        if [ -f "tests/$1" ] ; then
            cd tests
        fi
    fi
    if [ ! -f "$1" ] ; then
        echo "Cannot locate $1" >&2
        exit 1
    fi
}