summaryrefslogtreecommitdiffstats
path: root/apts
blob: 033b11b37c5c88cabd90c4138a7633bf3285de25 (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
#!/bin/sh

# apts is a testing suite for packages on the Alpine Linux system
# In theory, it will work on any distribution that uses the apk-tools
# package manager
# Author: Jeff Bilyk <jbilyk@gmail.com>
# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Version History available at http://git.alpinelinux.org/cgit/apts/

# Get file to output to 
program=$0

usage() {
	echo "Usage: $program [-he] [-o OUTDIR] [TEST...]"
	exit 1
}

# do not halt on fail by default
halt_on_fail=

# default outdir is current working dir
outdir="$PWD"

# default TESTDIR to $PWD/tests so we can run it directly from git dir
# allow override with APTS_DIR env var
if [ -d .git ] && [ -d tests ]; then
	TESTSDIR=${APTS_DIR:-"$PWD/tests"}
else
	# default to /usr/share/apts/tests
	TESTSDIR=${APTS_DIR:-"/usr/share/apts/tests"}
	# allow use to override
	if [ -r /etc/apts/apts.conf ]; then
		. /etc/apts/apts.conf
	fi
fi

# parse opts
while getopts "d:eho:" opt; do
	case "$opt" in
	d) TESTSDIR="$OPTARG";;
	e) halt_on_fail=yes;;
	h) usage;;
	o) outdir=$OPTARG;;
	esac
done

# Remove trailing slash from outdir
if ! [ $outdir == "$PWD" ]; then
	outdir=`echo "$outdir" | sed "s,/$,,"`;
fi

#remove opts so that package(s) is $@
shift $(( $OPTIND - 1 ))

# CD to test file dir. will print error message if needed so we dont bother
cd "$TESTSDIR" || exit 1

# if there aren't any args then test all packages
if [ $# -eq 0 ]; then
	set -- *
fi

totaltests=$#

# Initialize counter
i=0

echo "${totaltests} tests to be run"
passedtests="0"
failed=
for tst in "$@"; do
	# Increment counter for tested packages
	i=$((i + 1))
	outfile="$outdir"/$tst.out
	echo -n "Testing ${tst%.out} (${i}/${totaltests})..."
	if /bin/sh -e $tst $tst > "$outfile" 2>&1; then
		# test passed
		echo " ok"
		rm "$outfile"
		passedtests=$((passedtests + 1))
	else
		# test failed
		echo " failed"
		failed="$failed $tst"
		[ -n "$halt_on_fail" ] && break
	fi
done

echo ""
echo "$passedtests of $totaltests passed"

if [ -n "$failed" ]; then
	echo "The following packages failed:"
	echo $failed
fi

# return success if all tests passed
exit $(( $totaltests - $passedtests))