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 Repositories: []*GitRepositoryConfiguration{},
29 },
30 },
31 {
32 name: "complete scan",
33 config: `
34scan "/srv/git" {
35 public true
36}`,
37 expectedConfig: &configuration{
38 Scans: []*scan{
39 {
40 Public: true,
41 Path: "/srv/git",
42 },
43 },
44 ListenAddr: defaultAddr(),
45 Repositories: []*GitRepositoryConfiguration{},
46 },
47 },
48 {
49 name: "minimal repository",
50 config: `repository /srv/git/cerrado.git`,
51 expectedConfig: &configuration{
52 Scans: defaultScans(),
53 ListenAddr: defaultAddr(),
54 Repositories: []*GitRepositoryConfiguration{
55 {
56 Name: "cerrado.git",
57 Path: "/srv/git/cerrado.git",
58 Description: "",
59 Public: false,
60 About: "README.md",
61 },
62 },
63 },
64 },
65 {
66 name: "complete repository",
67 config: `
68repository /srv/git/cerrado.git {
69 name cerrado
70 description "Single person forge"
71 public true
72 about readme.txt
73}`,
74 expectedConfig: &configuration{
75 Scans: defaultScans(),
76 ListenAddr: defaultAddr(),
77 Repositories: []*GitRepositoryConfiguration{
78 {
79 Name: "cerrado",
80 Path: "/srv/git/cerrado.git",
81 Description: "Single person forge",
82 Public: true,
83 About: "readme.txt",
84 },
85 },
86 },
87 },
88 {
89 name: "minimal listen",
90 config: ``,
91 expectedConfig: &configuration{
92 Scans: defaultScans(),
93 ListenAddr: defaultAddr(),
94 Repositories: []*GitRepositoryConfiguration{},
95 },
96 },
97 {
98 name: "complete listen",
99 config: `listen-addr unix://var/run/cerrado/cerrado.sock`,
100 expectedConfig: &configuration{
101 Scans: defaultScans(),
102 ListenAddr: "unix://var/run/cerrado/cerrado.sock",
103 Repositories: []*GitRepositoryConfiguration{},
104 },
105 },
106 {
107 name: "complete",
108 config: `
109listen-addr unix://var/run/cerrado/cerrado.sock
110passphrase $2a$14$VnB/ZcB1DUDkMnosRA6Y7.dj8h5eroslDxTeXlLwfQX/x86mh6WAq
111aes-key 8XHptZxSWCGs1m7QzztX5zNQ7D9NiQevVX0DaUTNMbDpRwFzoJiB0U7K6O/kqIt01jJVgzBUfiR8ES46ZLLb4w==
112syntax-highlight monokailight
113
114scan "/srv/git" {
115 public true
116}
117
118repository /srv/git/linux.git
119
120repository /srv/git/cerrado.git {
121 name cerrado
122 description "Single person forge"
123 public true
124 about readme.txt
125}`,
126 expectedConfig: &configuration{
127 Scans: []*scan{
128 {
129 Public: true,
130 Path: "/srv/git",
131 },
132 },
133 ListenAddr: "unix://var/run/cerrado/cerrado.sock",
134 Passphrase: "$2a$14$VnB/ZcB1DUDkMnosRA6Y7.dj8h5eroslDxTeXlLwfQX/x86mh6WAq",
135 AESKey: "8XHptZxSWCGs1m7QzztX5zNQ7D9NiQevVX0DaUTNMbDpRwFzoJiB0U7K6O/kqIt01jJVgzBUfiR8ES46ZLLb4w==",
136 SyntaxHighlight: "monokailight",
137 Repositories: []*GitRepositoryConfiguration{
138 {
139 Name: "linux.git",
140 Path: "/srv/git/linux.git",
141 Description: "",
142 Public: false,
143 About: "README.md",
144 },
145 {
146 Name: "cerrado",
147 Path: "/srv/git/cerrado.git",
148 Description: "Single person forge",
149 Public: true,
150 About: "readme.txt",
151 },
152 },
153 },
154 },
155 }
156
157 for _, tc := range testCases {
158 t.Run(tc.name, func(t *testing.T) {
159 r := strings.NewReader(tc.config)
160 config, err := parse(r)
161 if err != nil {
162 t.Fatalf("Error parsing config %s", err.Error())
163 }
164
165 if diff := cmp.Diff(tc.expectedConfig, config); diff != "" {
166 t.Errorf("Wrong result given - wanted + got\n %s", diff)
167 }
168 })
169 }
170}