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