cerrado @ cb6060a60d71ce1be1591bb10f499916155160de

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