github.com/pankona/gometalinter@v2.0.11+incompatible/linters_test.go (about) 1 package main 2 3 import ( 4 "reflect" 5 "regexp" 6 "runtime" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestNewLinterWithCustomLinter(t *testing.T) { 14 config := LinterConfig{ 15 Command: "/usr/bin/custom", 16 Pattern: "path", 17 } 18 linter, err := NewLinter("thename", config) 19 require.NoError(t, err) 20 assert.Equal(t, functionName(partitionPathsAsDirectories), functionName(linter.LinterConfig.PartitionStrategy)) 21 assert.Equal(t, "(?m:path)", linter.regex.String()) 22 assert.Equal(t, "thename", linter.Name) 23 assert.Equal(t, config.Command, linter.Command) 24 } 25 26 func TestGetLinterByName(t *testing.T) { 27 config := LinterConfig{ 28 Command: "maligned", 29 Pattern: "path", 30 InstallFrom: "./install/path", 31 PartitionStrategy: partitionPathsAsDirectories, 32 IsFast: true, 33 } 34 overrideConfig := getLinterByName(config.Command, config) 35 assert.Equal(t, config.Command, overrideConfig.Command) 36 assert.Equal(t, config.Pattern, overrideConfig.Pattern) 37 assert.Equal(t, config.InstallFrom, overrideConfig.InstallFrom) 38 assert.Equal(t, functionName(config.PartitionStrategy), functionName(overrideConfig.PartitionStrategy)) 39 assert.Equal(t, config.IsFast, overrideConfig.IsFast) 40 } 41 42 func TestValidateLinters(t *testing.T) { 43 originalConfig := *config 44 defer func() { config = &originalConfig }() 45 46 config = &Config{ 47 Enable: []string{"_dummylinter_"}, 48 } 49 50 err := validateLinters(lintersFromConfig(config), config) 51 require.Error(t, err, "expected unknown linter error for _dummylinter_") 52 53 config = &Config{ 54 Enable: defaultEnabled(), 55 } 56 err = validateLinters(lintersFromConfig(config), config) 57 require.NoError(t, err) 58 } 59 60 func TestLinter_test(t *testing.T) { 61 exampleOutput := `--- FAIL: TestHello (0.00s) 62 other_test.go:11: 63 Error Trace: other_test.go:11 64 Error: Not equal: 65 expected: "This is not" 66 actual : "equal to this" 67 68 Diff: 69 --- Expected 70 +++ Actual 71 @@ -1 +1 @@ 72 -This is not 73 +equal to this 74 Test: TestHello 75 other_test.go:12: this should fail 76 other_test.go:13: fail again 77 other_test.go:14: last fail 78 other_test.go:15: 79 other_test.go:16: 80 require.go:1159: 81 Error Trace: other_test.go:17 82 Error: Should be true 83 Test: TestHello 84 FAIL 85 FAIL test 0.003s` 86 87 pattern := regexp.MustCompile(defaultLinters["test"].Pattern) 88 matches := pattern.FindAllStringSubmatch(exampleOutput, -1) 89 var errors []map[string]string 90 for _, match := range matches { 91 m := make(map[string]string) 92 for i, name := range pattern.SubexpNames() { 93 if i != 0 && name != "" { 94 m[name] = string(match[i]) 95 } 96 } 97 errors = append(errors, m) 98 } 99 100 // Assert expected errors 101 assert.Equal(t, "other_test.go", errors[0]["path"]) 102 assert.Equal(t, "12", errors[0]["line"]) 103 assert.Equal(t, "this should fail", errors[0]["message"]) 104 105 assert.Equal(t, "other_test.go", errors[1]["path"]) 106 assert.Equal(t, "13", errors[1]["line"]) 107 assert.Equal(t, "fail again", errors[1]["message"]) 108 109 assert.Equal(t, "other_test.go", errors[2]["path"]) 110 assert.Equal(t, "14", errors[2]["line"]) 111 assert.Equal(t, "last fail", errors[2]["message"]) 112 113 assert.Equal(t, "other_test.go", errors[3]["path"]) 114 assert.Equal(t, "15", errors[3]["line"]) 115 assert.Equal(t, " ", errors[3]["message"]) 116 117 // Go metalinter does not support errors without a message as there is little or no output to parse 118 // E.g. t.Fail() or t.Error("") 119 // assert.Equal(t, "other_test.go", errors[5]["path"]) 120 // assert.Equal(t, "15", errors[5]["line"]) 121 // assert.Equal(t, "", errors[5]["message"]) 122 } 123 124 func TestLinter_testify(t *testing.T) { 125 exampleOutput := `--- FAIL: TestHello (0.00s) 126 other_test.go:11: 127 Error Trace: other_test.go:11 128 Error: Not equal: 129 expected: "This is not" 130 actual : "equal to this" 131 132 Diff: 133 --- Expected 134 +++ Actual 135 @@ -1 +1 @@ 136 -This is not 137 +equal to this 138 Test: TestHello 139 other_test.go:12: this should fail 140 other_test.go:13: fail again 141 other_test.go:14: last fail 142 other_test.go:15: 143 other_test.go:16: 144 require.go:1159: 145 Error Trace: other_test.go:17 146 Error: Should be true 147 Test: TestHello 148 FAIL 149 FAIL test 0.003s` 150 151 pattern := regexp.MustCompile(defaultLinters["testify"].Pattern) 152 matches := pattern.FindAllStringSubmatch(exampleOutput, -1) 153 var errors []map[string]string 154 for _, match := range matches { 155 m := make(map[string]string) 156 for i, name := range pattern.SubexpNames() { 157 if i != 0 && name != "" { 158 m[name] = string(match[i]) 159 } 160 } 161 errors = append(errors, m) 162 } 163 164 // Assert expected errors 165 assert.Equal(t, "other_test.go", errors[0]["path"]) 166 assert.Equal(t, "11", errors[0]["line"]) 167 assert.Equal(t, "Not equal", errors[0]["message"]) 168 169 assert.Equal(t, "other_test.go", errors[1]["path"]) 170 assert.Equal(t, "17", errors[1]["line"]) 171 assert.Equal(t, "Should be true", errors[1]["message"]) 172 } 173 174 func functionName(i interface{}) string { 175 return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() 176 }