Skip to content

Commit 8ececbd

Browse files
Added tests for init.go
1 parent 88afbd6 commit 8ececbd

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

pkg/quarkus/v1alpha/init_test.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright 2021 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package v1
16+
17+
import (
18+
"os"
19+
"path/filepath"
20+
"strings"
21+
22+
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/gomega"
24+
"github.com/spf13/pflag"
25+
26+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
27+
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
28+
)
29+
30+
var _ = Describe("v1", func() {
31+
var (
32+
successInitSubcommand initSubcommand
33+
failureInitSubcommand initSubcommand
34+
)
35+
36+
BeforeEach(func() {
37+
failureInitSubcommand = initSubcommand{
38+
domain: "testDomain",
39+
}
40+
41+
failureInitSubcommand = initSubcommand{
42+
domain: "testDomain",
43+
projectName: "?&fail&?",
44+
commandName: "failureTest",
45+
}
46+
})
47+
48+
Describe("UpdateMetadata", func() {
49+
testCliMetadata := plugin.CLIMetadata{CommandName: "TestCommand"}
50+
testSubcommandMetadata := plugin.SubcommandMetadata{}
51+
52+
It("Check command name inequality pre function call", func() {
53+
Expect(failureInitSubcommand.commandName).NotTo(Equal(testCliMetadata.CommandName))
54+
})
55+
56+
It("Check command name equality post function call", func() {
57+
successInitSubcommand.UpdateMetadata(testCliMetadata, &testSubcommandMetadata)
58+
Expect(successInitSubcommand.commandName, testCliMetadata.CommandName)
59+
})
60+
})
61+
62+
Describe("BindFlags", func() {
63+
flagTest := pflag.NewFlagSet("testFlag", -1)
64+
successInitSubcommand.BindFlags(flagTest)
65+
66+
It("should set SortFlags to false", func() {
67+
Expect(flagTest.SortFlags).To(BeFalse())
68+
})
69+
70+
It("should set domain to my.domain", func() {
71+
Expect(successInitSubcommand.domain, "my.domain")
72+
})
73+
74+
It("should set projectName to an empty string", func() {
75+
Expect(successInitSubcommand.projectName, "")
76+
})
77+
78+
It("should set group to an empty string", func() {
79+
Expect(successInitSubcommand.group, "")
80+
})
81+
82+
It("should set version to an empty string", func() {
83+
Expect(successInitSubcommand.version, "")
84+
})
85+
86+
It("should set kind to an empty string", func() {
87+
Expect(successInitSubcommand.kind, "")
88+
})
89+
})
90+
91+
Describe("InjectConfig", func() {
92+
testConfig, _ := config.New(config.Version{Number: 3})
93+
dir, _ := os.Getwd()
94+
95+
It("should error due to project name", func() {
96+
Expect(failureInitSubcommand.InjectConfig(testConfig)).To(HaveOccurred())
97+
})
98+
99+
successInitSubcommand.InjectConfig(testConfig)
100+
101+
It("config equality check", func() {
102+
Expect(successInitSubcommand.config, testConfig)
103+
})
104+
105+
It("check that the domain was set to the config's domain", func() {
106+
Expect(successInitSubcommand.domain, testConfig.GetDomain())
107+
})
108+
109+
It("check that project name was set to the directory name", func() {
110+
Expect(successInitSubcommand.projectName, strings.ToLower(filepath.Base(dir)))
111+
})
112+
113+
It("check that the project name was set to the config's project name", func() {
114+
Expect(successInitSubcommand.projectName, testConfig.GetProjectName())
115+
})
116+
117+
It("error should be nil", func() {
118+
Expect(successInitSubcommand.InjectConfig(testConfig)).To(BeNil())
119+
})
120+
})
121+
122+
Describe("Validate", func() {
123+
It("should return nil", func() {
124+
Expect(successInitSubcommand.Validate()).To(BeNil())
125+
})
126+
})
127+
128+
Describe("PostScaffold", func() {
129+
It("should return nil", func() {
130+
Expect(successInitSubcommand.PostScaffold()).To(BeNil())
131+
})
132+
})
133+
})

pkg/quarkus/v1alpha/v1_suite_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2021 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package v1
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
func TestUtil(t *testing.T) {
25+
RegisterFailHandler(Fail)
26+
RunSpecs(t, "v1")
27+
}

0 commit comments

Comments
 (0)