mtest2make.py 4.9 KB

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