tests: fix issues with non-portable shell constructs
[rainbows.git] / t / GNUmakefile
blob7eac0d5aa05eb01832e6e2652c9aaf14c10c305f
1 # we can run tests in parallel with GNU make
3 all::
5 ruby = ruby
6 rainbows_lib := $(shell cd ../lib && pwd)
7 -include ../local.mk
8 ifeq ($(RUBY_VERSION),)
9 RUBY_VERSION := $(shell $(ruby) -e 'puts RUBY_VERSION')
10 endif
12 ifeq ($(RUBYLIB),)
13 RUBYLIB := $(rainbows_lib)
14 else
15 RUBYLIB := $(rainbows_lib):$(RUBYLIB)
16 endif
17 export RUBYLIB
19 T = $(wildcard t[0-9][0-9][0-9][0-9]-*.sh)
21 all:: $(T)
23 # can't rely on "set -o pipefail" since we don't require bash or ksh93 :<
24 t_pfx = trash/$@-$(RUBY_VERSION)
25 t_code = $(t_pfx).code
26 t_log = $(t_pfx).log
27 # TRACER = strace -f -o $(t_pfx).strace -s 100000
28 # TRACER = /usr/bin/time -o $(t_pfx).time
29 t_run = $(TRACER) $(SHELL) $(SH_TEST_OPTS) $@
31 # prefix stdout messages with ':', and stderr messages with '!'
32 t_wrap = ( ( ( echo 42 > $(t_code); \
33 $(t_run); \
34 echo $$? > $(t_code) ) \
35 | sed 's/^/$(pfx):/' 1>&3 ) 2>&1 \
36 | sed 's/^/$(pfx)!/' 1>&2 ) 3>&1
38 ifndef V
39 quiet_pre = @echo '* $@';
40 quiet_post = > $(t_log) 2>&1; exit $$(cat $(t_code))
41 pfx =
42 else
43 ifeq ($(V),2)
44 SH_TEST_OPTS += -x
45 endif
46 quiet_pre = @echo '* $@';
47 quiet_post = 2>&1 | ./bin/utee $(t_log); exit $$(cat $(t_code))
48 pfx = $@
49 endif
51 run_test = $(quiet_pre) ( $(t_wrap) ) $(quiet_post)
53 test-bin-$(RUBY_VERSION)/rainbows: ruby_bin = $(shell which $(ruby))
54 test-bin-$(RUBY_VERSION)/rainbows: ../bin/rainbows
55 mkdir -p $(@D)
56 install -m 755 $^ $@+
57 $(ruby) -i -p -e '$$_.gsub!(%r{^#!.*$$},"#!$(ruby_bin)")' $@+
58 cmp $@+ $@ 2>/dev/null || mv $@+ $@
59 $(RM) $@+
61 req_random_blob := t3100-revactor-tee-input
62 random_blob:
63 dd if=/dev/urandom bs=1M count=10 of=$@+
64 mv $@+ $@
66 $(addsuffix .sh,$(req_random_blob)): random_blob
68 $(T): trash/.gitignore
69 $(T): export ruby := $(ruby)
70 $(T): export PATH := $(CURDIR)/test-bin-$(RUBY_VERSION):$(PATH)
71 $(T): test-bin-$(RUBY_VERSION)/rainbows
72 $(run_test)
74 trash/.gitignore:
75 mkdir -p $(@D)
76 echo '*' > $@
78 clean:
79 $(RM) -r trash/*.log trash/*.code test-bin-$(RUBY_VERSION)
81 .PHONY: $(T) clean