3 # Copyright © 2020 Red Hat, Inc.
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 # DEALINGS IN THE SOFTWARE.
35 print('Failed to import pytest. Skipping.', file=sys.stderr)
39 top_builddir = os.environ['top_builddir']
41 logging.basicConfig(level=logging.DEBUG)
42 logger = logging.getLogger('test')
43 logger.setLevel(logging.DEBUG)
45 # Permutation of RMLVO that we use in multiple tests
46 rmlvos = [list(x) for x in itertools.permutations(
47 ['--rules=evdev', '--model=pc104',
48 '--layout=fr', '--options=eurosign:5']
52 def _disable_coredump():
53 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
56 def run_command(args):
57 logger.debug('run command: {}'.format(' '.join(args)))
60 p = subprocess.run(args, preexec_fn=_disable_coredump,
61 capture_output=True, text=True,
63 return p.returncode, p.stdout, p.stderr
64 except subprocess.TimeoutExpired as e:
65 return 0, e.stdout, e.stderr
69 xkbcli_tool = 'xkbcli'
72 def __init__(self, subtool=None):
73 self.tool_path = top_builddir
74 self.subtool = subtool
76 def run_command(self, args):
77 if self.subtool is not None:
78 tool = '{}-{}'.format(self.xkbcli_tool, self.subtool)
80 tool = self.xkbcli_tool
81 args = [os.path.join(self.tool_path, tool)] + args
83 return run_command(args)
85 def run_command_success(self, args):
86 rc, stdout, stderr = self.run_command(args)
87 assert rc == 0, (stdout, stderr)
90 def run_command_invalid(self, args):
91 rc, stdout, stderr = self.run_command(args)
92 assert rc == 2, (rc, stdout, stderr)
93 return rc, stdout, stderr
95 def run_command_unrecognized_option(self, args):
96 rc, stdout, stderr = self.run_command(args)
97 assert rc == 2, (rc, stdout, stderr)
98 assert stdout.startswith('Usage') or stdout == ''
99 assert 'unrecognized option' in stderr
101 def run_command_missing_arg(self, args):
102 rc, stdout, stderr = self.run_command(args)
103 assert rc == 2, (rc, stdout, stderr)
104 assert stdout.startswith('Usage') or stdout == ''
105 assert 'requires an argument' in stderr
108 def get_tool(subtool=None):
109 return XkbcliTool(subtool)
113 return [get_tool(x) for x in [None, 'list',
117 'interactive-wayland',
128 return get_tool('list')
132 def xkbcli_how_to_type():
133 return get_tool('how-to-type')
137 def xkbcli_compile_keymap():
138 return get_tool('compile-keymap')
142 def xkbcli_interactive_evdev():
143 return get_tool('interactive-evdev')
147 def xkbcli_interactive_x11():
148 return get_tool('interactive-x11')
152 def xkbcli_interactive_wayland():
153 return get_tool('interactive-wayland')
156 # --help is supported by all tools
157 @pytest.mark.parametrize('tool', get_all_tools())
159 stdout, stderr = tool.run_command_success(['--help'])
160 assert stdout.startswith('Usage:')
164 # --foobar generates "Usage:" for all tools
165 @pytest.mark.parametrize('tool', get_all_tools())
166 def test_invalid_option(tool):
167 tool.run_command_unrecognized_option(['--foobar'])
171 def test_xkbcli_version(xkbcli):
172 stdout, stderr = xkbcli.run_command_success(['--version'])
173 assert stdout.startswith('0')
177 def test_xkbcli_too_many_args(xkbcli):
178 xkbcli.run_command_invalid(['a'] * 64)
181 @pytest.mark.parametrize('args', [['--verbose'],
184 ['--verbose', '--rmlvo'],
185 # ['--verbose', '--kccgst'],
187 def test_compile_keymap_args(xkbcli_compile_keymap, args):
188 xkbcli_compile_keymap.run_command_success(args)
191 @pytest.mark.parametrize('rmlvos', rmlvos)
192 def test_compile_keymap_rmlvo(xkbcli_compile_keymap, rmlvos):
193 xkbcli_compile_keymap.run_command_success(rmlvos)
196 @pytest.mark.parametrize('args', [['--include', '.', '--include-defaults'],
197 ['--include', '/tmp', '--include-defaults'],
199 def test_compile_keymap_include(xkbcli_compile_keymap, args):
200 # Succeeds thanks to include-defaults
201 xkbcli_compile_keymap.run_command_success(args)
204 def test_compile_keymap_include_invalid(xkbcli_compile_keymap):
205 # A non-directory is rejected by default
206 args = ['--include', '/proc/version']
207 rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
208 assert rc == 1, (stdout, stderr)
209 assert "There are no include paths to search" in stderr
211 # A non-existing directory is rejected by default
212 args = ['--include', '/tmp/does/not/exist']
213 rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
214 assert rc == 1, (stdout, stderr)
215 assert "There are no include paths to search" in stderr
217 # Valid dir, but missing files
218 args = ['--include', '/tmp']
219 rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
220 assert rc == 1, (stdout, stderr)
221 assert "Couldn't look up rules" in stderr
224 # Unicode codepoint conversions, we support whatever strtol does
225 @pytest.mark.parametrize('args', [['123'], ['0x123'], ['0123']])
226 def test_how_to_type(xkbcli_how_to_type, args):
227 xkbcli_how_to_type.run_command_success(args)
230 @pytest.mark.parametrize('rmlvos', rmlvos)
231 def test_how_to_type_rmlvo(xkbcli_how_to_type, rmlvos):
232 args = rmlvos + ['0x1234']
233 xkbcli_how_to_type.run_command_success(args)
236 @pytest.mark.parametrize('args', [['--verbose'],
238 ['--verbose', '--load-exotic'],
243 def test_list_rmlvo(xkbcli_list, args):
244 xkbcli_list.run_command_success(args)
247 def test_list_rmlvo_includes(xkbcli_list):
249 xkbcli_list.run_command_success(args)
252 def test_list_rmlvo_includes_invalid(xkbcli_list):
253 args = ['/proc/version']
254 rc, stdout, stderr = xkbcli_list.run_command(args)
256 assert "Failed to append include path" in stderr
259 def test_list_rmlvo_includes_no_defaults(xkbcli_list):
260 args = ['--skip-default-paths', '/tmp']
261 rc, stdout, stderr = xkbcli_list.run_command(args)
263 assert "Failed to parse XKB description" in stderr
266 @pytest.mark.skipif(not os.path.exists('/dev/input/event0'), reason='event node required')
267 @pytest.mark.skipif(not os.access('/dev/input/event0', os.R_OK), reason='insufficient permissions')
268 @pytest.mark.parametrize('rmlvos', rmlvos)
269 def test_interactive_evdev_rmlvo(xkbcli_interactive_evdev, rmlvos):
271 xkbcli_interactive_evdev.run_command_success(rmlvos)
274 @pytest.mark.skipif(not os.path.exists('/dev/input/event0'),
275 reason='event node required')
276 @pytest.mark.skipif(not os.access('/dev/input/event0', os.R_OK),
277 reason='insufficient permissions')
278 @pytest.mark.parametrize('args', [['--report-state-changes'],
279 ['--enable-compose'],
280 ['--consumed-mode=xkb'],
281 ['--consumed-mode=gtk'],
282 ['--without-x11-offset'],
284 def test_interactive_evdev(xkbcli_interactive_evdev, args):
285 # Note: --enable-compose fails if $prefix doesn't have the compose tables
287 xkbcli_interactive_evdev.run_command_success(args)
290 @pytest.mark.skipif(not os.getenv('DISPLAY'), reason='DISPLAY not set')
291 def test_interactive_x11(xkbcli_interactive_x11):
292 # To be filled in if we handle something other than --help
296 @pytest.mark.skipif(not os.getenv('WAYLAND_DISPLAY'),
297 reason='WAYLAND_DISPLAY not set')
298 def test_interactive_wayland(xkbcli_interactive_wayland):
299 # To be filled in if we handle something other than --help
303 if __name__ == '__main__':
304 with tempfile.TemporaryDirectory() as tmpdir:
305 # libxkbcommon has fallbacks when XDG_CONFIG_HOME isn't set so we need
306 # to override it with a known (empty) directory. Otherwise our test
307 # behavior depends on the system the test is run on.
308 os.environ['XDG_CONFIG_HOME'] = tmpdir
309 # This needs to be separated if we do specific extra path testing
310 os.environ['XKB_CONFIG_EXTRA_PATH'] = tmpdir
312 sys.exit(pytest.main(args=[__file__]))