summaryrefslogtreecommitdiff
path: root/integration-tests/run-test.sh
blob: f166d2f89815a3f459df26280db4796d4d2be367 (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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/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
	omit	Omit the given test from test runs
	include	Include the given test in test runs

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 ;;
			"omit")
				shift
				omit "$@"
				break ;;
			"include")
				shift
				include "$@"
				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 omitted=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++))
			elif [ $state == 2 ]
			then
				((omitted++))
			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 ${YELLOW}omitted${END}.\n" $omitted $((omitted != 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"

	touch .omit
	if grep -q "$test" .omit
	then
		echo -e "${YELLOW}Omitting${END} test $test."
		return 2
	fi

	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
}

# omit
#
# Ignore the given test during future test runs
#
# Args:
#    test ($1) - The test to ignore
#
omit() {
	local test="$1"

	# Tell the user if we don't need to omit the file
	touch .omit
	if grep -q "$test" .omit
	then
		echo "Test $test is already omitted." 2>&1
		exit 1
	fi

	if [ -d "$test" ]
	then
		echo "$test" >> .omit
	else
		echo "No such test." 2>&1
		exit 1
	fi
}

# include
#
# After a test has been omitted, re-include it
#
# Args:
#    test ($1) - The test to include
#
include() {
	local test="$1"

	local temp

	touch .omit

	# Make a temporary file to inverse grep to
	temp=$(mktemp)

	# Tell the user if we don't need to include the file
	if ! grep -q "$test" .omit
	then
		echo "Test $test is already included." 2>&1
		exit 1
	fi

	# Remove the line containing the test string
	grep -v "$test" .omit > "$temp"; mv "$temp" .omit
}

main "$@"