cerrado @ 2fcc394c53f995750b52ad06153041f61f0a0c55

  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				Repositories: []*GitRepositoryConfiguration{},
 26			},
 27		},
 28		{
 29			name: "complete scan",
 30			config: `
 31scan "/srv/git" {
 32	public true
 33}`,
 34			expectedConfig: &configuration{
 35				Scan: &scan{
 36					Public: true,
 37					Path:   "/srv/git",
 38				},
 39				Repositories: []*GitRepositoryConfiguration{},
 40			},
 41		},
 42		{
 43			name:   "minimal repository",
 44			config: `repository /srv/git/cerrado.git`,
 45			expectedConfig: &configuration{
 46				Scan: defaultScan(),
 47				Repositories: []*GitRepositoryConfiguration{
 48					{
 49						Name:        "cerrado.git",
 50						Path:        "/srv/git/cerrado.git",
 51						Description: "",
 52						Public:      false,
 53					},
 54				},
 55			},
 56		},
 57		{
 58			name: "complete repository",
 59			config: `
 60repository /srv/git/cerrado.git {
 61	name cerrado
 62	description "Single person forge"
 63	public true
 64}`,
 65			expectedConfig: &configuration{
 66				Scan: defaultScan(),
 67				Repositories: []*GitRepositoryConfiguration{
 68					{
 69						Name:        "cerrado",
 70						Path:        "/srv/git/cerrado.git",
 71						Description: "Single person forge",
 72						Public:      true,
 73					},
 74				},
 75			},
 76		},
 77		{
 78			name: "complete",
 79			config: `
 80scan "/srv/git" {
 81	public true
 82}
 83
 84repository /srv/git/linux.git
 85
 86repository /srv/git/cerrado.git {
 87	name cerrado
 88	description "Single person forge"
 89	public true
 90}`,
 91			expectedConfig: &configuration{
 92				Scan: &scan{
 93					Public: true,
 94					Path:   "/srv/git",
 95				},
 96				Repositories: []*GitRepositoryConfiguration{
 97					{
 98						Name:        "linux.git",
 99						Path:        "/srv/git/linux.git",
100						Description: "",
101						Public:      false,
102					},
103					{
104						Name:        "cerrado",
105						Path:        "/srv/git/cerrado.git",
106						Description: "Single person forge",
107						Public:      true,
108					},
109				},
110			},
111		},
112	}
113
114	for _, tc := range testCases {
115		t.Run(tc.name, func(t *testing.T) {
116			r := strings.NewReader(tc.config)
117			config, err := parse(r)
118			if err != nil {
119				t.Fatalf("Error parsing config %s", err.Error())
120			}
121
122			if diff := cmp.Diff(tc.expectedConfig, config); diff != "" {
123				t.Errorf("Wrong result given - wanted + got\n %s", diff)
124			}
125		})
126
127	}
128}