Skip to content

Commit 69711f3

Browse files
committed
templates: refactor tests to have a bit less nesting
Use `itertools.product()` to reduce an indentation level or two, though we lose one again by changing to a function for fresh vs dirty. I plan on adding more to these tests and it makes it a bit more manageable. Best-viewed-with: --ignore-all-space
1 parent a06e649 commit 69711f3

File tree

1 file changed

+74
-54
lines changed

1 file changed

+74
-54
lines changed

unittests/allplatformstests.py

Lines changed: 74 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright 2016-2021 The Meson development team
33
# Copyright © 2023-2025 Intel Corporation
44

5+
import itertools
56
import subprocess
67
import re
78
import json
@@ -2523,61 +2524,80 @@ def test_templates(self):
25232524
if is_osx():
25242525
langs = [l for l in langs if l != 'd']
25252526

2526-
for lang in langs:
2527-
for target_type in ('executable', 'library'):
2528-
with self.subTest(f'Language: {lang}; type: {target_type}; fresh: yes'):
2529-
if is_windows() and lang == 'fortran' and target_type == 'library':
2530-
# non-Gfortran Windows Fortran compilers do not do shared libraries in a Fortran standard way
2531-
# see "test cases/fortran/6 dynamic"
2532-
fc = detect_compiler_for(env, 'fortran', MachineChoice.HOST, True, '')
2533-
if fc.get_id() in {'intel-cl', 'pgi'}:
2534-
continue
2535-
# test empty directory
2536-
with tempfile.TemporaryDirectory() as tmpdir:
2537-
self._run(self.meson_command + ['init', '--language', lang, '--type', target_type],
2538-
workdir=tmpdir)
2539-
self._run(self.setup_command + ['--backend=ninja', 'builddir'],
2540-
workdir=tmpdir)
2527+
def _template_test_fresh(lang, target_type):
2528+
if is_windows() and lang == 'fortran' and target_type == 'library':
2529+
# non-Gfortran Windows Fortran compilers do not do shared libraries in a Fortran standard way
2530+
# see "test cases/fortran/6 dynamic"
2531+
fc = detect_compiler_for(env, 'fortran', MachineChoice.HOST, True, '')
2532+
if fc.get_id() in {'intel-cl', 'pgi'}:
2533+
return
2534+
2535+
# test empty directory
2536+
with tempfile.TemporaryDirectory() as tmpdir:
2537+
self._run(self.meson_command + ['init', '--language', lang, '--type', target_type],
2538+
workdir=tmpdir)
2539+
self._run(self.setup_command + ['--backend=ninja', 'builddir'],
2540+
workdir=tmpdir)
2541+
self._run(ninja,
2542+
workdir=os.path.join(tmpdir, 'builddir'))
2543+
2544+
def _template_test_dirty(lang, target_type):
2545+
if is_windows() and lang == 'fortran' and target_type == 'library':
2546+
# non-Gfortran Windows Fortran compilers do not do shared libraries in a Fortran standard way
2547+
# see "test cases/fortran/6 dynamic"
2548+
fc = detect_compiler_for(env, 'fortran', MachineChoice.HOST, True, '')
2549+
if fc.get_id() in {'intel-cl', 'pgi'}:
2550+
return
2551+
2552+
# test empty directory
2553+
with tempfile.TemporaryDirectory() as tmpdir:
2554+
self._run(self.meson_command + ['init', '--language', lang, '--type', target_type],
2555+
workdir=tmpdir)
2556+
self._run(self.setup_command + ['--backend=ninja', 'builddir'],
2557+
workdir=tmpdir)
2558+
self._run(ninja,
2559+
workdir=os.path.join(tmpdir, 'builddir'))
2560+
2561+
# test directory with existing code file
2562+
if lang in {'c', 'cpp', 'd'}:
2563+
with tempfile.TemporaryDirectory() as tmpdir:
2564+
with open(os.path.join(tmpdir, 'foo.' + lang), 'w', encoding='utf-8') as f:
2565+
f.write('int main(void) {}')
2566+
self._run(self.meson_command + ['init', '-b'], workdir=tmpdir)
2567+
2568+
# Check for whether we're doing source collection by repeating
2569+
# with a bogus file we should pick up (and then fail to compile).
2570+
with tempfile.TemporaryDirectory() as tmpdir:
2571+
with open(os.path.join(tmpdir, 'bar.' + lang), 'w', encoding='utf-8') as f:
2572+
f.write('#error bar')
2573+
self._run(self.meson_command + ['init'], workdir=tmpdir)
2574+
self._run(self.setup_command + ['--backend=ninja', 'builddir'],
2575+
workdir=tmpdir)
2576+
with self.assertRaises(subprocess.CalledProcessError):
2577+
self._run(ninja,
2578+
workdir=os.path.join(tmpdir, 'builddir'))
2579+
2580+
elif lang in {'java'}:
2581+
with tempfile.TemporaryDirectory() as tmpdir:
2582+
with open(os.path.join(tmpdir, 'Foo.' + lang), 'w', encoding='utf-8') as f:
2583+
f.write('public class Foo { public static void main() {} }')
2584+
self._run(self.meson_command + ['init', '-b'], workdir=tmpdir)
2585+
2586+
# Check for whether we're doing source collection by repeating
2587+
# with a bogus file we should pick up (and then fail to compile).
2588+
with tempfile.TemporaryDirectory() as tmpdir:
2589+
with open(os.path.join(tmpdir, 'Bar.' + lang), 'w', encoding='utf-8') as f:
2590+
f.write('public class Bar { public private static void main() {} }')
2591+
self._run(self.meson_command + ['init'], workdir=tmpdir)
2592+
self._run(self.setup_command + ['--backend=ninja', 'builddir'],
2593+
workdir=tmpdir)
2594+
with self.assertRaises(subprocess.CalledProcessError):
25412595
self._run(ninja,
2542-
workdir=os.path.join(tmpdir, 'builddir'))
2543-
2544-
with self.subTest(f'Language: {lang}; type: {target_type}; fresh: no'):
2545-
# test directory with existing code file
2546-
if lang in {'c', 'cpp', 'd'}:
2547-
with tempfile.TemporaryDirectory() as tmpdir:
2548-
with open(os.path.join(tmpdir, 'foo.' + lang), 'w', encoding='utf-8') as f:
2549-
f.write('int main(void) {}')
2550-
self._run(self.meson_command + ['init', '-b'], workdir=tmpdir)
2551-
2552-
# Check for whether we're doing source collection by repeating
2553-
# with a bogus file we should pick up (and then fail to compile).
2554-
with tempfile.TemporaryDirectory() as tmpdir:
2555-
with open(os.path.join(tmpdir, 'bar.' + lang), 'w', encoding='utf-8') as f:
2556-
f.write('#error bar')
2557-
self._run(self.meson_command + ['init'], workdir=tmpdir)
2558-
self._run(self.setup_command + ['--backend=ninja', 'builddir'],
2559-
workdir=tmpdir)
2560-
with self.assertRaises(subprocess.CalledProcessError):
2561-
self._run(ninja,
2562-
workdir=os.path.join(tmpdir, 'builddir'))
2563-
2564-
elif lang in {'java'}:
2565-
with tempfile.TemporaryDirectory() as tmpdir:
2566-
with open(os.path.join(tmpdir, 'Foo.' + lang), 'w', encoding='utf-8') as f:
2567-
f.write('public class Foo { public static void main() {} }')
2568-
self._run(self.meson_command + ['init', '-b'], workdir=tmpdir)
2569-
2570-
# Check for whether we're doing source collection by repeating
2571-
# with a bogus file we should pick up (and then fail to compile).
2572-
with tempfile.TemporaryDirectory() as tmpdir:
2573-
with open(os.path.join(tmpdir, 'Bar.' + lang), 'w', encoding='utf-8') as f:
2574-
f.write('public class Bar { public private static void main() {} }')
2575-
self._run(self.meson_command + ['init'], workdir=tmpdir)
2576-
self._run(self.setup_command + ['--backend=ninja', 'builddir'],
2577-
workdir=tmpdir)
2578-
with self.assertRaises(subprocess.CalledProcessError):
2579-
self._run(ninja,
2580-
workdir=os.path.join(tmpdir, 'builddir'))
2596+
workdir=os.path.join(tmpdir, 'builddir'))
2597+
2598+
for lang, target_type, fresh in itertools.product(langs, ('executable', 'library'), (True, False)):
2599+
with self.subTest(f'Language: {lang}; type: {target_type}; fresh: {fresh}'):
2600+
_template_test_fresh(lang, target_type) if fresh else _template_test_dirty(lang, target_type)
25812601

25822602
def test_compiler_run_command(self):
25832603
'''

0 commit comments

Comments
 (0)