Skip to content

Commit 237b480

Browse files
authored
Merge pull request #4959 from mayuka-c/issue-4925-part4
🌱 (CLI): Add unit tests and improve coverage for plugins/golang pkg
2 parents 1b82a27 + f845b71 commit 237b480

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

pkg/plugins/golang/go_version_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,42 @@ limitations under the License.
1717
package golang
1818

1919
import (
20+
"errors"
2021
"sort"
2122

2223
. "github.com/onsi/ginkgo/v2"
2324
. "github.com/onsi/gomega"
2425
)
2526

2627
var _ = Describe("GoVersion", func() {
28+
Context("String", func() {
29+
It("patch is not empty", func() {
30+
v := GoVersion{major: 1, minor: 1, patch: 1}
31+
Expect(v.String()).To(Equal("go1.1.1"))
32+
})
33+
It("preRelease is not empty", func() {
34+
v := GoVersion{major: 1, minor: 1, prerelease: "-alpha"}
35+
Expect(v.String()).To(Equal("go1.1-alpha"))
36+
})
37+
It("default", func() {
38+
v := GoVersion{major: 1, minor: 1}
39+
Expect(v.String()).To(Equal("go1.1"))
40+
})
41+
})
42+
43+
Context("MustParse", func() {
44+
It("succeeds", func() {
45+
v := GoVersion{major: 1, minor: 1, patch: 1}
46+
Expect(MustParse("go1.1.1")).To(Equal(v))
47+
})
48+
It("panics", func() {
49+
triggerPanic := func() {
50+
MustParse("go1.a")
51+
}
52+
Expect(triggerPanic).To(PanicWith(errors.New("invalid version string")))
53+
})
54+
})
55+
2756
Context("parse", func() {
2857
var v GoVersion
2958

@@ -129,6 +158,24 @@ var _ = Describe("GoVersion", func() {
129158
})
130159
})
131160

161+
var _ = Describe("ValidateGoVersion", func() {
162+
DescribeTable("should return no error for valid/supported go versions", func(minVersion, maxVersion GoVersion) {
163+
Expect(ValidateGoVersion(minVersion, maxVersion)).To(Succeed())
164+
},
165+
Entry("for minVersion: 1.1.1 and maxVersion: 2000.1.1", GoVersion{major: 1, minor: 1, patch: 1},
166+
GoVersion{major: 2000, minor: 1, patch: 1}),
167+
Entry("for minVersion: 1.1.1 and maxVersion: 1.2000.2000", GoVersion{major: 1, minor: 1, patch: 1},
168+
GoVersion{major: 1, minor: 2000, patch: 1}),
169+
)
170+
171+
DescribeTable("should return error for invalid/unsupported go versions", func(minVersion, maxVersion GoVersion) {
172+
Expect(ValidateGoVersion(minVersion, maxVersion)).NotTo(Succeed())
173+
},
174+
Entry("for invalid min and maxVersions", GoVersion{major: 2, minor: 2, patch: 2},
175+
GoVersion{major: 1, minor: 1, patch: 1}),
176+
)
177+
})
178+
132179
var _ = Describe("checkGoVersion", func() {
133180
var (
134181
goVerMin GoVersion

pkg/plugins/golang/options_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ var _ = Describe("Options", func() {
8282
} else {
8383
Expect(res.Path).To(Equal(path.Join(cfg.GetRepository(), "api", gvk.Version)))
8484
}
85+
} else if len(options.ExternalAPIPath) > 0 {
86+
Expect(res.Path).To(Equal("testPath"))
8587
} else {
8688
// Core-resources have a path despite not having an API/Webhook but they are not tested here
8789
Expect(res.Path).To(Equal(""))
@@ -104,6 +106,12 @@ var _ = Describe("Options", func() {
104106
} else {
105107
Expect(res.Webhooks.IsEmpty()).To(BeTrue())
106108
}
109+
110+
if len(options.ExternalAPIPath) > 0 {
111+
Expect(res.External).To(BeTrue())
112+
Expect(res.Domain).To(Equal("test.io"))
113+
}
114+
107115
Expect(res.QualifiedGroup()).To(Equal(gvk.Group + "." + gvk.Domain))
108116
Expect(res.PackageName()).To(Equal(gvk.Group))
109117
Expect(res.ImportAlias()).To(Equal(gvk.Group + gvk.Version))
@@ -112,6 +120,9 @@ var _ = Describe("Options", func() {
112120
Entry("when updating nothing", Options{}),
113121
Entry("when updating the plural", Options{Plural: "mates"}),
114122
Entry("when updating the Controller", Options{DoController: true}),
123+
Entry("when updating with External API Path", Options{ExternalAPIPath: "testPath", ExternalAPIDomain: "test.io"}),
124+
Entry("when updating the API with setting webhooks params",
125+
Options{DoAPI: true, DoDefaulting: true, DoValidation: true, DoConversion: true}),
115126
)
116127

117128
DescribeTable("should use core apis",

pkg/plugins/golang/repository_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package golang
18+
19+
import (
20+
"os"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
var _ = Describe("golang:repository", func() {
27+
var (
28+
tmpDir string
29+
oldDir string
30+
)
31+
32+
BeforeEach(func() {
33+
var err error
34+
tmpDir, err = os.MkdirTemp("", "repo-test")
35+
Expect(err).NotTo(HaveOccurred())
36+
oldDir, err = os.Getwd()
37+
Expect(err).NotTo(HaveOccurred())
38+
Expect(os.Chdir(tmpDir)).To(Succeed())
39+
})
40+
41+
AfterEach(func() {
42+
Expect(os.Chdir(oldDir)).To(Succeed())
43+
Expect(os.RemoveAll(tmpDir)).To(Succeed())
44+
})
45+
46+
When("go.mod exists", func() {
47+
BeforeEach(func() {
48+
// Simulate `go mod edit -json` output by writing a go.mod file and using go commands
49+
Expect(os.WriteFile("go.mod", []byte("module github.com/example/repo\n"), 0o644)).To(Succeed())
50+
})
51+
52+
It("findGoModulePath returns the module path", func() {
53+
path, err := findGoModulePath()
54+
Expect(err).NotTo(HaveOccurred())
55+
Expect(path).To(Equal("github.com/example/repo"))
56+
})
57+
58+
It("FindCurrentRepo returns the module path", func() {
59+
path, err := FindCurrentRepo()
60+
Expect(err).NotTo(HaveOccurred())
61+
Expect(path).To(Equal("github.com/example/repo"))
62+
})
63+
})
64+
65+
When("go.mod does not exist", func() {
66+
It("findGoModulePath returns error", func() {
67+
got, err := findGoModulePath()
68+
Expect(err).To(HaveOccurred())
69+
Expect(got).To(Equal(""))
70+
})
71+
72+
It("FindCurrentRepo tries to init a module and returns the path or a helpful error", func() {
73+
path, err := FindCurrentRepo()
74+
if err != nil {
75+
Expect(path).To(Equal(""))
76+
Expect(err.Error()).To(ContainSubstring("could not determine repository path"))
77+
} else {
78+
Expect(path).NotTo(BeEmpty())
79+
}
80+
})
81+
})
82+
83+
When("go mod command fails with exec.ExitError", func() {
84+
var origPath string
85+
86+
BeforeEach(func() {
87+
// Move go binary out of PATH to force exec error
88+
origPath = os.Getenv("PATH")
89+
// Set PATH to empty so "go" cannot be found
90+
Expect(os.Setenv("PATH", "")).To(Succeed())
91+
})
92+
93+
AfterEach(func() {
94+
Expect(os.Setenv("PATH", origPath)).To(Succeed())
95+
})
96+
97+
It("findGoModulePath returns error with stderr message", func() {
98+
got, err := findGoModulePath()
99+
Expect(err).To(HaveOccurred())
100+
Expect(err.Error()).NotTo(BeEmpty())
101+
Expect(got).To(Equal(""))
102+
})
103+
104+
It("FindCurrentRepo returns error with stderr message", func() {
105+
got, err := FindCurrentRepo()
106+
Expect(err).To(HaveOccurred())
107+
Expect(err.Error()).To(ContainSubstring("could not determine repository path"))
108+
Expect(got).To(Equal(""))
109+
})
110+
})
111+
})

0 commit comments

Comments
 (0)