github.com/Johnny2210/revive@v1.0.8-0.20210625134200-febf37ccd0f5/config/config_test.go (about) 1 package config 2 3 import ( 4 "reflect" 5 "strings" 6 "testing" 7 8 "github.com/mgechev/revive/lint" 9 ) 10 11 func TestGetConfig(t *testing.T) { 12 tt := map[string]struct { 13 confPath string 14 wantConfig *lint.Config 15 wantError string 16 }{ 17 "non-reg issue #470": { 18 confPath: "testdata/issue-470.toml", 19 wantError: "", 20 }, 21 "unknown file": { 22 confPath: "unknown", 23 wantError: "cannot read the config file", 24 }, 25 "malformed file": { 26 confPath: "testdata/malformed.toml", 27 wantError: "cannot parse the config file", 28 }, 29 "default config": { 30 wantConfig: defaultConfig(), 31 }, 32 } 33 34 for name, tc := range tt { 35 t.Run(name, func(t *testing.T) { 36 cfg, err := GetConfig(tc.confPath) 37 switch { 38 case err != nil && tc.wantError == "": 39 t.Fatalf("Unexpected error\n\t%v", err) 40 case err != nil && !strings.Contains(err.Error(), tc.wantError): 41 t.Fatalf("Expected error\n\t%q\ngot:\n\t%v", tc.wantError, err) 42 case tc.wantConfig != nil && reflect.DeepEqual(cfg, tc.wantConfig): 43 t.Fatalf("Expected config\n\t%+v\ngot:\n\t%+v", tc.wantConfig, cfg) 44 } 45 46 }) 47 } 48 }