at master 2.8 kB view raw
1# The test template is taken from the `./keymap.nix` 2{ 3 system ? builtins.currentSystem, 4 config ? { }, 5 pkgs ? import ../.. { inherit system config; }, 6}: 7 8with import ../lib/testing-python.nix { inherit system pkgs; }; 9 10let 11 readyFile = "/tmp/readerReady"; 12 resultFile = "/tmp/readerResult"; 13 14 testReader = pkgs.writeScript "test-input-reader" '' 15 rm -f ${resultFile} ${resultFile}.tmp 16 logger "testReader: START: Waiting for $1 characters, expecting '$2'." 17 touch ${readyFile} 18 read -r -N $1 chars 19 rm -f ${readyFile} 20 if [ "$chars" == "$2" ]; then 21 logger -s "testReader: PASS: Got '$2' as expected." 2>${resultFile}.tmp 22 else 23 logger -s "testReader: FAIL: Expected '$2' but got '$chars'." 2>${resultFile}.tmp 24 fi 25 # rename after the file is written to prevent a race condition 26 mv ${resultFile}.tmp ${resultFile} 27 ''; 28 29 mkKeyboardTest = 30 name: 31 { default, test }: 32 with pkgs.lib; 33 makeTest { 34 inherit name; 35 36 nodes.machine = { 37 services.keyd = { 38 enable = true; 39 keyboards = { inherit default; }; 40 }; 41 }; 42 43 testScript = '' 44 import shlex 45 46 machine.wait_for_unit("keyd.service") 47 48 def run_test_case(cmd, test_case_name, inputs, expected): 49 with subtest(test_case_name): 50 assert len(inputs) == len(expected) 51 machine.execute("rm -f ${readyFile} ${resultFile}") 52 # set up process that expects all the keys to be entered 53 machine.succeed( 54 "{} {} {} {} >&2 &".format( 55 cmd, 56 "${testReader}", 57 len(inputs), 58 shlex.quote("".join(expected)), 59 ) 60 ) 61 # wait for reader to be ready 62 machine.wait_for_file("${readyFile}") 63 # send all keys 64 for key in inputs: 65 machine.send_key(key) 66 # wait for result and check 67 machine.wait_for_file("${resultFile}") 68 machine.succeed("grep -q 'PASS:' ${resultFile}") 69 test = ${builtins.toJSON test} 70 run_test_case("openvt -sw --", "${name}", test["press"], test["expect"]) 71 ''; 72 }; 73 74in 75pkgs.lib.mapAttrs mkKeyboardTest { 76 swap-ab_and_ctrl-as-shift = { 77 test.press = [ 78 "a" 79 "ctrl-b" 80 "c" 81 "alt_r-h" 82 ]; 83 test.expect = [ 84 "b" 85 "A" 86 "c" 87 "q" 88 ]; 89 90 default = { 91 settings.main = { 92 "a" = "b"; 93 "b" = "a"; 94 "control" = "oneshot(shift)"; 95 "rightalt" = "layer(rightalt)"; 96 }; 97 extraConfig = '' 98 [rightalt:G] 99 h = q 100 ''; 101 }; 102 }; 103}