Skip to content

Commit

Permalink
fix: don't remove obsolete files when synth raises exception (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
SurferJeffAtGoogle authored Aug 6, 2020
1 parent ee60c1c commit 5f2f711
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
27 changes: 15 additions & 12 deletions synthtool/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,22 @@ def __enter__(self):
self.observer.start()

def __exit__(self, type, value, traceback):
if should_track_obsolete_files():
time.sleep(2) # Finish collecting observations about modified files.
self.observer.stop()
self.observer.join()
for path in git_ignore(self.handler.get_touched_file_paths()):
_metadata.generated_files.append(path)
_remove_obsolete_files(self.old_metadata)
if value:
pass # An exception was raised. Don't write metadata or clean up.
else:
self.observer.stop()
_clear_local_paths(get())
_metadata.sources.sort(key=_source_key)
if _enable_write_metadata:
write(self.metadata_file_path)
if should_track_obsolete_files():
time.sleep(2) # Finish collecting observations about modified files.
self.observer.stop()
self.observer.join()
for path in git_ignore(self.handler.get_touched_file_paths()):
_metadata.generated_files.append(path)
_remove_obsolete_files(self.old_metadata)
else:
self.observer.stop()
_clear_local_paths(get())
_metadata.sources.sort(key=_source_key)
if _enable_write_metadata:
write(self.metadata_file_path)


def _get_git_source_map(metadata) -> Dict[str, object]:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,28 @@ def test_nothing_happens_when_disabled(source_tree, preserve_track_obsolete_file
assert os.path.exists("code/c")


def test_nothing_happens_when_exception(source_tree, preserve_track_obsolete_file_flag):
metadata.set_track_obsolete_files(True)

with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"):
source_tree.write("code/b")
source_tree.write("code/c")

metadata.reset()
try:
with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"):
source_tree.write("code/c")
raise "Exception!"
except: # noqa: E722
pass

assert 0 == len(metadata.get().new_files)

# Confirm no files were deleted.
assert os.path.exists("code/b")
assert os.path.exists("code/c")


def test_old_file_ignored_by_git_not_removed(
source_tree, preserve_track_obsolete_file_flag
):
Expand Down

0 comments on commit 5f2f711

Please sign in to comment.