1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-23 22:04:49 +02:00

test: fix testpackage lint issues for test and ifelse (#1582)

This commit is contained in:
Oleksandr Redko
2025-11-15 20:24:16 +02:00
committed by GitHub
parent d1dd6ee961
commit d81298b0ce
98 changed files with 152 additions and 137 deletions

View File

@@ -16,6 +16,7 @@ linters:
- musttag - musttag
- nolintlint - nolintlint
- revive - revive
- testpackage
- thelper - thelper
- staticcheck - staticcheck
- unparam - unparam
@@ -30,6 +31,18 @@ linters:
- linters: - linters:
- godoclint - godoclint
text: 'symbol should have a godoc \("Visit"\)' text: 'symbol should have a godoc \("Visit"\)'
- linters:
- testpackage
text: 'package should be `rule_test` instead of `rule`'
- linters:
- testpackage
text: 'package should be `lint_test` instead of `lint`'
- linters:
- testpackage
text: 'package should be `cli_test` instead of `cli`'
- linters:
- testpackage
text: 'package should be `formatter_test` instead of `formatter`'
settings: settings:
gocritic: gocritic:

View File

@@ -1,24 +1,26 @@
package ifelse package ifelse_test
import ( import (
"go/ast" "go/ast"
"go/token" "go/token"
"testing" "testing"
"github.com/mgechev/revive/internal/ifelse"
) )
func TestBlockBranch(t *testing.T) { func TestBlockBranch(t *testing.T) {
t.Run("empty", func(t *testing.T) { t.Run("empty", func(t *testing.T) {
block := &ast.BlockStmt{List: []ast.Stmt{}} block := &ast.BlockStmt{List: []ast.Stmt{}}
b := BlockBranch(block) b := ifelse.BlockBranch(block)
if b.BranchKind != Empty { if b.BranchKind != ifelse.Empty {
t.Errorf("want Empty branch, got %v", b.BranchKind) t.Errorf("want Empty branch, got %v", b.BranchKind)
} }
}) })
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
stmt := &ast.ReturnStmt{} stmt := &ast.ReturnStmt{}
block := &ast.BlockStmt{List: []ast.Stmt{stmt}} block := &ast.BlockStmt{List: []ast.Stmt{stmt}}
b := BlockBranch(block) b := ifelse.BlockBranch(block)
if b.BranchKind != Return { if b.BranchKind != ifelse.Return {
t.Errorf("want Return branch, got %v", b.BranchKind) t.Errorf("want Return branch, got %v", b.BranchKind)
} }
}) })
@@ -28,33 +30,33 @@ func TestStmtBranch(t *testing.T) {
cases := []struct { cases := []struct {
name string name string
stmt ast.Stmt stmt ast.Stmt
kind BranchKind kind ifelse.BranchKind
call *Call call *ifelse.Call
}{ }{
{ {
name: "ReturnStmt", name: "ReturnStmt",
stmt: &ast.ReturnStmt{}, stmt: &ast.ReturnStmt{},
kind: Return, kind: ifelse.Return,
}, },
{ {
name: "BreakStmt", name: "BreakStmt",
stmt: &ast.BranchStmt{Tok: token.BREAK}, stmt: &ast.BranchStmt{Tok: token.BREAK},
kind: Break, kind: ifelse.Break,
}, },
{ {
name: "ContinueStmt", name: "ContinueStmt",
stmt: &ast.BranchStmt{Tok: token.CONTINUE}, stmt: &ast.BranchStmt{Tok: token.CONTINUE},
kind: Continue, kind: ifelse.Continue,
}, },
{ {
name: "GotoStmt", name: "GotoStmt",
stmt: &ast.BranchStmt{Tok: token.GOTO}, stmt: &ast.BranchStmt{Tok: token.GOTO},
kind: Goto, kind: ifelse.Goto,
}, },
{ {
name: "EmptyStmt", name: "EmptyStmt",
stmt: &ast.EmptyStmt{}, stmt: &ast.EmptyStmt{},
kind: Empty, kind: ifelse.Empty,
}, },
{ {
name: "ExprStmt with DeviatingFunc (panic)", name: "ExprStmt with DeviatingFunc (panic)",
@@ -63,8 +65,8 @@ func TestStmtBranch(t *testing.T) {
Fun: &ast.Ident{Name: "panic"}, Fun: &ast.Ident{Name: "panic"},
}, },
}, },
kind: Panic, kind: ifelse.Panic,
call: &Call{Name: "panic"}, call: &ifelse.Call{Name: "panic"},
}, },
{ {
name: "ExprStmt with DeviatingFunc (os.Exit)", name: "ExprStmt with DeviatingFunc (os.Exit)",
@@ -76,8 +78,8 @@ func TestStmtBranch(t *testing.T) {
}, },
}, },
}, },
kind: Exit, kind: ifelse.Exit,
call: &Call{Pkg: "os", Name: "Exit"}, call: &ifelse.Call{Pkg: "os", Name: "Exit"},
}, },
{ {
name: "ExprStmt with non-deviating func", name: "ExprStmt with non-deviating func",
@@ -86,7 +88,7 @@ func TestStmtBranch(t *testing.T) {
Fun: &ast.Ident{Name: "foo"}, Fun: &ast.Ident{Name: "foo"},
}, },
}, },
kind: Regular, kind: ifelse.Regular,
}, },
{ {
name: "LabeledStmt wrapping ReturnStmt", name: "LabeledStmt wrapping ReturnStmt",
@@ -94,7 +96,7 @@ func TestStmtBranch(t *testing.T) {
Label: &ast.Ident{Name: "lbl"}, Label: &ast.Ident{Name: "lbl"},
Stmt: &ast.ReturnStmt{}, Stmt: &ast.ReturnStmt{},
}, },
kind: Return, kind: ifelse.Return,
}, },
{ {
name: "LabeledStmt wrapping ExprStmt", name: "LabeledStmt wrapping ExprStmt",
@@ -102,21 +104,21 @@ func TestStmtBranch(t *testing.T) {
Label: &ast.Ident{Name: "lbl"}, Label: &ast.Ident{Name: "lbl"},
Stmt: &ast.ExprStmt{X: &ast.CallExpr{Fun: &ast.Ident{Name: "foo"}}}, Stmt: &ast.ExprStmt{X: &ast.CallExpr{Fun: &ast.Ident{Name: "foo"}}},
}, },
kind: Regular, kind: ifelse.Regular,
}, },
{ {
name: "BlockStmt with ReturnStmt", name: "BlockStmt with ReturnStmt",
stmt: &ast.BlockStmt{List: []ast.Stmt{&ast.ReturnStmt{}}}, stmt: &ast.BlockStmt{List: []ast.Stmt{&ast.ReturnStmt{}}},
kind: Return, kind: ifelse.Return,
}, },
{ {
name: "BlockStmt with ExprStmt", name: "BlockStmt with ExprStmt",
stmt: &ast.BlockStmt{List: []ast.Stmt{&ast.ExprStmt{X: &ast.CallExpr{Fun: &ast.Ident{Name: "foo"}}}}}, stmt: &ast.BlockStmt{List: []ast.Stmt{&ast.ExprStmt{X: &ast.CallExpr{Fun: &ast.Ident{Name: "foo"}}}}},
kind: Regular, kind: ifelse.Regular,
}, },
} }
for _, c := range cases { for _, c := range cases {
b := StmtBranch(c.stmt) b := ifelse.StmtBranch(c.stmt)
if b.BranchKind != c.kind { if b.BranchKind != c.kind {
t.Errorf("%s: want %v, got %v", c.name, c.kind, b.BranchKind) t.Errorf("%s: want %v, got %v", c.name, c.kind, b.BranchKind)
} }
@@ -131,37 +133,37 @@ func TestStmtBranch(t *testing.T) {
func TestBranch_String_LongString(t *testing.T) { func TestBranch_String_LongString(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
branch Branch branch ifelse.Branch
wantStr string wantStr string
wantLong string wantLong string
}{ }{
{ {
name: "Return branch", name: "Return branch",
branch: Branch{BranchKind: Return}, branch: ifelse.Branch{BranchKind: ifelse.Return},
wantStr: "{ ... return }", wantStr: "{ ... return }",
wantLong: "a return statement", wantLong: "a return statement",
}, },
{ {
name: "Panic branch with Call", name: "Panic branch with Call",
branch: Branch{BranchKind: Panic, Call: Call{Name: "panic"}}, branch: ifelse.Branch{BranchKind: ifelse.Panic, Call: ifelse.Call{Name: "panic"}},
wantStr: "{ ... panic() }", wantStr: "{ ... panic() }",
wantLong: "call to panic function", wantLong: "call to panic function",
}, },
{ {
name: "Exit branch with Call", name: "Exit branch with Call",
branch: Branch{BranchKind: Exit, Call: Call{Pkg: "os", Name: "Exit"}}, branch: ifelse.Branch{BranchKind: ifelse.Exit, Call: ifelse.Call{Pkg: "os", Name: "Exit"}},
wantStr: "{ ... os.Exit() }", wantStr: "{ ... os.Exit() }",
wantLong: "call to os.Exit function", wantLong: "call to os.Exit function",
}, },
{ {
name: "Empty branch", name: "Empty branch",
branch: Branch{BranchKind: Empty}, branch: ifelse.Branch{BranchKind: ifelse.Empty},
wantStr: "{ }", wantStr: "{ }",
wantLong: "an empty block", wantLong: "an empty block",
}, },
{ {
name: "Regular branch", name: "Regular branch",
branch: Branch{BranchKind: Regular}, branch: ifelse.Branch{BranchKind: ifelse.Regular},
wantStr: "{ ... }", wantStr: "{ ... }",
wantLong: "a regular statement", wantLong: "a regular statement",
}, },
@@ -179,27 +181,27 @@ func TestBranch_String_LongString(t *testing.T) {
func TestBranch_HasDecls(t *testing.T) { func TestBranch_HasDecls(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
block []ast.Stmt block *ast.BlockStmt
want bool want bool
}{ }{
{ {
name: "DeclStmt", name: "DeclStmt",
block: []ast.Stmt{&ast.DeclStmt{}}, block: &ast.BlockStmt{List: []ast.Stmt{&ast.DeclStmt{}}},
want: true, want: true,
}, },
{ {
name: "AssignStmt with :=", name: "AssignStmt with :=",
block: []ast.Stmt{&ast.AssignStmt{Tok: token.DEFINE}}, block: &ast.BlockStmt{List: []ast.Stmt{&ast.AssignStmt{Tok: token.DEFINE}}},
want: true, want: true,
}, },
{ {
name: "ExprStmt", name: "ExprStmt",
block: []ast.Stmt{&ast.ExprStmt{}}, block: &ast.BlockStmt{List: []ast.Stmt{&ast.ExprStmt{}}},
want: false, want: false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
b := Branch{block: tt.block} b := ifelse.BlockBranch(tt.block)
if got := b.HasDecls(); got != tt.want { if got := b.HasDecls(); got != tt.want {
t.Errorf("%s: want HasDecls to be %v, got %v", tt.name, tt.want, got) t.Errorf("%s: want HasDecls to be %v, got %v", tt.name, tt.want, got)
} }
@@ -209,42 +211,42 @@ func TestBranch_HasDecls(t *testing.T) {
func TestBranch_IsShort(t *testing.T) { func TestBranch_IsShort(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
block []ast.Stmt block *ast.BlockStmt
want bool want bool
}{ }{
{ {
name: "nil block", name: "nil block",
block: nil, block: &ast.BlockStmt{},
want: true, want: true,
}, },
{ {
name: "single ExprStmt", name: "single ExprStmt",
block: []ast.Stmt{&ast.ExprStmt{}}, block: &ast.BlockStmt{List: []ast.Stmt{&ast.ExprStmt{}}},
want: true, want: true,
}, },
{ {
name: "single BlockStmt", name: "single BlockStmt",
block: []ast.Stmt{&ast.BlockStmt{}}, block: &ast.BlockStmt{List: []ast.Stmt{&ast.BlockStmt{}}},
want: false, want: false,
}, },
{ {
name: "two short statements", name: "two short statements",
block: []ast.Stmt{&ast.ExprStmt{}, &ast.ExprStmt{}}, block: &ast.BlockStmt{List: []ast.Stmt{&ast.ExprStmt{}, &ast.ExprStmt{}}},
want: true, want: true,
}, },
{ {
name: "second non-short statement", name: "second non-short statement",
block: []ast.Stmt{&ast.ExprStmt{}, &ast.BlockStmt{}}, block: &ast.BlockStmt{List: []ast.Stmt{&ast.ExprStmt{}, &ast.BlockStmt{}}},
want: false, want: false,
}, },
{ {
name: "three statements (should return false)", name: "three statements (should return false)",
block: []ast.Stmt{&ast.ExprStmt{}, &ast.ExprStmt{}, &ast.ExprStmt{}}, block: &ast.BlockStmt{List: []ast.Stmt{&ast.ExprStmt{}, &ast.ExprStmt{}, &ast.ExprStmt{}}},
want: false, want: false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
b := Branch{block: tt.block} b := ifelse.BlockBranch(tt.block)
if got := b.IsShort(); got != tt.want { if got := b.IsShort(); got != tt.want {
t.Errorf("%s: want IsShort to be %v, got %v", tt.name, tt.want, got) t.Errorf("%s: want IsShort to be %v, got %v", tt.name, tt.want, got)
} }

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"flag" "flag"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"encoding/json" "encoding/json"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package test package test_test
import ( import (
"testing" "testing"