"""Steam pinyin runtime requirements, without network access or a running Steam client.""" import os from pathlib import Path import shutil import subprocess import tempfile import unittest TOOLS = Path(__file__).resolve().parents[1] SESSION = TOOLS / 'linuxfs/overlay/usr/local/bin/droiddeck-session' WRAPPER = '/usr/local/bin/droiddeck-steam-ime' ENGINE = '/usr/local/lib/droiddeck-ime/ibus-engine-pinyin' DAEMON = '/usr/bin/ibus-daemon' CLIENT = '/usr/bin/ibus' class SteamImeRuntimeTest(unittest.TestCase): @unittest.skipUnless(shutil.which('bash') and shutil.which('timeout'), 'needs bash and timeout') def test_steam_start_only_exports_address_after_success(self): source = SESSION.read_text() start = source.index(' unset IBUS_ADDRESS\n ime_ibus_pid=') fragment = source[start:source.index(' while :; do', start)] with tempfile.TemporaryDirectory() as directory: root = Path(directory) commands = { WRAPPER: ('wrapper', '#!/bin/bash\nprintf launched > "$XDG_RUNTIME_DIR/wrapper-started"\nexec sleep 5\n'), ENGINE: ('engine', '#!/bin/bash\nexit 0\n'), DAEMON: ('daemon', '#!/bin/bash\nexit 0\n'), CLIENT: ('client', '#!/bin/bash\ntest -f "$XDG_RUNTIME_DIR/wrapper-started" || exit 1\nprintf "pinyin\\n"\n'), } for path, (basename, script) in commands.items(): f = root / basename if path != DAEMON: f.write_text(script) f.chmod(0o755) fragment = fragment.replace(f'{path} ', f'"{f}" ').replace(f'{path} ]', f'"{f}" ]') env = dict(os.environ, XDG_RUNTIME_DIR=directory, IBUS_ADDRESS='unrelated-address') cleanup = '\nprintf "address=%s\\n" "${IBUS_ADDRESS-unset}"\n' cleanup += 'printf "pid=%s\\n" "${ime_ibus_pid:-none}"; stop_steam_ime\n' process = subprocess.run(['bash', '-uc', 'nap() { sleep "$1"; }\n' + fragment + cleanup], text=True, capture_output=True, check=True, env=env, timeout=10) self.assertIn('does not include IBus', process.stdout) self.assertIn('address=unset', process.stdout) self.assertFalse((root / 'wrapper-started').exists()) (root / 'daemon').write_text(commands[DAEMON][1]) (root / 'daemon').chmod(0o755) (root / 'client').write_text('#!/bin/bash\nexit 1\n') refused = fragment.replace('SECONDS + 5', 'SECONDS + 1') process = subprocess.run(['bash', '-uc', 'nap() { sleep "$1"; }\n' + refused + cleanup], text=True, capture_output=True, check=True, env=env, timeout=10) self.assertIn('the pinyin engine did not start', process.stdout) self.assertIn('address=unset', process.stdout) self.assertIn('pid=none', process.stdout) (root / 'wrapper-started').unlink() (root / 'client').write_text(commands[CLIENT][1]) process = subprocess.run(['bash', '-uc', 'nap() { sleep "$1"; }\n' + fragment + cleanup], text=True, capture_output=True, check=True, env=env, timeout=10) self.assertIn('pinyin engine ready', process.stdout) self.assertIn('address=unix:path=', process.stdout) self.assertEqual((root / 'wrapper-started').read_text(), 'launched') if __name__ == '__main__': unittest.main()