Skip to content
Open
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
22 changes: 22 additions & 0 deletions tests/test_exporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,28 @@ def test_empty_database_export(self) -> None:
"[]\n",
)

def test_directory_source_is_rejected_without_writing(self) -> None:
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
source = root / "activity_dir"
source.mkdir()

existing = root / "existing.json"
existing.write_text("keep existing export", encoding="utf-8")
new_destination = root / "new_dir" / "out.json"

for destination in (existing, new_destination):
with self.subTest(destination=destination.name):
with self.assertRaisesRegex(ValueError, "must be a file"):
export_activity(source, destination, "json")
self.assertFalse((root / "new_dir").exists())
self.assertEqual(
list(root.glob("**/.*.tmp")),
[],
)

self.assertEqual(existing.read_text(encoding="utf-8"), "keep existing export")

def test_json_streaming_equivalence(self) -> None:
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
Expand Down
2 changes: 2 additions & 0 deletions timetracker/exporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def export_activity(database_path: str | Path, destination: str | Path, file_for
destination_path = Path(destination)
if _same_file(source_path, destination_path):
raise ValueError("The export destination must be a different file from the activity database")
if source_path.exists() and not source_path.is_file():
raise ValueError("The activity database must be a file")

destination_path.parent.mkdir(parents=True, exist_ok=True)
descriptor, temporary_name = tempfile.mkstemp(
Expand Down