Skip to content

Commit bb31df0

Browse files
authored
Merge pull request #4068 from camilamacedo86/sumpl-tests
🌱 pkg/plugin/utils: add tests to cover funcs
2 parents 85cb6a5 + cb5646b commit bb31df0

File tree

1 file changed

+67
-29
lines changed

1 file changed

+67
-29
lines changed

pkg/plugin/util/util_test.go

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
/*
22
Copyright 2022 The Kubernetes Authors.
3+
34
Licensed under the Apache License, Version 2.0 (the "License");
45
you may not use this file except in compliance with the License.
56
You may obtain a copy of the License at
7+
68
http://www.apache.org/licenses/LICENSE-2.0
9+
710
Unless required by applicable law or agreed to in writing, software
811
distributed under the License is distributed on an "AS IS" BASIS,
912
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,42 +24,77 @@ import (
2124
. "github.com/onsi/gomega"
2225
)
2326

24-
var _ = Describe("InsertCode", Ordered, func() {
25-
path := filepath.Join("testdata", "exampleFile.txt")
26-
var content []byte
27+
var _ = Describe("Cover plugin util helpers", func() {
28+
Describe("InsertCode", Ordered, func() {
29+
path := filepath.Join("testdata", "exampleFile.txt")
30+
var content []byte
31+
32+
BeforeAll(func() {
33+
err := os.MkdirAll("testdata", 0755)
34+
Expect(err).NotTo(HaveOccurred())
35+
36+
if _, err := os.Stat(path); os.IsNotExist(err) {
37+
err = os.WriteFile(path, []byte("exampleTarget"), 0644)
38+
Expect(err).NotTo(HaveOccurred())
39+
}
40+
41+
content, err = os.ReadFile(path)
42+
Expect(err).NotTo(HaveOccurred())
43+
})
2744

28-
BeforeAll(func() {
29-
err := os.MkdirAll("testdata", 0755)
30-
Expect(err).NotTo(HaveOccurred())
45+
AfterAll(func() {
46+
err := os.WriteFile(path, content, 0644)
47+
Expect(err).NotTo(HaveOccurred())
3148

32-
if _, err := os.Stat(path); os.IsNotExist(err) {
33-
err = os.WriteFile(path, []byte("exampleTarget"), 0644)
49+
err = os.RemoveAll("testdata")
3450
Expect(err).NotTo(HaveOccurred())
35-
}
51+
})
3652

37-
content, err = os.ReadFile(path)
38-
Expect(err).NotTo(HaveOccurred())
53+
DescribeTable("should not succeed",
54+
func(target string) {
55+
Expect(InsertCode(path, target, "exampleCode")).ShouldNot(Succeed())
56+
},
57+
Entry("target given is not present in file", "randomTarget"),
58+
)
59+
60+
DescribeTable("should succeed",
61+
func(target string) {
62+
Expect(InsertCode(path, target, "exampleCode")).Should(Succeed())
63+
},
64+
Entry("target given is present in file", "exampleTarget"),
65+
)
3966
})
4067

41-
AfterAll(func() {
42-
err := os.WriteFile(path, content, 0644)
43-
Expect(err).NotTo(HaveOccurred())
68+
Describe("RandomSuffix", func() {
69+
It("should return a string with 4 caracteres", func() {
70+
suffix, err := RandomSuffix()
71+
Expect(err).NotTo(HaveOccurred())
72+
Expect(suffix).To(HaveLen(4))
73+
})
4474

45-
err = os.RemoveAll("testdata")
46-
Expect(err).NotTo(HaveOccurred())
75+
It("should return different values when call more than once", func() {
76+
suffix1, _ := RandomSuffix()
77+
suffix2, _ := RandomSuffix()
78+
Expect(suffix1).NotTo(Equal(suffix2))
79+
})
4780
})
4881

49-
DescribeTable("should not succeed",
50-
func(target string) {
51-
Expect(InsertCode(path, target, "exampleCode")).ShouldNot(Succeed())
52-
},
53-
Entry("target given is not present in file", "randomTarget"),
54-
)
55-
56-
DescribeTable("should succeed",
57-
func(target string) {
58-
Expect(InsertCode(path, target, "exampleCode")).Should(Succeed())
59-
},
60-
Entry("target given is present in file", "exampleTarget"),
61-
)
82+
Describe("GetNonEmptyLines", func() {
83+
It("should return non-empty lines", func() {
84+
output := "text1\n\ntext2\ntext3\n\n"
85+
lines := GetNonEmptyLines(output)
86+
Expect(lines).To(Equal([]string{"text1", "text2", "text3"}))
87+
})
88+
89+
It("should return an empty when an empty value is passed", func() {
90+
lines := GetNonEmptyLines("")
91+
Expect(lines).To(BeEmpty())
92+
})
93+
94+
It("should return same string without empty lines", func() {
95+
output := "noemptylines"
96+
lines := GetNonEmptyLines(output)
97+
Expect(lines).To(Equal([]string{"noemptylines"}))
98+
})
99+
})
62100
})

0 commit comments

Comments
 (0)