From ad8275f294fd4ece8fd1ed9400ff6b4934fdb6a6 Mon Sep 17 00:00:00 2001 From: brianpursley Date: Sat, 3 Jul 2021 17:57:47 -0400 Subject: [PATCH] Added unit tests for ExpandPathsToFileVisitors --- .../cli-runtime/pkg/resource/visitor_test.go | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) diff --git a/staging/src/k8s.io/cli-runtime/pkg/resource/visitor_test.go b/staging/src/k8s.io/cli-runtime/pkg/resource/visitor_test.go index 414d460f9b4..1ecb5a61e30 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/resource/visitor_test.go +++ b/staging/src/k8s.io/cli-runtime/pkg/resource/visitor_test.go @@ -21,7 +21,10 @@ import ( "errors" "fmt" "io" + "io/fs" "io/ioutil" + "os" + "path/filepath" "strings" "testing" "time" @@ -197,3 +200,159 @@ func TestFlattenListVisitorWithVisitorError(t *testing.T) { 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) + }) + } +}