okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b7bcbcde9
main
1"""0.2.13: a data folder on an external drive is allowed (/run/media/$USER, /media/$USER, /mnt), real system folders are still refused, 2and a refusal says "Nothing was downloaded or changed" once.""" 3from __future__import annotations 4 5import contextlib 6import io 7import os 8import tempfile 9import unittest 10from pathlib import Path 11from unittest import mock 12 13import setup 14from strata_control import folders 15 16SAID = "Nothing was downloaded or changed" 17 18 19def posix_rules (): 20"""The POSIX folder rules, whatever system the test runs on.""" 21return mock .patch .object (folders ,"_system_roots" ,lambda :list (folders ._POSIX_SYSTEM )) 22 23 24class MountPoints (unittest .TestCase ): 25def test_the_drives_a_desktop_mounts_are_not_system_folders (self ): 26with posix_rules (),mock .patch .dict (os .environ , {"HOME" :"/home/ann" ,"USERPROFILE" :"/home/ann" }): 27for p in ("/run/media/ann/Backup/Strata" ,"/run/media/ann/My Drive/Strata-models" ,"/run/media/ann/Backup" , 28"/media/ann/Backup/Strata" ,"/media/ann/Backup" ,"/mnt/models/Strata" ,"/mnt/data" ): 29with self .subTest (p = p ): 30self .assertFalse (folders ._is_system (p ),p ) 31 32def test_real_system_folders_are_still_refused (self ): 33with posix_rules (),mock .patch .dict (os .environ , {"HOME" :"/home/ann" ,"USERPROFILE" :"/home/ann" }): 34for p in ("/usr/share/strata" ,"/etc/strata" ,"/bin" ,"/boot/x" ,"/dev/shm/x" ,"/proc/1" ,"/sys/x" ,"/root/x" ,"/var/lib/strata" , 35"/lib/x" ,"/sbin" ,"/run" ,"/run/user/1000/strata" ,"/run/systemd/x" ,"/run/lock" ,"/run/media" ,"/run/mediax/Strata" ): 36with self .subTest (p = p ): 37self .assertTrue (folders ._is_system (p ),p ) 38 39def test_the_run_media_folder_of_the_mounts_is_not_a_place_to_save (self ): 40with tempfile .TemporaryDirectory ()as d : 41run = Path (d )/ "run" 42 (run / "media" / "ann" / "Backup" ).mkdir (parents = True ) 43with mock .patch .object (folders ,"_system_roots" ,lambda : [str (run )]),mock .patch .object (folders ,"_POSIX_MOUNTS" , (str (run / "media" ),)), \ 44mock .patch .dict (os .environ , {"HOME" :str (Path (d )/ "home" ),"USERPROFILE" :str (Path (d )/ "home" )}): 45self .assertIsNone (folders .problem (str (run / "media" / "ann" / "Backup" / "Strata" )))# created on the drive: fine 46self .assertIn ("system folder" ,folders .problem (str (run / "media" ))[0 ]) 47self .assertIn ("system folder" ,folders .problem (str (run / "elsewhere" ))[0 ]) 48self .assertIn ("system folder" ,folders .problem (str (run / "user" / "1000" ))[0 ]) 49 50def test_a_mount_the_user_cannot_write_to_says_so_and_is_not_called_a_system_folder (self ): 51with tempfile .TemporaryDirectory ()as d : 52run = Path (d )/ "run" 53 (run / "media" / "bob" / "Drive" ).mkdir (parents = True )# somebody else's drive 54with mock .patch .object (folders ,"_system_roots" ,lambda : [str (run )]),mock .patch .object (folders ,"_POSIX_MOUNTS" , (str (run / "media" ),)), \ 55mock .patch .object (folders ,"_can_write" ,return_value = False ), \ 56mock .patch .dict (os .environ , {"HOME" :str (Path (d )/ "home" ),"USERPROFILE" :str (Path (d )/ "home" )}): 57what ,_ = folders .problem (str (run / "media" / "bob" / "Drive" / "Strata" )) 58self .assertIn ("cannot be created" ,what ) 59self .assertNotIn ("system folder" ,what ) 60 61def test_a_writable_folder_outside_home_is_fine (self ):# /mnt/... and the like, "outside /home" 62with tempfile .TemporaryDirectory ()as d : 63with posix_rules (),mock .patch .dict (os .environ , {"HOME" :"/home/nobody" ,"USERPROFILE" :"/home/nobody" }): 64self .assertIsNone (folders .problem (str (Path (d )/ "Strata" ))) 65 66 67class OneSentence (unittest .TestCase ): 68def test_the_closing_sentence_is_dropped_only_when_it_is_there (self ): 69both = ("/etc/x is a system folder." ,folders .NEXT_POSIX ) 70self .assertEqual (folders .without_nothing_changed (both ).count (SAID ),0 ) 71self .assertTrue (folders .without_nothing_changed (both ).endswith ("for example ~/strata-models." )) 72self .assertEqual (folders .without_nothing_changed (("It is bad." ,"Do that." )),"It is bad. Do that." ) 73 74def test_a_refused_data_dir_says_nothing_changed_once (self ): 75with tempfile .TemporaryDirectory ()as d : 76f = Path (d )/ "afile.txt" 77f .write_text ("x" ) 78for target in (f ,Path (d )/ "afile.txt" / "inside" ): 79out = io .StringIO () 80with self .subTest (target = target ),contextlib .redirect_stdout (out ),self .assertRaises (SystemExit ): 81setup .data_folder (str (target )) 82self .assertEqual (out .getvalue ().count (SAID ),1 ,out .getvalue ()) 83self .assertIn ("was not saved as your data folder" ,out .getvalue ()) 84 85@ unittest . skipIf ( os . name == "nt" , "POSIX paths" ) 86def test_a_system_folder_refusal_says_nothing_changed_once (self ): 87out = io .StringIO () 88with posix_rules (),mock .patch .dict (os .environ , {"HOME" :"/home/nobody" ,"USERPROFILE" :"/home/nobody" }), \ 89mock .patch .object (setup ,"load_settings" ,return_value = {}),contextlib .redirect_stdout (out ),self .assertRaises (SystemExit ): 90setup .data_folder ("/usr/share/Strata" ) 91self .assertEqual (out .getvalue ().count (SAID ),1 ,out .getvalue ()) 92self .assertIn ("is a system folder" ,out .getvalue ()) 93 94 95if __name__ == "__main__" : 96unittest .main ()