| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Copyright © 2020 Red Hat, Inc. |
| 4 |
# SPDX-License-Identifier: MIT |
| 5 |
|
| 6 |
import concurrent.futures |
| 7 |
import itertools |
| 8 |
import logging |
| 9 |
import os |
| 10 |
import resource |
| 11 |
import subprocess |
| 12 |
import sys |
| 13 |
import tempfile |
| 14 |
import unittest |
| 15 |
from abc import ABCMeta, abstractmethod |
| 16 |
from collections.abc import Callable, Generator |
| 17 |
from dataclasses import dataclass |
| 18 |
from functools import reduce |
| 19 |
from pathlib import Path |
| 20 |
from typing import ClassVar |
| 21 |
|
| 22 |
try: |
| 23 |
top_builddir = Path(os.environ["top_builddir"]) |
| 24 |
top_srcdir = Path(os.environ["top_srcdir"]) |
| 25 |
except KeyError: |
| 26 |
print( |
| 27 |
"Required environment variables not found: top_srcdir/top_builddir", |
| 28 |
file=sys.stderr, |
| 29 |
) |
| 30 |
|
| 31 |
top_srcdir = Path(".") |
| 32 |
try: |
| 33 |
top_builddir = next(Path(".").glob("**/meson-logs/")).parent |
| 34 |
except StopIteration: |
| 35 |
sys.exit(1) |
| 36 |
print( |
| 37 |
'Using srcdir "{}", builddir "{}"'.format(top_srcdir, top_builddir), |
| 38 |
file=sys.stderr, |
| 39 |
) |
| 40 |
|
| 41 |
TIMEOUT = 10.0 # seconds |
| 42 |
|
| 43 |
# Unset some environment variables, so that testing --enable-environment-names |
| 44 |
# does not actually depend on the current environment. |
| 45 |
for key in ( |
| 46 |
"XKB_DEFAULT_RULES", |
| 47 |
"XKB_DEFAULT_MODEL", |
| 48 |
"XKB_DEFAULT_LAYOUT", |
| 49 |
"XKB_DEFAULT_VARIANT", |
| 50 |
"XKB_DEFAULT_OPTIONS", |
| 51 |
): |
| 52 |
if key in os.environ: |
| 53 |
del os.environ[key] |
| 54 |
|
| 55 |
# Ensure locale is C, so we can check error messages in English |
| 56 |
os.environ["LC_ALL"] = "C.UTF-8" |
| 57 |
|
| 58 |
logging.basicConfig(level=logging.DEBUG) |
| 59 |
logger = logging.getLogger("test") |
| 60 |
logger.setLevel(logging.DEBUG) |
| 61 |
|
| 62 |
|
| 63 |
def powerset(iterable): |
| 64 |
"Subsequences of the iterable from shortest to longest." |
| 65 |
# powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3) |
| 66 |
s = tuple(iterable) |
| 67 |
return itertools.chain.from_iterable( |
| 68 |
itertools.combinations(s, r) for r in range(len(s) + 1) |
| 69 |
) |
| 70 |
|
| 71 |
|
| 72 |
# Permutation of RMLVO that we use in multiple tests |
| 73 |
rmlvo_options = ( |
| 74 |
"--rules=evdev", |
| 75 |
"--model=pc104", |
| 76 |
"--layout=ch", |
| 77 |
"--options=eurosign:5", |
| 78 |
"--enable-environment-names", |
| 79 |
) |
| 80 |
rmlvos = tuple( |
| 81 |
map( |
| 82 |
list, |
| 83 |
itertools.chain( |
| 84 |
itertools.permutations(rmlvo_options[:-1]), |
| 85 |
powerset(rmlvo_options), |
| 86 |
), |
| 87 |
) |
| 88 |
) |
| 89 |
|
| 90 |
|
| 91 |
@dataclass |
| 92 |
class RMLVO: |
| 93 |
rules: str | None = None |
| 94 |
model: str | None = None |
| 95 |
layout: str | None = None |
| 96 |
variant: str | None = None |
| 97 |
options: str | None = None |
| 98 |
env: bool = False |
| 99 |
|
| 100 |
def __iter__(self) -> Generator[tuple[str, ...]]: |
| 101 |
if self.rules is not None: |
| 102 |
yield "--rules", self.rules |
| 103 |
if self.model is not None: |
| 104 |
yield "--model", self.model |
| 105 |
if self.layout is not None: |
| 106 |
yield "--layout", self.layout |
| 107 |
if self.variant is not None: |
| 108 |
yield "--variant", self.variant |
| 109 |
if self.options is not None: |
| 110 |
yield "--options", self.options |
| 111 |
if self.env: |
| 112 |
yield ("--enable-environment-names",) |
| 113 |
|
| 114 |
@property |
| 115 |
def args(self) -> list[str]: |
| 116 |
return list(itertools.chain.from_iterable(self)) |
| 117 |
|
| 118 |
|
| 119 |
class Target(metaclass=ABCMeta): |
| 120 |
@property |
| 121 |
@abstractmethod |
| 122 |
def args(self) -> list[str]: ... |
| 123 |
|
| 124 |
@property |
| 125 |
def stdin(self) -> str | None: |
| 126 |
return None |
| 127 |
|
| 128 |
|
| 129 |
class RmlvoTarget(Target): |
| 130 |
@property |
| 131 |
def args(self) -> list[str]: |
| 132 |
return ["--rmlvo"] |
| 133 |
|
| 134 |
|
| 135 |
class KccgstTarget(Target): |
| 136 |
@property |
| 137 |
def args(self) -> list[str]: |
| 138 |
return ["--kccgst"] |
| 139 |
|
| 140 |
|
| 141 |
class KccgstYamlTarget(Target): |
| 142 |
@property |
| 143 |
def args(self) -> list[str]: |
| 144 |
return ["--kccgst-yaml"] |
| 145 |
|
| 146 |
|
| 147 |
@dataclass |
| 148 |
class KeymapTarget(Target, RMLVO): |
| 149 |
arg: bool = False |
| 150 |
path: Path | None = None |
| 151 |
stdin: str | None = None |
| 152 |
|
| 153 |
def _args(self) -> Generator[str]: |
| 154 |
yield from itertools.chain.from_iterable(self) |
| 155 |
if self.arg: |
| 156 |
yield "--keymap" |
| 157 |
if self.path: |
| 158 |
yield str(self.path) |
| 159 |
|
| 160 |
@property |
| 161 |
def args(self) -> list[str]: |
| 162 |
return list(self._args()) |
| 163 |
|
| 164 |
@property |
| 165 |
def use_rmlvo(self) -> bool: |
| 166 |
return ( |
| 167 |
not self.arg |
| 168 |
and not self.path |
| 169 |
and ( |
| 170 |
bool(self.rules) |
| 171 |
or bool(self.model) |
| 172 |
or bool(self.layout) |
| 173 |
or bool(self.variant) |
| 174 |
or bool(self.options) |
| 175 |
or bool(self.env) |
| 176 |
or not self.stdin |
| 177 |
) |
| 178 |
) |
| 179 |
|
| 180 |
|
| 181 |
def _disable_coredump(): |
| 182 |
resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) |
| 183 |
|
| 184 |
|
| 185 |
def run_command(args, input: str | None = None) -> tuple[int, str, str]: |
| 186 |
logger.debug("run command: {}".format(" ".join(args))) |
| 187 |
|
| 188 |
try: |
| 189 |
p = subprocess.run( |
| 190 |
args, |
| 191 |
preexec_fn=_disable_coredump, |
| 192 |
capture_output=True, |
| 193 |
input=input, |
| 194 |
text=True, |
| 195 |
timeout=0.7, |
| 196 |
) |
| 197 |
return p.returncode, p.stdout, p.stderr |
| 198 |
except subprocess.TimeoutExpired as e: |
| 199 |
return ( |
| 200 |
0, |
| 201 |
e.stdout.decode("utf-8") if e.stdout else "", |
| 202 |
e.stderr.decode("utf-8") if e.stderr else "", |
| 203 |
) |
| 204 |
|
| 205 |
|
| 206 |
@dataclass |
| 207 |
class XkbcliTool: |
| 208 |
xkbcli_tool: ClassVar[str] = "xkbcli" |
| 209 |
tool_path: ClassVar[Path] = top_builddir |
| 210 |
|
| 211 |
subtool: str | None = None |
| 212 |
skipIf: tuple[tuple[bool, str], ...] = () |
| 213 |
skipError: tuple[tuple[Callable[[int, str, str], bool], str], ...] = () |
| 214 |
|
| 215 |
def run_command(self, args, input: str | None = None) -> tuple[int, str, str]: |
| 216 |
for condition, reason in self.skipIf: |
| 217 |
if condition: |
| 218 |
raise unittest.SkipTest(reason) |
| 219 |
if self.subtool is not None: |
| 220 |
tool = "{}-{}".format(self.xkbcli_tool, self.subtool) |
| 221 |
else: |
| 222 |
tool = self.xkbcli_tool |
| 223 |
args = [os.path.join(self.tool_path, tool)] + args |
| 224 |
|
| 225 |
return run_command(args, input=input) |
| 226 |
|
| 227 |
def run_command_success(self, args, input: str | None = None) -> tuple[str, str]: |
| 228 |
rc, stdout, stderr = self.run_command(args, input=input) |
| 229 |
if rc != 0: |
| 230 |
for testfunc, reason in self.skipError: |
| 231 |
if testfunc(rc, stdout, stderr): |
| 232 |
raise unittest.SkipTest(reason) |
| 233 |
assert rc == 0, (rc, stdout, stderr) |
| 234 |
return stdout, stderr |
| 235 |
|
| 236 |
def run_command_invalid( |
| 237 |
self, args, input: str | None = None |
| 238 |
) -> tuple[int, str, str]: |
| 239 |
rc, stdout, stderr = self.run_command(args, input=input) |
| 240 |
assert rc == 2, (rc, stdout, stderr) |
| 241 |
return rc, stdout, stderr |
| 242 |
|
| 243 |
def run_command_unrecognized_option(self, args): |
| 244 |
rc, stdout, stderr = self.run_command(args) |
| 245 |
assert rc == 2, (rc, stdout, stderr) |
| 246 |
assert stdout.startswith("Usage") or stdout == "" |
| 247 |
assert "unrecognized option" in stderr |
| 248 |
|
| 249 |
def run_command_missing_arg(self, args): |
| 250 |
rc, stdout, stderr = self.run_command(args) |
| 251 |
assert rc == 2, (rc, stdout, stderr) |
| 252 |
assert stdout.startswith("Usage") or stdout == "" |
| 253 |
assert "requires an argument" in stderr |
| 254 |
|
| 255 |
def __str__(self): |
| 256 |
return str(self.subtool) |
| 257 |
|
| 258 |
|
| 259 |
class TestXkbcli(unittest.TestCase): |
| 260 |
xkbcli: ClassVar[XkbcliTool] |
| 261 |
xkbcli_list: ClassVar[XkbcliTool] |
| 262 |
xkbcli_how_to_type: ClassVar[XkbcliTool] |
| 263 |
xkbcli_compile_keymap: ClassVar[XkbcliTool] |
| 264 |
xkbcli_compile_compose: ClassVar[XkbcliTool] |
| 265 |
xkbcli_interactive_evdev: ClassVar[XkbcliTool] |
| 266 |
xkbcli_interactive_x11: ClassVar[XkbcliTool] |
| 267 |
xkbcli_interactive_wayland: ClassVar[XkbcliTool] |
| 268 |
xkbcli_interactive: ClassVar[XkbcliTool] |
| 269 |
xkbcli_dump_keymap_x11: ClassVar[XkbcliTool] |
| 270 |
xkbcli_dump_keymap_wayland: ClassVar[XkbcliTool] |
| 271 |
xkbcli_dump_keymap: ClassVar[XkbcliTool] |
| 272 |
all_tools: ClassVar[list[XkbcliTool]] |
| 273 |
|
| 274 |
@classmethod |
| 275 |
def setUpClass(cls): |
| 276 |
cls.xkbcli = XkbcliTool() |
| 277 |
cls.xkbcli_list = XkbcliTool( |
| 278 |
"list", |
| 279 |
skipIf=( |
| 280 |
( |
| 281 |
not int(os.getenv("HAVE_XKBCLI_LIST", "1")), |
| 282 |
"xkbregistory not enabled", |
| 283 |
), |
| 284 |
), |
| 285 |
) |
| 286 |
cls.xkbcli_how_to_type = XkbcliTool("how-to-type") |
| 287 |
cls.xkbcli_compile_keymap = XkbcliTool("compile-keymap") |
| 288 |
cls.xkbcli_compile_compose = XkbcliTool("compile-compose") |
| 289 |
no_interactive_evdev = ( |
| 290 |
( |
| 291 |
not int(os.getenv("HAVE_XKBCLI_INTERACTIVE_EVDEV", "1")), |
| 292 |
"evdev not enabled", |
| 293 |
), |
| 294 |
(not os.path.exists("/dev/input/event0"), "event node required"), |
| 295 |
( |
| 296 |
not os.access("/dev/input/event0", os.R_OK), |
| 297 |
"insufficient permissions", |
| 298 |
), |
| 299 |
) |
| 300 |
cls.xkbcli_interactive_evdev = XkbcliTool( |
| 301 |
"interactive-evdev", |
| 302 |
skipIf=no_interactive_evdev, |
| 303 |
skipError=( |
| 304 |
( |
| 305 |
lambda rc, stdout, stderr: "Couldn't find any keyboards" in stderr, |
| 306 |
"No keyboards available", |
| 307 |
), |
| 308 |
), |
| 309 |
) |
| 310 |
no_interactive_x11 = ( |
| 311 |
( |
| 312 |
not int(os.getenv("HAVE_XKBCLI_INTERACTIVE_X11", "1")), |
| 313 |
"x11 not enabled", |
| 314 |
), |
| 315 |
(not os.getenv("DISPLAY"), "DISPLAY not set"), |
| 316 |
) |
| 317 |
cls.xkbcli_interactive_x11 = XkbcliTool( |
| 318 |
"interactive-x11", |
| 319 |
skipIf=no_interactive_x11, |
| 320 |
) |
| 321 |
no_interactive_wayland = ( |
| 322 |
( |
| 323 |
not int(os.getenv("HAVE_XKBCLI_INTERACTIVE_WAYLAND", "1")), |
| 324 |
"wayland not enabled", |
| 325 |
), |
| 326 |
(not os.getenv("WAYLAND_DISPLAY"), "WAYLAND_DISPLAY not set"), |
| 327 |
) |
| 328 |
cls.xkbcli_interactive_wayland = XkbcliTool( |
| 329 |
"interactive-wayland", |
| 330 |
skipIf=no_interactive_wayland, |
| 331 |
) |
| 332 |
# NOTE: `interactive` cannot be tested, because it hardcodes the paths |
| 333 |
# of the `xkbcli-interactive-{wayland,x11}` tools it calls to the |
| 334 |
# *install* directory, while we need to use those of the *build* |
| 335 |
# directory. |
| 336 |
|
| 337 |
cls.xkbcli_dump_keymap_x11 = XkbcliTool( |
| 338 |
"dump-keymap-x11", |
| 339 |
skipIf=no_interactive_x11, |
| 340 |
) |
| 341 |
cls.xkbcli_dump_keymap_wayland = XkbcliTool( |
| 342 |
"dump-keymap-wayland", |
| 343 |
skipIf=no_interactive_wayland, |
| 344 |
) |
| 345 |
# NOTE: `dump-keymap` cannot be tested, because it hardcodes the paths |
| 346 |
# of the `xkbcli-dump-keymap-{wayland,x11}` tools it calls to the |
| 347 |
# *install* directory, while we need to use those of the *build* |
| 348 |
# directory. |
| 349 |
|
| 350 |
cls.all_tools = [ |
| 351 |
cls.xkbcli, |
| 352 |
cls.xkbcli_list, |
| 353 |
cls.xkbcli_how_to_type, |
| 354 |
cls.xkbcli_compile_keymap, |
| 355 |
cls.xkbcli_compile_compose, |
| 356 |
cls.xkbcli_interactive_evdev, |
| 357 |
cls.xkbcli_interactive_x11, |
| 358 |
cls.xkbcli_interactive_wayland, |
| 359 |
cls.xkbcli_dump_keymap_x11, |
| 360 |
cls.xkbcli_dump_keymap_wayland, |
| 361 |
] |
| 362 |
|
| 363 |
def test_help(self): |
| 364 |
# --help is supported by all tools |
| 365 |
for tool in self.all_tools: |
| 366 |
with self.subTest(tool=tool): |
| 367 |
stdout, stderr = tool.run_command_success(["--help"]) |
| 368 |
assert stdout.startswith("Usage:") |
| 369 |
assert stderr == "" |
| 370 |
|
| 371 |
def test_invalid_option(self): |
| 372 |
# --foobar generates "Usage:" for all tools |
| 373 |
for tool in self.all_tools: |
| 374 |
with self.subTest(tool=tool): |
| 375 |
tool.run_command_unrecognized_option(["--foobar"]) |
| 376 |
|
| 377 |
def test_xkbcli_version(self): |
| 378 |
# xkbcli --version |
| 379 |
stdout, stderr = self.xkbcli.run_command_success(["--version"]) |
| 380 |
assert stdout.startswith("1") |
| 381 |
assert stderr == "" |
| 382 |
|
| 383 |
def test_xkbcli_too_many_args(self): |
| 384 |
self.xkbcli.run_command_invalid(["a"] * 64) |
| 385 |
|
| 386 |
def test_compile_keymap_args(self): |
| 387 |
xkb_root = Path(os.environ["XKB_CONFIG_ROOT"]) |
| 388 |
keymap_path = xkb_root / "keymaps/masks.xkb" |
| 389 |
keymap_string = keymap_path.read_text(encoding="utf-8") |
| 390 |
test_option = "--test" |
| 391 |
target: Target |
| 392 |
for target in ( |
| 393 |
RmlvoTarget(), |
| 394 |
KccgstTarget(), |
| 395 |
KccgstYamlTarget(), |
| 396 |
# Keymap from RMLVO |
| 397 |
KeymapTarget(), |
| 398 |
# Keymap from RMLVO (stdin ignored) |
| 399 |
KeymapTarget(layout="us", stdin=keymap_string), |
| 400 |
KeymapTarget(env=True, stdin=keymap_string), |
| 401 |
# Keymap parsed from keymap file |
| 402 |
KeymapTarget(arg=False, path=keymap_path), |
| 403 |
KeymapTarget(arg=True, path=keymap_path), |
| 404 |
# Keymap parsed from stdin |
| 405 |
KeymapTarget(arg=False, stdin=keymap_string), |
| 406 |
KeymapTarget(arg=True, stdin=keymap_string), |
| 407 |
): |
| 408 |
for options in powerset(("--verbose", test_option)): |
| 409 |
args = list(options) + target.args |
| 410 |
with self.subTest(args=args): |
| 411 |
stdout, stderr = self.xkbcli_compile_keymap.run_command_success( |
| 412 |
args, input=target.stdin |
| 413 |
) |
| 414 |
if test_option in options: |
| 415 |
# Expect no output |
| 416 |
assert stdout == "", (target, stdout) |
| 417 |
if isinstance(target, KeymapTarget) and test_option not in options: |
| 418 |
# Check keymap output for excepted bits: |
| 419 |
# - <AB01> is not defined in mask.xkb |
| 420 |
# - virtual_modifiers Test01 is only defined in masks.xkb |
| 421 |
assert ("<AB01>" in stdout) ^ (not target.use_rmlvo), ( |
| 422 |
target, |
| 423 |
stdout, |
| 424 |
stderr, |
| 425 |
) |
| 426 |
assert ("virtual_modifiers Test01" in stdout) ^ ( |
| 427 |
target.use_rmlvo |
| 428 |
), (target, stdout, stderr) |
| 429 |
|
| 430 |
def test_compile_keymap_mutually_exclusive_args(self): |
| 431 |
xkb_root = Path(os.environ["XKB_CONFIG_ROOT"]) |
| 432 |
keymap_path = xkb_root / "keymaps/basic.xkb" |
| 433 |
keymap_string = keymap_path.read_text(encoding="utf-8") |
| 434 |
keymap_from_stdin = KeymapTarget(arg=True, stdin=keymap_string) |
| 435 |
keymap_from_path1 = KeymapTarget(arg=True, path=Path(keymap_path)) |
| 436 |
keymap_from_path2 = KeymapTarget(arg=False, path=Path(keymap_path)) |
| 437 |
rmlvo = RmlvoTarget() |
| 438 |
kccgst = KccgstTarget() |
| 439 |
kccgstYaml = KccgstYamlTarget() |
| 440 |
for entry in ( |
| 441 |
# --keymap does not use RMLVO options |
| 442 |
("--rules", "some-rules", keymap_from_stdin), |
| 443 |
("--model", "some-model", keymap_from_stdin), |
| 444 |
("--layout", "some-layout", keymap_from_stdin), |
| 445 |
("--variant", "some-variant", keymap_from_stdin), |
| 446 |
("--options", "some-option", keymap_from_stdin), |
| 447 |
("--rules", "some-rules", keymap_from_path1), |
| 448 |
("--model", "some-model", keymap_from_path1), |
| 449 |
("--layout", "some-layout", keymap_from_path1), |
| 450 |
("--variant", "some-variant", keymap_from_path1), |
| 451 |
("--options", "some-option", keymap_from_path1), |
| 452 |
# Trailing keymap file with RMLVO options |
| 453 |
("--rules", "some-rules", keymap_from_path2), |
| 454 |
("--model", "some-model", keymap_from_path2), |
| 455 |
("--layout", "some-layout", keymap_from_path2), |
| 456 |
("--variant", "some-variant", keymap_from_path2), |
| 457 |
("--options", "some-option", keymap_from_path2), |
| 458 |
# Incompatible output types |
| 459 |
(rmlvo, keymap_from_stdin), |
| 460 |
(rmlvo, keymap_from_path1), |
| 461 |
(rmlvo, keymap_from_path2), |
| 462 |
(kccgst, kccgstYaml), |
| 463 |
(kccgst, keymap_from_stdin), |
| 464 |
(kccgst, keymap_from_path1), |
| 465 |
(kccgst, keymap_from_path2), |
| 466 |
(kccgst, rmlvo), |
| 467 |
(kccgst, rmlvo, keymap_from_stdin), |
| 468 |
(kccgst, rmlvo, keymap_from_path1), |
| 469 |
(kccgst, rmlvo, keymap_from_path2), |
| 470 |
(kccgstYaml, keymap_from_stdin), |
| 471 |
(kccgstYaml, keymap_from_path1), |
| 472 |
(kccgstYaml, keymap_from_path2), |
| 473 |
(kccgstYaml, rmlvo), |
| 474 |
(kccgstYaml, rmlvo, keymap_from_stdin), |
| 475 |
(kccgstYaml, rmlvo, keymap_from_path1), |
| 476 |
(kccgstYaml, rmlvo, keymap_from_path2), |
| 477 |
): |
| 478 |
with self.subTest(args=entry): |
| 479 |
args: list[str] = list( |
| 480 |
itertools.chain.from_iterable( |
| 481 |
arg.args if isinstance(arg, Target) else arg for arg in entry |
| 482 |
) |
| 483 |
) |
| 484 |
input: str | None = reduce( |
| 485 |
lambda acc, arg: acc |
| 486 |
or (arg.stdin if isinstance(arg, Target) else None), |
| 487 |
args, |
| 488 |
None, |
| 489 |
) |
| 490 |
self.xkbcli_compile_keymap.run_command_invalid(args, input=input) |
| 491 |
|
| 492 |
def test_compile_keymap_rmlvo(self): |
| 493 |
def run(target, rmlvo): |
| 494 |
return self.xkbcli_compile_keymap.run_command_success(target + rmlvo) |
| 495 |
|
| 496 |
with concurrent.futures.ThreadPoolExecutor() as executor: |
| 497 |
futures = { |
| 498 |
executor.submit(run, target.args, rmlvo): (target, rmlvo) |
| 499 |
for target in ( |
| 500 |
RmlvoTarget(), |
| 501 |
KccgstTarget(), |
| 502 |
KccgstYamlTarget(), |
| 503 |
KeymapTarget(), |
| 504 |
) |
| 505 |
for rmlvo in rmlvos |
| 506 |
} |
| 507 |
for future in concurrent.futures.as_completed(futures, TIMEOUT): |
| 508 |
target, rmlvo = futures[future] |
| 509 |
with self.subTest(target=target, rmlvo=rmlvo): |
| 510 |
future.result() |
| 511 |
|
| 512 |
def test_compile_keymap_include(self): |
| 513 |
for args in ( |
| 514 |
["--include", ".", "--include-defaults"], |
| 515 |
["--include", "/tmp", "--include-defaults"], |
| 516 |
): |
| 517 |
with self.subTest(args=args): |
| 518 |
# Succeeds thanks to include-defaults |
| 519 |
self.xkbcli_compile_keymap.run_command_success(args) |
| 520 |
|
| 521 |
def test_compile_keymap_include_invalid(self): |
| 522 |
# A non-directory is rejected by default |
| 523 |
args = ["--include", "/proc/version"] |
| 524 |
rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args) |
| 525 |
assert rc == 1, (stdout, stderr) |
| 526 |
assert "There are no include paths to search" in stderr |
| 527 |
|
| 528 |
# A non-existing directory is rejected by default |
| 529 |
args = ["--include", "/tmp/does/not/exist"] |
| 530 |
rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args) |
| 531 |
assert rc == 1, (stdout, stderr) |
| 532 |
assert "There are no include paths to search" in stderr |
| 533 |
|
| 534 |
# Valid dir, but missing files |
| 535 |
args = ["--include", "/tmp"] |
| 536 |
rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args) |
| 537 |
assert rc == 1, (stdout, stderr) |
| 538 |
assert "Couldn't look up rules" in stderr |
| 539 |
|
| 540 |
def test_compile_compose(self): |
| 541 |
for args in (["--verbose"],): |
| 542 |
with self.subTest(args=args): |
| 543 |
self.xkbcli_compile_compose.run_command_success(args) |
| 544 |
|
| 545 |
def test_how_to_type(self): |
| 546 |
for args in (["--verbose", "1"],): |
| 547 |
with self.subTest(args=args): |
| 548 |
self.xkbcli_how_to_type.run_command_success(args) |
| 549 |
|
| 550 |
@dataclass |
| 551 |
class Entry: |
| 552 |
args: list[str] |
| 553 |
name: str |
| 554 |
value: int |
| 555 |
|
| 556 |
for entry in ( |
| 557 |
# Unicode codepoint conversions, we support whatever strtol does |
| 558 |
Entry(args=["123"], name="braceleft", value=0x007B), |
| 559 |
Entry(args=["0123"], name="braceleft", value=0x007B), |
| 560 |
Entry(args=["0a"], name="Linefeed", value=0xFF0A), |
| 561 |
Entry(args=["0x123"], name="gcedilla", value=0x03BB), |
| 562 |
Entry(args=["U+123"], name="gcedilla", value=0x03BB), |
| 563 |
# Characters |
| 564 |
Entry(args=["1"], name="1", value=0x0031), |
| 565 |
Entry(args=["a"], name="a", value=0x0061), |
| 566 |
Entry(args=["á"], name="aacute", value=0x00E1), |
| 567 |
# Keysyms names (fallback without --keysym option) |
| 568 |
Entry(args=["acute"], name="acute", value=0x00B4), |
| 569 |
Entry(args=["U123"], name="U0123", value=0x1000123), |
| 570 |
# Keysyms names (with --keysym) |
| 571 |
Entry(args=["--keysym", "1"], name="1", value=0x0031), |
| 572 |
Entry(args=["--keysym", "a"], name="a", value=0x0061), |
| 573 |
Entry(args=["--keysym", "acute"], name="acute", value=0x00B4), |
| 574 |
Entry(args=["--keysym", "U123"], name="U0123", value=0x1000123), |
| 575 |
# Keysym values |
| 576 |
Entry(args=["--keysym", "123"], name="braceleft", value=0x007B), |
| 577 |
Entry(args=["--keysym", "0x123"], name="0x00000123", value=0x0123), |
| 578 |
): |
| 579 |
with self.subTest(args=args): |
| 580 |
stdout, _stderr = self.xkbcli_how_to_type.run_command_success( |
| 581 |
entry.args |
| 582 |
) |
| 583 |
expected = f"keysym: {entry.name} (0x{entry.value:04x})" |
| 584 |
lines = stdout.splitlines() |
| 585 |
assert len(lines) >= 1 |
| 586 |
assert lines[0] == expected, ( |
| 587 |
entry, |
| 588 |
f'expected: "{expected}", but got: "{lines[0]}"', |
| 589 |
) |
| 590 |
|
| 591 |
def test_how_to_type_rmlvo(self): |
| 592 |
def run(rmlvo): |
| 593 |
args = rmlvo + ["0x1234"] |
| 594 |
return self.xkbcli_how_to_type.run_command_success(args) |
| 595 |
|
| 596 |
with concurrent.futures.ThreadPoolExecutor() as executor: |
| 597 |
futures = {executor.submit(run, rmlvo): rmlvo for rmlvo in rmlvos} |
| 598 |
for future in concurrent.futures.as_completed(futures, TIMEOUT): |
| 599 |
rmlvo = futures[future] |
| 600 |
with self.subTest(rmlvo=rmlvo): |
| 601 |
future.result() |
| 602 |
|
| 603 |
def test_list_rmlvo(self): |
| 604 |
for args in ( |
| 605 |
["--verbose"], |
| 606 |
["-v"], |
| 607 |
["--verbose", "--load-exotic"], |
| 608 |
["--load-exotic"], |
| 609 |
["--ruleset=evdev"], |
| 610 |
["--ruleset=base"], |
| 611 |
): |
| 612 |
with self.subTest(args=args): |
| 613 |
self.xkbcli_list.run_command_success(args) |
| 614 |
|
| 615 |
def test_list_rmlvo_includes(self): |
| 616 |
args = ["/tmp/"] |
| 617 |
self.xkbcli_list.run_command_success(args) |
| 618 |
|
| 619 |
def test_list_rmlvo_includes_invalid(self): |
| 620 |
args = ["/proc/version"] |
| 621 |
rc, stdout, stderr = self.xkbcli_list.run_command(args) |
| 622 |
assert rc == 1 |
| 623 |
assert "Failed to append include path" in stderr |
| 624 |
|
| 625 |
def test_list_rmlvo_includes_no_defaults(self): |
| 626 |
args = ["--skip-default-paths", "/tmp"] |
| 627 |
rc, stdout, stderr = self.xkbcli_list.run_command(args) |
| 628 |
assert rc == 1 |
| 629 |
assert "Failed to parse XKB description" in stderr |
| 630 |
|
| 631 |
def test_interactive_evdev_rmlvo(self): |
| 632 |
def run(rmlvo): |
| 633 |
return self.xkbcli_interactive_evdev.run_command_success(rmlvo) |
| 634 |
|
| 635 |
with concurrent.futures.ThreadPoolExecutor() as executor: |
| 636 |
futures = {executor.submit(run, rmlvo): rmlvo for rmlvo in rmlvos} |
| 637 |
for future in concurrent.futures.as_completed(futures, TIMEOUT): |
| 638 |
rmlvo = futures[future] |
| 639 |
with self.subTest(rmlvo=rmlvo): |
| 640 |
future.result() |
| 641 |
|
| 642 |
def test_interactive_evdev(self): |
| 643 |
# Note: --enable-compose fails if $prefix doesn't have the compose tables |
| 644 |
# installed |
| 645 |
for args in ( |
| 646 |
["--verbose"], |
| 647 |
["--uniline"], |
| 648 |
["--multiline"], |
| 649 |
["--report-state-changes"], |
| 650 |
["--enable-compose"], |
| 651 |
["--consumed-mode=xkb"], |
| 652 |
["--consumed-mode=gtk"], |
| 653 |
["--without-x11-offset"], |
| 654 |
): |
| 655 |
with self.subTest(args=args): |
| 656 |
self.xkbcli_interactive_evdev.run_command_success(args) |
| 657 |
|
| 658 |
def test_interactive_x11(self): |
| 659 |
for args in ( |
| 660 |
["--verbose"], |
| 661 |
["--uniline"], |
| 662 |
["--multiline"], |
| 663 |
): |
| 664 |
with self.subTest(args=args): |
| 665 |
self.xkbcli_interactive_x11.run_command_success(args) |
| 666 |
|
| 667 |
def test_interactive_wayland(self): |
| 668 |
for args in ( |
| 669 |
["--verbose"], |
| 670 |
["--uniline"], |
| 671 |
["--multiline"], |
| 672 |
): |
| 673 |
with self.subTest(args=args): |
| 674 |
self.xkbcli_interactive_wayland.run_command_success(args) |
| 675 |
|
| 676 |
|
| 677 |
if __name__ == "__main__": |
| 678 |
with tempfile.TemporaryDirectory() as tmpdir: |
| 679 |
# Use our own test xkeyboard-config copy. |
| 680 |
os.environ["XKB_CONFIG_ROOT"] = str(top_srcdir / "test/data") |
| 681 |
# Use our own X11 locale copy. |
| 682 |
os.environ["XLOCALEDIR"] = str(top_srcdir / "test/data/locale") |
| 683 |
# Use our own locale. |
| 684 |
os.environ["LC_CTYPE"] = "en_US.UTF-8" |
| 685 |
# libxkbcommon has fallbacks when XDG_CONFIG_HOME isn't set so we need |
| 686 |
# to override it with a known (empty) directory. Otherwise our test |
| 687 |
# behavior depends on the system the test is run on. |
| 688 |
os.environ["XDG_CONFIG_HOME"] = tmpdir |
| 689 |
# Prevent the legacy $HOME/.xkb from kicking in. |
| 690 |
del os.environ["HOME"] |
| 691 |
# This needs to be separated if we do specific extra path testing |
| 692 |
os.environ["XKB_CONFIG_EXTRA_PATH"] = tmpdir |
| 693 |
os.environ["XKB_CONFIG_VERSIONED_EXTENSIONS_PATH"] = tmpdir |
| 694 |
os.environ["XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH"] = tmpdir |
| 695 |
|
| 696 |
unittest.main() |