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