|
| 1 | +"""Unit tests for west_env.buildcheck — stale build directory detection.""" |
| 2 | + |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import sys |
| 6 | +import tempfile |
| 7 | +import unittest |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 11 | +if str(REPO_ROOT) not in sys.path: |
| 12 | + sys.path.insert(0, str(REPO_ROOT)) |
| 13 | + |
| 14 | +from west_env.buildcheck import ( |
| 15 | + CONTAINER_WORK_PREFIX, |
| 16 | + _read_cmake_source_dir, |
| 17 | + clean_build_dir, |
| 18 | + detect_stale_build, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def _write_cmake_cache(build_dir: Path, source_dir: str) -> None: |
| 23 | + """Write a minimal CMakeCache.txt with the given source directory.""" |
| 24 | + cache = build_dir / "CMakeCache.txt" |
| 25 | + cache.write_text( |
| 26 | + f"# CMake generated cache\nCMAKE_SOURCE_DIR:STATIC={source_dir}\nCMAKE_BUILD_TYPE:STRING=\n", |
| 27 | + encoding="utf-8", |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +class TestReadCmakeSourceDir(unittest.TestCase): |
| 32 | + def test_returns_none_when_no_build_dir(self): |
| 33 | + with tempfile.TemporaryDirectory() as tmp: |
| 34 | + build = Path(tmp) / "build" |
| 35 | + self.assertIsNone(_read_cmake_source_dir(build)) |
| 36 | + |
| 37 | + def test_returns_none_when_no_cmake_cache(self): |
| 38 | + with tempfile.TemporaryDirectory() as tmp: |
| 39 | + build = Path(tmp) / "build" |
| 40 | + build.mkdir() |
| 41 | + self.assertIsNone(_read_cmake_source_dir(build)) |
| 42 | + |
| 43 | + def test_reads_cmake_source_dir_from_cache(self): |
| 44 | + with tempfile.TemporaryDirectory() as tmp: |
| 45 | + build = Path(tmp) / "build" |
| 46 | + build.mkdir() |
| 47 | + _write_cmake_cache(build, "/home/user/workspace") |
| 48 | + result = _read_cmake_source_dir(build) |
| 49 | + self.assertEqual(result, "/home/user/workspace") |
| 50 | + |
| 51 | + def test_reads_container_path(self): |
| 52 | + with tempfile.TemporaryDirectory() as tmp: |
| 53 | + build = Path(tmp) / "build" |
| 54 | + build.mkdir() |
| 55 | + _write_cmake_cache(build, "/work") |
| 56 | + result = _read_cmake_source_dir(build) |
| 57 | + self.assertEqual(result, "/work") |
| 58 | + |
| 59 | + def test_reads_subdirectory_container_path(self): |
| 60 | + with tempfile.TemporaryDirectory() as tmp: |
| 61 | + build = Path(tmp) / "build" |
| 62 | + build.mkdir() |
| 63 | + _write_cmake_cache(build, "/work/modules/my-app") |
| 64 | + result = _read_cmake_source_dir(build) |
| 65 | + self.assertEqual(result, "/work/modules/my-app") |
| 66 | + |
| 67 | + def test_returns_none_for_empty_cache(self): |
| 68 | + with tempfile.TemporaryDirectory() as tmp: |
| 69 | + build = Path(tmp) / "build" |
| 70 | + build.mkdir() |
| 71 | + (build / "CMakeCache.txt").write_text("# empty cache\n", encoding="utf-8") |
| 72 | + self.assertIsNone(_read_cmake_source_dir(build)) |
| 73 | + |
| 74 | + |
| 75 | +class TestDetectStaleBuild(unittest.TestCase): |
| 76 | + """Tests for the main stale-build detection logic.""" |
| 77 | + |
| 78 | + def test_no_warning_when_build_dir_does_not_exist(self): |
| 79 | + with tempfile.TemporaryDirectory() as tmp: |
| 80 | + build = Path(tmp) / "build" |
| 81 | + # Native mode, no build dir yet |
| 82 | + self.assertIsNone(detect_stale_build(build, use_container=False)) |
| 83 | + # Container mode, no build dir yet |
| 84 | + self.assertIsNone(detect_stale_build(build, use_container=True)) |
| 85 | + |
| 86 | + def test_no_warning_when_build_has_no_cmake_cache(self): |
| 87 | + with tempfile.TemporaryDirectory() as tmp: |
| 88 | + build = Path(tmp) / "build" |
| 89 | + build.mkdir() |
| 90 | + self.assertIsNone(detect_stale_build(build, use_container=False)) |
| 91 | + self.assertIsNone(detect_stale_build(build, use_container=True)) |
| 92 | + |
| 93 | + def test_no_warning_native_build_used_natively(self): |
| 94 | + """Same mode: native build used natively — no stale.""" |
| 95 | + with tempfile.TemporaryDirectory() as tmp: |
| 96 | + build = Path(tmp) / "build" |
| 97 | + build.mkdir() |
| 98 | + _write_cmake_cache(build, "/home/user/workspace") |
| 99 | + self.assertIsNone(detect_stale_build(build, use_container=False)) |
| 100 | + |
| 101 | + def test_no_warning_container_build_used_in_container(self): |
| 102 | + """Same mode: container build used in container — no stale.""" |
| 103 | + with tempfile.TemporaryDirectory() as tmp: |
| 104 | + build = Path(tmp) / "build" |
| 105 | + build.mkdir() |
| 106 | + _write_cmake_cache(build, "/work") |
| 107 | + self.assertIsNone(detect_stale_build(build, use_container=True)) |
| 108 | + |
| 109 | + def test_warning_native_to_container(self): |
| 110 | + """Stale: build was native, now switching to container.""" |
| 111 | + with tempfile.TemporaryDirectory() as tmp: |
| 112 | + build = Path(tmp) / "build" |
| 113 | + build.mkdir() |
| 114 | + _write_cmake_cache(build, "/home/user/workspace") |
| 115 | + msg = detect_stale_build(build, use_container=True) |
| 116 | + self.assertIsNotNone(msg) |
| 117 | + self.assertIn("native mode", msg) |
| 118 | + self.assertIn("container mode", msg) |
| 119 | + self.assertIn("--clean", msg) |
| 120 | + self.assertIn("/home/user/workspace", msg) |
| 121 | + |
| 122 | + def test_warning_container_to_native(self): |
| 123 | + """Stale: build was container, now switching to native.""" |
| 124 | + with tempfile.TemporaryDirectory() as tmp: |
| 125 | + build = Path(tmp) / "build" |
| 126 | + build.mkdir() |
| 127 | + _write_cmake_cache(build, "/work") |
| 128 | + msg = detect_stale_build(build, use_container=False) |
| 129 | + self.assertIsNotNone(msg) |
| 130 | + self.assertIn("container mode", msg) |
| 131 | + self.assertIn("native mode", msg) |
| 132 | + self.assertIn("--clean", msg) |
| 133 | + self.assertIn("/work", msg) |
| 134 | + |
| 135 | + def test_warning_contains_warn_prefix(self): |
| 136 | + with tempfile.TemporaryDirectory() as tmp: |
| 137 | + build = Path(tmp) / "build" |
| 138 | + build.mkdir() |
| 139 | + _write_cmake_cache(build, "/work/app") |
| 140 | + msg = detect_stale_build(build, use_container=False) |
| 141 | + self.assertTrue(msg.startswith("[WARN]")) |
| 142 | + |
| 143 | + def test_container_subdir_path_detected_as_container_build(self): |
| 144 | + """A source path of /work/some/subdir should count as container build.""" |
| 145 | + with tempfile.TemporaryDirectory() as tmp: |
| 146 | + build = Path(tmp) / "build" |
| 147 | + build.mkdir() |
| 148 | + _write_cmake_cache(build, "/work/modules/zephyr/samples/hello_world") |
| 149 | + # Used natively — should warn |
| 150 | + msg = detect_stale_build(build, use_container=False) |
| 151 | + self.assertIsNotNone(msg) |
| 152 | + # Used in container — should not warn |
| 153 | + self.assertIsNone(detect_stale_build(build, use_container=True)) |
| 154 | + |
| 155 | + def test_windows_host_path_detected_as_native(self): |
| 156 | + """A Windows-style host path should count as native build.""" |
| 157 | + with tempfile.TemporaryDirectory() as tmp: |
| 158 | + build = Path(tmp) / "build" |
| 159 | + build.mkdir() |
| 160 | + _write_cmake_cache(build, "C:/Users/dev/workspace") |
| 161 | + # Used in container — should warn |
| 162 | + msg = detect_stale_build(build, use_container=True) |
| 163 | + self.assertIsNotNone(msg) |
| 164 | + # Used natively — should not warn |
| 165 | + self.assertIsNone(detect_stale_build(build, use_container=False)) |
| 166 | + |
| 167 | + |
| 168 | +class TestCleanBuildDir(unittest.TestCase): |
| 169 | + def test_removes_build_directory(self): |
| 170 | + with tempfile.TemporaryDirectory() as tmp: |
| 171 | + build = Path(tmp) / "build" |
| 172 | + build.mkdir() |
| 173 | + (build / "CMakeCache.txt").write_text("cache", encoding="utf-8") |
| 174 | + self.assertTrue(build.exists()) |
| 175 | + clean_build_dir(build) |
| 176 | + self.assertFalse(build.exists()) |
| 177 | + |
| 178 | + def test_no_error_when_build_dir_does_not_exist(self): |
| 179 | + with tempfile.TemporaryDirectory() as tmp: |
| 180 | + build = Path(tmp) / "nonexistent" |
| 181 | + # Should not raise |
| 182 | + clean_build_dir(build) |
| 183 | + |
| 184 | + def test_removes_nested_contents(self): |
| 185 | + with tempfile.TemporaryDirectory() as tmp: |
| 186 | + build = Path(tmp) / "build" |
| 187 | + nested = build / "zephyr" / "CMakeFiles" |
| 188 | + nested.mkdir(parents=True) |
| 189 | + (nested / "somefile.o").write_text("obj", encoding="utf-8") |
| 190 | + clean_build_dir(build) |
| 191 | + self.assertFalse(build.exists()) |
| 192 | + |
| 193 | + |
| 194 | +class TestContainerWorkPrefix(unittest.TestCase): |
| 195 | + def test_prefix_is_slash_work(self): |
| 196 | + self.assertEqual(CONTAINER_WORK_PREFIX, "/work") |
| 197 | + |
| 198 | + |
| 199 | +if __name__ == "__main__": |
| 200 | + unittest.main() |
0 commit comments