1
0
mirror of https://github.com/mgechev/revive.git synced 2025-02-03 13:11:25 +02:00

Refactoring of the tests

This commit is contained in:
mgechev 2018-02-04 15:02:45 -08:00
parent b5fc0eaffc
commit 8187cdf97d
30 changed files with 71 additions and 77 deletions

70
test/golint_test.go Normal file
View File

@ -0,0 +1,70 @@
package test
import (
"flag"
"io/ioutil"
"path"
"regexp"
"testing"
"github.com/mgechev/revive/rule"
"github.com/mgechev/revive/lint"
)
var lintMatch = flag.String("lint.match", "", "restrict fixtures matches to this pattern")
var rules = []lint.Rule{
&rule.VarDeclarationsRule{},
&rule.PackageCommentsRule{},
&rule.DotImportsRule{},
&rule.BlankImportsRule{},
&rule.ExportedRule{},
&rule.NamesRule{},
&rule.ElseRule{},
&rule.IfReturnRule{},
&rule.RangeRule{},
&rule.ErrorfRule{},
&rule.ErrorsRule{},
&rule.ErrorStringsRule{},
&rule.ReceiverNameRule{},
&rule.IncrementDecrementRule{},
&rule.ErrorReturnRule{},
&rule.UnexportedReturnRule{},
&rule.TimeNamesRule{},
&rule.ContextKeyTypeRule{},
&rule.ContextArgumentsRule{},
}
func TestAll(t *testing.T) {
baseDir := "../fixtures/golint/"
rx, err := regexp.Compile(*lintMatch)
if err != nil {
t.Fatalf("Bad -lint.match value %q: %v", *lintMatch, err)
}
fis, err := ioutil.ReadDir(baseDir)
if err != nil {
t.Fatalf("ioutil.ReadDir: %v", err)
}
if len(fis) == 0 {
t.Fatalf("no files in %v", baseDir)
}
for _, fi := range fis {
if !rx.MatchString(fi.Name()) {
continue
}
//t.Logf("Testing %s", fi.Name())
src, err := ioutil.ReadFile(path.Join(baseDir, fi.Name()))
if err != nil {
t.Fatalf("Failed reading %s: %v", fi.Name(), err)
}
err = assertFailures(t, baseDir, fi, src, rules, map[string]lint.RuleConfig{})
if err != nil {
t.Errorf("Linting %s: %v", fi.Name(), err)
continue
}
}
}

View File

@ -3,7 +3,6 @@ package test
import (
"bufio"
"bytes"
"flag"
"fmt"
"go/ast"
"go/parser"
@ -12,42 +11,14 @@ import (
"go/types"
"io/ioutil"
"os"
"path"
"regexp"
"strconv"
"strings"
"testing"
"github.com/mgechev/revive/rule"
"github.com/pkg/errors"
"github.com/mgechev/revive/lint"
"github.com/pkg/errors"
)
var lintMatch = flag.String("lint.match", "", "restrict fixtures matches to this pattern")
var rules = []lint.Rule{
&rule.VarDeclarationsRule{},
&rule.PackageCommentsRule{},
&rule.DotImportsRule{},
&rule.BlankImportsRule{},
&rule.ExportedRule{},
&rule.NamesRule{},
&rule.ElseRule{},
&rule.IfReturnRule{},
&rule.RangeRule{},
&rule.ErrorfRule{},
&rule.ErrorsRule{},
&rule.ErrorStringsRule{},
&rule.ReceiverNameRule{},
&rule.IncrementDecrementRule{},
&rule.ErrorReturnRule{},
&rule.UnexportedReturnRule{},
&rule.TimeNamesRule{},
&rule.ContextKeyTypeRule{},
&rule.ContextArgumentsRule{},
}
func testRule(t *testing.T, filename string, rule lint.Rule, config ...*lint.RuleConfig) {
baseDir := "../fixtures/"
filename = filename + ".go"
@ -70,53 +41,6 @@ func testRule(t *testing.T, filename string, rule lint.Rule, config ...*lint.Rul
assertFailures(t, baseDir, stat, src, []lint.Rule{rule}, c)
}
func TestAll(t *testing.T) {
baseDir := "../fixtures/"
ignoreFiles := map[string]bool{
"cyclomatic.go": true,
"cyclomatic-2.go": true,
"max-public-structs.go": true,
"lint-file-header1.go": true,
"lint-file-header2.go": true,
"lint-file-header3.go": true,
"lint-file-header4.go": true,
"lint-file-header5.go": true,
}
rx, err := regexp.Compile(*lintMatch)
if err != nil {
t.Fatalf("Bad -lint.match value %q: %v", *lintMatch, err)
}
fis, err := ioutil.ReadDir(baseDir)
if err != nil {
t.Fatalf("ioutil.ReadDir: %v", err)
}
if len(fis) == 0 {
t.Fatalf("no files in %v", baseDir)
}
for _, fi := range fis {
if !rx.MatchString(fi.Name()) {
continue
}
if _, ok := ignoreFiles[fi.Name()]; ok {
continue
}
//t.Logf("Testing %s", fi.Name())
src, err := ioutil.ReadFile(path.Join(baseDir, fi.Name()))
if err != nil {
t.Fatalf("Failed reading %s: %v", fi.Name(), err)
}
err = assertFailures(t, baseDir, fi, src, rules, map[string]lint.RuleConfig{})
if err != nil {
t.Errorf("Linting %s: %v", fi.Name(), err)
continue
}
}
}
func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, src []byte, rules []lint.Rule, config map[string]lint.RuleConfig) error {
l := lint.New(func(file string) ([]byte, error) {
return ioutil.ReadFile(baseDir + file)