summaryrefslogtreecommitdiff
path: root/integration-tests/run-test.sh
blob: c9556ebdd7441556f2c09847629efaa1b7eec71d (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
#!/bin/bash

set -u

GREEN="\e[0;32m"
YELLOW="\e[0;33m"
RED="\e[0;31m"
END="\e[0m"

usage () {
	cat <<EOF
Usage:
	run-test.sh [-h|--help] [-a <arg>|--arg <arg>] <command> [<args>]

Run various commands to test bst.

Commands:

	test	Run the test suite. If no arguments are given, the full
                suite is run, otherwise the given arguments will be run
	run	Run the test suite.  (Does not clean)
	clean	Clean temporary test files

Options:
	--help  	Display this help message and exit
	--arg   	Specify an argument for bst, such as --colors
	--cov   	Specify a coverage rcfile
	--sources	Specify a location for the source cache
EOF
}

BST_COVERAGE=
BST_FLAGS=
BST_SOURCE_CACHE=
export BST_COVERAGE
export BST_FLAGS
export BST_SOURCE_CACHE

main () {
	while : ;
	do
		case "${1:-}" in
			"test")
				shift
				configure
				clean "$@"
				run "$@"
				break ;;
			"run")
				shift
				configure
				run "$@"
				break ;;
			"clean")
				shift
				clean "$@"
				break ;;
			--sources)
				export BST_SOURCE_CACHE=$(realpath "${2}")
				shift 2 ;;
			-c|--cov)
				export BST_COVERAGE=$(realpath "${2}")
				shift 2 ;;
			-a|--arg)
				export BST_FLAGS="${BST_FLAGS:-} $2"
				shift 2 ;;
			-h|--help)
				usage
				break ;;
			*)
				echo "Error: Unrecognized argument '${1:-}'" 1>&2
				usage
				break ;;
		esac
	done
}


# configure
#
# Creates the buildstream.conf configuration
configure () {
    	# Treat source cache specially, we want to reuse it when
	# running automated CI
	if [ -z "${BST_SOURCE_CACHE}" ]; then
	    BST_SOURCE_CACHE="$(pwd)/tmp/sources"
	fi

        # Create buildstream.conf
        cat > "$(pwd)/buildstream.conf" <<EOF
sourcedir: "${BST_SOURCE_CACHE}"
builddir: "$(pwd)/tmp/build"
artifactdir: "$(pwd)/tmp/artifacts"
logdir: "$(pwd)/tmp/logs"
EOF
        CONFIG_LOCATION="$(pwd)/buildstream.conf"
        export CONFIG_LOCATION
}


# run
#
# Run all tests in the current directory.
run () {
	local succeeded=0
	local failed=0
	local state
	local tests
	local dir

	if [ $# -ge 1 ];
	then
		tests=$@
	else
		tests="*"
	fi

	for dir in $tests;
	do
		if [ -d "$dir" ] && [ "$dir" != "tmp" ]
		then
			run-test "$dir"
			state=$?
			if [ $state == 0 ]
			then
				((succeeded++))
			else
				((failed++))
			fi
		fi
	done

	if [ ! -z "${BST_COVERAGE}" ]; then
	    if [ -f .coverage ]; then
		rm -f .coverage
	    fi

	    for file in $(find . -name ".coverage.*"); do
		coverage combine -a ${file}
	    done
	    coverage report -m
	fi

	echo
	printf "%4s test%.*s ${GREEN}succeeded${END}.\n" $succeeded $((succeeded != 1)) "s"
	printf "%4s test%.*s ${RED}failed${END}.\n" $failed $((failed != 1)) "s"

	if [ $failed != 0 ]
	then
	   exit 1
	fi
}

# clean
#
# Clean all tests in the current directory.
clean () {
	local dir

	for dir in *;
	do
		if [ -d "$dir" ]
		then
			(cd "$dir" || exit 1
			 rm -rf "results/"*
			 rm -rf ".bst/"
			 rm -rf "$(pwd)/tmp/")
		fi
	done
}

# run-test
#
# Run the test in the given directory
#
# Args:
#    test ($1) - The test to run
#
run-test () {
	local test="$1"

	echo "============================================================"
	echo "Running tests for test case '$test'"
	echo "============================================================"

	(cd "$test" || exit 1
	 bash "run-$(basename "$test").sh")

	if [ ! "$?" -eq 0 ]
	then
		echo -e "Tests for '$test' ${RED}failed${END}.\n" 2>&1
		return 1
	fi
}

main "$@"