okami.mom/strata

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

Bortlesboatsetup: stop on EOF instead of accepting prompt defaults36beee903

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