cerrado @ 78af329f2c7bc1739bcd36baf45ab95aaff43434

  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==
108
109scan "/srv/git" {
110	public true
111}
112
113repository /srv/git/linux.git
114
115repository /srv/git/cerrado.git {
116	name cerrado
117	description "Single person forge"
118	public true
119	about readme.txt
120}`,
121			expectedConfig: &configuration{
122				Scan: &scan{
123					Public: true,
124					Path:   "/srv/git",
125				},
126				ListenAddr: "unix://var/run/cerrado/cerrado.sock",
127				Passphrase: "$2a$14$VnB/ZcB1DUDkMnosRA6Y7.dj8h5eroslDxTeXlLwfQX/x86mh6WAq",
128				AESKey:     "8XHptZxSWCGs1m7QzztX5zNQ7D9NiQevVX0DaUTNMbDpRwFzoJiB0U7K6O/kqIt01jJVgzBUfiR8ES46ZLLb4w==",
129				Repositories: []*GitRepositoryConfiguration{
130					{
131						Name:        "linux.git",
132						Path:        "/srv/git/linux.git",
133						Description: "",
134						Public:      false,
135						About:       "README.md",
136					},
137					{
138						Name:        "cerrado",
139						Path:        "/srv/git/cerrado.git",
140						Description: "Single person forge",
141						Public:      true,
142						About:       "readme.txt",
143					},
144				},
145			},
146		},
147	}
148
149	for _, tc := range testCases {
150		t.Run(tc.name, func(t *testing.T) {
151			r := strings.NewReader(tc.config)
152			config, err := parse(r)
153			if err != nil {
154				t.Fatalf("Error parsing config %s", err.Error())
155			}
156
157			if diff := cmp.Diff(tc.expectedConfig, config); diff != "" {
158				t.Errorf("Wrong result given - wanted + got\n %s", diff)
159			}
160		})
161
162	}
163}