Skip to content

Commit

Permalink
enable linter: revive (superfluous-else) (#3082)
Browse files Browse the repository at this point in the history
* enable linter: revive (superfluous-else)

* lint (whitespace)
  • Loading branch information
mmetc authored Jun 13, 2024
1 parent 2f6d4cc commit e6ebf7a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 26 deletions.
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ linters-settings:
disabled: true
- name: struct-tag
disabled: true
- name: superfluous-else
disabled: true
- name: time-equal
disabled: true
- name: var-naming
Expand Down
87 changes: 63 additions & 24 deletions pkg/parser/parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestParser(t *testing.T) {
debug = true

log.SetLevel(log.InfoLevel)

envSetting := os.Getenv("TEST_ONLY")

pctx, ectx, err := prepTests()
Expand Down Expand Up @@ -208,6 +209,7 @@ func loadTestFile(file string) []TestFile {

dec := yaml.NewDecoder(yamlFile)
dec.SetStrict(true)

var testSet []TestFile

for {
Expand All @@ -232,70 +234,84 @@ func loadTestFile(file string) []TestFile {

func matchEvent(expected types.Event, out types.Event, debug bool) ([]string, bool) {
var retInfo []string
var valid = false

valid := false
expectMaps := []map[string]string{expected.Parsed, expected.Meta, expected.Enriched}
outMaps := []map[string]string{out.Parsed, out.Meta, out.Enriched}
outLabels := []string{"Parsed", "Meta", "Enriched"}

//allow to check as well for stage and processed flags
// allow to check as well for stage and processed flags
if expected.Stage != "" {
if expected.Stage != out.Stage {
if debug {
retInfo = append(retInfo, fmt.Sprintf("mismatch stage %s != %s", expected.Stage, out.Stage))
}

goto checkFinished
} else {
valid = true
if debug {
retInfo = append(retInfo, fmt.Sprintf("ok stage %s == %s", expected.Stage, out.Stage))
}
}

valid = true

if debug {
retInfo = append(retInfo, fmt.Sprintf("ok stage %s == %s", expected.Stage, out.Stage))
}
}

if expected.Process != out.Process {
if debug {
retInfo = append(retInfo, fmt.Sprintf("mismatch process %t != %t", expected.Process, out.Process))
}

goto checkFinished
} else {
valid = true
if debug {
retInfo = append(retInfo, fmt.Sprintf("ok process %t == %t", expected.Process, out.Process))
}
}

valid = true

if debug {
retInfo = append(retInfo, fmt.Sprintf("ok process %t == %t", expected.Process, out.Process))
}

if expected.Whitelisted != out.Whitelisted {
if debug {
retInfo = append(retInfo, fmt.Sprintf("mismatch whitelist %t != %t", expected.Whitelisted, out.Whitelisted))
}

goto checkFinished
} else {
if debug {
retInfo = append(retInfo, fmt.Sprintf("ok whitelist %t == %t", expected.Whitelisted, out.Whitelisted))
}
valid = true
}

if debug {
retInfo = append(retInfo, fmt.Sprintf("ok whitelist %t == %t", expected.Whitelisted, out.Whitelisted))
}

valid = true

for mapIdx := 0; mapIdx < len(expectMaps); mapIdx++ {
for expKey, expVal := range expectMaps[mapIdx] {
outVal, ok := outMaps[mapIdx][expKey]
if !ok {
if debug {
retInfo = append(retInfo, fmt.Sprintf("missing entry %s[%s]", outLabels[mapIdx], expKey))
}

valid = false

goto checkFinished
}
if outVal != expVal { //ok entry

if outVal != expVal { // ok entry
if debug {
retInfo = append(retInfo, fmt.Sprintf("mismatch %s[%s] %s != %s", outLabels[mapIdx], expKey, expVal, outVal))
}

valid = false

goto checkFinished
}

if debug {
retInfo = append(retInfo, fmt.Sprintf("ok %s[%s] %s == %s", outLabels[mapIdx], expKey, expVal, outVal))
}

valid = true
}
}
Expand All @@ -309,6 +325,7 @@ checkFinished:
retInfo = append(retInfo, fmt.Sprintf("KO ! \n\t%s", strings.Join(retInfo, "\n\t")))
}
}

return retInfo, valid
}

Expand All @@ -320,9 +337,10 @@ func testSubSet(testSet TestFile, pctx UnixParserCtx, nodes []Node) (bool, error
if err != nil {
log.Errorf("Failed to process %s : %v", spew.Sdump(in), err)
}
//log.Infof("Parser output : %s", spew.Sdump(out))
// log.Infof("Parser output : %s", spew.Sdump(out))
results = append(results, out)
}

log.Infof("parsed %d lines", len(testSet.Lines))
log.Infof("got %d results", len(results))

Expand All @@ -336,50 +354,58 @@ func testSubSet(testSet TestFile, pctx UnixParserCtx, nodes []Node) (bool, error

reCheck:
failinfo := []string{}

for ridx, result := range results {
for eidx, expected := range testSet.Results {
explain, match := matchEvent(expected, result, debug)
if match {
log.Infof("expected %d/%d matches result %d/%d", eidx, len(testSet.Results), ridx, len(results))

if len(explain) > 0 {
log.Printf("-> %s", explain[len(explain)-1])
}
//don't do this at home : delete current element from list and redo
// don't do this at home : delete current element from list and redo
results[len(results)-1], results[ridx] = results[ridx], results[len(results)-1]
results = results[:len(results)-1]

testSet.Results[len(testSet.Results)-1], testSet.Results[eidx] = testSet.Results[eidx], testSet.Results[len(testSet.Results)-1]
testSet.Results = testSet.Results[:len(testSet.Results)-1]

goto reCheck
} else {
failinfo = append(failinfo, explain...)
}

failinfo = append(failinfo, explain...)
}
}

if len(results) > 0 {
log.Printf("Errors : %s", strings.Join(failinfo, " / "))
return false, fmt.Errorf("leftover results : %+v", results)
}

if len(testSet.Results) > 0 {
log.Printf("Errors : %s", strings.Join(failinfo, " / "))
return false, fmt.Errorf("leftover expected results : %+v", testSet.Results)
}

return true, nil
}

func testFile(testSet []TestFile, pctx UnixParserCtx, nodes []Node) bool {
log.Warning("Going to process one test set")

for _, tf := range testSet {
//func testSubSet(testSet TestFile, pctx UnixParserCtx, nodes []Node) (bool, error) {
// func testSubSet(testSet TestFile, pctx UnixParserCtx, nodes []Node) (bool, error) {
testOk, err := testSubSet(tf, pctx, nodes)
if err != nil {
log.Fatalf("test failed : %s", err)
}

if !testOk {
log.Fatalf("failed test : %+v", tf)
}
}

return true
}

Expand All @@ -404,48 +430,61 @@ func TestGeneratePatternsDoc(t *testing.T) {
if err != nil {
t.Fatalf("unable to load patterns : %s", err)
}

log.Infof("-> %s", spew.Sdump(pctx))
/*don't judge me, we do it for the users*/
p := make(PairList, len(pctx.Grok.Patterns))

i := 0

for key, val := range pctx.Grok.Patterns {
p[i] = Pair{key, val}
p[i].Value = strings.ReplaceAll(p[i].Value, "{%{", "\\{\\%\\{")
i++
}

sort.Sort(p)

f, err := os.OpenFile("./patterns-documentation.md", os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
f, err := os.OpenFile("./patterns-documentation.md", os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
t.Fatalf("failed to open : %s", err)
}

if _, err := f.WriteString("# Patterns documentation\n\n"); err != nil {
t.Fatal("failed to write to file")
}

if _, err := f.WriteString("You will find here a generated documentation of all the patterns loaded by crowdsec.\n"); err != nil {
t.Fatal("failed to write to file")
}

if _, err := f.WriteString("They are sorted by pattern length, and are meant to be used in parsers, in the form %{PATTERN_NAME}.\n"); err != nil {
t.Fatal("failed to write to file")
}

if _, err := f.WriteString("\n\n"); err != nil {
t.Fatal("failed to write to file")
}

for _, k := range p {
if _, err := fmt.Fprintf(f, "## %s\n\nPattern :\n```\n%s\n```\n\n", k.Key, k.Value); err != nil {
t.Fatal("failed to write to file")
}

fmt.Printf("%v\t%v\n", k.Key, k.Value)
}

if _, err := f.WriteString("\n"); err != nil {
t.Fatal("failed to write to file")
}

if _, err := f.WriteString("# Documentation generation\n"); err != nil {
t.Fatal("failed to write to file")
}

if _, err := f.WriteString("This documentation is generated by `pkg/parser` : `GO_WANT_TEST_DOC=1 go test -run TestGeneratePatternsDoc`\n"); err != nil {
t.Fatal("failed to write to file")
}

f.Close()
}

0 comments on commit e6ebf7a

Please sign in to comment.