Debian Codegraph @

tests/thirdparty/config_test.go

raw symbols SCIP
1 package thirdparty
2
3 import (
4 "bytes"
5 "reflect"
6 "strings"
7 "testing"
8 )
9
10 func TestValidateErrors(t *testing.T) {
11 cases := []struct {
12 Name string
13 Item interface{ Validate() error }
14 ErrorSubstring string
15 }{
16 {
17 Name: "step_missing_name",
18 Item: &Step{},
19 ErrorSubstring: "missing name",
20 },
21 {
22 Name: "step_missing_commands",
23 Item: &Step{
24 Name: "Setup",
25 },
26 ErrorSubstring: "missing commands",
27 },
28 {
29 Name: "project_missing_default_branch",
30 Item: &Project{
31 Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
32 },
33 ErrorSubstring: "missing default branch",
34 },
35 {
36 Name: "project_missing_version",
37 Item: &Project{
38 Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
39 DefaultBranch: "main",
40 },
41 ErrorSubstring: "missing version",
42 },
43 {
44 Name: "project_missing_packages",
45 Item: &Project{
46 Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
47 DefaultBranch: "main",
48 Version: "v0.1.1",
49 },
50 ErrorSubstring: "missing packages",
51 },
52 {
53 Name: "project_package_error",
54 Item: &Project{
55 Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
56 DefaultBranch: "main",
57 Version: "v0.1.1",
58 Packages: []*Package{
59 {},
60 },
61 },
62 ErrorSubstring: "package root: missing module",
63 },
64 {
65 Name: "package_missing_module",
66 Item: &Package{},
67 ErrorSubstring: "missing module",
68 },
69 {
70 Name: "package_no_generate_commands",
71 Item: &Package{
72 Module: "avo/go.mod",
73 },
74 ErrorSubstring: "no generate commands",
75 },
76 {
77 Name: "package_invalid_generate_commands",
78 Item: &Package{
79 Module: "avo/go.mod",
80 Generate: []*Step{
81 {},
82 },
83 },
84 ErrorSubstring: "generate step: missing name",
85 },
86 {
87 Name: "projects_invalid_package",
88 Item: Projects{
89 {
90 Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
91 },
92 },
93 ErrorSubstring: "missing default branch",
94 },
95 }
96 for _, c := range cases {
97 c := c // scopelint
98 t.Run(c.Name, func(t *testing.T) {
99 err := c.Item.Validate()
100 if err == nil {
101 t.Fatal("expected error; got nil")
102 }
103 if !strings.Contains(err.Error(), c.ErrorSubstring) {
104 t.Fatalf("expected error message to contain %q; got %q", c.ErrorSubstring, err)
105 }
106 })
107 }
108 }
109
110 func TestLoadSuiteBad(t *testing.T) {
111 r := strings.NewReader(`[{"unknown_field": "value"}]`)
112 _, err := LoadSuite(r)
113 if err == nil {
114 t.Fatal("expected non-nil error")
115 }
116 }
117
118 func TestLoadSuiteFileNotExist(t *testing.T) {
119 s, err := LoadSuiteFile("does_not_exist")
120 if s != nil {
121 t.Fatal("expected nil return")
122 }
123 if err == nil {
124 t.Fatal("expected non-nil error")
125 }
126 }
127
128 func TestSuiteFileValid(t *testing.T) {
129 s, err := LoadSuiteFile("suite.json")
130 if err != nil {
131 t.Fatal(err)
132 }
133 for _, prj := range s.Projects {
134 t.Logf("read: %s", prj.ID())
135 }
136 if len(s.Projects) == 0 {
137 t.Fatal("no packages loaded")
138 }
139 if err := s.Validate(); err != nil {
140 t.Fatal(err)
141 }
142 }
143
144 func TestSuiteFileStepsValid(t *testing.T) {
145 suite, err := LoadSuiteFile("suite.json")
146 if err != nil {
147 t.Fatal(err)
148 }
149 c := &Context{
150 AvoDirectory: "avo",
151 RepositoryDirectory: "repo",
152 }
153 for _, prj := range suite.Projects {
154 for _, pkg := range prj.Packages {
155 for _, s := range pkg.Steps(c) {
156 if err := s.Validate(); err != nil {
157 t.Errorf("project %s: package %s: %s", prj.ID(), pkg.Name(), err)
158 }
159 }
160 }
161 }
162 }
163
164 func TestSuiteFileRoundtrip(t *testing.T) {
165 s, err := LoadSuiteFile("suite.json")
166 if err != nil {
167 t.Fatal(err)
168 }
169
170 // Write and read back.
171 buf := bytes.NewBuffer(nil)
172 if err := StoreSuite(buf, s); err != nil {
173 t.Fatal(err)
174 }
175
176 roundtrip, err := LoadSuite(buf)
177 if err != nil {
178 t.Fatal(err)
179 }
180
181 // Should be identical.
182 if !reflect.DeepEqual(s, roundtrip) {
183 t.Fatal("roundtrip mismatch")
184 }
185 }