aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/integration/example/test.sh
blob: 4d8ab3a1ee8ace1ef555f6cb20d60b404e0b5d07 (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
#!/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

readonly cgitize_toml_path="$script_dir/../../../examples/cgitize.toml"
readonly output_dir='/tmp/cgitize'

clone_via_ssh_true() {
    sed -i -E -e 's/^clone_via_ssh = (true|false)$/clone_via_ssh = true/' -- "$cgitize_toml_path"
}

clone_via_ssh_false() {
    sed -i -E -e 's/^clone_via_ssh = (true|false)$/clone_via_ssh = false/' -- "$cgitize_toml_path"
}

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

    echo "Removing output directory: $output_dir"
    rm -rf -- "$output_dir"
    echo "Reverting clone_via_ssh settings: $cgitize_toml_path"
    clone_via_ssh_true
}

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

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

setup_ssh() {
    clone_via_ssh_true
}

setup_https() {
    clone_via_ssh_false
}

verify_origin() {
    if [ "$#" -ne 2 ]; then
        echo "usage: ${FUNCNAME[0]} REPO URL" >&2
        return 1
    fi

    local repo="$1"
    local expected="$2"

    echo
    echo ----------------------------------------------------------------------
    echo "Verifying origin: $repo"
    echo ----------------------------------------------------------------------

    local repo_dir="$output_dir/$repo"

    local actual
    actual="$( GIT_DIR="$repo_dir" git config --get remote.origin.url )"

    if [ "$expected" = "$actual" ]; then
        echo 'It matches.'
    else
        echo "It doesn't match!"
        echo "    Expected: $expected"
        echo "    Actual:   $actual"
        return 1
    fi
}

verify_repos() {
    local repo
    for repo; do
        echo
        echo ----------------------------------------------------------------------
        echo "Verifying repository: $repo"
        echo ----------------------------------------------------------------------

        local repo_dir="$output_dir/$repo"
        GIT_DIR="$repo_dir" git rev-parse HEAD > /dev/null
        echo 'HEAD is fine.'

        if test -f "$repo_dir/info/web/last-modified"; then
            echo 'last-modified is fine.'
        else
            echo 'last-modified is missing!'
            return 1
        fi
    done
}

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

    setup_ssh
    cgitize
    verify_repos lens cef wintun
    verify_origin lens 'git@github.com:ekmett/lens.git'
    verify_origin cef 'git@bitbucket.org:chromiumembedded/cef.git'
    cleanup
}

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

    setup_https
    cgitize
    verify_repos lens cef wintun
    verify_origin lens 'https://github.com/ekmett/lens.git'
    verify_origin cef 'https://bitbucket.org/chromiumembedded/cef.git'
    cleanup
}

main() {
    trap cleanup EXIT
    if [ -z "${CI+y}" ]; then
        # Skip this on CI; there're no SSH keys to authenticate against GitHub,
        # etc. there.
        test_ssh
    fi
    test_https
}

main