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
17 self
.slow_tests
= list()
18 self
.executables
= set()
23 # $1 = environment, $2 = test command, $3 = test name, $4 = dir
24 .test-human-tap = $1 $(if $4,(cd $4 && $2),$2) -m $(SPEED) < /dev/null | ./scripts/tap-driver.pl --test-name="$3" $(if $(V),,--show-failures-only)
25 .test-human-exitcode = $1 $(PYTHON) scripts/test-driver.py $(if $4,-C$4) $(if $(V),--verbose) -- $2 < /dev/null
26 .test-tap-tap = $1 $(if $4,(cd $4 && $2),$2) < /dev/null | sed "s/^[a-z][a-z]* [0-9]*/& $3/" || true
27 .test-tap-exitcode = printf "%s\\n" 1..1 "`$1 $(if $4,(cd $4 && $2),$2) < /dev/null > /dev/null || echo "not "`ok 1 $3"
28 .test.human-print = echo $(if $(V),'$1 $2','Running test $3') &&
29 .test.env = MALLOC_PERTURB_=$${MALLOC_PERTURB_:-$$(( $${RANDOM:-0} % 255 + 1))}
31 # $1 = test name, $2 = test target (human or tap)
32 .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))
34 .test.output-format = human
37 introspect
= json
.load(sys
.stdin
)
40 def process_tests(test
, targets
, suites
):
42 env
= ' '.join(('%s=%s' % (shlex
.quote(k
), shlex
.quote(v
))
43 for k
, v
in test
['env'].items()))
44 executable
= test
['cmd'][0]
46 executable
= os
.path
.relpath(executable
)
49 if test
['workdir'] is not None:
51 test
['cmd'][0] = os
.path
.relpath(executable
, test
['workdir'])
53 test
['cmd'][0] = executable
55 test
['cmd'][0] = executable
56 cmd
= ' '.join((shlex
.quote(x
) for x
in test
['cmd']))
57 driver
= test
['protocol'] if 'protocol' in test
else 'exitcode'
60 if test
['workdir'] is not None:
61 print('.test.dir.%d := %s' % (i
, shlex
.quote(test
['workdir'])))
63 deps
= (targets
.get(x
, []) for x
in test
['depends'])
64 deps
= itertools
.chain
.from_iterable(deps
)
66 print('.test.name.%d := %s' % (i
, test
['name']))
67 print('.test.driver.%d := %s' % (i
, driver
))
68 print('.test.env.%d := $(.test.env) %s' % (i
, env
))
69 print('.test.cmd.%d := %s' % (i
, cmd
))
70 print('.test.deps.%d := %s' % (i
, ' '.join(deps
)))
71 print('.PHONY: run-test-%d' % (i
,))
72 print('run-test-%d: $(.test.deps.%d)' % (i
,i
))
73 print('\t@$(call .test.run,%d,$(.test.output-format))' % (i
,))
75 test_suites
= test
['suite'] or ['default']
76 is_slow
= any(s
.endswith('-slow') for s
in test_suites
)
78 # The suite name in the introspection info is "PROJECT:SUITE"
80 if s
.endswith('-slow'):
83 suites
[s
].slow_tests
.append(i
)
85 suites
[s
].tests
.append(i
)
86 suites
[s
].executables
.add(executable
)
88 def emit_prolog(suites
, prefix
):
89 all_tap
= ' '.join(('%s-report-%s.tap' % (prefix
, k
) for k
in suites
.keys()))
90 print('.PHONY: %s %s-report.tap %s' % (prefix
, prefix
, all_tap
))
91 print('%s: run-tests' % (prefix
,))
92 print('%s-report.tap %s: %s-report%%.tap: all' % (prefix
, all_tap
, prefix
))
93 print('''\t$(MAKE) .test.output-format=tap --quiet -Otarget V=1 %s$* | ./scripts/tap-merge.pl | tee "$@" \\
94 | ./scripts/tap-driver.pl $(if $(V),, --show-failures-only)''' % (prefix
, ))
96 def emit_suite(name
, suite
, prefix
):
97 executables
= ' '.join(suite
.executables
)
98 slow_test_numbers
= ' '.join((str(x
) for x
in suite
.slow_tests
))
99 test_numbers
= ' '.join((str(x
) for x
in suite
.tests
))
100 target
= '%s-%s' % (prefix
, name
)
101 print('.test.quick.%s := %s' % (target
, test_numbers
))
102 print('.test.slow.%s := $(.test.quick.%s) %s' % (target
, target
, slow_test_numbers
))
103 print('%s-build: %s' % (prefix
, executables
))
104 print('.PHONY: %s' % (target
, ))
105 print('.PHONY: %s-report-%s.tap' % (prefix
, name
))
106 print('%s: run-tests' % (target
, ))
107 print('ifneq ($(filter %s %s, $(MAKECMDGOALS)),)' % (target
, prefix
))
108 print('.tests += $(.test.$(SPEED).%s)' % (target
, ))
110 print('all-%s-targets += %s' % (prefix
, target
))
112 targets
= {t
['id']: [os
.path
.relpath(f
) for f
in t
['filename']]
113 for t
in introspect
['targets']}
115 testsuites
= defaultdict(Suite
)
116 for test
in introspect
['tests']:
117 process_tests(test
, targets
, testsuites
)
118 emit_prolog(testsuites
, 'check')
119 for name
, suite
in testsuites
.items():
120 emit_suite(name
, suite
, 'check')
122 benchsuites
= defaultdict(Suite
)
123 for test
in introspect
['benchmarks']:
124 process_tests(test
, targets
, benchsuites
)
125 emit_prolog(benchsuites
, 'bench')
126 for name
, suite
in benchsuites
.items():
127 emit_suite(name
, suite
, 'bench')
129 print('run-tests: $(patsubst %, run-test-%, $(.tests))')