summaryrefslogtreecommitdiff
path: root/yarns/morph.shell-lib
blob: faf094ff7167a60723e9482be6de7c1a346a7347 (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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Shell library for Morph yarns.
#
# The shell functions in this library are meant to make writing IMPLEMENTS
# sections for yarn scenario tests easier.

# Copyright (C) 2013-2015  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.


# Add $SRCDIR to PYTHONPATH.

case "$PYTHONPATH" in
    '') PYTHONPATH="$SRCDIR)" ;;
    *) PYTHONPATH="$SRCDIR:$PYTHONPATH" ;;
esac
export PYTHONPATH


# Run Morph from the source tree, ignoring any configuration files.
# This way the test suite is not affected by any configuration the user
# or system may have. Instead, we'll use the `$DATADIR/morph.conf` file,
# which tests can create, if they want to.

run_morph()
{
    {
        set +e
        "$SRCDIR"/morph --debug \
            --cachedir-min-space=0 --tempdir-min-space=0 \
            --no-default-config --config "$DATADIR/morph.conf" \
            --log="$DATADIR/log-$1"  \
            "$@" 2> "$DATADIR/result-$1" > "$DATADIR/out-$1"
        local exit_code="$?"
        for o in log result out; do
            ln -sf "$o-$1" "$DATADIR/$o-latest"
        done
        cat "$DATADIR/out-$1"
        cat "$DATADIR/result-$1" >&2
        return "$exit_code"
    }
}


# Sometimes we want to try running morph, but are OK if it fails, we just
# need to remember that it did.

attempt_morph()
{
    if run_morph "$@"
    then
        echo 0 > "$DATADIR/morph-exit"
    else
        echo "$?" > "$DATADIR/morph-exit"
    fi
}


# Perl's die() function is often very useful: it prints an error message
# and terminates the process with a non-zero exit code. Let's have a
# shell function to do that.

die()
{
    echo "ERROR: $@" 1>&2
    exit 1
}


# Tests often need to check that specific files or directories exist
# and have the right ownerships etc. Here's some shell functions to
# test that kind of thing.

is_dir()
{
    if [ ! -d "$1" ]
    then
        die "Expected $1 to be a directory"
    fi
}

is_file()
{
    if [ ! -f "$1" ]
    then
        die "Expected $1 to be a regular file"
    fi
}


# General assertions.

assert_equal()
{
    if [ "$1" != "$2" ]
    then
        die "Expected '$1' and '$2' to be equal"
    fi
}


# Sometimes it's nice to run a command in a different directory, without
# having to bother changing the directory before and after the command,
# or spawning subshells. This function helps with that.

run_in()
{
    (cd "$1" && shift && exec "$@")
}


# Extract all refs in all given morphologies. Each ref is reported
# as filename:ref. The referred-to repository is not listed.

list_refs()
{
    awk '/ ref: / { printf "%s %s\n", FILENAME, $NF }' "$@"
}


# Is a ref petrified? Or a specific branch?

is_petrified_or_branch()
{
    if echo "$1" |
       awk -v "branch=$2" '$NF ~ /[0-9a-fA-F]{40}/ || $NF == branch' |
        grep .
    then
        return 0
    else
        return 1
    fi
}


# Are named morphologies petrified? Die if not. First arg is the
# branch that is allowed in addition to SHA1s.

assert_morphologies_are_petrified()
{
    local branch="$1"
    shift
    list_refs "$@" |
    while read filename ref
    do
        if ! is_petrified_or_branch "$ref" "$branch"
        then
            die "Found non-SHA1 ref in $filename: $ref"
        fi
    done
}


# Added until it's fixed in upstream.
# It's a solution to create an empty home directory each execution
export HOME="$DATADIR/home"
if [ ! -d "$HOME" ]
then
    mkdir "$HOME"
fi

# Generating a default git user to run the tests
if ! test -r "$HOME/.gitconfig"
then
    cat > "$HOME/.gitconfig" <<EOF
[user]
     name = Tomjon Codethinker
     email = tomjon@codethink.co.uk
EOF
fi


# Change colons to slashes. This is used when converting an aliases
# repository URL (e.g., test:morphs) into a directory path.

slashify_colons()
{
    echo "$1" | sed s,:,/,g
}

start_cache_server(){
    mkfifo "$1"
    start-stop-daemon --start --pidfile="$2" \
        --background --make-pidfile --verbose \
        --startas="$SRCDIR/morph-cache-server" -- \
        --port-file="$1" --no-fcgi \
        --repo-dir="$DATADIR/gits" --direct-mode \
        --bundle-dir="$DATADIR/bundles" \
        --artifact-dir="$3" "$@"
    port="$(cat "$1")"
    rm "$1"
    echo "$port" >"$1"
}

stop_daemon(){
    if [ -e "$1" ]; then
        start-stop-daemon --stop --pidfile "$1" --oknodo
    fi
}