|
| 1 | +import os |
| 2 | +import stat as stat_module |
| 3 | +import sys |
| 4 | +import tempfile |
| 5 | +import unittest |
| 6 | +from pathlib import Path |
| 7 | +from unittest import mock |
| 8 | + |
| 9 | + |
| 10 | +ROOT = Path(__file__).resolve().parents[1] |
| 11 | +sys.path.insert(0, str(ROOT / "lib" / "src")) |
| 12 | + |
| 13 | +from session_environment import ensure_wayland_display |
| 14 | + |
| 15 | + |
| 16 | +class SessionEnvironmentTests(unittest.TestCase): |
| 17 | + def _socket_file_at(self, path, mtime=100): |
| 18 | + path.write_text("", encoding="utf-8") |
| 19 | + path.chmod(0o600) |
| 20 | + os.utime(path, (mtime, mtime)) |
| 21 | + return path |
| 22 | + |
| 23 | + def _non_socket_file_at(self, path, mtime=100): |
| 24 | + path.write_text("", encoding="utf-8") |
| 25 | + path.chmod(0o644) |
| 26 | + os.utime(path, (mtime, mtime)) |
| 27 | + return path |
| 28 | + |
| 29 | + def _socket_mode_patch(self): |
| 30 | + # The sandbox can deny AF_UNIX bind() in temp dirs, so tests mark fake |
| 31 | + # sockets with a distinct mode while production uses the real stat check. |
| 32 | + return mock.patch( |
| 33 | + "session_environment.stat.S_ISSOCK", |
| 34 | + side_effect=lambda mode: stat_module.S_IMODE(mode) == 0o600, |
| 35 | + ) |
| 36 | + |
| 37 | + def test_does_nothing_when_wayland_display_already_set(self): |
| 38 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 39 | + self._socket_file_at(Path(tmpdir) / "wayland-1") |
| 40 | + with ( |
| 41 | + mock.patch.dict( |
| 42 | + os.environ, |
| 43 | + {"WAYLAND_DISPLAY": "wayland-existing", "XDG_RUNTIME_DIR": tmpdir}, |
| 44 | + clear=True, |
| 45 | + ), |
| 46 | + mock.patch("builtins.print") as print_mock, |
| 47 | + self._socket_mode_patch(), |
| 48 | + ): |
| 49 | + ensure_wayland_display() |
| 50 | + self.assertEqual(os.environ.get("WAYLAND_DISPLAY"), "wayland-existing") |
| 51 | + |
| 52 | + print_mock.assert_not_called() |
| 53 | + |
| 54 | + def test_sets_wayland_display_when_one_socket_exists(self): |
| 55 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 56 | + self._socket_file_at(Path(tmpdir) / "wayland-1") |
| 57 | + with ( |
| 58 | + mock.patch.dict(os.environ, {"XDG_RUNTIME_DIR": tmpdir}, clear=True), |
| 59 | + self._socket_mode_patch(), |
| 60 | + ): |
| 61 | + ensure_wayland_display() |
| 62 | + self.assertEqual(os.environ.get("WAYLAND_DISPLAY"), "wayland-1") |
| 63 | + |
| 64 | + def test_chooses_newest_socket_when_multiple_exist(self): |
| 65 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 66 | + runtime_dir = Path(tmpdir) |
| 67 | + self._socket_file_at(runtime_dir / "wayland-0", mtime=100) |
| 68 | + self._socket_file_at(runtime_dir / "wayland-1", mtime=200) |
| 69 | + |
| 70 | + with ( |
| 71 | + mock.patch.dict(os.environ, {"XDG_RUNTIME_DIR": tmpdir}, clear=True), |
| 72 | + self._socket_mode_patch(), |
| 73 | + ): |
| 74 | + ensure_wayland_display() |
| 75 | + self.assertEqual(os.environ.get("WAYLAND_DISPLAY"), "wayland-1") |
| 76 | + |
| 77 | + def test_ignores_non_socket_files_matching_wayland_pattern(self): |
| 78 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 79 | + runtime_dir = Path(tmpdir) |
| 80 | + self._non_socket_file_at(runtime_dir / "wayland-9", mtime=300) |
| 81 | + self._socket_file_at(runtime_dir / "wayland-1", mtime=100) |
| 82 | + |
| 83 | + with ( |
| 84 | + mock.patch.dict(os.environ, {"XDG_RUNTIME_DIR": tmpdir}, clear=True), |
| 85 | + self._socket_mode_patch(), |
| 86 | + ): |
| 87 | + ensure_wayland_display() |
| 88 | + self.assertEqual(os.environ.get("WAYLAND_DISPLAY"), "wayland-1") |
| 89 | + |
| 90 | + def test_does_nothing_when_runtime_dir_has_no_sockets(self): |
| 91 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 92 | + runtime_dir = Path(tmpdir) |
| 93 | + self._non_socket_file_at(runtime_dir / "wayland-0") |
| 94 | + |
| 95 | + with ( |
| 96 | + mock.patch.dict(os.environ, {"XDG_RUNTIME_DIR": tmpdir}, clear=True), |
| 97 | + self._socket_mode_patch(), |
| 98 | + ): |
| 99 | + ensure_wayland_display() |
| 100 | + self.assertIsNone(os.environ.get("WAYLAND_DISPLAY")) |
| 101 | + |
| 102 | + def test_does_not_raise_when_xdg_runtime_dir_unset_or_missing(self): |
| 103 | + with mock.patch.dict(os.environ, {}, clear=True): |
| 104 | + ensure_wayland_display() |
| 105 | + self.assertIsNone(os.environ.get("WAYLAND_DISPLAY")) |
| 106 | + |
| 107 | + with mock.patch.dict(os.environ, {"XDG_RUNTIME_DIR": "/path/that/does/not/exist"}, clear=True): |
| 108 | + ensure_wayland_display() |
| 109 | + self.assertIsNone(os.environ.get("WAYLAND_DISPLAY")) |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + unittest.main() |
0 commit comments