From 6d5540cdf63669b025c7aad4f8fcd361529c55da Mon Sep 17 00:00:00 2001 From: ashmrtn Date: Thu, 15 Sep 2022 14:06:26 -0700 Subject: [PATCH] Empty path generates empty short ref (#868) ## Description Fixup corner case on ShortRef so that empty paths generate the empty string. This makes some cases with generating short refs for each parent path to an item easier because the base case (empty path) is no different than other cases. ## Type of change - [ ] :sunflower: Feature - [x] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Test - [ ] :computer: CI/Deployment - [ ] :hamster: Trivial/Minor ## Issue(s) * part of #818 ## Test Plan - [ ] :muscle: Manual - [x] :zap: Unit test - [ ] :green_heart: E2E --- src/internal/path/path.go | 4 ++++ src/internal/path/path_test.go | 26 +++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/internal/path/path.go b/src/internal/path/path.go index 2ea47c319..efe675fdd 100644 --- a/src/internal/path/path.go +++ b/src/internal/path/path.go @@ -198,6 +198,10 @@ func (pb Builder) String() string { } func (pb Builder) ShortRef() string { + if len(pb.elements) == 0 { + return "" + } + data := bytes.Buffer{} for _, element := range pb.elements { diff --git a/src/internal/path/path_test.go b/src/internal/path/path_test.go index 1af35d019..d90715e7b 100644 --- a/src/internal/path/path_test.go +++ b/src/internal/path/path_test.go @@ -366,9 +366,29 @@ func (suite *PathUnitSuite) TestPopFront() { } func (suite *PathUnitSuite) TestShortRef() { - pb := Builder{}.Append("this", "is", "a", "path") - ref := pb.ShortRef() - assert.Len(suite.T(), ref, shortRefCharacters) + table := []struct { + name string + inputElements []string + expectedLen int + }{ + { + name: "PopulatedPath", + inputElements: []string{"this", "is", "a", "path"}, + expectedLen: shortRefCharacters, + }, + { + name: "EmptyPath", + inputElements: nil, + expectedLen: 0, + }, + } + for _, test := range table { + suite.T().Run(test.name, func(t *testing.T) { + pb := Builder{}.Append(test.inputElements...) + ref := pb.ShortRef() + assert.Len(suite.T(), ref, test.expectedLen) + }) + } } func (suite *PathUnitSuite) TestShortRefIsStable() {