From 53578d892082e384e840636a0d83630174f9e05d Mon Sep 17 00:00:00 2001 From: zhaque44 Date: Thu, 8 May 2025 18:54:05 -0500 Subject: [PATCH 1/4] chore: Enhance Digester test coverage with edge case scenarios Signed-off-by: zhaque44 --- internal/index/digest_test.go | 61 +++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/internal/index/digest_test.go b/internal/index/digest_test.go index 8afc4fd0..5f2d8467 100644 --- a/internal/index/digest_test.go +++ b/internal/index/digest_test.go @@ -49,6 +49,13 @@ func TestWithIndex(t *testing.T) { g.Expect(d.digests).To(BeEmpty()) }) + + t.Run("handles nil index", func(t *testing.T) { + g := NewWithT(t) + d := &Digester{} + WithIndex(nil)(d) + g.Expect(d.index).To(BeNil()) + }) } func TestNewDigester(t *testing.T) { @@ -72,6 +79,14 @@ func TestNewDigester(t *testing.T) { g.Expect(d.index).To(Equal(i)) g.Expect(d.digests).ToNot(BeNil()) }) + + t.Run("handles multiple WithIndex options, applying last one", func(t *testing.T) { + g := NewWithT(t) + firstIndex := map[string]string{"a": "b"} + secondIndex := map[string]string{"c": "d"} + d := NewDigester(WithIndex(firstIndex), WithIndex(secondIndex)) + g.Expect(d.index).To(Equal(secondIndex)) + }) } func TestDigester_Add(t *testing.T) { @@ -107,6 +122,13 @@ func TestDigester_Add(t *testing.T) { g.Expect(d.digests).To(BeEmpty()) }) + + t.Run("adds empty key and value", func(t *testing.T) { + g := NewWithT(t) + d := NewDigester() + d.Add("", "") + g.Expect(d.index).To(HaveKeyWithValue("", "")) + }) } func TestDigester_Delete(t *testing.T) { @@ -138,6 +160,14 @@ func TestDigester_Delete(t *testing.T) { d.Delete("foo") g.Expect(d.digests).To(BeEmpty()) }) + + t.Run("deletes non-existent key without error", func(t *testing.T) { + g := NewWithT(t) + d := NewDigester() + d.Delete("non-existent") + g.Expect(d.index).To(BeEmpty()) // Index should remain empty + g.Expect(d.digests).To(BeEmpty()) // Digests should remain empty as no change + }) } func TestDigester_Get(t *testing.T) { @@ -161,17 +191,26 @@ func TestDigester_Has(t *testing.T) { } func TestDigester_Index(t *testing.T) { - g := NewWithT(t) + t.Run("returns a copy of the index", func(t *testing.T) { + g := NewWithT(t) - i := map[string]string{ - "foo": "bar", - "bar": "baz", - } - d := NewDigester(WithIndex(i)) + i := map[string]string{ + "foo": "bar", + "bar": "baz", + } + d := NewDigester(WithIndex(i)) - iCopy := d.Index() - g.Expect(iCopy).To(Equal(i)) - g.Expect(iCopy).ToNot(BeIdenticalTo(i)) + iCopy := d.Index() + g.Expect(iCopy).To(Equal(i)) + g.Expect(iCopy).ToNot(BeIdenticalTo(i)) + }) + + t.Run("returns an empty copy for an empty index", func(t *testing.T) { + g := NewWithT(t) + d := NewDigester() + emptyIndex := d.Index() + g.Expect(emptyIndex).To(BeEmpty()) + }) } func TestDigester_Len(t *testing.T) { @@ -183,6 +222,8 @@ func TestDigester_Len(t *testing.T) { })) g.Expect(d.Len()).To(Equal(2)) + + g.Expect(NewDigester().Len()).To(Equal(0)) } func TestDigester_String(t *testing.T) { @@ -196,6 +237,8 @@ func TestDigester_String(t *testing.T) { g.Expect(d.String()).To(Equal(`bar baz foo bar `)) + + g.Expect(NewDigester().String()).To(Equal("")) } func TestDigester_WriteTo(t *testing.T) { From 15bbf61c5002c191507e46442618a49db2df6e07 Mon Sep 17 00:00:00 2001 From: zhaque44 Date: Thu, 8 May 2025 19:05:06 -0500 Subject: [PATCH 2/4] fix new digester Signed-off-by: zhaque44 --- internal/index/digest_test.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/internal/index/digest_test.go b/internal/index/digest_test.go index 5f2d8467..001b067b 100644 --- a/internal/index/digest_test.go +++ b/internal/index/digest_test.go @@ -59,27 +59,31 @@ func TestWithIndex(t *testing.T) { } func TestNewDigester(t *testing.T) { + g := NewWithT(t) + t.Run("default", func(t *testing.T) { - g := NewWithT(t) - d := NewDigester() - g.Expect(d).ToNot(BeNil()) - g.Expect(d.index).ToNot(BeNil()) + g.Expect(d.index).To(BeEmpty()) g.Expect(d.digests).ToNot(BeNil()) }) t.Run("with index", func(t *testing.T) { - g := NewWithT(t) - - i := map[string]string{"foo": "bar"} - d := NewDigester(WithIndex(i)) - + initialIndex := map[string]string{"foo": "bar"} + d := NewDigester(WithIndex(initialIndex)) g.Expect(d).ToNot(BeNil()) - g.Expect(d.index).To(Equal(i)) + g.Expect(d.index).To(Equal(initialIndex)) g.Expect(d.digests).ToNot(BeNil()) }) + t.Run("handles multiple WithIndex options, applying last one", func(t *testing.T) { + firstIndex := map[string]string{"a": "b"} + secondIndex := map[string]string{"c": "d"} + d := NewDigester(WithIndex(firstIndex), WithIndex(secondIndex)) + g.Expect(d.index).To(Equal(secondIndex)) + }) +} + t.Run("handles multiple WithIndex options, applying last one", func(t *testing.T) { g := NewWithT(t) firstIndex := map[string]string{"a": "b"} From 374e4ba495db295d9f3ad52aa680255321e7c376 Mon Sep 17 00:00:00 2001 From: zhaque44 Date: Thu, 8 May 2025 19:08:42 -0500 Subject: [PATCH 3/4] rm unecessary scenarios Signed-off-by: zhaque44 --- internal/index/digest_test.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/internal/index/digest_test.go b/internal/index/digest_test.go index 001b067b..c56289d3 100644 --- a/internal/index/digest_test.go +++ b/internal/index/digest_test.go @@ -84,15 +84,6 @@ func TestNewDigester(t *testing.T) { }) } - t.Run("handles multiple WithIndex options, applying last one", func(t *testing.T) { - g := NewWithT(t) - firstIndex := map[string]string{"a": "b"} - secondIndex := map[string]string{"c": "d"} - d := NewDigester(WithIndex(firstIndex), WithIndex(secondIndex)) - g.Expect(d.index).To(Equal(secondIndex)) - }) -} - func TestDigester_Add(t *testing.T) { t.Run("adds", func(t *testing.T) { g := NewWithT(t) @@ -169,8 +160,8 @@ func TestDigester_Delete(t *testing.T) { g := NewWithT(t) d := NewDigester() d.Delete("non-existent") - g.Expect(d.index).To(BeEmpty()) // Index should remain empty - g.Expect(d.digests).To(BeEmpty()) // Digests should remain empty as no change + g.Expect(d.index).To(BeEmpty()) + g.Expect(d.digests).To(BeEmpty()) }) } From bb4671cc0ebc38dfe04e337a3a08f6ead3bc4464 Mon Sep 17 00:00:00 2001 From: zhaque44 Date: Thu, 8 May 2025 19:19:08 -0500 Subject: [PATCH 4/4] set NewDigester back Signed-off-by: zhaque44 --- internal/index/digest_test.go | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/internal/index/digest_test.go b/internal/index/digest_test.go index c56289d3..531bb932 100644 --- a/internal/index/digest_test.go +++ b/internal/index/digest_test.go @@ -59,28 +59,25 @@ func TestWithIndex(t *testing.T) { } func TestNewDigester(t *testing.T) { - g := NewWithT(t) - t.Run("default", func(t *testing.T) { + g := NewWithT(t) + d := NewDigester() + g.Expect(d).ToNot(BeNil()) - g.Expect(d.index).To(BeEmpty()) + g.Expect(d.index).ToNot(BeNil()) g.Expect(d.digests).ToNot(BeNil()) }) t.Run("with index", func(t *testing.T) { - initialIndex := map[string]string{"foo": "bar"} - d := NewDigester(WithIndex(initialIndex)) - g.Expect(d).ToNot(BeNil()) - g.Expect(d.index).To(Equal(initialIndex)) - g.Expect(d.digests).ToNot(BeNil()) - }) + g := NewWithT(t) - t.Run("handles multiple WithIndex options, applying last one", func(t *testing.T) { - firstIndex := map[string]string{"a": "b"} - secondIndex := map[string]string{"c": "d"} - d := NewDigester(WithIndex(firstIndex), WithIndex(secondIndex)) - g.Expect(d.index).To(Equal(secondIndex)) + i := map[string]string{"foo": "bar"} + d := NewDigester(WithIndex(i)) + + g.Expect(d).ToNot(BeNil()) + g.Expect(d.index).To(Equal(i)) + g.Expect(d.digests).ToNot(BeNil()) }) }