New ๐ŸŒŠ wave for @helm

Commit 777d31d7

777d31d7f05dMerged

Svyatoslav Dumchenko <[email protected]> ยท 4 years ago ยท 2 files ยท ~2 modified

Description

Add more tests

Details

Commit
777d31d7f05d
Parent
c486a810e655

pkg/template/extra_test.gomodifiedView file

@@ -5,6 +5,7 @@
55 import (
66 "os"
77 "path/filepath"
8+ "strings"
89 "testing"
910
1011 "github.com/stretchr/testify/suite"
@@ -29,20 +30,138 @@
2930 s.Require().YAMLEq(yamlData, y)
3031 }
3132
33+type raw struct{}
34+
35+func (r raw) MarshalYAML() (interface{}, error) {
36+ return nil, os.ErrNotExist
37+}
38+
39+func (s *ExtraTestSuite) TestToYamlNil() {
40+ data := raw{}
41+ y, err := ToYaml(data)
42+ s.Require().Equal("", y)
43+ s.Require().ErrorIs(err, os.ErrNotExist)
44+}
45+
3246 func (s *ExtraTestSuite) TestFromYaml() {
33- s.T().Skip("not implemented")
47+ tests := []struct {
48+ yaml string
49+ result Values
50+ fails bool
51+ }{
52+ {
53+ yaml: "abc: 123",
54+ result: Values{"abc": 123},
55+ fails: false,
56+ },
57+ {
58+ yaml: "123!!123",
59+ fails: true,
60+ },
61+ }
62+
63+ for _, test := range tests {
64+ v, err := FromYaml(test.yaml)
65+ if test.fails {
66+ s.Require().Error(err)
67+ s.Require().Empty(v)
68+ } else {
69+ s.Require().NoError(err)
70+ s.Require().Equal(test.result, v)
71+ }
72+ }
3473 }
3574
3675 func (s *ExtraTestSuite) TestExec() {
37- s.T().Skip("not implemented")
76+ res, err := Exec("pwd", []interface{}{})
77+ s.Require().NoError(err)
78+
79+ pwd, err := os.Getwd()
80+ s.Require().NoError(err)
81+
82+ s.Require().Equal(pwd, strings.TrimSpace(res))
83+}
84+
85+func (s *ExtraTestSuite) TestExecInvalidArg() {
86+ res, err := Exec("pwd", []interface{}{123})
87+ s.Require().Error(err)
88+ s.Require().Empty(res)
3889 }
3990
4091 func (s *ExtraTestSuite) TestSetValueAtPath() {
41- s.T().Skip("not implemented")
92+ data := Values{
93+ "a": map[string]interface{}{
94+ "b": "123",
95+ },
96+ "c": 123,
97+ }
98+
99+ tests := []struct {
100+ path string
101+ value interface{}
102+ result Values
103+ fails bool
104+ }{
105+ {
106+ path: "c",
107+ value: 321,
108+ result: Values{
109+ "a": map[string]interface{}{"b": "123"},
110+ "c": 321,
111+ },
112+ fails: false,
113+ },
114+ {
115+ path: "a.b",
116+ value: "321",
117+ result: Values{
118+ "a": map[string]interface{}{"b": "321"},
119+ "c": 321,
120+ },
121+ fails: false,
122+ },
123+ {
124+ path: "a.c",
125+ value: "321",
126+ result: Values{
127+ "a": map[string]interface{}{"b": "321", "c": "321"},
128+ "c": 321,
129+ },
130+ fails: false,
131+ },
132+ {
133+ path: "c.a",
134+ value: "321",
135+ result: nil,
136+ fails: true,
137+ },
138+ }
139+
140+ for _, test := range tests {
141+ res, err := SetValueAtPath(test.path, test.value, data)
142+ if test.fails {
143+ s.Require().Error(err)
144+ s.Require().Nil(res)
145+ } else {
146+ s.Require().NoError(err)
147+ s.Require().Equal(test.result, res)
148+ }
149+ }
42150 }
43151
44152 func (s *ExtraTestSuite) TestRequiredEnv() {
45- s.T().Skip("not implemented")
153+ name := s.T().Name()
154+
155+ res, err := RequiredEnv(name)
156+ s.Require().Error(err)
157+ s.Require().Empty(res)
158+
159+ data := "test"
160+ s.T().Setenv(name, data)
161+
162+ res, err = RequiredEnv(name)
163+ s.Require().NoError(err)
164+ s.Require().Equal(data, res)
46165 }
47166
48167 func (s *ExtraTestSuite) TestRequired() {
@@ -101,11 +220,97 @@
101220 }
102221
103222 func (s *ExtraTestSuite) TestGet() {
104- s.T().Skip("not implemented")
223+ data := Values{
224+ "a": map[string]interface{}{
225+ "b": "123",
226+ },
227+ "c": 123,
228+ }
229+
230+ tests := []struct {
231+ path string
232+ result interface{}
233+ fails bool
234+ }{
235+ {
236+ path: "c",
237+ result: 123,
238+ fails: false,
239+ },
240+ {
241+ path: "a.b",
242+ result: "123",
243+ fails: false,
244+ },
245+ {
246+ path: "a.c",
247+ result: nil,
248+ fails: true,
249+ },
250+ {
251+ path: "c.a",
252+ result: nil,
253+ fails: true,
254+ },
255+ }
256+
257+ for _, test := range tests {
258+ res, err := Get(test.path, data)
259+ if test.fails {
260+ s.Require().Error(err)
261+ s.Require().Nil(res)
262+ } else {
263+ s.Require().NoError(err)
264+ s.Require().Equal(test.result, res)
265+ }
266+ }
105267 }
106268
107269 func (s *ExtraTestSuite) TestHasKey() {
108- s.T().Skip("not implemented")
270+ data := Values{
271+ "a": map[string]interface{}{
272+ "b": "123",
273+ },
274+ "c": 123,
275+ }
276+
277+ tests := []struct {
278+ path string
279+ result bool
280+ fails bool
281+ }{
282+ {
283+ path: "c",
284+ result: true,
285+ fails: false,
286+ },
287+ {
288+ path: "a.b",
289+ result: true,
290+ fails: false,
291+ },
292+ {
293+ path: "a.c",
294+ result: false,
295+ fails: false,
296+ },
297+ {
298+ path: "c.a",
299+ result: false,
300+ fails: true,
301+ },
302+ }
303+
304+ for _, test := range tests {
305+ res, err := HasKey(test.path, data)
306+ s.Require().Equal(test.result, res)
307+
308+ if test.fails {
309+ s.Require().Error(err)
310+ } else {
311+ s.Require().NoError(err)
312+ }
313+ }
109314 }
110315
111316 func TestExtraTestSuite(t *testing.T) {