From d03534ef600a4b7bc478621b34fc3f4584951e3b Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 8 Oct 2023 09:58:53 +0800 Subject: [PATCH] chore: add testcases for pkg/utils/path.go --- pkg/utils/path.go | 6 +++++- pkg/utils/path_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/pkg/utils/path.go b/pkg/utils/path.go index 222daa30..0dd48198 100644 --- a/pkg/utils/path.go +++ b/pkg/utils/path.go @@ -53,7 +53,11 @@ func ParseValues(dir string, reference *apiextensionsv1.JSON) (fileName string, func GetOCIEntryName(url string) string { nameSegments := strings.Split(url, "/") - entryName := nameSegments[len(nameSegments)-1] + l := len(nameSegments) + if l <= 3 { + return "" + } + entryName := nameSegments[l-1] return entryName } diff --git a/pkg/utils/path_test.go b/pkg/utils/path_test.go index dfa44edb..024fe393 100644 --- a/pkg/utils/path_test.go +++ b/pkg/utils/path_test.go @@ -105,3 +105,51 @@ func TestParseValues(t *testing.T) { }) } } + +func TestGetOCIEntryName(t *testing.T) { + type input struct { + url, expectName string + } + for _, tc := range []input{ + { + url: "oci://a.com/abc/def", + expectName: "def", + }, + { + url: "oci://a.com/aa", + expectName: "aa", + }, + { + url: "oci://a.com", + expectName: "", + }, + } { + if r := GetOCIEntryName(tc.url); r != tc.expectName { + t.Fatalf("Test Failed. expect %s, actual: %s", tc.expectName, r) + } + } +} + +func TestGetHTTPEntryName(t *testing.T) { + type input struct { + url, expectName string + } + for _, tc := range []input{ + { + url: "https://github.com/kubebb/components/releases/download/bc-apis-0.0.3/bc-apis-0.0.3.tgz ", + expectName: "bc-apis", + }, + { + url: "https://github.com/kubebb/components/releases/download/bc-apis-0.0.3/bc-apis0.0.3.tgz", + expectName: "bc", + }, + { + url: "https://github.com/kubebb/components/releases/download/bc-apis-0.0.3/bcapis", + expectName: "", + }, + } { + if r := GetHTTPEntryName(tc.url); r != tc.expectName { + t.Fatalf("Test Failed. expect %s, actual: %s", tc.expectName, r) + } + } +}