okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
66331f9bc
main
1"""#4463: `strata update` on a source install. 2 3Gap 1: with no folder named, it used the per-user component folder even when the checkout has its own engine store (`engine-feed`, which setup.py 4fills); the download, its half-written state and its part file then sat in a folder the checkout never read. 5Gap 2: the busy check did not see an engine the desktop app's source install had started (it runs serve/server.py directly, which took no lease), so 6`strata update` test-started a second copy of the model beside it. 7 8python -m pytest tests/terminal/test_update_source_install.py 9""" 10import os 11import tempfile 12import unittest 13from pathlib import Path 14from unittest import mock 15 16from serve import server 17from strata_cli import update_cmd 18from strata_control import engine_component as ec 19from strata_control import updater 20 21 22class DefaultFolder (unittest .TestCase ): 23def setUp (self ): 24self .temp = tempfile .TemporaryDirectory (prefix = "strata-updsrc-" ) 25self .addCleanup (self .temp .cleanup ) 26self .root = Path (self .temp .name ) 27self .user = self .root / "user-components" 28self .store = self .root / "checkout" / "engine-feed" 29for folder in (self .user ,self .store ): 30folder .mkdir (parents = True ) 31env = mock .patch .dict (os .environ , {},clear = False ) 32env .start () 33self .addCleanup (env .stop ) 34os .environ .pop (ec .ENV_DIR ,None ) 35self .patches = [mock .patch .object (ec ,"components_dir" ,lambda * a ,** k :self .user )] 36for patch in self .patches : 37patch .start () 38self .addCleanup (patch .stop ) 39 40def _scan (self ,user_has ,store_has ): 41def scan (root = None ): 42has = user_has if Path (root )== self .user else store_has if Path (root )== self .store else False 43return ([object ()]if has else []), [] 44return mock .patch .object (ec ,"scan" ,scan ) 45 46def test_a_checkout_with_its_own_store_updates_it_when_the_user_folder_has_no_engine (self ): 47with self ._scan (False ,True ),mock .patch .object (update_cmd ,"source_store" ,return_value = self .store ): 48self .assertEqual (update_cmd .default_components_dir (ec ),self .store ) 49 50def test_a_user_folder_that_holds_an_engine_is_kept (self ): 51with self ._scan (True ,True ),mock .patch .object (update_cmd ,"source_store" ,return_value = self .store ): 52self .assertEqual (update_cmd .default_components_dir (ec ),self .user ) 53 54def test_no_store_in_the_checkout_changes_nothing (self ): 55with self ._scan (False ,False ),mock .patch .object (update_cmd ,"source_store" ,return_value = None ): 56self .assertEqual (update_cmd .default_components_dir (ec ),self .user ) 57 58def test_an_explicit_folder_in_the_environment_always_wins (self ): 59with self ._scan (False ,True ),mock .patch .object (update_cmd ,"source_store" ,return_value = self .store ), \ 60mock .patch .dict (os .environ , {ec .ENV_DIR :str (self .root / "named" )}): 61self .assertEqual (update_cmd .default_components_dir (ec ),self .user )# (components_dir() itself returns the named folder) 62 63def test_a_store_without_an_engine_package_is_not_one (self ): 64with mock .patch .object (ec ,"scan" ,return_value = ([], [])): 65self .assertIsNone (update_cmd .source_store (self .root / "checkout" )) 66 67 68class AppLaunchedEngine (unittest .TestCase ): 69def setUp (self ): 70self .temp = tempfile .TemporaryDirectory (prefix = "strata-updsrc-rt-" ) 71self .addCleanup (self .temp .cleanup ) 72env = mock .patch .dict (os .environ , {"STRATA_RUNTIME_DIR" :str (Path (self .temp .name )/ "runtime" )}) 73env .start () 74self .addCleanup (env .stop ) 75 76def test_a_server_started_by_running_serve_server_is_seen_by_the_busy_check (self ): 77seen = {} 78 79def serving (* args ,** kwargs ): 80seen ["during" ]= updater .serving_now () 81return 0 82self .assertEqual (updater .serving_now (), []) 83with mock .patch .object (server ,"_main" ,serving ): 84self .assertEqual (server .main ([]),0 ) 85self .assertTrue (seen ["during" ],"the update's busy check did not see the running server" ) 86self .assertEqual (updater .serving_now (), [],"the lease is released when the server ends" ) 87 88def test_serving_does_not_depend_on_the_lease (self ): 89from strata_control import runtime 90with mock .patch .object (runtime ,"acquire_activity_lease" ,side_effect = runtime .ProfileError ("a managed runtime or calibration owns the local engine lease" ,6 )), \ 91mock .patch .object (server ,"_main" ,return_value = 7 ): 92self .assertEqual (server .main ([]),7 ) 93 94 95if __name__ == "__main__" : 96unittest .main ()