at 23.05-pre 6.9 kB view raw
1# Terminal emulators all present a pretty similar interface. 2# That gives us an opportunity to easily test their basic functionality with a single codebase. 3# 4# There are two tests run on each terminal emulator 5# - can it successfully execute a command passed on the cmdline? 6# - can it successfully display a colour? 7# the latter is used as a proxy for "can it display text?", without going through all the intricacies of OCR. 8# 9# 256-colour terminal mode is used to display the test colour, since it has a universally-applicable palette (unlike 8- and 16- colour, where the colours are implementation-defined), and it is widely supported (unlike 24-bit colour). 10# 11# Future work: 12# - Wayland support (both for testing the existing terminals, and for testing wayland-only terminals like foot and havoc) 13# - Test keyboard input? (skipped for now, to eliminate the possibility of race conditions and focus issues) 14 15{ system ? builtins.currentSystem, 16 config ? {}, 17 pkgs ? import ../.. { inherit system config; } 18}: 19 20with import ../lib/testing-python.nix { inherit system pkgs; }; 21with pkgs.lib; 22 23let tests = { 24 alacritty.pkg = p: p.alacritty; 25 26 # times out after spending many hours 27 #contour.pkg = p: p.contour; 28 #contour.cmd = "contour $command"; 29 30 cool-retro-term.pkg = p: p.cool-retro-term; 31 cool-retro-term.colourTest = false; # broken by gloss effect 32 33 ctx.pkg = p: p.ctx; 34 ctx.pinkValue = "#FE0065"; 35 36 darktile.pkg = p: p.darktile; 37 38 eterm.pkg = p: p.eterm; 39 eterm.executable = "Eterm"; 40 eterm.pinkValue = "#D40055"; 41 42 germinal.pkg = p: p.germinal; 43 44 gnome-terminal.pkg = p: p.gnome.gnome-terminal; 45 46 guake.pkg = p: p.guake; 47 guake.cmd = "SHELL=$command guake --show"; 48 guake.kill = true; 49 50 hyper.pkg = p: p.hyper; 51 52 kermit.pkg = p: p.kermit-terminal; 53 54 kgx.pkg = p: p.kgx; 55 kgx.cmd = "kgx -e $command"; 56 kgx.kill = true; 57 58 kitty.pkg = p: p.kitty; 59 kitty.cmd = "kitty $command"; 60 61 konsole.pkg = p: p.plasma5Packages.konsole; 62 63 lxterminal.pkg = p: p.lxterminal; 64 65 mate-terminal.pkg = p: p.mate.mate-terminal; 66 mate-terminal.cmd = "SHELL=$command mate-terminal --disable-factory"; # factory mode uses dbus, and we don't have a proper dbus session set up 67 68 mlterm.pkg = p: p.mlterm; 69 70 mrxvt.pkg = p: p.mrxvt; 71 72 qterminal.pkg = p: p.lxqt.qterminal; 73 qterminal.kill = true; 74 75 roxterm.pkg = p: p.roxterm; 76 roxterm.cmd = "roxterm -e $command"; 77 78 sakura.pkg = p: p.sakura; 79 80 st.pkg = p: p.st; 81 st.kill = true; 82 83 stupidterm.pkg = p: p.stupidterm; 84 stupidterm.cmd = "stupidterm -- $command"; 85 86 terminator.pkg = p: p.terminator; 87 terminator.cmd = "terminator -e $command"; 88 89 terminology.pkg = p: p.enlightenment.terminology; 90 terminology.cmd = "SHELL=$command terminology --no-wizard=true"; 91 terminology.colourTest = false; # broken by gloss effect 92 93 termite.pkg = p: p.termite; 94 95 termonad.pkg = p: p.termonad; 96 97 tilda.pkg = p: p.tilda; 98 99 tilix.pkg = p: p.tilix; 100 tilix.cmd = "tilix -e $command"; 101 102 urxvt.pkg = p: p.rxvt-unicode; 103 104 wayst.pkg = p: p.wayst; 105 wayst.pinkValue = "#FF0066"; 106 107 # times out after spending many hours 108 #wezterm.pkg = p: p.wezterm; 109 110 xfce4-terminal.pkg = p: p.xfce.xfce4-terminal; 111 112 xterm.pkg = p: p.xterm; 113 }; 114in mapAttrs (name: { pkg, executable ? name, cmd ? "SHELL=$command ${executable}", colourTest ? true, pinkValue ? "#FF0087", kill ? false }: makeTest 115{ 116 name = "terminal-emulator-${name}"; 117 meta = with pkgs.lib.maintainers; { 118 maintainers = [ jjjollyjim ]; 119 }; 120 121 machine = { pkgsInner, ... }: 122 123 { 124 imports = [ ./common/x11.nix ./common/user-account.nix ]; 125 126 # Hyper (and any other electron-based terminals) won't run as root 127 test-support.displayManager.auto.user = "alice"; 128 129 environment.systemPackages = [ 130 (pkg pkgs) 131 (pkgs.writeShellScriptBin "report-success" '' 132 echo 1 > /tmp/term-ran-successfully 133 ${optionalString kill "pkill ${executable}"} 134 '') 135 (pkgs.writeShellScriptBin "display-colour" '' 136 # A 256-colour background colour code for pink, then spaces. 137 # 138 # Background is used rather than foreground to minimize the effect of anti-aliasing. 139 # 140 # Keep adding more in case the window is partially offscreen to the left or requires 141 # a change to correctly redraw after initialising the window (as with ctx). 142 143 while : 144 do 145 echo -ne "\e[48;5;198m " 146 sleep 0.5 147 done 148 sleep infinity 149 '') 150 (pkgs.writeShellScriptBin "run-in-this-term" "sudo -u alice run-in-this-term-wrapped $1") 151 152 (pkgs.writeShellScriptBin "run-in-this-term-wrapped" "command=\"$(which \"$1\")\"; ${cmd}") 153 ]; 154 155 # Helpful reminder to add this test to passthru.tests 156 warnings = if !((pkg pkgs) ? "passthru" && (pkg pkgs).passthru ? "tests") then [ "The package for ${name} doesn't have a passthru.tests" ] else [ ]; 157 }; 158 159 # We need imagemagick, though not tesseract 160 enableOCR = true; 161 162 testScript = { nodes, ... }: let 163 in '' 164 with subtest("wait for x"): 165 start_all() 166 machine.wait_for_x() 167 168 with subtest("have the terminal run a command"): 169 # We run this command synchronously, so we can be certain the exit codes are happy 170 machine.${if kill then "execute" else "succeed"}("run-in-this-term report-success") 171 machine.wait_for_file("/tmp/term-ran-successfully") 172 ${optionalString colourTest '' 173 174 import tempfile 175 import subprocess 176 177 178 def check_for_pink(final=False) -> bool: 179 with tempfile.NamedTemporaryFile() as tmpin: 180 machine.send_monitor_command("screendump {}".format(tmpin.name)) 181 182 cmd = 'convert {} -define histogram:unique-colors=true -format "%c" histogram:info:'.format( 183 tmpin.name 184 ) 185 ret = subprocess.run(cmd, shell=True, capture_output=True) 186 if ret.returncode != 0: 187 raise Exception( 188 "image analysis failed with exit code {}".format(ret.returncode) 189 ) 190 191 text = ret.stdout.decode("utf-8") 192 return "${pinkValue}" in text 193 194 195 with subtest("ensuring no pink is present without the terminal"): 196 assert ( 197 check_for_pink() == False 198 ), "Pink was present on the screen before we even launched a terminal!" 199 200 with subtest("have the terminal display a colour"): 201 # We run this command in the background 202 assert machine.shell is not None 203 machine.shell.send(b"(run-in-this-term display-colour |& systemd-cat -t terminal) &\n") 204 205 with machine.nested("Waiting for the screen to have pink on it:"): 206 retry(check_for_pink) 207 ''}''; 208} 209 210 ) tests