on master help sign in
Add a script to run tests

This allows us to build and run individual tests or groups of tests. The script is supposed to be extended to run hpc/coverage manually after running the tests. Note: cabal fails to run coverage if the test module is separated from the library.

commit299bdffdd60fd64ec3bbfc31f946903fe13cf4e7 authorHarendra Kumar <harendra@composewell.com> dateSat 19 Dec 2020, 18:13 (6 years ago) parent9e37229
8 files changed+387−5
bench.sh+1−1 benchmark/streamly-benchmarks.cabal+1−1 bin/build-lib.sh+207 bin/targets.sh+86 bin/test.sh+86 streamly.cabal+3 test/Streamly/Test/Prelude/MaxRate.hs → test/Streamly/Test/Prelude/Rate.hs test/streamly-tests.cabal+3−3
bench.sh+1−1
@@ -30,8 +30,8 @@ prelude_other_grp="\
3030 Prelude.Adaptive"
3131
3232array_grp="\
33− Memory.Array \
3433 Data.Array \
34+ Data.Array.Storable.Foreign \
3535 Data.Array.Prim \
3636 Data.SmallArray \
3737 Data.Array.Prim.Pinned"
benchmark/streamly-benchmarks.cabal+1−1
@@ -431,7 +431,7 @@ benchmark Data.Array.Prim.Pinned
431431 other-modules: Streamly.Benchmark.Data.ArrayOps
432432 cpp-options: -DDATA_ARRAY_PRIM_PINNED
433433
434−benchmark Memory.Array
434+benchmark Data.Array.Storable.Foreign
435435 import: bench-options
436436 type: exitcode-stdio-1.0
437437 hs-source-dirs: .
bin/build-lib.shnew file+207
@@ -0,0 +1,207 @@
1+# $1: message
2+die () {
3+ >&2 echo -e "Error: $1"
4+ exit 1
5+}
6+
7+# $1: command
8+function run_verbose() {
9+ echo "$*"
10+ bash -c "$*"
11+}
12+
13+has_item () {
14+ for i in $1
15+ do
16+ if test "$i" = "$2"
17+ then
18+ echo "$i"
19+ break
20+ fi
21+ done
22+}
23+
24+#------------------------------------------------------------------------------
25+# target groups
26+#------------------------------------------------------------------------------
27+
28+test_only () {
29+ if test -n "$RUNNING_TESTS"
30+ then
31+ echo $1
32+ fi
33+}
34+
35+bench_only () {
36+ if test -n "$RUNNING_BENCHMARKS"
37+ then
38+ echo $1
39+ fi
40+}
41+
42+dev_build () {
43+ if test -n "$RUNNING_DEVBUILD"
44+ then
45+ echo $1
46+ fi
47+}
48+
49+# A group consisting of all known individual targets
50+all_grp () {
51+ { for i in $GROUP_TARGETS
52+ do
53+ eval "echo \$$i"
54+ done
55+ echo $INDIVIDUAL_TARGETS
56+ } | sort | uniq
57+}
58+
59+# All groups including all_grp
60+all_target_groups () {
61+ echo all_grp
62+ echo $GROUP_TARGETS
63+}
64+
65+# XXX pass as arg
66+list_targets () {
67+ echo "Individual targets:"
68+ for i in $(all_grp)
69+ do
70+ echo "$i"
71+ done
72+ echo
73+}
74+
75+# XXX pass as arg
76+list_target_groups () {
77+ echo "Target groups:"
78+ for i in $(all_target_groups)
79+ do
80+ echo -n "$i ["
81+ eval "echo -n \$$i"
82+ echo "]"
83+ done
84+ echo
85+}
86+
87+# XXX pass as arg
88+set_targets() {
89+ if test -z "$TARGETS"
90+ then
91+ echo $DEFAULT_TARGETS
92+ else
93+ for i in $(echo $TARGETS)
94+ do
95+ case $i in
96+ *_grp) eval "echo -n \$${i}" ;;
97+ *_cmp) eval "echo -n \$${i} $i" ;;
98+ *) echo -n $i ;;
99+ esac
100+ echo -n " "
101+ done
102+ fi
103+}
104+
105+# We run the benchmarks in isolation in a separate process so that different
106+# benchmarks do not interfere with other. To enable that we need to pass the
107+# benchmark exe path to gauge as an argument. Unfortunately it cannot find its
108+# own path currently.
109+
110+# The path is dependent on the architecture and cabal version.
111+
112+# $1: package name
113+# $2: target
114+cabal_target_prog () {
115+ local target_prog=`$WHICH_COMMAND $1 $2`
116+ if test -x "$target_prog"
117+ then
118+ echo $target_prog
119+ else
120+ return 1
121+ fi
122+}
123+
124+set_common_vars () {
125+ SLOW=0
126+ QUICK_MODE=0
127+
128+ RUNNING_TESTS=
129+ RUNNING_BENCHMARKS=
130+ RUNNING_DEVBUILD=
131+
132+ TARGET_EXE_ARGS=
133+ RTS_OPTIONS=
134+
135+ GHC_VERSION=$(ghc --numeric-version)
136+
137+ CABAL_BUILD_OPTIONS=""
138+ CABAL_EXECUTABLE=cabal
139+
140+ # Use branch specific builds if git-cabal is present in PATH
141+ BUILD_DIR=dist-newstyle
142+ if which git-cabal 2>/dev/null
143+ then
144+ echo "Using git-cabal for branch specific builds"
145+ CABAL_EXECUTABLE=git-cabal
146+ BUILD_DIR=$(git-cabal show-builddir)
147+ fi
148+}
149+
150+# XXX cabal issue "cabal v2-exec which" cannot find benchmark/test executables
151+
152+# $1: builddir
153+# $2: package name
154+# $3: command to find
155+cabal_which_builddir() {
156+ find $1 -type f -path "*${GHC_VERSION}/${2}*/$3" 2>/dev/null
157+}
158+
159+# $1: package name
160+# $2: command to find
161+cabal_which() {
162+ cabal_which_builddir $BUILD_DIR $1 $2
163+}
164+
165+# $1: build program
166+# $2: package name
167+# $3: component prefix
168+# $4: targets
169+run_build () {
170+ local build_prog=$1
171+ local package=$2
172+ local component_prefix=$3
173+ local COMPONENTS
174+ local c
175+
176+ for c in $4
177+ do
178+ COMPONENTS+="$package:$component_prefix:$c "
179+ done
180+ run_verbose $build_prog $COMPONENTS || die "build failed"
181+}
182+
183+# $1: package name
184+# $2: target
185+run_target () {
186+ local package_name=$1
187+ local target_name=$2
188+ local target_exe=$target_name
189+ local target_prog
190+ target_prog=$(cabal_target_prog $package_name $target_exe) || \
191+ die "Cannot find executable for target $target_name"
192+
193+ echo "Running executable $target_name ..."
194+ TARGET_EXE_EXTRA_ARGS="$RTS_OPTIONS"
195+
196+ run_verbose $target_prog $TARGET_EXE_EXTRA_ARGS $TARGET_EXE_ARGS \
197+ || die "Target exe failed"
198+}
199+
200+# $1: package name
201+# $2: targets
202+run_targets() {
203+ for i in $2
204+ do
205+ run_target $1 $i
206+ done
207+}
bin/targets.shnew file+86
@@ -0,0 +1,86 @@
1+#------------------------------------------------------------------------------
2+# test and benchmark groups
3+#------------------------------------------------------------------------------
4+
5+# IMPORTANT NOTE: the names "_grp" and "_cmp" suffixes are special, do
6+# not rename them to something else.
7+
8+base_stream_grp="\
9+ `bench_only Data.Stream.StreamD` \
10+ `bench_only Data.Stream.StreamK` \
11+ `bench_only Data.Stream.StreamDK`"
12+
13+prelude_serial_grp="\
14+ Prelude.Serial \
15+ Prelude.WSerial \
16+ Prelude.ZipSerial"
17+
18+prelude_concurrent_grp="\
19+ Prelude.Async \
20+ Prelude.WAsync \
21+ Prelude.Ahead \
22+ Prelude.Parallel \
23+ Prelude.ZipAsync"
24+
25+
26+prelude_other_grp="\
27+ `test_only Prelude` \
28+ $(test_only $(dev_build Prelude.Rate)) \
29+ `bench_only Prelude.Rate` \
30+ `test_only Prelude.Fold` \
31+ Prelude.Concurrent \
32+ `bench_only Prelude.Adaptive`"
33+
34+array_grp="\
35+ Data.Array \
36+ Data.Array.Storable.Foreign \
37+ Data.Array.Prim \
38+ Data.SmallArray \
39+ Data.Array.Prim.Pinned"
40+
41+base_parser_grp="\
42+ Data.Parser.ParserD \
43+ `bench_only Data.Parser.ParserK`"
44+
45+parser_grp="\
46+ Data.Fold \
47+ Data.Parser"
48+
49+list_grp="\
50+ Data.List.Base \
51+ Data.List"
52+
53+#------------------------------------------------------------------------------
54+# Streaming vs non-streaming
55+#------------------------------------------------------------------------------
56+# The "o-1-space" groups of these benchmarks are run with long stream
57+# sizes when --long option is used.
58+
59+infinite_grp="\
60+ $prelude_serial_grp \
61+ $prelude_concurrent_grp \
62+ `bench_only Prelude.Rate`"
63+
64+#------------------------------------------------------------------------------
65+# All test/benchmark modules must be in at least one of these
66+#------------------------------------------------------------------------------
67+
68+# All groups
69+GROUP_TARGETS="\
70+ base_stream_grp \
71+ prelude_serial_grp \
72+ prelude_concurrent_grp \
73+ prelude_other_grp \
74+ array_grp \
75+ base_parser_grp \
76+ parser_grp \
77+ list_grp \
78+ infinite_grp"
79+
80+# Not in any groups
81+INDIVIDUAL_TARGETS="\
82+ Data.Unfold \
83+ Unicode.Stream \
84+ `bench_only FileSystem.Handle` \
85+ `test_only FileSystem.Event` \
86+ `test_only version-bounds`"
bin/test.shnew file+86
@@ -0,0 +1,86 @@
1+#!/usr/bin/env bash
2+
3+#------------------------------------------------------------------------------
4+# Script
5+#------------------------------------------------------------------------------
6+
7+SCRIPT_DIR=$(dirname $0)
8+
9+print_help () {
10+ echo "Usage: $0 "
11+ echo " [--targets <"target1 target2 ..." | help>]"
12+ echo " [--slow]"
13+ echo " [--quick]"
14+ echo " [--cabal-build-options <option>]"
15+ echo " -- <hspec options or test names>"
16+ echo
17+ echo "--targets: targets to run, use 'help' for list of targets"
18+ echo "--slow: take longer but run more tests"
19+ echo "--quick: faster but run fewer tests"
20+ echo "--cabal-build-options: Pass any cabal build options to be used for build"
21+ echo
22+ echo "Any arguments after a '--' are passed directly to hspec"
23+ exit
24+}
25+
26+#-----------------------------------------------------------------------------
27+# Read command line
28+#-----------------------------------------------------------------------------
29+
30+RUNNING_TESTS=y
31+source $SCRIPT_DIR/build-lib.sh
32+
33+set_common_vars
34+
35+# XXX add a bisect option
36+while test -n "$1"
37+do
38+ case $1 in
39+ -h|--help|help) print_help ;;
40+ # options with arguments
41+ --targets) shift; TARGETS=$1; shift ;;
42+ --cabal-build-options) shift; CABAL_BUILD_OPTIONS=$1; shift ;;
43+ --rtsopts) shift; RTS_OPTIONS=$1; shift ;;
44+ # flags
45+ --slow) SLOW=1; shift ;;
46+ --quick) QUICK_MODE=1; shift ;;
47+ --dev-build) RUNNING_DEVBUILD=1; shift ;;
48+ --) shift; break ;;
49+ -*|--*) echo "Unknown flags: $*"; echo; print_help ;;
50+ *) break ;;
51+ esac
52+done
53+TARGET_EXE_ARGS=$*
54+
55+#-----------------------------------------------------------------------------
56+# Determine targets
57+#-----------------------------------------------------------------------------
58+
59+# Requires RUNNING_DEVBUILD var
60+source $SCRIPT_DIR/targets.sh
61+
62+if test "$(has_item "$TARGETS" help)" = "help"
63+then
64+ list_target_groups
65+ list_targets
66+ exit
67+fi
68+
69+DEFAULT_TARGETS="$(all_grp)"
70+TARGETS=$(set_targets)
71+
72+echo "Using targets [$TARGETS]"
73+
74+#-----------------------------------------------------------------------------
75+# Build targets
76+#-----------------------------------------------------------------------------
77+
78+WHICH_COMMAND=cabal_which
79+BUILD_TEST="$CABAL_EXECUTABLE v2-build $CABAL_BUILD_OPTIONS --enable-tests"
80+run_build "$BUILD_TEST" streamly-tests test "$TARGETS"
81+
82+#-----------------------------------------------------------------------------
83+# Run targets
84+#-----------------------------------------------------------------------------
85+
86+run_targets streamly-tests "$TARGETS"
streamly.cabal+3
@@ -121,8 +121,11 @@ extra-source-files:
121121 benchmark/lib/Streamly/Benchmark/Common/*.hs
122122 benchmark/streamly-benchmarks.cabal
123123 bin/bench-exec-one.sh
124+ bin/build-lib.sh
124125 bin/mk-hscope.sh
125126 bin/mk-tags.sh
127+ bin/targets.sh
128+ bin/test.sh
126129 cabal.project
127130 cabal.project.ci
128131 charts-0/streamly-vs-list-time.svg
test/Streamly/Test/Prelude/MaxRate.hs → test/Streamly/Test/Prelude/Rate.hsrenamed

The content is the same.

test/streamly-tests.cabal+3−3
@@ -247,11 +247,11 @@ test-suite Prelude.Fold
247247 main-is: Streamly/Test/Prelude/Fold.hs
248248 ghc-options: -main-is Streamly.Test.Prelude.Fold.main
249249
250−test-suite Prelude.MaxRate
250+test-suite Prelude.Rate
251251 import:always-optimized
252252 type: exitcode-stdio-1.0
253− main-is: Streamly/Test/Prelude/MaxRate.hs
254− ghc-options: -main-is Streamly.Test.Prelude.MaxRate.main
253+ main-is: Streamly/Test/Prelude/Rate.hs
254+ ghc-options: -main-is Streamly.Test.Prelude.Rate.main
255255 if flag(dev)
256256 buildable: True
257257 else