1// go:build unit
2package config
3
4import (
5 "strings"
6 "testing"
7
8 "github.com/google/go-cmp/cmp"
9)
10
11func TestFileParsing(t *testing.T) {
12 testCases := []struct {
13 name string
14 config string
15 expectedConfig *configuration
16 }{
17 {
18 name: "minimal scan",
19 config: `scan "/srv/git"`,
20 expectedConfig: &configuration{
21 Scans: []*scan{
22 {
23 Public: false,
24 Path: "/srv/git",
25 },
26 },
27 ListenAddr: defaultAddr(),
28 Hostname: defaultHostname(),
29 Repositories: []*GitRepositoryConfiguration{},
30 },
31 },
32 {
33 name: "complete scan",
34 config: `
35scan "/srv/git" {
36 public true
37}`,
38 expectedConfig: &configuration{
39 Scans: []*scan{
40 {
41 Public: true,
42 Path: "/srv/git",
43 },
44 },
45 ListenAddr: defaultAddr(),
46 Hostname: defaultHostname(),
47 Repositories: []*GitRepositoryConfiguration{},
48 },
49 },
50 {
51 name: "themes",
52 config: `
53syntax-highlight light dark`,
54 expectedConfig: &configuration{
55 Scans: defaultScans(),
56 ListenAddr: defaultAddr(),
57 Hostname: defaultHostname(),
58 Repositories: []*GitRepositoryConfiguration{},
59 SyntaxHighlight: SyntaxHighlight{
60 Light: "light",
61 Dark: "dark",
62 },
63 },
64 },
65 {
66 name: "minimal repository",
67 config: `repository /srv/git/cerrado.git`,
68 expectedConfig: &configuration{
69 Scans: defaultScans(),
70 ListenAddr: defaultAddr(),
71 Hostname: defaultHostname(),
72 Repositories: []*GitRepositoryConfiguration{
73 {
74 Name: "cerrado.git",
75 Path: "/srv/git/cerrado.git",
76 Description: "",
77 Public: false,
78 About: "README.md",
79 },
80 },
81 },
82 },
83 {
84 name: "complete repository",
85 config: `
86repository /srv/git/cerrado.git {
87 name cerrado
88 description "Single person forge"
89 public true
90 about readme.txt
91}`,
92 expectedConfig: &configuration{
93 Scans: defaultScans(),
94 ListenAddr: defaultAddr(),
95 Hostname: defaultHostname(),
96 Repositories: []*GitRepositoryConfiguration{
97 {
98 Name: "cerrado",
99 Path: "/srv/git/cerrado.git",
100 Description: "Single person forge",
101 Public: true,
102 About: "readme.txt",
103 },
104 },
105 },
106 },
107 {
108 name: "minimal listen",
109 config: ``,
110 expectedConfig: &configuration{
111 Scans: defaultScans(),
112 ListenAddr: defaultAddr(),
113 Hostname: defaultHostname(),
114 Repositories: []*GitRepositoryConfiguration{},
115 },
116 },
117 {
118 name: "complete listen",
119 config: `listen-addr unix://var/run/cerrado/cerrado.sock`,
120 expectedConfig: &configuration{
121 Scans: defaultScans(),
122 Hostname: defaultHostname(),
123 ListenAddr: "unix://var/run/cerrado/cerrado.sock",
124 Repositories: []*GitRepositoryConfiguration{},
125 },
126 },
127 {
128 name: "complete",
129 config: `
130listen-addr unix://var/run/cerrado/cerrado.sock
131passphrase $2a$14$VnB/ZcB1DUDkMnosRA6Y7.dj8h5eroslDxTeXlLwfQX/x86mh6WAq
132aes-key 8XHptZxSWCGs1m7QzztX5zNQ7D9NiQevVX0DaUTNMbDpRwFzoJiB0U7K6O/kqIt01jJVgzBUfiR8ES46ZLLb4w==
133syntax-highlight monokailight
134order-by lastcommit-desc
135remove-suffix true
136hostname https://domain.tld
137
138scan "/srv/git" {
139 public true
140}
141
142repository /srv/git/linux.git
143
144repository /srv/git/cerrado.git {
145 name cerrado
146 description "Single person forge"
147 public true
148 about readme.txt
149}`,
150 expectedConfig: &configuration{
151 Scans: []*scan{
152 {
153 Public: true,
154 Path: "/srv/git",
155 },
156 },
157 ListenAddr: "unix://var/run/cerrado/cerrado.sock",
158 Passphrase: "$2a$14$VnB/ZcB1DUDkMnosRA6Y7.dj8h5eroslDxTeXlLwfQX/x86mh6WAq",
159 AESKey: "8XHptZxSWCGs1m7QzztX5zNQ7D9NiQevVX0DaUTNMbDpRwFzoJiB0U7K6O/kqIt01jJVgzBUfiR8ES46ZLLb4w==",
160 SyntaxHighlight: SyntaxHighlight{
161 Light: "monokailight",
162 Dark: "monokailight",
163 },
164 OrderBy: "lastcommit-desc",
165 RemoveSuffix: true,
166 Hostname: "https://domain.tld",
167 Repositories: []*GitRepositoryConfiguration{
168 {
169 Name: "linux.git",
170 Path: "/srv/git/linux.git",
171 Description: "",
172 Public: false,
173 About: "README.md",
174 },
175 {
176 Name: "cerrado",
177 Path: "/srv/git/cerrado.git",
178 Description: "Single person forge",
179 Public: true,
180 About: "readme.txt",
181 },
182 },
183 },
184 },
185 }
186
187 for _, tc := range testCases {
188 t.Run(tc.name, func(t *testing.T) {
189 r := strings.NewReader(tc.config)
190 config, err := parse(r)
191 if err != nil {
192 t.Fatalf("Error parsing config %s", err.Error())
193 }
194
195 if diff := cmp.Diff(tc.expectedConfig, config); diff != "" {
196 t.Errorf("Wrong result given - wanted + got\n %s", diff)
197 }
198 })
199 }
200}