@@ -19,42 +19,37 @@ package common
1919import (
2020 "os"
2121 "path/filepath"
22- "testing"
2322
2423 . "github.com/onsi/ginkgo/v2"
2524 . "github.com/onsi/gomega"
2625
2726 "sigs.k8s.io/kubebuilder/v4/pkg/config"
2827 "sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml"
2928 v3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
29+ "sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
3030)
3131
32- func TestCommon (t * testing.T ) {
33- RegisterFailHandler (Fail )
34- RunSpecs (t , "Common Pkg Suite" )
35- }
36-
3732var _ = Describe ("LoadProjectConfig" , func () {
3833 var (
39- tmpDir string
34+ kbc * utils. TestContext
4035 projectFile string
4136 )
4237
4338 BeforeEach (func () {
4439 var err error
45- tmpDir , err = os . MkdirTemp ( " " , "kubebuilder-common-test " )
40+ kbc , err = utils . NewTestContext ( "kubebuilder " , "GO111MODULE=on " )
4641 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 )
4844 })
4945
5046 AfterEach (func () {
51- err := os . RemoveAll ( tmpDir )
52- Expect ( err ). NotTo ( HaveOccurred () )
47+ By ( "cleaning up test artifacts" )
48+ kbc . Destroy ( )
5349 })
5450
5551 Context ("when PROJECT file exists and is valid" , func () {
5652 It ("should load the project config successfully" , func () {
57- // Register version 3 config
5853 config .Register (config.Version {Number : 3 }, func () config.Config {
5954 return & v3.Cfg {Version : config.Version {Number : 3 }}
6055 })
@@ -63,26 +58,25 @@ var _ = Describe("LoadProjectConfig", func() {
6358`
6459 Expect (os .WriteFile (projectFile , []byte (version ), 0o644 )).To (Succeed ())
6560
66- config , err := LoadProjectConfig (tmpDir )
61+ cfg , err := LoadProjectConfig (kbc . Dir )
6762 Expect (err ).NotTo (HaveOccurred ())
68- Expect (config ).NotTo (BeNil ())
63+ Expect (cfg ).NotTo (BeNil ())
6964 })
7065 })
7166
7267 Context ("when PROJECT file does not exist" , func () {
7368 It ("should return an error" , func () {
74- _ , err := LoadProjectConfig (tmpDir )
69+ _ , err := LoadProjectConfig (kbc . Dir )
7570 Expect (err ).To (HaveOccurred ())
7671 Expect (err .Error ()).To (ContainSubstring ("failed to load PROJECT file" ))
7772 })
7873 })
7974
8075 Context ("when PROJECT file is invalid" , func () {
8176 It ("should return an error" , func () {
82- // Write an invalid YAML content
8377 Expect (os .WriteFile (projectFile , []byte (":?!" ), 0o644 )).To (Succeed ())
8478
85- _ , err := LoadProjectConfig (tmpDir )
79+ _ , err := LoadProjectConfig (kbc . Dir )
8680 Expect (err ).To (HaveOccurred ())
8781 Expect (err .Error ()).To (ContainSubstring ("failed to load PROJECT file" ))
8882 })
@@ -91,69 +85,64 @@ var _ = Describe("LoadProjectConfig", func() {
9185
9286var _ = Describe ("GetInputPath" , func () {
9387 var (
94- tmpDir string
88+ kbc * utils. TestContext
9589 projectFile string
9690 )
9791
9892 BeforeEach (func () {
9993 var err error
100- tmpDir , err = os . MkdirTemp ( " " , "kubebuilder-common-test " )
94+ kbc , err = utils . NewTestContext ( "kubebuilder " , "GO111MODULE=on " )
10195 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 )
10398 })
10499
105100 AfterEach (func () {
106- err := os . RemoveAll ( tmpDir )
107- Expect ( err ). NotTo ( HaveOccurred () )
101+ By ( "cleaning up test artifacts" )
102+ kbc . Destroy ( )
108103 })
109104
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 ())
117108
118- currWd , err := os . Getwd ( )
109+ inputPath , err := GetInputPath ( kbc . Dir + "/" )
119110 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 + "/" ))
124112 })
113+ })
125114
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 () {
130117 inputPath , err := GetInputPath ("" )
131118 Expect (err ).To (HaveOccurred ())
132119 Expect (inputPath ).To (Equal ("" ))
133120 Expect (err .Error ()).To (ContainSubstring ("does not exist" ))
134121 })
135122 })
136123
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 () {
139126 Expect (os .WriteFile (projectFile , []byte ("test" ), 0o644 )).To (Succeed ())
140127
141- inputPath , err := GetInputPath (tmpDir )
128+ inputPath , err := GetInputPath (kbc . Dir )
142129 Expect (err ).NotTo (HaveOccurred ())
143- Expect (inputPath ).To (Equal (tmpDir ))
130+ Expect (inputPath ).To (Equal (kbc . Dir ))
144131 })
132+ })
145133
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 )
148137 Expect (err ).To (HaveOccurred ())
149138 Expect (inputPath ).To (Equal ("" ))
150139 Expect (err .Error ()).To (ContainSubstring ("does not exist" ))
151140 })
152141 })
153142
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" )
157146 inputPath , err := GetInputPath (invalidPath )
158147 Expect (err ).To (HaveOccurred ())
159148 Expect (inputPath ).To (Equal ("" ))
0 commit comments