cerrado @ 519af559f94aab1bd1446cc01144cbf2640fcc6a

  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					},
 57				},
 58			},
 59		},
 60		{
 61			name: "complete repository",
 62			config: `
 63repository /srv/git/cerrado.git {
 64	name cerrado
 65	description "Single person forge"
 66	public true
 67}`,
 68			expectedConfig: &configuration{
 69				Scan:       defaultScan(),
 70				ListenAddr: defaultAddr(),
 71				Repositories: []*GitRepositoryConfiguration{
 72					{
 73						Name:        "cerrado",
 74						Path:        "/srv/git/cerrado.git",
 75						Description: "Single person forge",
 76						Public:      true,
 77					},
 78				},
 79			},
 80		},
 81		{
 82			name:   "minimal listen",
 83			config: ``,
 84			expectedConfig: &configuration{
 85				Scan:         defaultScan(),
 86				ListenAddr:   defaultAddr(),
 87				Repositories: []*GitRepositoryConfiguration{},
 88			},
 89		},
 90		{
 91			name:   "complete listen",
 92			config: `listen-addr unix://var/run/cerrado/cerrado.sock`,
 93			expectedConfig: &configuration{
 94				Scan:         defaultScan(),
 95				ListenAddr:   "unix://var/run/cerrado/cerrado.sock",
 96				Repositories: []*GitRepositoryConfiguration{},
 97			},
 98		},
 99		{
100			name: "complete",
101			config: `
102listen-addr unix://var/run/cerrado/cerrado.sock
103
104scan "/srv/git" {
105	public true
106}
107
108repository /srv/git/linux.git
109
110repository /srv/git/cerrado.git {
111	name cerrado
112	description "Single person forge"
113	public true
114}`,
115			expectedConfig: &configuration{
116				Scan: &scan{
117					Public: true,
118					Path:   "/srv/git",
119				},
120				ListenAddr: "unix://var/run/cerrado/cerrado.sock",
121				Repositories: []*GitRepositoryConfiguration{
122					{
123						Name:        "linux.git",
124						Path:        "/srv/git/linux.git",
125						Description: "",
126						Public:      false,
127					},
128					{
129						Name:        "cerrado",
130						Path:        "/srv/git/cerrado.git",
131						Description: "Single person forge",
132						Public:      true,
133					},
134				},
135			},
136		},
137	}
138
139	for _, tc := range testCases {
140		t.Run(tc.name, func(t *testing.T) {
141			r := strings.NewReader(tc.config)
142			config, err := parse(r)
143			if err != nil {
144				t.Fatalf("Error parsing config %s", err.Error())
145			}
146
147			if diff := cmp.Diff(tc.expectedConfig, config); diff != "" {
148				t.Errorf("Wrong result given - wanted + got\n %s", diff)
149			}
150		})
151
152	}
153}