kunit: tool: show list of valid --arch options when invalid
authorDaniel Latypov <dlatypov@google.com>
Wed, 29 Sep 2021 23:25:34 +0000 (16:25 -0700)
committerShuah Khan <skhan@linuxfoundation.org>
Tue, 19 Oct 2021 20:18:50 +0000 (14:18 -0600)
Consider this attempt to run KUnit in QEMU:
$ ./tools/testing/kunit/kunit.py run --arch=x86

Before you'd get this error message:
kunit_kernel.ConfigError: x86 is not a valid arch

After:
kunit_kernel.ConfigError: x86 is not a valid arch, options are ['alpha', 'arm', 'arm64', 'i386', 'powerpc', 'riscv', 's390', 'sparc', 'x86_64']

This should make it a bit easier for people to notice when they make
typos, etc. Currently, one would have to dive into the python code to
figure out what the valid set is.

Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
tools/testing/kunit/kunit_kernel.py
tools/testing/kunit/kunit_tool_test.py

index 1870e75ff153d59e37a3c7b67c02f8404f72faaa..a6b3cee3f0d0c19d36721874d22aa25d967ff3b6 100644 (file)
@@ -198,8 +198,9 @@ def get_source_tree_ops(arch: str, cross_compile: Optional[str]) -> LinuxSourceT
                return LinuxSourceTreeOperationsUml(cross_compile=cross_compile)
        elif os.path.isfile(config_path):
                return get_source_tree_ops_from_qemu_config(config_path, cross_compile)[1]
-       else:
-               raise ConfigError(arch + ' is not a valid arch')
+
+       options = [f[:-3] for f in os.listdir(QEMU_CONFIGS_DIR) if f.endswith('.py')]
+       raise ConfigError(arch + ' is not a valid arch, options are ' + str(sorted(options)))
 
 def get_source_tree_ops_from_qemu_config(config_path: str,
                                         cross_compile: Optional[str]) -> Tuple[
index bc6b85db6beb7bd52031040bf9d2188dfb02e082..1116882ddf694e036b8d502d238a006e2ec87ed9 100755 (executable)
@@ -289,6 +289,10 @@ class LinuxSourceTreeTest(unittest.TestCase):
                                pass
                        kunit_kernel.LinuxSourceTree('', kunitconfig_path=dir)
 
+       def test_invalid_arch(self):
+               with self.assertRaisesRegex(kunit_kernel.ConfigError, 'not a valid arch, options are.*x86_64'):
+                       kunit_kernel.LinuxSourceTree('', arch='invalid')
+
        # TODO: add more test cases.