Skip to content

Commit 3c27b72

Browse files
committed
fix: use destination filename instead of source basename for manifest storage
Signed-off-by: Lorenzo Buitizon <the.keikun@gmail.com>
1 parent 470bba6 commit 3c27b72

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

cmd/add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ Examples:
254254
}
255255

256256
// Save the manifest
257-
err = manifest.StoreFileManifest(vaultRoot, filepath.Base(pair.Source), fileManifest)
257+
err = manifest.StoreFileManifest(vaultRoot, destFileName, fileManifest)
258258
if err != nil {
259259
if err.Error() == "skipped" {
260260
errorMsg := fmt.Sprintf("✗ '%s': skipped", fileManifest.Destination+fileManifest.FilePath)

cmd/add_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"os"
5+
"path/filepath"
56
"strings"
67
"testing"
78

@@ -264,3 +265,76 @@ func TestAddCommandBatchProcessingOutput(t *testing.T) {
264265
}
265266
}
266267
}
268+
269+
func TestAddCommandFilenameBugFix(t *testing.T) {
270+
// Test for the bug fix: files with same basename from different directories
271+
// should be stored with correct destination filenames, not source basenames
272+
testutil.SkipIfShort(t, "integration test")
273+
274+
// Create a mock vault for testing
275+
mockConfig := testutil.NewMockConfig(t, "filename-bug-test")
276+
mockConfig.SetupTestVault(t)
277+
278+
// Create test files with same basename in different directories
279+
sourceDir1 := testutil.TempDir(t, "source1")
280+
sourceDir2 := testutil.TempDir(t, "source2")
281+
282+
testFile1 := testutil.CreateTestFile(t, sourceDir1, "test.txt", "content from dir 1")
283+
testFile2 := testutil.CreateTestFile(t, sourceDir2, "test.txt", "content from dir 2")
284+
285+
// Change to vault directory
286+
originalDir, _ := os.Getwd()
287+
os.Chdir(mockConfig.VaultPath)
288+
defer os.Chdir(originalDir)
289+
290+
// Test adding files with same basename to different destinations
291+
// This tests the fix for the bug where filepath.Base(pair.Source) was used
292+
// instead of destFileName for manifest storage
293+
args := []string{testFile1, "docs/file1.txt", testFile2, "data/file2.txt"}
294+
295+
// Parse arguments to verify they are correct
296+
filePairs, err := parseFileArguments(args)
297+
if err != nil {
298+
t.Fatalf("Failed to parse arguments: %v", err)
299+
}
300+
301+
expected := []FilePair{
302+
{Source: testFile1, Destination: "docs/file1.txt"},
303+
{Source: testFile2, Destination: "data/file2.txt"},
304+
}
305+
306+
if len(filePairs) != len(expected) {
307+
t.Fatalf("Expected %d pairs, got %d", len(expected), len(filePairs))
308+
}
309+
310+
// Verify the destinations are different (this is what the bug fix ensures)
311+
if filePairs[0].Destination == filePairs[1].Destination {
312+
t.Errorf("Bug not fixed: both files have same destination %s", filePairs[0].Destination)
313+
}
314+
315+
// Verify that the destination filenames are different
316+
destFileName1 := filepath.Base(filePairs[0].Destination)
317+
destFileName2 := filepath.Base(filePairs[1].Destination)
318+
319+
if destFileName1 == destFileName2 {
320+
t.Errorf("Bug not fixed: both files have same destination filename %s", destFileName1)
321+
}
322+
323+
// Verify that source basenames are the same (this was causing the bug)
324+
sourceBaseName1 := filepath.Base(filePairs[0].Source)
325+
sourceBaseName2 := filepath.Base(filePairs[1].Source)
326+
327+
if sourceBaseName1 != sourceBaseName2 {
328+
t.Errorf("Test setup error: source basenames should be same, got %s vs %s", sourceBaseName1, sourceBaseName2)
329+
}
330+
331+
if sourceBaseName1 != "test.txt" {
332+
t.Errorf("Test setup error: expected source basename 'test.txt', got %s", sourceBaseName1)
333+
}
334+
335+
// The key test: destination filenames should be different
336+
if destFileName1 != "file1.txt" || destFileName2 != "file2.txt" {
337+
t.Errorf("Bug not fixed: expected destination filenames 'file1.txt' and 'file2.txt', got '%s' and '%s'",
338+
destFileName1, destFileName2)
339+
}
340+
}

0 commit comments

Comments
 (0)