@@ -24,22 +24,22 @@ import (
24
24
"k8s.io/kubectl/pkg/kinflate/util/fs"
25
25
)
26
26
27
- var rootDir = "/home/seans/project"
28
- var rootFile = "file.yaml"
29
- var rootFilePath = filepath .Join (rootDir , rootFile )
30
- var rootFileContent = []byte ("This is a yaml file" )
31
-
32
- var subDirectory = "subdir"
33
- var subDirectoryPath = filepath .Join (subDirectory , rootFile )
34
- var subDirectoryContent = []byte ("Subdirectory file content" )
35
-
36
- var anotherRootDir = "/home/seans/project2"
37
- var anotherFilePath = filepath .Join (anotherRootDir , rootFile )
38
- var anotherFileContent = []byte ("This is another yaml file" )
27
+ func initializeRootLoader (fakefs fs.FileSystem ) Loader {
28
+ var schemes []SchemeLoader
29
+ schemes = append (schemes , NewFileLoader (fakefs ))
30
+ rootLoader := Init (schemes )
31
+ return rootLoader
32
+ }
39
33
40
34
func TestLoader_Root (t * testing.T ) {
41
35
42
- rootLoader := initializeRootLoader ()
36
+ // Initialize the fake file system and the root loader.
37
+ fakefs := fs .MakeFakeFS ()
38
+ fakefs .WriteFile ("/home/seans/project/file.yaml" , []byte ("Unused" ))
39
+ fakefs .WriteFile ("/home/seans/project/subdir/file.yaml" , []byte ("Unused" ))
40
+ fakefs .WriteFile ("/home/seans/project2/file.yaml" , []byte ("Unused" ))
41
+ rootLoader := initializeRootLoader (fakefs )
42
+
43
43
_ , err := rootLoader .New ("" )
44
44
if err == nil {
45
45
t .Fatalf ("Expected error for empty root location not returned" )
@@ -49,30 +49,31 @@ func TestLoader_Root(t *testing.T) {
49
49
t .Fatalf ("Expected error for unknown scheme not returned" )
50
50
}
51
51
52
- loader , err := rootLoader .New (rootFilePath )
52
+ loader , err := rootLoader .New ("/home/seans/project/file.yaml" )
53
53
if err != nil {
54
54
t .Fatalf ("Unexpected in New(): %v\n " , err )
55
55
}
56
- if rootDir != loader .Root () {
56
+ if "/home/seans/project" != loader .Root () {
57
57
t .Fatalf ("Incorrect Loader Root: %s\n " , loader .Root ())
58
58
}
59
59
60
- subLoader , err := loader .New (subDirectoryPath )
60
+ subLoader , err := loader .New ("subdir/file.yaml" )
61
61
if err != nil {
62
62
t .Fatalf ("Unexpected in New(): %v\n " , err )
63
63
}
64
- if filepath . Join ( rootDir , subDirectory ) != subLoader .Root () {
64
+ if "/home/seans/project/subdir" != subLoader .Root () {
65
65
t .Fatalf ("Incorrect Loader Root: %s\n " , subLoader .Root ())
66
66
}
67
67
68
- anotherLoader , err := loader .New (anotherFilePath )
68
+ anotherLoader , err := loader .New ("/home/seans/project2/file.yaml" )
69
69
if err != nil {
70
70
t .Fatalf ("Unexpected in New(): %v\n " , err )
71
71
}
72
- if anotherRootDir != anotherLoader .Root () {
72
+ if "/home/seans/project2" != anotherLoader .Root () {
73
73
t .Fatalf ("Incorrect Loader Root: %s\n " , anotherLoader .Root ())
74
74
}
75
75
76
+ // Current directory should be expanded to a full absolute file path.
76
77
currentDirLoader , err := loader .New ("." )
77
78
if err != nil {
78
79
t .Fatalf ("Unexpected in New(): %v\n " , err )
@@ -83,47 +84,40 @@ func TestLoader_Root(t *testing.T) {
83
84
}
84
85
85
86
func TestLoader_Load (t * testing.T ) {
86
- rootLoader := initializeRootLoader ()
87
- loader , err := rootLoader .New (rootFilePath )
87
+
88
+ // Initialize the fake file system and the root loader.
89
+ fakefs := fs .MakeFakeFS ()
90
+ fakefs .WriteFile ("/home/seans/project/file.yaml" , []byte ("This is a yaml file" ))
91
+ fakefs .WriteFile ("/home/seans/project/subdir/file.yaml" , []byte ("Subdirectory file content" ))
92
+ fakefs .WriteFile ("/home/seans/project2/file.yaml" , []byte ("This is another yaml file" ))
93
+ rootLoader := initializeRootLoader (fakefs )
94
+
95
+ loader , err := rootLoader .New ("/home/seans/project/file.yaml" )
88
96
if err != nil {
89
97
t .Fatalf ("Unexpected in New(): %v\n " , err )
90
98
}
91
- fileBytes , err := loader .Load (rootFilePath )
99
+ fileBytes , err := loader .Load ("file.yaml" ) // Load relative to root location
92
100
if err != nil {
93
101
t .Fatalf ("Unexpected error in Load(): %v" , err )
94
102
}
95
- if ! reflect .DeepEqual (rootFileContent , fileBytes ) {
96
- t .Fatalf ("Load failed. Expected %s, but got %s" , rootFileContent , fileBytes )
103
+ if ! reflect .DeepEqual ([] byte ( "This is a yaml file" ) , fileBytes ) {
104
+ t .Fatalf ("Load failed. Expected %s, but got %s" , "This is a yaml file" , fileBytes )
97
105
}
98
- fileBytes , err = loader .Load (subDirectoryPath )
106
+
107
+ fileBytes , err = loader .Load ("subdir/file.yaml" )
99
108
if err != nil {
100
109
t .Fatalf ("Unexpected error in Load(): %v" , err )
101
110
}
102
- if ! reflect .DeepEqual (subDirectoryContent , fileBytes ) {
103
- t .Fatalf ("Load failed. Expected %s, but got %s" , subDirectoryContent , fileBytes )
111
+ if ! reflect .DeepEqual ([] byte ( "Subdirectory file content" ) , fileBytes ) {
112
+ t .Fatalf ("Load failed. Expected %s, but got %s" , "Subdirectory file content" , fileBytes )
104
113
}
105
- fileBytes , err = loader .Load (anotherFilePath )
114
+
115
+ fileBytes , err = loader .Load ("/home/seans/project2/file.yaml" )
106
116
if err != nil {
107
117
t .Fatalf ("Unexpected error in Load(): %v" , err )
108
118
}
109
- if ! reflect .DeepEqual (anotherFileContent , fileBytes ) {
110
- t .Fatalf ("Load failed. Expected %s, but got %s" , anotherFileContent , fileBytes )
119
+ if ! reflect .DeepEqual ([] byte ( "This is another yaml file" ) , fileBytes ) {
120
+ t .Fatalf ("Load failed. Expected %s, but got %s" , "This is another yaml file" , fileBytes )
111
121
}
112
122
113
123
}
114
-
115
- func initializeRootLoader () Loader {
116
- fs := initializeFakeFilesystem ()
117
- var schemes []SchemeLoader
118
- schemes = append (schemes , NewFileLoader (fs ))
119
- rootLoader := Init (schemes )
120
- return rootLoader
121
- }
122
-
123
- func initializeFakeFilesystem () fs.FileSystem {
124
- fakefs := fs .MakeFakeFS ()
125
- fakefs .WriteFile (rootFilePath , rootFileContent )
126
- fakefs .WriteFile (filepath .Join (rootDir , subDirectoryPath ), subDirectoryContent )
127
- fakefs .WriteFile (anotherFilePath , anotherFileContent )
128
- return fakefs
129
- }
0 commit comments