1
1
/*
2
2
Copyright 2022 The Kubernetes Authors.
3
+
3
4
Licensed under the Apache License, Version 2.0 (the "License");
4
5
you may not use this file except in compliance with the License.
5
6
You may obtain a copy of the License at
7
+
6
8
http://www.apache.org/licenses/LICENSE-2.0
9
+
7
10
Unless required by applicable law or agreed to in writing, software
8
11
distributed under the License is distributed on an "AS IS" BASIS,
9
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,42 +24,77 @@ import (
21
24
. "github.com/onsi/gomega"
22
25
)
23
26
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
+ })
27
44
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 ())
31
48
32
- if _ , err := os .Stat (path ); os .IsNotExist (err ) {
33
- err = os .WriteFile (path , []byte ("exampleTarget" ), 0644 )
49
+ err = os .RemoveAll ("testdata" )
34
50
Expect (err ).NotTo (HaveOccurred ())
35
- }
51
+ })
36
52
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
+ )
39
66
})
40
67
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
+ })
44
74
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
+ })
47
80
})
48
81
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 \n text2\n text3\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
+ })
62
100
})
0 commit comments