1package config
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/suite"
7)
8
9type ModulesSuite struct {
10 suite.Suite
11}
12
13func TestModulesSuite(t *testing.T) {
14 suite.Run(t, new(ModulesSuite))
15}
16
17func (s *ModulesSuite) TestValidateMissingURL() {
18 m := &Submodule{Path: "foo"}
19 s.Equal(ErrModuleEmptyURL, m.Validate())
20}
21
22func (s *ModulesSuite) TestValidateBadPath() {
23 input := []string{
24 `..`,
25 `../`,
26 `../bar`,
27
28 `/..`,
29 `/../bar`,
30
31 `foo/..`,
32 `foo/../`,
33 `foo/../bar`,
34 }
35
36 for _, p := range input {
37 m := &Submodule{
38 Path: p,
39 URL: "https://example.com/",
40 }
41 s.Equal(ErrModuleBadPath, m.Validate())
42 }
43}
44
45func (s *ModulesSuite) TestValidateMissingName() {
46 m := &Submodule{URL: "bar"}
47 s.Equal(ErrModuleEmptyPath, m.Validate())
48}
49
50func (s *ModulesSuite) TestMarshal() {
51 input := []byte(`[submodule "qux"]
52 path = qux
53 url = baz
54 branch = bar
55`)
56
57 cfg := NewModules()
58 cfg.Submodules["qux"] = &Submodule{Path: "qux", URL: "baz", Branch: "bar"}
59
60 output, err := cfg.Marshal()
61 s.NoError(err)
62 s.Equal(input, output)
63}
64
65func (s *ModulesSuite) TestUnmarshal() {
66 input := []byte(`[submodule "qux"]
67 path = qux
68 url = https://github.com/foo/qux.git
69[submodule "foo/bar"]
70 path = foo/bar
71 url = https://github.com/foo/bar.git
72 branch = dev
73[submodule "suspicious"]
74 path = ../../foo/bar
75 url = https://github.com/foo/bar.git
76`)
77
78 cfg := NewModules()
79 err := cfg.Unmarshal(input)
80 s.NoError(err)
81
82 s.Len(cfg.Submodules, 2)
83 s.Equal("qux", cfg.Submodules["qux"].Name)
84 s.Equal("https://github.com/foo/qux.git", cfg.Submodules["qux"].URL)
85 s.Equal("foo/bar", cfg.Submodules["foo/bar"].Name)
86 s.Equal("https://github.com/foo/bar.git", cfg.Submodules["foo/bar"].URL)
87 s.Equal("dev", cfg.Submodules["foo/bar"].Branch)
88}
89
90func (s *ModulesSuite) TestUnmarshalMarshal() {
91 input := []byte(`[submodule "foo/bar"]
92 path = foo/bar
93 url = https://github.com/foo/bar.git
94 ignore = all
95`)
96
97 cfg := NewModules()
98 err := cfg.Unmarshal(input)
99 s.NoError(err)
100
101 output, err := cfg.Marshal()
102 s.NoError(err)
103 s.Equal(string(input), string(output))
104}