okami.mom/strata

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

Niko1221Merge f-0250-tui into f-0243-rel0c53372a0

main
2.8 KiB65 linesraw
1"""setup.find_tool takes the virtualenv's own cmake before a PATH one and skips a cmake older than CMakeLists.txt needs (#3822).
2
3    python -m unittest tools.test_setup_find_tool
4"""
5from __future__ import annotations
6
7import sys
8import tempfile
9import unittest
10from pathlib import Path
11from unittest import mock
12
13ROOT = Path(__file__).resolve().parents[1]
14sys.path.insert(0, str(ROOT))
15import setup  # noqa: E402
16
17
18class FindTool(unittest.TestCase):
19    def setUp(self):
20        self.tmp = tempfile.TemporaryDirectory()
21        self.addCleanup(self.tmp.cleanup)
22        self.bin = Path(self.tmp.name) / "venv" / "bin"
23        self.bin.mkdir(parents=True)
24        self.venv_python = self.bin / "python"
25
26    def versions(self, table):
27        return lambda path, name: table.get(str(path), (False, ()))
28
29    def venv_tool(self, name="cmake"):
30        return str(self.bin / (name + (".exe" if setup.WIN else "")))
31
32    def find(self, table, on_path, name="cmake", venv_copy=True):
33        if venv_copy:
34            Path(self.venv_tool(name)).write_text("x")
35        with mock.patch.object(setup.sys, "executable", str(self.venv_python)), \
36                mock.patch.object(setup.shutil, "which", return_value=on_path), \
37                mock.patch.object(setup, "_tool_version", side_effect=self.versions(table)), \
38                mock.patch.object(setup.Path, "home", return_value=Path(self.tmp.name) / "home"):
39            return setup.find_tool(name), setup.tool_usable(name)
40
41    def test_the_virtualenv_copy_wins_over_an_old_system_cmake(self):
42        got, usable = self.find({"/usr/bin/cmake": (True, (3, 22, 1)), self.venv_tool(): (True, (4, 1, 0))}, "/usr/bin/cmake")
43        self.assertEqual(got, self.venv_tool())
44        self.assertTrue(usable)
45
46    def test_an_old_cmake_on_its_own_is_returned_but_not_usable(self):
47        got, usable = self.find({"/usr/bin/cmake": (True, (3, 22, 1))}, "/usr/bin/cmake", venv_copy=False)
48        self.assertEqual(got, "/usr/bin/cmake")                # the build would say "CMake 3.24 or higher is required"
49        self.assertFalse(usable)                               # so setup installs the pip cmake first
50
51    def test_a_new_enough_system_cmake_is_fine_without_a_venv_one(self):
52        got, usable = self.find({"/usr/bin/cmake": (True, (3, 28, 3))}, "/usr/bin/cmake", venv_copy=False)
53        self.assertEqual((got, usable), ("/usr/bin/cmake", True))
54
55    def test_ninja_has_no_version_floor(self):
56        got, usable = self.find({"/usr/bin/ninja": (True, (1, 10))}, "/usr/bin/ninja", "ninja", venv_copy=False)
57        self.assertTrue(usable)
58
59    def test_a_launcher_that_does_not_run_is_skipped(self):
60        got, _ = self.find({self.venv_tool(): (False, ()), "/usr/bin/cmake": (True, (3, 30, 0))}, "/usr/bin/cmake")
61        self.assertEqual(got, "/usr/bin/cmake")
62
63
64if __name__ == "__main__":
65    unittest.main()