cerrado @ 1b1460c8d4fa358433c51fd5293fd1c79f32aeff

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