Skip to content

Commit 588866f

Browse files
committed
Test_filterPaths
1 parent 37a6088 commit 588866f

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

pkg/github/repositories.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,10 @@ func GetTag(getClient GetClientFn, t translations.TranslationHelperFunc) (tool m
12901290

12911291
// filterPaths filters the entries in a GitHub tree to find paths that
12921292
// match the given suffix.
1293+
// maxResults limits the number of results returned to first maxResults entries,
1294+
// a maxResults of -1 means no limit.
1295+
// It returns a slice of strings containing the matching paths.
1296+
// Directories are returned with a trailing slash.
12931297
func filterPaths(entries []*github.TreeEntry, path string, maxResults int) []string {
12941298
path = strings.TrimSuffix(path, "/") // Normalize path to avoid double slashes
12951299

pkg/github/repositories_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2085,8 +2085,49 @@ func Test_GetTag(t *testing.T) {
20852085
}
20862086
}
20872087

2088-
func Test_ResolveGitReference(t *testing.T) {
2088+
func Test_filterPaths(t *testing.T) {
2089+
tests := []struct {
2090+
name string
2091+
tree []*github.TreeEntry
2092+
path string
2093+
maxResults int
2094+
expected []string
2095+
}{
2096+
{
2097+
name: "file name",
2098+
tree: []*github.TreeEntry{
2099+
{Path: github.Ptr("folder/foo.txt"), Type: github.Ptr("blob")},
2100+
{Path: github.Ptr("bar.txt"), Type: github.Ptr("blob")},
2101+
{Path: github.Ptr("nested/folder/foo.txt"), Type: github.Ptr("blob")},
2102+
{Path: github.Ptr("nested/folder/baz.txt"), Type: github.Ptr("blob")},
2103+
},
2104+
path: "foo.txt",
2105+
maxResults: -1,
2106+
expected: []string{"folder/foo.txt", "nested/folder/foo.txt"},
2107+
},
2108+
{
2109+
name: "dir name",
2110+
tree: []*github.TreeEntry{
2111+
{Path: github.Ptr("folder"), Type: github.Ptr("tree")},
2112+
{Path: github.Ptr("bar.txt"), Type: github.Ptr("blob")},
2113+
{Path: github.Ptr("nested/folder"), Type: github.Ptr("tree")},
2114+
{Path: github.Ptr("nested/folder/baz.txt"), Type: github.Ptr("blob")},
2115+
},
2116+
path: "folder/",
2117+
maxResults: -1,
2118+
expected: []string{"folder/", "nested/folder/"},
2119+
},
2120+
}
2121+
2122+
for _, tc := range tests {
2123+
t.Run(tc.name, func(t *testing.T) {
2124+
result := filterPaths(tc.tree, tc.path, tc.maxResults)
2125+
assert.Equal(t, tc.expected, result)
2126+
})
2127+
}
2128+
}
20892129

2130+
func Test_resolveGitReference(t *testing.T) {
20902131
ctx := context.Background()
20912132
owner := "owner"
20922133
repo := "repo"

0 commit comments

Comments
 (0)