File: functions

package info (click to toggle)
dbconfig-common 2.0.25
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,184 kB
  • sloc: sh: 1,487; makefile: 51; sql: 40; perl: 12
file content (161 lines) | stat: -rw-r--r-- 4,317 bytes parent folder | download | duplicates (2)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# -*- mode: sh -*-
# override the installation root
_dbc_root="$(dirname "$0")/.."

# print out a tab-indented line informing of what's happening in a test
log_tc(){
    tc_case_no=$(expr $tc_case_no + 1)
    printf "\\tcase $tc_case_no: $1\\n"
}

# takes a single argument and escapes it literally in a fashion that
# allows it to be used without further quoting
escape(){
    #echo "$1" | sed -e 's/\([$`\!]\)/\\\1/g'
    echo "$1" | sed -e "s/'/'\\\''/g" -e "s/^/'/" -e "s/\$/'/"
}

dq_escape(){
    echo "$1" | sed -e 's/\([$`\!]\)/\\\1/g'
}

# replaces any occurrence of a tmpfile made from a mktemp-like template
# to the "template" form, i.e. foo.A5jfN4 -> foo.XXXXXX
# also will call normalize_tmpdir on the logfile
#
# helpful for comparing testcase output that includes these testfiles
subst_tmpfile(){
    local template match logfile
    template="$1"
    logfile="$2"
    match="$(echo "$template" | sed -e 's/XXXXXX/....../')"
    # Don't do the substitution when we're skipping in case the file doesn't exist
    (isSkipping && [ ! -f "$logfile" ]) || sed -i -e "s,$match,$template,g" "$logfile"
}

assertFileExists(){
    local f msg
    if [ $# -eq 2 ]; then
        msg="$1"
        shift
    fi
    f="$1"
    msg="${msg:-File $f does not exist}"

    assertTrue "$msg" '[ -f "$f" ]'
}

assertFileEmpty(){
    local f msg
    if [ $# -eq 2 ]; then
        msg="$1"
        shift
    fi
    f="$1"
    msg="${msg:-File $f does not exist or is nonempty}"
    [ -f "$f" ] && [ ! -s "$f" ]
    ret=$?

    assertTrue "$msg" $ret
    [ $ret -eq 0 ] || isSkipping || head "$f" >&2
}

assertDirectoryEmpty(){
    local d msg
    if [ $# -eq 2 ]; then
        msg="$1"
        shift
    fi
    d="$1"
    msg="${msg:-Directory $d is not empty}"

    [ -d "$d" ] && [ -z "$(ls "$d")" ]
    ret=$?
    assertTrue "$msg" $ret
    [ $ret -eq 0 ] || isSkipping || ls "$d" >&2
}

assertFilesEqual(){
    local msg outfile errfile oldargs f1 f2
    outfile=./tmp/assertFilesEqual.stdout
    errfile=./tmp/assertFilesEqual.stderr
    if [ $# -eq 3 ]; then
        msg="$1"
        shift
    else
        msg="Files not equal"
    fi
    f1=$1
    f2=$2
    eval set ${DIFF:-diff -Nu}
    "$@" "$f1" "$f2" > $outfile 2>$errfile
    assertTrue "$msg" "[ $? -eq 0 ]"
    if [ -s "$errfile" ]; then
        isSkipping || cat $errfile
    fi
    if [ -s "$outfile" ]; then
        isSkipping || cat $outfile
    fi
}

oneTimeSetUp(){
    local curdir basedir
    curdir="$(pwd)"
    basedir=$(readlink -f $(dirname "$0"))

    # make sure we're called from the test dir, even if we weren't
    if [ "$curdir" != "$basedir" ]; then
        cd "$basedir"
        exec "./$(basename $0)"
    fi

    . ${curdir}/mockup-functions

    # Originily set a really strange TMPDIR to see if it causes problems.
    # However, passing pgsql commands via here files fails because one can't
    # properly escape the command.
    # export TMPDIR="${curdir}/tmp/\`\`it's a '\$funny\\' path!\""
    if [ "${TMPDIR:-}" ] ; then
        export TMPDIR="${TMPDIR}/dbcdummy"
    else
        export TMPDIR="/tmp/dbcdummy"
    fi

    # override the dbc logfile location
    _dbc_logfile="${curdir}/tmp/dbc.log"

    # specify where the mockup logs should go
    MOCKUP_WORKDIR="${curdir}/tmp/mockup"
}

purge_tmp(){
    rm -rf ./tmp
    mkdir -p "$TMPDIR" "$MOCKUP_WORKDIR"
    # for convenience, since we make a really ugly TMPDIR for tests
    ln -s "$TMPDIR" ./tmp/tmpdir
    touch $_dbc_logfile
}

setUp(){
    # Probably all database type tests are going to overload this function
    # they probably need a copy of whatever is below, so keep in sync
    purge_tmp
    tc_case_no=0
}

tearDown(){
    if ls "$MOCKUP_WORKDIR" | grep -qE '^(done)?[0-9]*\.errors\.'; then
        for f in $(ls $MOCKUP_WORKDIR/*.errors.*); do
            test -s "$f"
            ret=$?
            assertFalse "mockup errors found" $ret
            [ $ret -ne 0 ] || ( echo "  $f:"; cat "$f" )
        done
    fi
    # To ensure that all mocked up functions are removed again
    # so that any next test can call the real thing again
    if [ -f "$MOCKUP_WORKDIR"/mockup.control ] ; then
        unset -f $(grep "(){$" "$MOCKUP_WORKDIR"/mockup.control | sed 's/(){//') 2> /dev/null
    fi
    assertDirectoryEmpty "cruft files left behind" "$TMPDIR"
}