mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
Merge pull request #103407 from brianpursley/visit-order-tests
Added unit tests for ExpandPathsToFileVisitors
This commit is contained in:
commit
657c6fe033
@ -21,7 +21,10 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -197,3 +200,159 @@ func TestFlattenListVisitorWithVisitorError(t *testing.T) {
|
|||||||
t.Fatal(spew.Sdump(test.Infos))
|
t.Fatal(spew.Sdump(test.Infos))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExpandPathsToFileVisitors(t *testing.T) {
|
||||||
|
// Define a directory structure that will be used for testing and create empty files
|
||||||
|
testDir := t.TempDir()
|
||||||
|
filePaths := []string{
|
||||||
|
filepath.Join(testDir, "0", "10.yaml"),
|
||||||
|
filepath.Join(testDir, "0", "a", "10.yaml"),
|
||||||
|
filepath.Join(testDir, "02.yaml"),
|
||||||
|
filepath.Join(testDir, "10.yaml"),
|
||||||
|
filepath.Join(testDir, "2.yaml"),
|
||||||
|
filepath.Join(testDir, "AB.yaml"),
|
||||||
|
filepath.Join(testDir, "a", "a.yaml"),
|
||||||
|
filepath.Join(testDir, "a", "b.json"),
|
||||||
|
filepath.Join(testDir, "a.yaml"),
|
||||||
|
filepath.Join(testDir, "aa.yaml"),
|
||||||
|
filepath.Join(testDir, "b.yml"),
|
||||||
|
}
|
||||||
|
for _, fp := range filePaths {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(fp), 0700); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
func() {
|
||||||
|
f, err := os.Create(fp)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define and execute test cases
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
path string
|
||||||
|
recursive bool
|
||||||
|
fileExtensions []string
|
||||||
|
expectedPaths []string
|
||||||
|
expectPathError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Recursive with default file extensions",
|
||||||
|
path: testDir,
|
||||||
|
recursive: true,
|
||||||
|
fileExtensions: FileExtensions,
|
||||||
|
expectedPaths: []string{
|
||||||
|
filepath.Join(testDir, "0", "10.yaml"),
|
||||||
|
filepath.Join(testDir, "0", "a", "10.yaml"),
|
||||||
|
filepath.Join(testDir, "02.yaml"),
|
||||||
|
filepath.Join(testDir, "10.yaml"),
|
||||||
|
filepath.Join(testDir, "2.yaml"),
|
||||||
|
filepath.Join(testDir, "AB.yaml"),
|
||||||
|
filepath.Join(testDir, "a", "a.yaml"),
|
||||||
|
filepath.Join(testDir, "a", "b.json"),
|
||||||
|
filepath.Join(testDir, "a.yaml"),
|
||||||
|
filepath.Join(testDir, "aa.yaml"),
|
||||||
|
filepath.Join(testDir, "b.yml"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Non-recursive with default file extensions",
|
||||||
|
path: testDir,
|
||||||
|
fileExtensions: FileExtensions,
|
||||||
|
expectedPaths: []string{
|
||||||
|
filepath.Join(testDir, "02.yaml"),
|
||||||
|
filepath.Join(testDir, "10.yaml"),
|
||||||
|
filepath.Join(testDir, "2.yaml"),
|
||||||
|
filepath.Join(testDir, "AB.yaml"),
|
||||||
|
filepath.Join(testDir, "a.yaml"),
|
||||||
|
filepath.Join(testDir, "aa.yaml"),
|
||||||
|
filepath.Join(testDir, "b.yml"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Recursive with yaml file extension",
|
||||||
|
path: testDir,
|
||||||
|
recursive: true,
|
||||||
|
fileExtensions: []string{".yaml"},
|
||||||
|
expectedPaths: []string{
|
||||||
|
filepath.Join(testDir, "0", "10.yaml"),
|
||||||
|
filepath.Join(testDir, "0", "a", "10.yaml"),
|
||||||
|
filepath.Join(testDir, "02.yaml"),
|
||||||
|
filepath.Join(testDir, "10.yaml"),
|
||||||
|
filepath.Join(testDir, "2.yaml"),
|
||||||
|
filepath.Join(testDir, "AB.yaml"),
|
||||||
|
filepath.Join(testDir, "a", "a.yaml"),
|
||||||
|
filepath.Join(testDir, "a.yaml"),
|
||||||
|
filepath.Join(testDir, "aa.yaml"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Recursive with json and yml file extensions",
|
||||||
|
path: testDir,
|
||||||
|
recursive: true,
|
||||||
|
fileExtensions: []string{".json", ".yml"},
|
||||||
|
expectedPaths: []string{
|
||||||
|
filepath.Join(testDir, "a", "b.json"),
|
||||||
|
filepath.Join(testDir, "b.yml"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Non-recursive with json and yml file extensions",
|
||||||
|
path: testDir,
|
||||||
|
fileExtensions: []string{".json", ".yml"},
|
||||||
|
expectedPaths: []string{
|
||||||
|
filepath.Join(testDir, "b.yml"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Non-existent file extensions should return nothing",
|
||||||
|
path: testDir,
|
||||||
|
recursive: true,
|
||||||
|
fileExtensions: []string{".foo"},
|
||||||
|
expectedPaths: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Non-existent path should return file not found error",
|
||||||
|
path: filepath.Join(testDir, "does", "not", "exist"),
|
||||||
|
recursive: true,
|
||||||
|
fileExtensions: []string{".foo"},
|
||||||
|
expectedPaths: []string{},
|
||||||
|
expectPathError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Visitor for single file is returned even if extension does not match",
|
||||||
|
path: filepath.Join(testDir, "a.yaml"),
|
||||||
|
recursive: true,
|
||||||
|
fileExtensions: []string{"foo"},
|
||||||
|
expectedPaths: []string{
|
||||||
|
filepath.Join(testDir, "a.yaml"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
visitors, err := ExpandPathsToFileVisitors(nil, tt.path, tt.recursive, tt.fileExtensions, nil)
|
||||||
|
if err != nil {
|
||||||
|
switch e := err.(type) {
|
||||||
|
case *fs.PathError:
|
||||||
|
if tt.expectPathError {
|
||||||
|
// The other details of PathError are os-specific, so only assert that the error has the path
|
||||||
|
assert.Equal(t, tt.path, e.Path)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actualPaths := []string{}
|
||||||
|
for _, v := range visitors {
|
||||||
|
actualPaths = append(actualPaths, v.(*FileVisitor).Path)
|
||||||
|
}
|
||||||
|
assert.Equal(t, tt.expectedPaths, actualPaths)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user