aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/.ci/local/test.sh
blob: f0500e4a6120c5512eef979af51720cb73c7acbb (plain) (blame)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env bash

set -o errexit -o nounset -o pipefail

script_dir="$( dirname -- "${BASH_SOURCE[0]}" )"
script_dir="$( cd -- "$script_dir" && pwd )"
readonly script_dir

upstream_repo_dir=
readonly etc_dir="$script_dir/etc"
readonly cgitize_conf_path="$etc_dir/cgitize.conf"
readonly my_repos_path="$etc_dir/my_repos.py"
readonly output_dir="$script_dir/output"

cleanup() {
    echo
    echo ----------------------------------------------------------------------
    echo Cleaning up
    echo ----------------------------------------------------------------------

    echo "Removing upstream repository directory: $upstream_repo_dir"
    rm -rf -- "$upstream_repo_dir"
    echo "Removing etc directory: $etc_dir"
    rm -rf -- "$etc_dir"
    echo "Removing output directory: $output_dir"
    rm -rf -- "$output_dir"
}

setup_upstream_repo() {
    echo
    echo ----------------------------------------------------------------------
    echo Setting up upstream repository
    echo ----------------------------------------------------------------------

    upstream_repo_dir="$( mktemp -d )"
    pushd -- "$upstream_repo_dir" > /dev/null

    git init
    echo '1' > 1.txt
    git add .
    git commit -m 'first commit'
    echo '2' > 2.txt
    git add .
    git commit -m 'second commit'

    popd > /dev/null
}

add_commits() {
    echo
    echo ----------------------------------------------------------------------
    echo Adding new commits
    echo ----------------------------------------------------------------------

    pushd -- "$upstream_repo_dir" > /dev/null

    echo '3' > 3.txt
    git add .
    git commit -m 'third commit'

    popd > /dev/null
}

setup_cgitize_conf() {
    echo
    echo ----------------------------------------------------------------------
    echo cgitize.conf
    echo ----------------------------------------------------------------------

    local conf_dir
    conf_dir="$( dirname -- "$cgitize_conf_path" )"
    mkdir -p -- "$conf_dir"

    cat <<EOF | tee "$cgitize_conf_path"
[DEFAULT]

my_repos = $( basename -- "$my_repos_path" )
output = $output_dir
EOF
}

setup_my_repos_py() {
    echo
    echo ----------------------------------------------------------------------
    echo my_repos.py
    echo ----------------------------------------------------------------------

    local conf_dir
    conf_dir="$( dirname -- "$my_repos_path" )"
    mkdir -p -- "$conf_dir"

    cat <<EOF | tee "$my_repos_path"
from cgitize.repo import Repo


MY_REPOS = (
    Repo('test_repo', clone_url='$upstream_repo_dir'),
)
EOF
}

setup_cgitize() {
    setup_cgitize_conf
    setup_my_repos_py
}

setup_bare() {
    setup_upstream_repo
    setup_cgitize
}

setup_workdir() {
    setup_bare

    echo
    echo ----------------------------------------------------------------------
    echo Setting up local repository clone
    echo ----------------------------------------------------------------------

    mkdir -p -- "$output_dir"
    git clone --quiet -- "$upstream_repo_dir" "$output_dir/test_repo"
}

cgitize() {
    echo
    echo ----------------------------------------------------------------------
    echo Running cgitize
    echo ----------------------------------------------------------------------

    python3 -m cgitize.main --config "$cgitize_conf_path"
}

check_contains() (
    # No pipefail so that grep doesn't fuck everything up:
    # https://mywiki.wooledge.org/BashPitfalls#pipefail

    set -o errexit -o nounset

    if [ "$#" -lt 1 ]; then
        echo "usage: ${FUNCNAME[0]} TEST_STRING [PATTERN...]" >&2
        return 1
    fi
    local test_string="$1"
    shift
    local pattern
    for pattern; do
        if ! echo "$test_string" | grep --fixed-strings --silent -- "$pattern"; then
            echo "${FUNCNAME[0]}: couldn't find the following pattern: $pattern" >&2
            return 1
        fi
    done
)

verify_commits() {
    # This is fucking stupid, but otherwise stuff like `if verify_commits;`
    # doesn't work: https://stackoverflow.com/q/4072984/514684
    # TODO: figure this out?
    pushd -- "$output_dir" > /dev/null &&
        cd -- test_repo &&
        local output &&
        output="$( git log --oneline )" &&
        echo "$output" &&
        check_contains "$output" "$@" &&
        popd > /dev/null
}

verify_initial_commits() {
    echo
    echo ----------------------------------------------------------------------
    echo Checking the initial commits
    echo ----------------------------------------------------------------------

    verify_commits 'first commit' 'second commit'
}

verify_added_commits() {
    echo
    echo ----------------------------------------------------------------------
    echo Checking the added commits
    echo ----------------------------------------------------------------------

    verify_commits 'first commit' 'second commit' 'third commit'
}

test_bare() {
    echo
    echo ======================================================================
    echo "${FUNCNAME[0]}"
    echo ======================================================================

    setup_bare
    cgitize
    verify_initial_commits
    add_commits
    cgitize
    verify_added_commits
    cleanup
}

test_workdir() {
    echo
    echo ======================================================================
    echo "${FUNCNAME[0]}"
    echo ======================================================================

    setup_workdir
    cgitize
    verify_initial_commits
    add_commits
    cgitize
    verify_added_commits
    cleanup
}

test_failure() {
    echo
    echo ======================================================================
    echo "${FUNCNAME[0]}"
    echo ======================================================================

    setup_bare
    cgitize
    verify_initial_commits
    add_commits
    rm -rf -- "$upstream_repo_dir"
    if cgitize; then
        echo "cgitize should have failed to pull the upstream repository." >&2
        return 1
    fi
    verify_initial_commits
    if verify_added_commits; then
        echo "The added commits should not have been pulled." >&2
        return 1
    fi
    cleanup
}

main() {
    trap cleanup EXIT
    test_bare
    test_workdir
    test_failure
}

main