1 #! /usr/bin/env python3
3 # Create Makefile targets to run tests, from Meson's test introspection data.
5 # Author: Paolo Bonzini <pbonzini@redhat.com>
7 from collections
import defaultdict
16 self
.slow_tests
= list()
17 self
.executables
= set()
22 # $1 = environment, $2 = test command, $3 = test name, $4 = dir
23 .test-human-tap = $1 $(if $4,(cd $4 && $2),$2) < /dev/null | ./scripts/tap-driver.pl --test-name="$3" $(if $(V),,--show-failures-only)
24 .test-human-exitcode = $1 $(PYTHON) scripts/test-driver.py $(if $4,-C$4) $(if $(V),--verbose) -- $2 < /dev/null
25 .test-tap-tap = $1 $(if $4,(cd $4 && $2),$2) < /dev/null | sed "s/^[a-z][a-z]* [0-9]*/& $3/" || true
26 .test-tap-exitcode = printf "%s\\n" 1..1 "`$1 $(if $4,(cd $4 && $2),$2) < /dev/null > /dev/null || echo "not "`ok 1 $3"
27 .test.human-print = echo $(if $(V),'$1 $2','Running test $3') &&
28 .test.env = MALLOC_PERTURB_=$${MALLOC_PERTURB_:-$$(( $${RANDOM:-0} % 255 + 1))}
30 # $1 = test name, $2 = test target (human or tap)
31 .test.run = $(call .test.$2-print,$(.test.env.$1),$(.test.cmd.$1),$(.test.name.$1)) $(call .test-$2-$(.test.driver.$1),$(.test.env.$1),$(.test.cmd.$1),$(.test.name.$1),$(.test.dir.$1))
33 .test.output-format = human
36 introspect
= json
.load(sys
.stdin
)
39 def process_tests(test
, suites
):
41 env
= ' '.join(('%s=%s' % (shlex
.quote(k
), shlex
.quote(v
))
42 for k
, v
in test
['env'].items()))
43 executable
= test
['cmd'][0]
45 executable
= os
.path
.relpath(executable
)
48 if test
['workdir'] is not None:
50 test
['cmd'][0] = os
.path
.relpath(executable
, test
['workdir'])
52 test
['cmd'][0] = executable
54 test
['cmd'][0] = executable
55 cmd
= ' '.join((shlex
.quote(x
) for x
in test
['cmd']))
56 driver
= test
['protocol'] if 'protocol' in test
else 'exitcode'
59 if test
['workdir'] is not None:
60 print('.test.dir.%d := %s' % (i
, shlex
.quote(test
['workdir'])))
61 print('.test.name.%d := %s' % (i
, test
['name']))
62 print('.test.driver.%d := %s' % (i
, driver
))
63 print('.test.env.%d := $(.test.env) %s' % (i
, env
))
64 print('.test.cmd.%d := %s' % (i
, cmd
))
65 print('.PHONY: run-test-%d' % (i
,))
66 print('run-test-%d: all' % (i
,))
67 print('\t@$(call .test.run,%d,$(.test.output-format))' % (i
,))
69 test_suites
= test
['suite'] or ['default']
70 is_slow
= any(s
.endswith('-slow') for s
in test_suites
)
72 # The suite name in the introspection info is "PROJECT:SUITE"
74 if s
.endswith('-slow'):
77 suites
[s
].slow_tests
.append(i
)
79 suites
[s
].tests
.append(i
)
80 suites
[s
].executables
.add(executable
)
82 def emit_prolog(suites
, prefix
):
83 all_tap
= ' '.join(('%s-report-%s.tap' % (prefix
, k
) for k
in suites
.keys()))
84 print('.PHONY: %s %s-report.tap %s' % (prefix
, prefix
, all_tap
))
85 print('%s: run-tests' % (prefix
,))
86 print('%s-report.tap %s: %s-report%%.tap: all' % (prefix
, all_tap
, prefix
))
87 print('''\t$(MAKE) .test.output-format=tap --quiet -Otarget V=1 %s$* | ./scripts/tap-merge.pl | tee "$@" \\
88 | ./scripts/tap-driver.pl $(if $(V),, --show-failures-only)''' % (prefix
, ))
90 def emit_suite(name
, suite
, prefix
):
91 executables
= ' '.join(suite
.executables
)
92 slow_test_numbers
= ' '.join((str(x
) for x
in suite
.slow_tests
))
93 test_numbers
= ' '.join((str(x
) for x
in suite
.tests
))
94 target
= '%s-%s' % (prefix
, name
)
95 print('.test.quick.%s := %s' % (target
, test_numbers
))
96 print('.test.slow.%s := $(.test.quick.%s) %s' % (target
, target
, slow_test_numbers
))
97 print('%s-build: %s' % (prefix
, executables
))
98 print('.PHONY: %s' % (target
, ))
99 print('.PHONY: %s-report-%s.tap' % (prefix
, name
))
100 print('%s: run-tests' % (target
, ))
101 print('ifneq ($(filter %s %s, $(MAKECMDGOALS)),)' % (target
, prefix
))
102 print('.tests += $(.test.$(SPEED).%s)' % (target
, ))
105 testsuites
= defaultdict(Suite
)
106 for test
in introspect
['tests']:
107 process_tests(test
, testsuites
)
108 emit_prolog(testsuites
, 'check')
109 for name
, suite
in testsuites
.items():
110 emit_suite(name
, suite
, 'check')
112 benchsuites
= defaultdict(Suite
)
113 for test
in introspect
['benchmarks']:
114 process_tests(test
, benchsuites
)
115 emit_prolog(benchsuites
, 'bench')
116 for name
, suite
in benchsuites
.items():
117 emit_suite(name
, suite
, 'bench')
119 print('run-tests: $(patsubst %, run-test-%, $(.tests))')