okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
32975a8
main
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 ): 19def setUp (self ): 20scratch = tempfile .TemporaryDirectory () 21self .addCleanup (scratch .cleanup ) 22self .root = Path (scratch .name ).resolve () 23self .addCleanup (os .environ .pop ,"ALBEDO_HOME" ,None ) 24os .environ ["ALBEDO_HOME" ]= str (self .root / "home" ) 25self .thin = self .root / "thin" 26self .thin .mkdir () 27self .profile = self .root / "profile" 28self .profile .mkdir () 29fallbacks = mock .patch .object ( 30albedo_shims , 31"FALLBACKS" , 32 {"nix" : (str (self .root / "missing" ),str (self .profile ))}, 33 ) 34fallbacks .start () 35self .addCleanup (fallbacks .stop ) 36 37def program (self ,directory :Path ,name :str = "nix" )-> None : 38 (directory / name ).write_text ("#!/bin/sh\n" ) 39 (directory / name ).chmod (0o755 ) 40 41def entries (self ,environment :dict [str ,str ]| None )-> list [str ]: 42assert environment is not None 43return environment ["PATH" ].split (os .pathsep ) 44 45def test_a_missing_nix_is_found_in_the_first_directory_that_holds_it (self ): 46self .program (self .profile ) 47environment = albedo_shims .prepend ({"PATH" :str (self .thin )}) 48entries = self .entries (environment ) 49self .assertEqual (entries [1 :], [str (self .thin ),str (self .profile )]) 50self .assertEqual (Path (entries [0 ]).parent .name ,"shims" ) 51 52def test_the_fallback_never_shadows_a_nix_the_user_put_on_path (self ): 53self .program (self .profile ) 54users = self .root / "users" 55users .mkdir () 56self .program (users ) 57entries = self .entries (albedo_shims .prepend ({"PATH" :str (users )})) 58self .assertEqual (entries [1 :], [str (users )]) 59 60def test_nothing_is_added_where_no_directory_holds_nix (self ): 61self .assertEqual ( 62albedo_shims .prepend ({"PATH" :str (self .thin )}), {"PATH" :str (self .thin )} 63 ) 64 65def test_a_directory_without_an_executable_nix_is_not_added (self ): 66 (self .profile / "nix" ).write_text ("not executable" ) 67self .assertEqual ( 68albedo_shims .prepend ({"PATH" :str (self .thin )}), {"PATH" :str (self .thin )} 69 ) 70 71def test_only_the_directory_that_holds_nix_joins_path (self ): 72other = self .root / "other" 73other .mkdir () 74self .program (other ,"unrelated" ) 75self .program (self .profile ) 76with mock .patch .object ( 77albedo_shims ,"FALLBACKS" , {"nix" : (str (other ),str (self .profile ))} 78 ): 79entries = self .entries (albedo_shims .prepend ({"PATH" :str (self .thin )})) 80self .assertNotIn (str (other ),entries ) 81self .assertIn (str (self .profile ),entries ) 82 83 84if __name__ == "__main__" : 85unittest .main ()