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']
40 top_srcdir = os.environ['top_srcdir']
42 logging.basicConfig(level=logging.DEBUG)
43 logger = logging.getLogger('test')
44 logger.setLevel(logging.DEBUG)
46 # Permutation of RMLVO that we use in multiple tests
47 rmlvos = [list(x) for x in itertools.permutations(
48 ['--rules=evdev', '--model=pc104',
49 '--layout=ch', '--options=eurosign:5']
53 def _disable_coredump():
54 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
57 def run_command(args):
58 logger.debug('run command: {}'.format(' '.join(args)))
61 p = subprocess.run(args, preexec_fn=_disable_coredump,
62 capture_output=True, text=True,
64 return p.returncode, p.stdout, p.stderr
65 except subprocess.TimeoutExpired as e:
66 return 0, e.stdout, e.stderr
70 xkbcli_tool = 'xkbcli'
73 def __init__(self, subtool=None):
74 self.tool_path = top_builddir
75 self.subtool = subtool
77 def run_command(self, args):
78 if self.subtool is not None:
79 tool = '{}-{}'.format(self.xkbcli_tool, self.subtool)
81 tool = self.xkbcli_tool
82 args = [os.path.join(self.tool_path, tool)] + args
84 return run_command(args)
86 def run_command_success(self, args):
87 rc, stdout, stderr = self.run_command(args)
88 assert rc == 0, (stdout, stderr)
91 def run_command_invalid(self, args):
92 rc, stdout, stderr = self.run_command(args)
93 assert rc == 2, (rc, stdout, stderr)
94 return rc, stdout, stderr
96 def run_command_unrecognized_option(self, args):
97 rc, stdout, stderr = self.run_command(args)
98 assert rc == 2, (rc, stdout, stderr)
99 assert stdout.startswith('Usage') or stdout == ''
100 assert 'unrecognized option' in stderr
102 def run_command_missing_arg(self, args):
103 rc, stdout, stderr = self.run_command(args)
104 assert rc == 2, (rc, stdout, stderr)
105 assert stdout.startswith('Usage') or stdout == ''
106 assert 'requires an argument' in stderr
109 def get_tool(subtool=None):
110 return XkbcliTool(subtool)
114 return [get_tool(x) for x in [None, 'list',
118 'interactive-wayland',
129 return get_tool('list')
133 def xkbcli_how_to_type():
134 return get_tool('how-to-type')
138 def xkbcli_compile_keymap():
139 return get_tool('compile-keymap')
143 def xkbcli_interactive_evdev():
144 return get_tool('interactive-evdev')
148 def xkbcli_interactive_x11():
149 return get_tool('interactive-x11')
153 def xkbcli_interactive_wayland():
154 return get_tool('interactive-wayland')
157 # --help is supported by all tools
158 @pytest.mark.parametrize('tool', get_all_tools())
160 stdout, stderr = tool.run_command_success(['--help'])
161 assert stdout.startswith('Usage:')
165 # --foobar generates "Usage:" for all tools
166 @pytest.mark.parametrize('tool', get_all_tools())
167 def test_invalid_option(tool):
168 tool.run_command_unrecognized_option(['--foobar'])
172 def test_xkbcli_version(xkbcli):
173 stdout, stderr = xkbcli.run_command_success(['--version'])
174 assert stdout.startswith('1')
178 def test_xkbcli_too_many_args(xkbcli):
179 xkbcli.run_command_invalid(['a'] * 64)
182 @pytest.mark.parametrize('args', [['--verbose'],
185 ['--verbose', '--rmlvo'],
186 # ['--verbose', '--kccgst'],
188 def test_compile_keymap_args(xkbcli_compile_keymap, args):
189 xkbcli_compile_keymap.run_command_success(args)
192 @pytest.mark.parametrize('rmlvos', rmlvos)
193 def test_compile_keymap_rmlvo(xkbcli_compile_keymap, rmlvos):
194 xkbcli_compile_keymap.run_command_success(rmlvos)
197 @pytest.mark.parametrize('args', [['--include', '.', '--include-defaults'],
198 ['--include', '/tmp', '--include-defaults'],
200 def test_compile_keymap_include(xkbcli_compile_keymap, args):
201 # Succeeds thanks to include-defaults
202 xkbcli_compile_keymap.run_command_success(args)
205 def test_compile_keymap_include_invalid(xkbcli_compile_keymap):
206 # A non-directory is rejected by default
207 args = ['--include', '/proc/version']
208 rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
209 assert rc == 1, (stdout, stderr)
210 assert "There are no include paths to search" in stderr
212 # A non-existing directory is rejected by default
213 args = ['--include', '/tmp/does/not/exist']
214 rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
215 assert rc == 1, (stdout, stderr)
216 assert "There are no include paths to search" in stderr
218 # Valid dir, but missing files
219 args = ['--include', '/tmp']
220 rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
221 assert rc == 1, (stdout, stderr)
222 assert "Couldn't look up rules" in stderr
225 # Unicode codepoint conversions, we support whatever strtol does
226 @pytest.mark.parametrize('args', [['123'], ['0x123'], ['0123']])
227 def test_how_to_type(xkbcli_how_to_type, args):
228 xkbcli_how_to_type.run_command_success(args)
231 @pytest.mark.parametrize('rmlvos', rmlvos)
232 def test_how_to_type_rmlvo(xkbcli_how_to_type, rmlvos):
233 args = rmlvos + ['0x1234']
234 xkbcli_how_to_type.run_command_success(args)
237 @pytest.mark.parametrize('args', [['--verbose'],
239 ['--verbose', '--load-exotic'],
244 def test_list_rmlvo(xkbcli_list, args):
245 xkbcli_list.run_command_success(args)
248 def test_list_rmlvo_includes(xkbcli_list):
250 xkbcli_list.run_command_success(args)
253 def test_list_rmlvo_includes_invalid(xkbcli_list):
254 args = ['/proc/version']
255 rc, stdout, stderr = xkbcli_list.run_command(args)
257 assert "Failed to append include path" in stderr
260 def test_list_rmlvo_includes_no_defaults(xkbcli_list):
261 args = ['--skip-default-paths', '/tmp']
262 rc, stdout, stderr = xkbcli_list.run_command(args)
264 assert "Failed to parse XKB description" in stderr
267 @pytest.mark.skipif(not os.path.exists('/dev/input/event0'), reason='event node required')
268 @pytest.mark.skipif(not os.access('/dev/input/event0', os.R_OK), reason='insufficient permissions')
269 @pytest.mark.parametrize('rmlvos', rmlvos)
270 def test_interactive_evdev_rmlvo(xkbcli_interactive_evdev, rmlvos):
272 xkbcli_interactive_evdev.run_command_success(rmlvos)
275 @pytest.mark.skipif(not os.path.exists('/dev/input/event0'),
276 reason='event node required')
277 @pytest.mark.skipif(not os.access('/dev/input/event0', os.R_OK),
278 reason='insufficient permissions')
279 @pytest.mark.parametrize('args', [['--report-state-changes'],
280 ['--enable-compose'],
281 ['--consumed-mode=xkb'],
282 ['--consumed-mode=gtk'],
283 ['--without-x11-offset'],
285 def test_interactive_evdev(xkbcli_interactive_evdev, args):
286 # Note: --enable-compose fails if $prefix doesn't have the compose tables
288 xkbcli_interactive_evdev.run_command_success(args)
291 @pytest.mark.skipif(not os.getenv('DISPLAY'), reason='DISPLAY not set')
292 def test_interactive_x11(xkbcli_interactive_x11):
293 # To be filled in if we handle something other than --help
297 @pytest.mark.skipif(not os.getenv('WAYLAND_DISPLAY'),
298 reason='WAYLAND_DISPLAY not set')
299 def test_interactive_wayland(xkbcli_interactive_wayland):
300 # To be filled in if we handle something other than --help
304 if __name__ == '__main__':
305 with tempfile.TemporaryDirectory() as tmpdir:
306 # Use our own test xkeyboard-config copy.
307 os.environ['XKB_CONFIG_ROOT'] = top_srcdir + '/test/data'
308 # libxkbcommon has fallbacks when XDG_CONFIG_HOME isn't set so we need
309 # to override it with a known (empty) directory. Otherwise our test
310 # behavior depends on the system the test is run on.
311 os.environ['XDG_CONFIG_HOME'] = tmpdir
312 # Prevent the legacy $HOME/.xkb from kicking in.
313 del os.environ['HOME']
314 # This needs to be separated if we do specific extra path testing
315 os.environ['XKB_CONFIG_EXTRA_PATH'] = tmpdir
317 sys.exit(pytest.main(args=[__file__]))