test_ukify: rework how --flakes argument is appended

The usual approach is to put 'addopts = --flakes' in setup.cfg. Unfortunately
this fails badly when pytest-flakes is not installed:
  ERROR: usage: test_ukify.py [options] [file_or_dir] [file_or_dir] [...]
  test_ukify.py: error: unrecognized arguments: --flakes

pytest-flakes is not packaged everywhere, and this test is not very important,
so let's just do it only if pytest-flakes is available. We now detect if
pytest-flakes is available and only add '--flakes' conditionally. This
unfortunately means that when invoked via 'pytest' or directly as
'src/ukify/test/test_ukify.py', '--flakes' will not be appended automatically.
But I don't see a nice way to achieve previous automatic behaviour.

(I first considered making 'setup.cfg' templated. But then it is created
in the build directory, but we would need it in the source directory for
pytest to load it automatically. So to load the file, we'd need to give an
argument to pytest anyway, so we don't gain anything with this more complex
approach.)
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2023-04-22 13:10:28 +02:00
parent 041f536f9a
commit 55be961f48
3 changed files with 16 additions and 6 deletions

View file

@ -1,7 +1,19 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
if want_ukify and want_tests != 'false'
test('test-ukify',
files('test_ukify.py'),
env : test_env)
have_pytest_flakes = pymod.find_installation(
'python3',
required : false,
modules : ['pytest_flakes'],
).found()
args = ['-v']
if have_pytest_flakes
args += ['--flakes']
endif
test('test-ukify',
files('test_ukify.py'),
args: args,
env : test_env)
endif

View file

@ -1,2 +0,0 @@
[tool:pytest]
addopts = --flakes

View file

@ -495,4 +495,4 @@ def test_pcr_signing2(kernel_initrd, tmpdir):
assert len(sig['sha1']) == 6 # six items for six phases paths
if __name__ == '__main__':
sys.exit(pytest.main([__file__, '-v']))
sys.exit(pytest.main(sys.argv))