Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion entangled/io/filedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ def managed_files(self) -> set[Path]:
return {Path(p) for p in self.targets}

def changed_files(self, fs: AbstractFileCache) -> Generator[Path]:
# A tracked file that no longer exists (e.g. a source that was moved or
# deleted) counts as changed. Without this guard `fs[Path(p)]` would
# raise `FileNotFoundError` and crash, see issue #88.
return (Path(p) for p, known_stat in self.files.items()
if fs[Path(p)].stat != known_stat)
if Path(p) not in fs or fs[Path(p)].stat != known_stat)

def create_target(self, fs: AbstractFileCache, path: Path):
if path.is_absolute():
Expand Down
21 changes: 21 additions & 0 deletions test/io/test_filedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,24 @@ def test_filedb(example_files: Path):
assert list(db.changed_files(fs)) == [Path("d")]
db.update(fs, Path("d"))
assert list(db.changed_files(fs)) == []


def test_changed_files_missing(tmp_path: Path):
"""Regression test for issue #88: a tracked file that no longer exists on
disk (e.g. a source markdown file that was moved or renamed) should be
reported as changed rather than crashing with `FileNotFoundError`."""
with chdir(tmp_path):
with open("source", "w") as f:
_ = f.write("hello")

fs = FileCache()
with filedb(fs=fs) as db:
db.update(fs, Path("source"))
assert list(db.changed_files(fs)) == []

# the file is moved/removed while still tracked in the db
Path("source").unlink()
fs.reset()

with filedb(fs=fs) as db:
assert list(db.changed_files(fs)) == [Path("source")]
Loading