|
| 1 | +# ******************************************************************************* |
| 2 | +# Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 3 | +# |
| 4 | +# See the NOTICE file(s) distributed with this work for additional |
| 5 | +# information regarding copyright ownership. |
| 6 | +# |
| 7 | +# This program and the accompanying materials are made available under the |
| 8 | +# terms of the Apache License Version 2.0 which is available at |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# SPDX-License-Identifier: Apache-2.0 |
| 12 | +# ******************************************************************************* |
| 13 | + |
| 14 | +# Unit Tests of incremental.py |
| 15 | + |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +from incremental import clean_builddir_if_stale, update_module_hash |
| 19 | + |
| 20 | +_BUILD = Path("/build") |
| 21 | +_MODULE = Path("/MODULE.bazel") |
| 22 | +_LOCK = Path("/MODULE.bazel.lock") |
| 23 | + |
| 24 | + |
| 25 | +def _simulate_old_state(fs, warnings: str | None) -> None: |
| 26 | + """Helper function to set up a build directory with an old hash and warnings.""" |
| 27 | + |
| 28 | + fs.create_dir(_BUILD) |
| 29 | + fs.create_file(_MODULE, contents="stable") |
| 30 | + fs.create_file(_LOCK, contents="old lock") |
| 31 | + update_module_hash(_BUILD, [_MODULE, _LOCK]) |
| 32 | + if warnings is not None: |
| 33 | + fs.create_file(_BUILD / "warnings.txt", contents=warnings) |
| 34 | + |
| 35 | + |
| 36 | +def test_clean_removes_build_dir_when_previous_build_had_warnings(fs) -> None: |
| 37 | + """If warnings.txt exists and is not empty, the build dir is removed.""" |
| 38 | + |
| 39 | + _simulate_old_state(fs, warnings="WARNING: something went wrong") |
| 40 | + |
| 41 | + clean_builddir_if_stale(_BUILD, [_MODULE]) |
| 42 | + |
| 43 | + assert not _BUILD.exists() |
| 44 | + |
| 45 | + |
| 46 | +def test_clean_keeps_build_dir_when_warnings_txt_is_empty(fs) -> None: |
| 47 | + """If warnings.txt exists and is empty, the build dir is kept.""" |
| 48 | + |
| 49 | + _simulate_old_state(fs, warnings="") |
| 50 | + |
| 51 | + clean_builddir_if_stale(_BUILD, [_MODULE, _LOCK]) |
| 52 | + |
| 53 | + assert _BUILD.exists() |
| 54 | + |
| 55 | + |
| 56 | +def test_clean_is_noop_when_warnings_txt_is_absent(fs) -> None: |
| 57 | + """If warnings.txt does not exist, the build dir is kept (no error).""" |
| 58 | + |
| 59 | + _simulate_old_state(fs, warnings=None) |
| 60 | + |
| 61 | + clean_builddir_if_stale(_BUILD, [_MODULE, _LOCK]) |
| 62 | + |
| 63 | + assert _BUILD.exists() |
| 64 | + |
| 65 | + |
| 66 | +def test_clean_is_noop_when_build_dir_is_absent(fs) -> None: |
| 67 | + fs.create_file(_MODULE, contents="stable") |
| 68 | + |
| 69 | + clean_builddir_if_stale(_BUILD, [_MODULE]) |
| 70 | + |
| 71 | + |
| 72 | +def test_module_changed_removes_build_dir_when_one_sentinel_file_changed(fs) -> None: |
| 73 | + _simulate_old_state(fs, warnings=None) |
| 74 | + |
| 75 | + _LOCK.write_bytes(b"new lock") |
| 76 | + clean_builddir_if_stale(_BUILD, [_MODULE, _LOCK]) |
| 77 | + |
| 78 | + assert not _BUILD.exists() |
| 79 | + |
| 80 | + |
| 81 | +def test_module_changed_keeps_build_dir_when_all_sentinel_files_unchanged(fs) -> None: |
| 82 | + _simulate_old_state(fs, warnings=None) |
| 83 | + |
| 84 | + clean_builddir_if_stale(_BUILD, [_MODULE, _LOCK]) |
| 85 | + |
| 86 | + assert _BUILD.exists() |
| 87 | + |
| 88 | + |
| 89 | +def test_module_change_after_successful_build_forces_clean(fs) -> None: |
| 90 | + _simulate_old_state(fs, warnings=None) |
| 91 | + |
| 92 | + _MODULE.write_bytes(b"version 2") |
| 93 | + clean_builddir_if_stale(_BUILD, [_MODULE]) |
| 94 | + |
| 95 | + assert not _BUILD.exists() |
| 96 | + |
| 97 | + |
| 98 | +def test_missing_hash_file_triggers_clean(fs) -> None: |
| 99 | + """If _build/ exists but hash file is absent, treat as stale (e.g. upgrade from old version).""" |
| 100 | + fs.create_dir(_BUILD) |
| 101 | + fs.create_file(_MODULE, contents="stable") |
| 102 | + # No hash file written |
| 103 | + |
| 104 | + clean_builddir_if_stale(_BUILD, [_MODULE]) |
| 105 | + |
| 106 | + assert not _BUILD.exists() |
0 commit comments