summaryrefslogtreecommitdiff
path: root/test/driver-lib.bash
blob: dcbcba60621f9efbdc40d2bebaf628d5cce9399e (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
red() {
    if [ -t 1 ]; then
        printf "\033[31m%s\033[0m\n" "$*"
    else
        printf "%s\n" "$*"
    fi
}

green() {
    if [ -t 1 ]; then
        printf "\033[32m%s\033[0m\n" "$*"
    else
        printf "%s\n" "$*"
    fi
}

yellow() {
    if [ -t 1 ]; then
        printf "\033[33m%s\033[0m\n" "$*"
    else
        printf "%s\n" "$*"
    fi
}


check_env() {
    if [[ "$(whoami)" != "vagrant" && ( "${TRAVIS}" != true || "${CI}" != true ) ]]; then
        die "tests must be run inside Travis or Vagrant"
    fi
}

cleanup() {
    (
    if [ "$(whoami)" == "vagrant" ]; then
        cd $HOME
        find . -not \( \
            -path './.pyenv' -o \
            -path './.pyenv/*' -o \
            -path './.bashrc' -o \
            -path './.profile' -o \
            -path './.ssh' -o \
            -path './.ssh/*' \
            \) -delete >/dev/null 2>&1
    else
        find ~ -mindepth 1 -newermt "${date_stamp}" \
            -not \( -path ~ -o -path "${BASEDIR}/*" \
                -o -path ~/dotfiles \) \
            -exec rm -rf {} +
    fi
    ) || true
}

initialize() {
    echo "initializing."
    tests_run=0
    tests_passed=0
    tests_failed=0
    tests_total="${1}"
    local plural="" && [ "${tests_total}" -gt 1 ] && plural="s"
    printf -- "running %d test%s...\n\n" "${tests_total}" "${plural}"
}

pass() {
    tests_passed=$((tests_passed + 1))
    green "-> ok."
    echo
}

fail() {
    tests_failed=$((tests_failed + 1))
    yellow "-> fail!"
    echo
}

run_test() {
    tests_run=$((tests_run + 1))
    printf '[%d/%d] (%s)\n' "${tests_run}" "${tests_total}" "${1}"
    cleanup
    if (cd "${BASEDIR}/test/tests" && DOTBOT_TEST=true bash "${1}"); then
        pass
    else
        fail
    fi
}

report() {
    printf -- "test report\n"
    printf -- "-----------\n"
    printf -- "- %3d run\n" ${tests_run}
    printf -- "- %3d passed\n" ${tests_passed}
    if [ ${tests_failed} -gt 0 ]; then
        printf -- "- %3d failed\n" ${tests_failed}
        echo
        red "==> not ok!"
        return 1
    else
        echo
        green "==> all ok."
        return 0
    fi
}

die() {
    >&2 echo $@
    >&2 echo "terminating..."
    exit 1
}