@@ -19,42 +19,37 @@ package common
19
19
import (
20
20
"os"
21
21
"path/filepath"
22
- "testing"
23
22
24
23
. "github.com/onsi/ginkgo/v2"
25
24
. "github.com/onsi/gomega"
26
25
27
26
"sigs.k8s.io/kubebuilder/v4/pkg/config"
28
27
"sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml"
29
28
v3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
29
+ "sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
30
30
)
31
31
32
- func TestCommon (t * testing.T ) {
33
- RegisterFailHandler (Fail )
34
- RunSpecs (t , "Common Pkg Suite" )
35
- }
36
-
37
32
var _ = Describe ("LoadProjectConfig" , func () {
38
33
var (
39
- tmpDir string
34
+ kbc * utils. TestContext
40
35
projectFile string
41
36
)
42
37
43
38
BeforeEach (func () {
44
39
var err error
45
- tmpDir , err = os . MkdirTemp ( " " , "kubebuilder-common-test " )
40
+ kbc , err = utils . NewTestContext ( "kubebuilder " , "GO111MODULE=on " )
46
41
Expect (err ).NotTo (HaveOccurred ())
47
- projectFile = filepath .Join (tmpDir , yaml .DefaultPath )
42
+ Expect (kbc .Prepare ()).To (Succeed ())
43
+ projectFile = filepath .Join (kbc .Dir , yaml .DefaultPath )
48
44
})
49
45
50
46
AfterEach (func () {
51
- err := os . RemoveAll ( tmpDir )
52
- Expect ( err ). NotTo ( HaveOccurred () )
47
+ By ( "cleaning up test artifacts" )
48
+ kbc . Destroy ( )
53
49
})
54
50
55
51
Context ("when PROJECT file exists and is valid" , func () {
56
52
It ("should load the project config successfully" , func () {
57
- // Register version 3 config
58
53
config .Register (config.Version {Number : 3 }, func () config.Config {
59
54
return & v3.Cfg {Version : config.Version {Number : 3 }}
60
55
})
@@ -63,26 +58,25 @@ var _ = Describe("LoadProjectConfig", func() {
63
58
`
64
59
Expect (os .WriteFile (projectFile , []byte (version ), 0o644 )).To (Succeed ())
65
60
66
- config , err := LoadProjectConfig (tmpDir )
61
+ cfg , err := LoadProjectConfig (kbc . Dir )
67
62
Expect (err ).NotTo (HaveOccurred ())
68
- Expect (config ).NotTo (BeNil ())
63
+ Expect (cfg ).NotTo (BeNil ())
69
64
})
70
65
})
71
66
72
67
Context ("when PROJECT file does not exist" , func () {
73
68
It ("should return an error" , func () {
74
- _ , err := LoadProjectConfig (tmpDir )
69
+ _ , err := LoadProjectConfig (kbc . Dir )
75
70
Expect (err ).To (HaveOccurred ())
76
71
Expect (err .Error ()).To (ContainSubstring ("failed to load PROJECT file" ))
77
72
})
78
73
})
79
74
80
75
Context ("when PROJECT file is invalid" , func () {
81
76
It ("should return an error" , func () {
82
- // Write an invalid YAML content
83
77
Expect (os .WriteFile (projectFile , []byte (":?!" ), 0o644 )).To (Succeed ())
84
78
85
- _ , err := LoadProjectConfig (tmpDir )
79
+ _ , err := LoadProjectConfig (kbc . Dir )
86
80
Expect (err ).To (HaveOccurred ())
87
81
Expect (err .Error ()).To (ContainSubstring ("failed to load PROJECT file" ))
88
82
})
@@ -91,69 +85,64 @@ var _ = Describe("LoadProjectConfig", func() {
91
85
92
86
var _ = Describe ("GetInputPath" , func () {
93
87
var (
94
- tmpDir string
88
+ kbc * utils. TestContext
95
89
projectFile string
96
90
)
97
91
98
92
BeforeEach (func () {
99
93
var err error
100
- tmpDir , err = os . MkdirTemp ( " " , "kubebuilder-common-test " )
94
+ kbc , err = utils . NewTestContext ( "kubebuilder " , "GO111MODULE=on " )
101
95
Expect (err ).NotTo (HaveOccurred ())
102
- projectFile = filepath .Join (tmpDir , yaml .DefaultPath )
96
+ Expect (kbc .Prepare ()).To (Succeed ())
97
+ projectFile = filepath .Join (kbc .Dir , yaml .DefaultPath )
103
98
})
104
99
105
100
AfterEach (func () {
106
- err := os . RemoveAll ( tmpDir )
107
- Expect ( err ). NotTo ( HaveOccurred () )
101
+ By ( "cleaning up test artifacts" )
102
+ kbc . Destroy ( )
108
103
})
109
104
110
- Context ("when inputPath is empty" , func () {
111
- It ("should return current working directory if PROJECT file exists" , func () {
112
- // Create PROJECT file in tmpDir
113
- Expect (os .WriteFile (projectFile , []byte ("test-data" ), 0o644 )).To (Succeed ())
114
-
115
- // Change working directory to tmpDir
116
- Expect (os .Chdir (tmpDir )).To (Succeed ())
105
+ Context ("when inputPath has trailing slash" , func () {
106
+ It ("should handle trailing slash and find PROJECT file" , func () {
107
+ Expect (os .WriteFile (projectFile , []byte ("test" ), 0o644 )).To (Succeed ())
117
108
118
- currWd , err := os . Getwd ( )
109
+ inputPath , err := GetInputPath ( kbc . Dir + "/" )
119
110
Expect (err ).NotTo (HaveOccurred ())
120
-
121
- inputPath , err := GetInputPath ("" )
122
- Expect (err ).NotTo (HaveOccurred ())
123
- Expect (inputPath ).To (Equal (currWd ))
111
+ Expect (inputPath ).To (Equal (kbc .Dir + "/" ))
124
112
})
113
+ })
125
114
126
- It ("should return error if PROJECT file does not exist in cwd" , func () {
127
- // Change working directory to tmpDir (no PROJECT file)
128
- Expect (os .Chdir (tmpDir )).To (Succeed ())
129
-
115
+ Context ("when inputPath is empty" , func () {
116
+ It ("should return error if PROJECT file does not exist in CWD" , func () {
130
117
inputPath , err := GetInputPath ("" )
131
118
Expect (err ).To (HaveOccurred ())
132
119
Expect (inputPath ).To (Equal ("" ))
133
120
Expect (err .Error ()).To (ContainSubstring ("does not exist" ))
134
121
})
135
122
})
136
123
137
- Context ("when inputPath is provided " , func () {
138
- It ("should return inputPath if PROJECT file exists " , func () {
124
+ Context ("when inputPath is valid and PROJECT file exists " , func () {
125
+ It ("should return the inputPath " , func () {
139
126
Expect (os .WriteFile (projectFile , []byte ("test" ), 0o644 )).To (Succeed ())
140
127
141
- inputPath , err := GetInputPath (tmpDir )
128
+ inputPath , err := GetInputPath (kbc . Dir )
142
129
Expect (err ).NotTo (HaveOccurred ())
143
- Expect (inputPath ).To (Equal (tmpDir ))
130
+ Expect (inputPath ).To (Equal (kbc . Dir ))
144
131
})
132
+ })
145
133
146
- It ("should return error if PROJECT file does not exist at provided inputPath" , func () {
147
- inputPath , err := GetInputPath (tmpDir )
134
+ Context ("when inputPath is valid but PROJECT file does not exist" , func () {
135
+ It ("should return an error" , func () {
136
+ inputPath , err := GetInputPath (kbc .Dir )
148
137
Expect (err ).To (HaveOccurred ())
149
138
Expect (inputPath ).To (Equal ("" ))
150
139
Expect (err .Error ()).To (ContainSubstring ("does not exist" ))
151
140
})
152
141
})
153
142
154
- Context ("when inputPath is invalid " , func () {
155
- It ("should return error if inputPath does not exist " , func () {
156
- invalidPath := filepath .Join (tmpDir , "nonexistent" )
143
+ Context ("when inputPath does not exist " , func () {
144
+ It ("should return an error " , func () {
145
+ invalidPath := filepath .Join (kbc . Dir , "nonexistent" )
157
146
inputPath , err := GetInputPath (invalidPath )
158
147
Expect (err ).To (HaveOccurred ())
159
148
Expect (inputPath ).To (Equal ("" ))
0 commit comments