okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

nekomimi.petshims: find nix for jobs when the daemon started with a thin PATH32975a8

main
3.1 KiB85 linesraw
1"""Where the job PATH finds nix when the daemon started with a thin one: a GUI
2or launchd start never ran the user's profile, so nix was missing from every
3job and its shim was never put in front of it."""
4
5from __future__ import annotations
6
7import os
8from pathlib import Path
9import sys
10import tempfile
11import unittest
12from unittest import mock
13
14sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "priv" / "python"))
15import albedo_shims  # noqa: E402
16
17
18class FallbackPathTest(unittest.TestCase):
19    def setUp(self):
20        scratch = tempfile.TemporaryDirectory()
21        self.addCleanup(scratch.cleanup)
22        self.root = Path(scratch.name).resolve()
23        self.addCleanup(os.environ.pop, "ALBEDO_HOME", None)
24        os.environ["ALBEDO_HOME"] = str(self.root / "home")
25        self.thin = self.root / "thin"
26        self.thin.mkdir()
27        self.profile = self.root / "profile"
28        self.profile.mkdir()
29        fallbacks = mock.patch.object(
30            albedo_shims,
31            "FALLBACKS",
32            {"nix": (str(self.root / "missing"), str(self.profile))},
33        )
34        fallbacks.start()
35        self.addCleanup(fallbacks.stop)
36
37    def program(self, directory: Path, name: str = "nix") -> None:
38        (directory / name).write_text("#!/bin/sh\n")
39        (directory / name).chmod(0o755)
40
41    def entries(self, environment: dict[str, str] | None) -> list[str]:
42        assert environment is not None
43        return environment["PATH"].split(os.pathsep)
44
45    def test_a_missing_nix_is_found_in_the_first_directory_that_holds_it(self):
46        self.program(self.profile)
47        environment = albedo_shims.prepend({"PATH": str(self.thin)})
48        entries = self.entries(environment)
49        self.assertEqual(entries[1:], [str(self.thin), str(self.profile)])
50        self.assertEqual(Path(entries[0]).parent.name, "shims")
51
52    def test_the_fallback_never_shadows_a_nix_the_user_put_on_path(self):
53        self.program(self.profile)
54        users = self.root / "users"
55        users.mkdir()
56        self.program(users)
57        entries = self.entries(albedo_shims.prepend({"PATH": str(users)}))
58        self.assertEqual(entries[1:], [str(users)])
59
60    def test_nothing_is_added_where_no_directory_holds_nix(self):
61        self.assertEqual(
62            albedo_shims.prepend({"PATH": str(self.thin)}), {"PATH": str(self.thin)}
63        )
64
65    def test_a_directory_without_an_executable_nix_is_not_added(self):
66        (self.profile / "nix").write_text("not executable")
67        self.assertEqual(
68            albedo_shims.prepend({"PATH": str(self.thin)}), {"PATH": str(self.thin)}
69        )
70
71    def test_only_the_directory_that_holds_nix_joins_path(self):
72        other = self.root / "other"
73        other.mkdir()
74        self.program(other, "unrelated")
75        self.program(self.profile)
76        with mock.patch.object(
77            albedo_shims, "FALLBACKS", {"nix": (str(other), str(self.profile))}
78        ):
79            entries = self.entries(albedo_shims.prepend({"PATH": str(self.thin)}))
80        self.assertNotIn(str(other), entries)
81        self.assertIn(str(self.profile), entries)
82
83
84if __name__ == "__main__":
85    unittest.main()