Paolo Bonzini | 245dac4 | 2020-01-28 14:48:54 +0100 | [diff] [blame] | 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 |
| 8 | import json |
| 9 | import os |
| 10 | import shlex |
| 11 | import sys |
| 12 | |
| 13 | class Suite(object): |
| 14 | def __init__(self): |
| 15 | self.tests = list() |
| 16 | self.slow_tests = list() |
| 17 | self.executables = set() |
| 18 | |
| 19 | print(''' |
| 20 | SPEED = quick |
| 21 | |
Paolo Bonzini | 555b27a | 2020-09-01 09:58:48 -0400 | [diff] [blame^] | 22 | # $1 = environment, $2 = test command, $3 = test name |
| 23 | .test-human-tap = $1 $2 < /dev/null | ./scripts/tap-driver.pl --test-name="$3" $(if $(V),,--show-failures-only) |
| 24 | .test-human-exitcode = $1 $2 < /dev/null |
| 25 | .test-tap-tap = $1 $2 < /dev/null | sed "s/^[a-z][a-z]* [0-9]*/& $3/" || true |
| 26 | .test-tap-exitcode = printf "%s\\n" 1..1 "`$1 $2 < /dev/null > /dev/null || echo "not "`ok 1 $3" |
| 27 | .test.print = echo $(if $(V),'$1 $2','Running test $3') >&3 |
Paolo Bonzini | 245dac4 | 2020-01-28 14:48:54 +0100 | [diff] [blame] | 28 | .test.env = MALLOC_PERTURB_=$${MALLOC_PERTURB_:-$$(( $${RANDOM:-0} % 255 + 1))} |
| 29 | |
| 30 | # $1 = test name, $2 = test target (human or tap) |
Paolo Bonzini | 555b27a | 2020-09-01 09:58:48 -0400 | [diff] [blame^] | 31 | .test.run = $(call .test.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)) |
Paolo Bonzini | 245dac4 | 2020-01-28 14:48:54 +0100 | [diff] [blame] | 32 | |
| 33 | define .test.human_k |
| 34 | @exec 3>&1; rc=0; $(foreach TEST, $1, $(call .test.run,$(TEST),human) || rc=$$?;) \\ |
| 35 | exit $$rc |
| 36 | endef |
| 37 | define .test.human_no_k |
| 38 | $(foreach TEST, $1, @exec 3>&1; $(call .test.run,$(TEST),human) |
| 39 | ) |
| 40 | endef |
| 41 | .test.human = \\ |
| 42 | $(if $(findstring k, $(MAKEFLAGS)), $(.test.human_k), $(.test.human_no_k)) |
| 43 | |
| 44 | define .test.tap |
| 45 | @exec 3>&1; { $(foreach TEST, $1, $(call .test.run,$(TEST),tap); ) } \\ |
| 46 | | ./scripts/tap-merge.pl | tee "$@" \\ |
| 47 | | ./scripts/tap-driver.pl $(if $(V),, --show-failures-only) |
| 48 | endef |
| 49 | ''') |
| 50 | |
| 51 | suites = defaultdict(Suite) |
| 52 | i = 0 |
| 53 | for test in json.load(sys.stdin): |
| 54 | env = ' '.join(('%s=%s' % (shlex.quote(k), shlex.quote(v)) |
| 55 | for k, v in test['env'].items())) |
Yonggang Luo | cb23fd4 | 2020-08-26 23:10:02 +0800 | [diff] [blame] | 56 | executable = test['cmd'][0] |
| 57 | try: |
| 58 | executable = os.path.relpath(executable) |
| 59 | except: |
| 60 | pass |
Paolo Bonzini | 245dac4 | 2020-01-28 14:48:54 +0100 | [diff] [blame] | 61 | if test['workdir'] is not None: |
Yonggang Luo | cb23fd4 | 2020-08-26 23:10:02 +0800 | [diff] [blame] | 62 | try: |
| 63 | test['cmd'][0] = os.path.relpath(executable, test['workdir']) |
| 64 | except: |
| 65 | test['cmd'][0] = executable |
Paolo Bonzini | 245dac4 | 2020-01-28 14:48:54 +0100 | [diff] [blame] | 66 | else: |
| 67 | test['cmd'][0] = executable |
Paolo Bonzini | 555b27a | 2020-09-01 09:58:48 -0400 | [diff] [blame^] | 68 | cmd = ' '.join((shlex.quote(x) for x in test['cmd'])) |
Paolo Bonzini | 245dac4 | 2020-01-28 14:48:54 +0100 | [diff] [blame] | 69 | if test['workdir'] is not None: |
| 70 | cmd = '(cd %s && %s)' % (shlex.quote(test['workdir']), cmd) |
| 71 | driver = test['protocol'] if 'protocol' in test else 'exitcode' |
| 72 | |
| 73 | i += 1 |
| 74 | print('.test.name.%d := %s' % (i, test['name'])) |
| 75 | print('.test.driver.%d := %s' % (i, driver)) |
Paolo Bonzini | 555b27a | 2020-09-01 09:58:48 -0400 | [diff] [blame^] | 76 | print('.test.env.%d := $(.test.env) %s' % (i, env)) |
Paolo Bonzini | 245dac4 | 2020-01-28 14:48:54 +0100 | [diff] [blame] | 77 | print('.test.cmd.%d := %s' % (i, cmd)) |
| 78 | |
| 79 | test_suites = test['suite'] or ['default'] |
| 80 | is_slow = any(s.endswith('-slow') for s in test_suites) |
| 81 | for s in test_suites: |
| 82 | # The suite name in the introspection info is "PROJECT:SUITE" |
| 83 | s = s.split(':')[1] |
| 84 | if s.endswith('-slow'): |
| 85 | s = s[:-5] |
| 86 | if is_slow: |
| 87 | suites[s].slow_tests.append(i) |
| 88 | else: |
| 89 | suites[s].tests.append(i) |
| 90 | suites[s].executables.add(executable) |
| 91 | |
| 92 | print('.PHONY: check check-report.tap') |
| 93 | print('check:') |
| 94 | print('check-report.tap:') |
| 95 | print('\t@cat $^ | scripts/tap-merge.pl >$@') |
| 96 | for name, suite in suites.items(): |
| 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 | print('.test.suite-quick.%s := %s' % (name, test_numbers)) |
| 101 | print('.test.suite-slow.%s := $(.test.suite-quick.%s) %s' % (name, name, slow_test_numbers)) |
| 102 | print('check-build: %s' % executables) |
| 103 | print('.PHONY: check-%s' % name) |
| 104 | print('.PHONY: check-report-%s.tap' % name) |
| 105 | print('check: check-%s' % name) |
| 106 | print('check-%s: all %s' % (name, executables)) |
| 107 | print('\t$(call .test.human, $(.test.suite-$(SPEED).%s))' % (name, )) |
| 108 | print('check-report.tap: check-report-%s.tap' % name) |
| 109 | print('check-report-%s.tap: %s' % (name, executables)) |
| 110 | print('\t$(call .test.tap, $(.test.suite-$(SPEED).%s))' % (name, )) |