diff --git a/flopy4/mf6/_sync.py b/flopy4/mf6/_sync.py index 082969b2..47b990fd 100644 --- a/flopy4/mf6/_sync.py +++ b/flopy4/mf6/_sync.py @@ -124,8 +124,12 @@ def _populate_remote_cache(registry, release_id: str, force: bool = False) -> No Re-fetch even if the cache already exists. Should be True for branch refs (mutable) and False for pinned release tags. """ - if not force and registry.cache_path.exists() and any(registry.cache_path.iterdir()): - return + if registry.cache_path.exists() and any(registry.cache_path.iterdir()): + if not force: + return + # The registry's sync skips a non-empty cache, so clear it: otherwise + # a branch ref keeps its first fetch, and files dropped upstream stay. + shutil.rmtree(registry.cache_path) try: registry.sync() except Exception: diff --git a/test/mf6/test_mf6_sync.py b/test/mf6/test_mf6_sync.py index 949be7cb..4ecd1c63 100644 --- a/test/mf6/test_mf6_sync.py +++ b/test/mf6/test_mf6_sync.py @@ -629,3 +629,25 @@ def test_latest_pins_tag(dfn_path, outdir, monkeypatch, fake_install): assert result.source == "MODFLOW-ORG/modflow6@6.8.1" assert result.version == "6.8.1" assert fake_install[0]["version"] == "6.8.1" + + +class _FakeRegistry: + def __init__(self, cache_path: Path): + self.cache_path = cache_path + + def sync(self): + if self.cache_path.exists() and any(self.cache_path.iterdir()): + return + self.cache_path.mkdir(parents=True, exist_ok=True) + (self.cache_path / "new.dfn").write_text("fresh") + + +@pytest.mark.parametrize("force", [False, True]) +def test_populate_remote_cache_force_refetches(tmp_path, force): + """A forced populate replaces a stale cache; an unforced one keeps it.""" + cache = tmp_path / "develop" + cache.mkdir() + (cache / "old.dfn").write_text("stale") + _sync._populate_remote_cache(_FakeRegistry(cache), "MODFLOW-ORG/modflow6@develop", force=force) + names = sorted(p.name for p in cache.iterdir()) + assert names == (["new.dfn"] if force else ["old.dfn"])