fix: prevent overwrite when document has multiple files
This commit is contained in:
1 parent
5e88eaea97
commit
cf391172a1
2 files changed
+79
-4
No files matched your search
@@ -19,3 +19,41 @@ func TestConfigFilePathUsesXDGConfigDir(t *testing.T) {
|
||||
t.Fatalf("expected %q, got %q", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArchiveDestinationIndexesMultipleAttachmentsAndPreservesDistinctFiles(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
sourcePath := filepath.Join(t.TempDir(), "source.pdf")
|
||||
if err := os.WriteFile(sourcePath, []byte("source"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got, alreadyArchived, err := archiveDestination(directory, "Document", "pdf", 2, sourcePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if alreadyArchived || got != filepath.Join(directory, "Document.1.pdf") {
|
||||
t.Fatalf("expected first indexed destination, got %q (already archived: %t)", got, alreadyArchived)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(got, []byte("different"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, alreadyArchived, err = archiveDestination(directory, "Document", "pdf", 2, sourcePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if alreadyArchived || got != filepath.Join(directory, "Document.2.pdf") {
|
||||
t.Fatalf("expected second indexed destination, got %q (already archived: %t)", got, alreadyArchived)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(got, []byte("source"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, alreadyArchived, err = archiveDestination(directory, "Document", "pdf", 2, sourcePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !alreadyArchived || got != filepath.Join(directory, "Document.2.pdf") {
|
||||
t.Fatalf("expected matching indexed destination, got %q (already archived: %t)", got, alreadyArchived)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user