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.
34 print('Failed to import pytest. Skipping.', file=sys.stderr)
38 logging.basicConfig(level=logging.DEBUG)
39 logger = logging.getLogger('test')
40 logger.setLevel(logging.DEBUG)
42 # Permutation of RMLVO that we use in multiple tests
43 rmlvos = [list(x) for x in itertools.permutations(
44 ['--rules=evdev', '--model=pc104',
45 '--layout=fr', '--options=eurosign:5']
49 def _disable_coredump():
50 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
53 def run_command(args):
54 logger.debug('run command: {}'.format(' '.join(args)))
57 p = subprocess.run(args, preexec_fn=_disable_coredump,
58 capture_output=True, text=True,
60 return p.returncode, p.stdout, p.stderr
61 except subprocess.TimeoutExpired as e:
62 return 0, e.stdout, e.stderr
66 xkbcli_tool = 'xkbcli'
69 def __init__(self, subtool=None):
70 self.tool_path = "@MESON_BUILD_ROOT@"
71 self.subtool = subtool
73 def run_command(self, args):
74 if self.subtool is not None:
75 tool = '{}-{}'.format(self.xkbcli_tool, self.subtool)
77 tool = self.xkbcli_tool
78 args = [os.path.join(self.tool_path, tool)] + args
80 return run_command(args)
82 def run_command_success(self, args):
83 rc, stdout, stderr = self.run_command(args)
84 assert rc == 0, (stdout, stderr)
87 def run_command_invalid(self, args):
88 rc, stdout, stderr = self.run_command(args)
89 assert rc == 2, (rc, stdout, stderr)
90 return rc, stdout, stderr
92 def run_command_unrecognized_option(self, args):
93 rc, stdout, stderr = self.run_command(args)
94 assert rc == 2, (rc, stdout, stderr)
95 assert stdout.startswith('Usage') or stdout == ''
96 assert 'unrecognized option' in stderr
98 def run_command_missing_arg(self, args):
99 rc, stdout, stderr = self.run_command(args)
100 assert rc == 2, (rc, stdout, stderr)
101 assert stdout.startswith('Usage') or stdout == ''
102 assert 'requires an argument' in stderr
105 def get_tool(subtool=None):
106 return XkbcliTool(subtool)
110 return [get_tool(x) for x in [None, 'list',
114 'interactive-wayland',
125 return get_tool('list')
129 def xkbcli_how_to_type():
130 return get_tool('how-to-type')
134 def xkbcli_compile_keymap():
135 return get_tool('compile-keymap')
139 def xkbcli_interactive_evdev():
140 return get_tool('interactive-evdev')
144 def xkbcli_interactive_x11():
145 return get_tool('interactive-x11')
149 def xkbcli_interactive_wayland():
150 return get_tool('interactive-wayland')
153 # --help is supported by all tools
154 @pytest.mark.parametrize('tool', get_all_tools())
156 stdout, stderr = tool.run_command_success(['--help'])
157 assert stdout.startswith('Usage:')
161 # --foobar generates "Usage:" for all tools
162 @pytest.mark.parametrize('tool', get_all_tools())
163 def test_invalid_option(tool):
164 tool.run_command_unrecognized_option(['--foobar'])
168 def test_xkbcli_version(xkbcli):
169 stdout, stderr = xkbcli.run_command_success(['--version'])
170 assert stdout.startswith('0')
174 def test_xkbcli_too_many_args(xkbcli):
175 xkbcli.run_command_invalid(['a'] * 64)
178 @pytest.mark.parametrize('args', [['--verbose'],
181 ['--verbose', '--rmlvo'],
182 ['--verbose', '--kccgst'],
184 def test_compile_keymap_args(xkbcli_compile_keymap, args):
185 xkbcli_compile_keymap.run_command_success(args)
188 @pytest.mark.parametrize('rmlvos', rmlvos)
189 def test_compile_keymap_rmlvo(xkbcli_compile_keymap, rmlvos):
190 xkbcli_compile_keymap.run_command_success(rmlvos)
193 @pytest.mark.parametrize('args', [['--include', '.', '--include-defaults'],
194 ['--include', '/tmp', '--include-defaults'],
196 def test_compile_keymap_include(xkbcli_compile_keymap, args):
197 # Succeeds thanks to include-defaults
198 xkbcli_compile_keymap.run_command_success(args)
201 def test_compile_keymap_include_invalid(xkbcli_compile_keymap):
202 # A non-directory is rejected by default
203 args = ['--include', '/proc/version']
204 rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
205 assert rc == 1, (stdout, stderr)
206 assert "There are no include paths to search" in stderr
208 # A non-existing directory is rejected by default
209 args = ['--include', '/tmp/does/not/exist']
210 rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
211 assert rc == 1, (stdout, stderr)
212 assert "There are no include paths to search" in stderr
214 # Valid dir, but missing files
215 args = ['--include', '/tmp']
216 rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
217 assert rc == 1, (stdout, stderr)
218 assert "Couldn't look up rules" in stderr
221 # Unicode codepoint conversions, we support whatever strtol does
222 @pytest.mark.parametrize('args', [['123'], ['0x123'], ['0123']])
223 def test_how_to_type(xkbcli_how_to_type, args):
224 xkbcli_how_to_type.run_command_success(args)
227 @pytest.mark.parametrize('rmlvos', rmlvos)
228 def test_how_to_type_rmlvo(xkbcli_how_to_type, rmlvos):
229 args = rmlvos + ['0x1234']
230 xkbcli_how_to_type.run_command_success(args)
233 @pytest.mark.parametrize('args', [['--verbose'],
235 ['--verbose', '--load-exotic'],
240 def test_list_rmlvo(xkbcli_list, args):
241 xkbcli_list.run_command_success(args)
244 def test_list_rmlvo_includes(xkbcli_list):
246 xkbcli_list.run_command_success(args)
249 def test_list_rmlvo_includes_invalid(xkbcli_list):
250 args = ['/proc/version']
251 rc, stdout, stderr = xkbcli_list.run_command(args)
253 assert "Failed to append include path" in stderr
256 def test_list_rmlvo_includes_no_defaults(xkbcli_list):
257 args = ['--skip-default-paths', '/tmp']
258 rc, stdout, stderr = xkbcli_list.run_command(args)
260 assert "Failed to parse XKB description" in stderr
263 @pytest.mark.skipif(not os.path.exists('/dev/input/event0'), reason='event node required')
264 @pytest.mark.skipif(not os.access('/dev/input/event0', os.R_OK), reason='insufficient permissions')
265 @pytest.mark.parametrize('rmlvos', rmlvos)
266 def test_interactive_evdev_rmlvo(xkbcli_interactive_evdev, rmlvos):
268 xkbcli_interactive_evdev.run_command_success(rmlvos)
271 @pytest.mark.skipif(not os.path.exists('/dev/input/event0'),
272 reason='event node required')
273 @pytest.mark.skipif(not os.access('/dev/input/event0', os.R_OK),
274 reason='insufficient permissions')
275 @pytest.mark.parametrize('args', [['--report-state-changes'],
276 ['--enable-compose'],
277 ['--consumed-mode=xkb'],
278 ['--consumed-mode=gtk'],
280 def test_interactive_evdev(xkbcli_interactive_evdev, args):
281 # Note: --enable-compose fails if $prefix doesn't have the compose tables
283 xkbcli_interactive_evdev.run_command_success(args)
286 @pytest.mark.skipif(not os.getenv('DISPLAY'), reason='DISPLAY not set')
287 def test_interactive_x11(xkbcli_interactive_x11):
288 # To be filled in if we handle something other than --help
292 @pytest.mark.skipif(not os.getenv('WAYLAND_DISPLAY'),
293 reason='WAYLAND_DISPLAY not set')
294 def test_interactive_wayland(xkbcli_interactive_wayland):
295 # To be filled in if we handle something other than --help
299 if __name__ == '__main__':
300 pytest.main(args=['@MESON_BUILD_ROOT@'])