Skip to content

Commit 6f11522

Browse files
committed
tests: add tests on Configuration
1 parent 4a5e50b commit 6f11522

File tree

2 files changed

+128
-2
lines changed

2 files changed

+128
-2
lines changed

pkg/commands/internal/configuration.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package internal
33
import (
44
"errors"
55
"fmt"
6-
"log"
76
"os"
87
"path/filepath"
98
"strings"
@@ -67,7 +66,7 @@ func (c *Configuration) Validate() error {
6766

6867
abs, err := filepath.Abs(plugin.Path)
6968
if err != nil {
70-
log.Fatal(err)
69+
return err
7170
}
7271

7372
plugin.Path = abs
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package internal
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestConfiguration_Validate(t *testing.T) {
11+
testCases := []struct {
12+
desc string
13+
cfg *Configuration
14+
}{
15+
{
16+
desc: "version",
17+
cfg: &Configuration{
18+
Version: "v1.57.0",
19+
Plugins: []*Plugin{
20+
{
21+
Module: "example.org/foo/bar",
22+
Import: "example.org/foo/bar/test",
23+
Version: "v1.2.3",
24+
},
25+
},
26+
},
27+
},
28+
{
29+
desc: "path",
30+
cfg: &Configuration{
31+
Version: "v1.57.0",
32+
Plugins: []*Plugin{
33+
{
34+
Module: "example.org/foo/bar",
35+
Import: "example.org/foo/bar/test",
36+
Path: "/my/path",
37+
},
38+
},
39+
},
40+
},
41+
}
42+
43+
for _, test := range testCases {
44+
test := test
45+
t.Run(test.desc, func(t *testing.T) {
46+
t.Parallel()
47+
48+
err := test.cfg.Validate()
49+
require.NoError(t, err)
50+
})
51+
}
52+
}
53+
54+
func TestConfiguration_Validate_error(t *testing.T) {
55+
testCases := []struct {
56+
desc string
57+
cfg *Configuration
58+
expected string
59+
}{
60+
{
61+
desc: "missing version",
62+
cfg: &Configuration{},
63+
expected: "root field 'version' is required",
64+
},
65+
{
66+
desc: "no plugins",
67+
cfg: &Configuration{
68+
Version: "v1.57.0",
69+
},
70+
expected: "no plugins defined",
71+
},
72+
{
73+
desc: "missing module",
74+
cfg: &Configuration{
75+
Version: "v1.57.0",
76+
Plugins: []*Plugin{
77+
{
78+
Module: "",
79+
Import: "example.org/foo/bar/test",
80+
Version: "v1.2.3",
81+
Path: "/my/path",
82+
},
83+
},
84+
},
85+
expected: "field 'module' is required",
86+
},
87+
{
88+
desc: "module version and path",
89+
cfg: &Configuration{
90+
Version: "v1.57.0",
91+
Plugins: []*Plugin{
92+
{
93+
Module: "example.org/foo/bar",
94+
Import: "example.org/foo/bar/test",
95+
Version: "v1.2.3",
96+
Path: "/my/path",
97+
},
98+
},
99+
},
100+
expected: "invalid configuration: 'version' and 'path' should not be provided at the same time",
101+
},
102+
{
103+
desc: "no module version and path",
104+
cfg: &Configuration{
105+
Version: "v1.57.0",
106+
Plugins: []*Plugin{
107+
{
108+
Module: "example.org/foo/bar",
109+
Import: "example.org/foo/bar/test",
110+
},
111+
},
112+
},
113+
expected: "missing information: 'version' or 'path' should be provided",
114+
},
115+
}
116+
117+
for _, test := range testCases {
118+
test := test
119+
t.Run(test.desc, func(t *testing.T) {
120+
t.Parallel()
121+
122+
err := test.cfg.Validate()
123+
124+
assert.EqualError(t, err, test.expected)
125+
})
126+
}
127+
}

0 commit comments

Comments
 (0)