From c621319e53a37ad05f8c8f3b5d09ab677039d058 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Mon, 7 Sep 2020 10:36:18 +0300 Subject: [PATCH] test/tool-option-parsing: switch from pytest to unittest For me, installing pytest for libxkbcommon is a bit problematic, so I end up skipping it which is not great. Switch to unittest which is built in to Python. It's not as nice as pytest but good enough in this case. Note: I was too lazy to switch the plain asserts to unittest assertions... Signed-off-by: Ran Benita --- test/tool-option-parsing.py | 321 +++++++++++++++++------------------- 1 file changed, 149 insertions(+), 172 deletions(-) diff --git a/test/tool-option-parsing.py b/test/tool-option-parsing.py index 3863be1..1c092b2 100755 --- a/test/tool-option-parsing.py +++ b/test/tool-option-parsing.py @@ -28,12 +28,7 @@ import sys import subprocess import logging import tempfile - -try: - import pytest -except ImportError: - print('Failed to import pytest. Skipping.', file=sys.stderr) - sys.exit(77) +import unittest top_builddir = os.environ['top_builddir'] @@ -105,200 +100,182 @@ class XkbcliTool: assert stdout.startswith('Usage') or stdout == '' assert 'requires an argument' in stderr + def __str__(self): + return str(self.subtool) + def get_tool(subtool=None): return XkbcliTool(subtool) -def get_all_tools(): - return [get_tool(x) for x in [None, 'list', - 'compile-keymap', - 'how-to-type', - 'interactive-evdev', - 'interactive-wayland', - 'interactive-x11']] +class TestXkbcli(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.xkbcli = get_tool() + cls.xkbcli_list = get_tool('list') + cls.xkbcli_how_to_type = get_tool('how-to-type') + cls.xkbcli_compile_keymap = get_tool('compile-keymap') + cls.xkbcli_interactive_evdev = get_tool('interactive-evdev') + cls.xkbcli_interactive_x11 = get_tool('interactive-x11') + cls.xkbcli_interactive_wayland = get_tool('interactive-wayland') + cls.all_tools = [ + cls.xkbcli, + cls.xkbcli_list, + cls.xkbcli_how_to_type, + cls.xkbcli_compile_keymap, + cls.xkbcli_interactive_evdev, + cls.xkbcli_interactive_x11, + cls.xkbcli_interactive_wayland, + ] + def test_help(self): + # --help is supported by all tools + for tool in self.all_tools: + with self.subTest(tool=tool): + stdout, stderr = tool.run_command_success(['--help']) + assert stdout.startswith('Usage:') + assert stderr == '' -@pytest.fixture -def xkbcli(): - return get_tool() + def test_invalid_option(self): + # --foobar generates "Usage:" for all tools + for tool in self.all_tools: + with self.subTest(tool=tool): + tool.run_command_unrecognized_option(['--foobar']) + def test_xkbcli_version(self): + # xkbcli --version + stdout, stderr = self.xkbcli.run_command_success(['--version']) + assert stdout.startswith('1') + assert stderr == '' -@pytest.fixture -def xkbcli_list(): - return get_tool('list') + def test_xkbcli_too_many_args(self): + self.xkbcli.run_command_invalid(['a'] * 64) + def test_compile_keymap_args(self): + for args in ( + ['--verbose'], + ['--rmlvo'], + # ['--kccgst'], + ['--verbose', '--rmlvo'], + # ['--verbose', '--kccgst'], + ): + with self.subTest(args=args): + self.xkbcli_compile_keymap.run_command_success(args) -@pytest.fixture -def xkbcli_how_to_type(): - return get_tool('how-to-type') + def test_compile_keymap_rmlvo(self): + for rmlvo in rmlvos: + with self.subTest(rmlvo=rmlvo): + self.xkbcli_compile_keymap.run_command_success(rmlvo) + def test_compile_keymap_include(self): + for args in ( + ['--include', '.', '--include-defaults'], + ['--include', '/tmp', '--include-defaults'], + ): + with self.subTest(args=args): + # Succeeds thanks to include-defaults + self.xkbcli_compile_keymap.run_command_success(args) -@pytest.fixture -def xkbcli_compile_keymap(): - return get_tool('compile-keymap') + def test_compile_keymap_include_invalid(self): + # A non-directory is rejected by default + args = ['--include', '/proc/version'] + rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args) + assert rc == 1, (stdout, stderr) + assert "There are no include paths to search" in stderr + # A non-existing directory is rejected by default + args = ['--include', '/tmp/does/not/exist'] + rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args) + assert rc == 1, (stdout, stderr) + assert "There are no include paths to search" in stderr -@pytest.fixture -def xkbcli_interactive_evdev(): - return get_tool('interactive-evdev') + # Valid dir, but missing files + args = ['--include', '/tmp'] + rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args) + assert rc == 1, (stdout, stderr) + assert "Couldn't look up rules" in stderr + def test_how_to_type(self): + # Unicode codepoint conversions, we support whatever strtol does + for args in (['123'], ['0x123'], ['0123']): + with self.subTest(args=args): + self.xkbcli_how_to_type.run_command_success(args) -@pytest.fixture -def xkbcli_interactive_x11(): - return get_tool('interactive-x11') + def test_how_to_type_rmlvo(self): + for rmlvo in rmlvos: + with self.subTest(rmlvo=rmlvo): + args = rmlvo + ['0x1234'] + self.xkbcli_how_to_type.run_command_success(args) + def test_list_rmlvo(self): + for args in ( + ['--verbose'], + ['-v'], + ['--verbose', '--load-exotic'], + ['--load-exotic'], + ['--ruleset=evdev'], + ['--ruleset=base'], + ): + with self.subTest(args=args): + self.xkbcli_list.run_command_success(args) -@pytest.fixture -def xkbcli_interactive_wayland(): - return get_tool('interactive-wayland') + def test_list_rmlvo_includes(self): + args = ['/tmp/'] + self.xkbcli_list.run_command_success(args) + def test_list_rmlvo_includes_invalid(self): + args = ['/proc/version'] + rc, stdout, stderr = self.xkbcli_list.run_command(args) + assert rc == 1 + assert "Failed to append include path" in stderr -# --help is supported by all tools -@pytest.mark.parametrize('tool', get_all_tools()) -def test_help(tool): - stdout, stderr = tool.run_command_success(['--help']) - assert stdout.startswith('Usage:') - assert stderr == '' + def test_list_rmlvo_includes_no_defaults(self): + args = ['--skip-default-paths', '/tmp'] + rc, stdout, stderr = self.xkbcli_list.run_command(args) + assert rc == 1 + assert "Failed to parse XKB description" in stderr + def test_interactive_evdev_rmlvo(self): + if not os.path.exists('/dev/input/event0'): + self.skipTest('event node required') + if not os.access('/dev/input/event0', os.R_OK): + self.skipTest('insufficient permissions') -# --foobar generates "Usage:" for all tools -@pytest.mark.parametrize('tool', get_all_tools()) -def test_invalid_option(tool): - tool.run_command_unrecognized_option(['--foobar']) + for rmlvo in rmlvos: + with self.subTest(rmlvo=rmlvo): + self.xkbcli_interactive_evdev.run_command_success(rmlvos) + def test_interactive_evdev(self): + if not os.path.exists('/dev/input/event0'): + self.skipTest('event node required') + if not os.access('/dev/input/event0', os.R_OK): + self.skipTest('insufficient permissions') -# xkbcli --version -def test_xkbcli_version(xkbcli): - stdout, stderr = xkbcli.run_command_success(['--version']) - assert stdout.startswith('1') - assert stderr == '' + # Note: --enable-compose fails if $prefix doesn't have the compose tables + # installed + for args in ( + ['--report-state-changes'], + ['--enable-compose'], + ['--consumed-mode=xkb'], + ['--consumed-mode=gtk'], + ['--without-x11-offset'], + ): + with self.subTest(args=args): + self.xkbcli_interactive_evdev.run_command_success(args) + def test_interactive_x11(self): + if not os.getenv('DISPLAY'): + self.skipTest('DISPLAY not set') -def test_xkbcli_too_many_args(xkbcli): - xkbcli.run_command_invalid(['a'] * 64) + # To be filled in if we handle something other than --help + pass + def test_interactive_wayland(self): + if not os.getenv('WAYLAND_DISPLAY'): + self.skipTest('WAYLAND_DISPLAY not set') -@pytest.mark.parametrize('args', [['--verbose'], - ['--rmlvo'], - # ['--kccgst'], - ['--verbose', '--rmlvo'], - # ['--verbose', '--kccgst'], - ]) -def test_compile_keymap_args(xkbcli_compile_keymap, args): - xkbcli_compile_keymap.run_command_success(args) - - -@pytest.mark.parametrize('rmlvos', rmlvos) -def test_compile_keymap_rmlvo(xkbcli_compile_keymap, rmlvos): - xkbcli_compile_keymap.run_command_success(rmlvos) - - -@pytest.mark.parametrize('args', [['--include', '.', '--include-defaults'], - ['--include', '/tmp', '--include-defaults'], - ]) -def test_compile_keymap_include(xkbcli_compile_keymap, args): - # Succeeds thanks to include-defaults - xkbcli_compile_keymap.run_command_success(args) - - -def test_compile_keymap_include_invalid(xkbcli_compile_keymap): - # A non-directory is rejected by default - args = ['--include', '/proc/version'] - rc, stdout, stderr = xkbcli_compile_keymap.run_command(args) - assert rc == 1, (stdout, stderr) - assert "There are no include paths to search" in stderr - - # A non-existing directory is rejected by default - args = ['--include', '/tmp/does/not/exist'] - rc, stdout, stderr = xkbcli_compile_keymap.run_command(args) - assert rc == 1, (stdout, stderr) - assert "There are no include paths to search" in stderr - - # Valid dir, but missing files - args = ['--include', '/tmp'] - rc, stdout, stderr = xkbcli_compile_keymap.run_command(args) - assert rc == 1, (stdout, stderr) - assert "Couldn't look up rules" in stderr - - -# Unicode codepoint conversions, we support whatever strtol does -@pytest.mark.parametrize('args', [['123'], ['0x123'], ['0123']]) -def test_how_to_type(xkbcli_how_to_type, args): - xkbcli_how_to_type.run_command_success(args) - - -@pytest.mark.parametrize('rmlvos', rmlvos) -def test_how_to_type_rmlvo(xkbcli_how_to_type, rmlvos): - args = rmlvos + ['0x1234'] - xkbcli_how_to_type.run_command_success(args) - - -@pytest.mark.parametrize('args', [['--verbose'], - ['-v'], - ['--verbose', '--load-exotic'], - ['--load-exotic'], - ['--ruleset=evdev'], - ['--ruleset=base'], - ]) -def test_list_rmlvo(xkbcli_list, args): - xkbcli_list.run_command_success(args) - - -def test_list_rmlvo_includes(xkbcli_list): - args = ['/tmp/'] - xkbcli_list.run_command_success(args) - - -def test_list_rmlvo_includes_invalid(xkbcli_list): - args = ['/proc/version'] - rc, stdout, stderr = xkbcli_list.run_command(args) - assert rc == 1 - assert "Failed to append include path" in stderr - - -def test_list_rmlvo_includes_no_defaults(xkbcli_list): - args = ['--skip-default-paths', '/tmp'] - rc, stdout, stderr = xkbcli_list.run_command(args) - assert rc == 1 - assert "Failed to parse XKB description" in stderr - - -@pytest.mark.skipif(not os.path.exists('/dev/input/event0'), reason='event node required') -@pytest.mark.skipif(not os.access('/dev/input/event0', os.R_OK), reason='insufficient permissions') -@pytest.mark.parametrize('rmlvos', rmlvos) -def test_interactive_evdev_rmlvo(xkbcli_interactive_evdev, rmlvos): - return - xkbcli_interactive_evdev.run_command_success(rmlvos) - - -@pytest.mark.skipif(not os.path.exists('/dev/input/event0'), - reason='event node required') -@pytest.mark.skipif(not os.access('/dev/input/event0', os.R_OK), - reason='insufficient permissions') -@pytest.mark.parametrize('args', [['--report-state-changes'], - ['--enable-compose'], - ['--consumed-mode=xkb'], - ['--consumed-mode=gtk'], - ['--without-x11-offset'], - ]) -def test_interactive_evdev(xkbcli_interactive_evdev, args): - # Note: --enable-compose fails if $prefix doesn't have the compose tables - # installed - xkbcli_interactive_evdev.run_command_success(args) - - -@pytest.mark.skipif(not os.getenv('DISPLAY'), reason='DISPLAY not set') -def test_interactive_x11(xkbcli_interactive_x11): - # To be filled in if we handle something other than --help - pass - - -@pytest.mark.skipif(not os.getenv('WAYLAND_DISPLAY'), - reason='WAYLAND_DISPLAY not set') -def test_interactive_wayland(xkbcli_interactive_wayland): - # To be filled in if we handle something other than --help - pass + # To be filled in if we handle something other than --help + pass if __name__ == '__main__': @@ -314,4 +291,4 @@ if __name__ == '__main__': # This needs to be separated if we do specific extra path testing os.environ['XKB_CONFIG_EXTRA_PATH'] = tmpdir - sys.exit(pytest.main(args=[__file__])) + unittest.main()