From 9009742f676da85dd90953011acbb4d8aba11054 Mon Sep 17 00:00:00 2001 From: Ayooluwa Isaiah Date: Fri, 11 Oct 2024 11:43:58 +0100 Subject: [PATCH] ensure empty filenames are reported properly --- internal/file/file.go | 10 ++++++++++ validate/validate.go | 2 ++ validate/validate_test/validate_test.go | 20 ++++++++++++-------- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/internal/file/file.go b/internal/file/file.go index 5d17fbf..94084d1 100644 --- a/internal/file/file.go +++ b/internal/file/file.go @@ -37,6 +37,16 @@ type Change struct { func (c *Change) AutoFixTarget(newTarget string) { c.Target = newTarget c.TargetPath = filepath.Join(c.BaseDir, c.Target) + + // Ensure empty targets is reported as empty instead of as a dot + if c.TargetPath == "." { + c.TargetPath = "" + } + + if c.Target == "" && c.TargetPath != "" { + c.TargetPath += "/" + } + c.Status = status.OK } diff --git a/validate/validate.go b/validate/validate.go index 9c86626..558963e 100644 --- a/validate/validate.go +++ b/validate/validate.go @@ -107,6 +107,8 @@ func checkEmptyFilenameConflict( ) (conflictDetected bool) { if ctx.change.Target == "." || ctx.change.Target == "" { conflictDetected = true + + ctx.change.AutoFixTarget("") ctx.change.Status = status.EmptyFilename if ctx.autoFix { diff --git a/validate/validate_test/validate_test.go b/validate/validate_test/validate_test.go index b38f063..38e7775 100644 --- a/validate/validate_test/validate_test.go +++ b/validate/validate_test/validate_test.go @@ -32,10 +32,13 @@ func validateTest(t *testing.T, cases []testutil.TestCase) { ch.BaseDir, ch.Source, ) - cases[i].Changes[j].TargetPath = filepath.Join( - ch.BaseDir, - ch.Target, - ) + + if cases[i].Changes[j].TargetPath == "" { + cases[i].Changes[j].TargetPath = filepath.Join( + ch.BaseDir, + ch.Target, + ) + } } } @@ -93,10 +96,11 @@ func TestValidate(t *testing.T) { Name: "detect empty filename conflict", Changes: file.Changes{ { - Source: "1984.pdf", - Target: "", - BaseDir: "ebooks", - Status: status.EmptyFilename, + Source: "1984.pdf", + Target: "", + TargetPath: "ebooks/", + BaseDir: "ebooks", + Status: status.EmptyFilename, }, }, ConflictDetected: true,