okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
36beee903
main
1"""Setup prompts require input or --yes; EOF must not authorize system installs (#393). 2No GPU, downloads or installers are used. 3 4python -m unittest tools.test_setup_prompts 5""" 6from __future__import annotations 7 8import contextlib 9import io 10import sys 11import unittest 12from pathlib import Path 13from unittest import mock 14 15ROOT = Path (__file__ ).resolve ().parents [1 ] 16sys .path .insert (0 ,str (ROOT )) 17import setup # noqa: E402 18 19 20class Prompts (unittest .TestCase ): 21def test_eof_stops_with_guidance (self ): 22out = io .StringIO () 23with mock .patch ("sys.stdin" ,io .StringIO ()),contextlib .redirect_stdout (out ): 24with self .assertRaises (SystemExit )as cm : 25setup .ask ("Install them now?" , ["y" ,"n" ],"y" ,False ) 26self .assertEqual (cm .exception .code ,1 ) 27self .assertIn ("Install them now?" ,out .getvalue ()) 28self .assertIn ("--yes" ,out .getvalue ()) 29self .assertIn ("Setup stopped" ,out .getvalue ()) 30 31def test_yes_accepts_the_default_without_reading_input (self ): 32with mock .patch ("builtins.input" ,side_effect = AssertionError ("unexpected prompt" )): 33for default in ("y" ,"n" ): 34with self .subTest (default = default ): 35self .assertEqual (setup .ask ("Install?" , ["y" ,"n" ],default ,True ),default ) 36 37def test_enter_and_explicit_answers (self ): 38for answer ,expected in (("\n" ,"y" ), (" \n" ,"y" ), ("y\n" ,"y" ), (" N \n" ,"n" )): 39with self .subTest (answer = answer ),mock .patch ("sys.stdin" ,io .StringIO (answer )), \ 40contextlib .redirect_stdout (io .StringIO ()): 41self .assertEqual (setup .ask ("Install?" , ["y" ,"n" ],"y" ,False ),expected ) 42 43def test_invalid_answer_reprompts_and_preserves_choice_case (self ): 44out = io .StringIO () 45with mock .patch ("sys.stdin" ,io .StringIO ("invalid\nyArN\n" )),contextlib .redirect_stdout (out ): 46self .assertEqual (setup .ask ("Method?" , ["Yarn" ,"Linear" ],"Linear" ,False ),"Yarn" ) 47self .assertIn ("please answer one of: Yarn, Linear" ,out .getvalue ()) 48 49def test_eof_after_invalid_answer_stops (self ): 50with mock .patch ("sys.stdin" ,io .StringIO ("invalid\n" )),contextlib .redirect_stdout (io .StringIO ()): 51with self .assertRaises (SystemExit )as cm : 52setup .ask ("Install?" , ["y" ,"n" ],"y" ,False ) 53self .assertEqual (cm .exception .code ,1 ) 54 55 56class BuildTools (unittest .TestCase ): 57def test_eof_never_starts_an_installer (self ): 58for win in (True ,False ): 59with self .subTest (win = win ),mock .patch .object (setup ,"WIN" ,win ), \ 60mock .patch .object (setup ,"find_nvcc" ,return_value = (None , (0 ,0 ))), \ 61mock .patch .object (setup ,"find_vcvars" ,return_value = None ), \ 62mock .patch .object (setup .shutil ,"which" ,side_effect = lambda name :None if name == "g++" else name ), \ 63mock .patch .object (setup ,"run" ,side_effect = AssertionError ("installer started" ))as run , \ 64mock .patch .object (setup ,"download" ,side_effect = AssertionError ("download started" ))as download , \ 65mock .patch ("sys.stdin" ,io .StringIO ()),contextlib .redirect_stdout (io .StringIO ()): 66with self .assertRaises (SystemExit )as cm : 67setup .install_build_tools ({"arch" :"120" },False ) 68self .assertEqual (cm .exception .code ,1 ) 69run .assert_not_called () 70download .assert_not_called () 71 72def test_yes_still_installs_missing_windows_tools (self ): 73nvcc ,vcvars = Path ("nvcc" ),Path ("vcvars64.bat" ) 74with mock .patch .object (setup ,"WIN" ,True ), \ 75mock .patch .object (setup ,"find_nvcc" ,side_effect = [(None , (0 ,0 )), (nvcc , (13 ,0 ))]), \ 76mock .patch .object (setup ,"find_vcvars" ,side_effect = [None ,vcvars ,vcvars ,vcvars ]), \ 77mock .patch .object (setup .shutil ,"which" ,return_value = "winget" ), \ 78mock .patch .object (setup ,"run" )as run , \ 79mock .patch ("builtins.input" ,side_effect = AssertionError ("unexpected prompt" )), \ 80contextlib .redirect_stdout (io .StringIO ()): 81self .assertEqual (setup .install_build_tools ({"arch" :"120" },True ), (nvcc ,vcvars )) 82commands = [call .args [0 ]for call in run .call_args_list ] 83self .assertEqual (len (commands ),2 ) 84self .assertIn ("Microsoft.VisualStudio.2022.BuildTools" ,commands [0 ]) 85self .assertIn ("Nvidia.CUDA" ,commands [1 ]) 86 87 88if __name__ == "__main__" : 89unittest .main ()