]>
Commit | Line | Data |
---|---|---|
245dac4a PB |
1 | #! /usr/bin/env python3 |
2 | ||
3 | # Create Makefile targets to run tests, from Meson's test introspection data. | |
4 | # | |
5 | # Author: Paolo Bonzini <pbonzini@redhat.com> | |
6 | ||
7 | from collections import defaultdict | |
48a81fd5 | 8 | import itertools |
245dac4a PB |
9 | import json |
10 | import os | |
11 | import shlex | |
12 | import sys | |
13 | ||
14 | class Suite(object): | |
15 | def __init__(self): | |
16 | self.tests = list() | |
17 | self.slow_tests = list() | |
18 | self.executables = set() | |
19 | ||
20 | print(''' | |
21 | SPEED = quick | |
22 | ||
42d729e1 | 23 | # $1 = environment, $2 = test command, $3 = test name, $4 = dir |
c2f4c1a8 | 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) |
d322e84e | 25 | .test-human-exitcode = $1 $(PYTHON) scripts/test-driver.py $(if $4,-C$4) $(if $(V),--verbose) -- $2 < /dev/null |
42d729e1 PB |
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" | |
40d9b74e | 28 | .test.human-print = echo $(if $(V),'$1 $2','Running test $3') && |
245dac4a PB |
29 | .test.env = MALLOC_PERTURB_=$${MALLOC_PERTURB_:-$$(( $${RANDOM:-0} % 255 + 1))} |
30 | ||
31 | # $1 = test name, $2 = test target (human or tap) | |
40d9b74e | 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)) |
245dac4a | 33 | |
40d9b74e | 34 | .test.output-format = human |
245dac4a PB |
35 | ''') |
36 | ||
40d9b74e | 37 | introspect = json.load(sys.stdin) |
245dac4a | 38 | i = 0 |
40d9b74e | 39 | |
48a81fd5 | 40 | def process_tests(test, targets, suites): |
40d9b74e | 41 | global i |
245dac4a PB |
42 | env = ' '.join(('%s=%s' % (shlex.quote(k), shlex.quote(v)) |
43 | for k, v in test['env'].items())) | |
cb23fd47 YL |
44 | executable = test['cmd'][0] |
45 | try: | |
46 | executable = os.path.relpath(executable) | |
47 | except: | |
48 | pass | |
245dac4a | 49 | if test['workdir'] is not None: |
cb23fd47 YL |
50 | try: |
51 | test['cmd'][0] = os.path.relpath(executable, test['workdir']) | |
52 | except: | |
53 | test['cmd'][0] = executable | |
245dac4a PB |
54 | else: |
55 | test['cmd'][0] = executable | |
555b27a7 | 56 | cmd = ' '.join((shlex.quote(x) for x in test['cmd'])) |
245dac4a PB |
57 | driver = test['protocol'] if 'protocol' in test else 'exitcode' |
58 | ||
59 | i += 1 | |
42d729e1 PB |
60 | if test['workdir'] is not None: |
61 | print('.test.dir.%d := %s' % (i, shlex.quote(test['workdir']))) | |
48a81fd5 PB |
62 | |
63 | if 'depends' in test: | |
64 | deps = (targets.get(x, []) for x in test['depends']) | |
65 | deps = itertools.chain.from_iterable(deps) | |
66 | else: | |
67 | deps = ['all'] | |
68 | ||
245dac4a PB |
69 | print('.test.name.%d := %s' % (i, test['name'])) |
70 | print('.test.driver.%d := %s' % (i, driver)) | |
555b27a7 | 71 | print('.test.env.%d := $(.test.env) %s' % (i, env)) |
245dac4a | 72 | print('.test.cmd.%d := %s' % (i, cmd)) |
09e93326 | 73 | print('.test.deps.%d := %s' % (i, ' '.join(deps))) |
40d9b74e | 74 | print('.PHONY: run-test-%d' % (i,)) |
09e93326 | 75 | print('run-test-%d: $(.test.deps.%d)' % (i,i)) |
40d9b74e | 76 | print('\t@$(call .test.run,%d,$(.test.output-format))' % (i,)) |
245dac4a PB |
77 | |
78 | test_suites = test['suite'] or ['default'] | |
79 | is_slow = any(s.endswith('-slow') for s in test_suites) | |
80 | for s in test_suites: | |
81 | # The suite name in the introspection info is "PROJECT:SUITE" | |
82 | s = s.split(':')[1] | |
83 | if s.endswith('-slow'): | |
84 | s = s[:-5] | |
85 | if is_slow: | |
86 | suites[s].slow_tests.append(i) | |
87 | else: | |
88 | suites[s].tests.append(i) | |
89 | suites[s].executables.add(executable) | |
90 | ||
40d9b74e PB |
91 | def emit_prolog(suites, prefix): |
92 | all_tap = ' '.join(('%s-report-%s.tap' % (prefix, k) for k in suites.keys())) | |
93 | print('.PHONY: %s %s-report.tap %s' % (prefix, prefix, all_tap)) | |
94 | print('%s: run-tests' % (prefix,)) | |
95 | print('%s-report.tap %s: %s-report%%.tap: all' % (prefix, all_tap, prefix)) | |
96 | print('''\t$(MAKE) .test.output-format=tap --quiet -Otarget V=1 %s$* | ./scripts/tap-merge.pl | tee "$@" \\ | |
97 | | ./scripts/tap-driver.pl $(if $(V),, --show-failures-only)''' % (prefix, )) | |
98 | ||
99 | def emit_suite(name, suite, prefix): | |
245dac4a PB |
100 | executables = ' '.join(suite.executables) |
101 | slow_test_numbers = ' '.join((str(x) for x in suite.slow_tests)) | |
102 | test_numbers = ' '.join((str(x) for x in suite.tests)) | |
40d9b74e PB |
103 | target = '%s-%s' % (prefix, name) |
104 | print('.test.quick.%s := %s' % (target, test_numbers)) | |
105 | print('.test.slow.%s := $(.test.quick.%s) %s' % (target, target, slow_test_numbers)) | |
106 | print('%s-build: %s' % (prefix, executables)) | |
107 | print('.PHONY: %s' % (target, )) | |
108 | print('.PHONY: %s-report-%s.tap' % (prefix, name)) | |
109 | print('%s: run-tests' % (target, )) | |
110 | print('ifneq ($(filter %s %s, $(MAKECMDGOALS)),)' % (target, prefix)) | |
111 | print('.tests += $(.test.$(SPEED).%s)' % (target, )) | |
112 | print('endif') | |
47e3424a | 113 | print('all-%s-targets += %s' % (prefix, target)) |
40d9b74e | 114 | |
48a81fd5 PB |
115 | targets = {t['id']: [os.path.relpath(f) for f in t['filename']] |
116 | for t in introspect['targets']} | |
117 | ||
40d9b74e | 118 | testsuites = defaultdict(Suite) |
9ed7247a | 119 | for test in introspect['tests']: |
48a81fd5 | 120 | process_tests(test, targets, testsuites) |
40d9b74e PB |
121 | emit_prolog(testsuites, 'check') |
122 | for name, suite in testsuites.items(): | |
123 | emit_suite(name, suite, 'check') | |
124 | ||
9ed7247a PB |
125 | benchsuites = defaultdict(Suite) |
126 | for test in introspect['benchmarks']: | |
48a81fd5 | 127 | process_tests(test, targets, benchsuites) |
9ed7247a PB |
128 | emit_prolog(benchsuites, 'bench') |
129 | for name, suite in benchsuites.items(): | |
130 | emit_suite(name, suite, 'bench') | |
131 | ||
40d9b74e | 132 | print('run-tests: $(patsubst %, run-test-%, $(.tests))') |