From 55be961f48cfb5116776bf43956eb3b9600a3f0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sat, 22 Apr 2023 13:10:28 +0200 Subject: [PATCH] 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.) --- src/ukify/test/meson.build | 18 +++++++++++++++--- src/ukify/test/setup.cfg | 2 -- src/ukify/test/test_ukify.py | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) delete mode 100644 src/ukify/test/setup.cfg diff --git a/src/ukify/test/meson.build b/src/ukify/test/meson.build index e39178f892..e78e76c673 100644 --- a/src/ukify/test/meson.build +++ b/src/ukify/test/meson.build @@ -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 diff --git a/src/ukify/test/setup.cfg b/src/ukify/test/setup.cfg deleted file mode 100644 index 1f655da834..0000000000 --- a/src/ukify/test/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[tool:pytest] -addopts = --flakes diff --git a/src/ukify/test/test_ukify.py b/src/ukify/test/test_ukify.py index 1013f649be..3cd895fe36 100755 --- a/src/ukify/test/test_ukify.py +++ b/src/ukify/test/test_ukify.py @@ -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))