Skip to content

Commit

Permalink
Merge pull request #369 from 0xff-dev/utils-path-test
Browse files Browse the repository at this point in the history
chore: add testcases for pkg/utils/path.go
  • Loading branch information
bjwswang authored Oct 9, 2023
2 parents adb86ce + d03534e commit 82d94da
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/utils/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
48 changes: 48 additions & 0 deletions pkg/utils/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 82d94da

Please sign in to comment.