mirror of
https://github.com/mgechev/revive.git
synced 2025-11-25 22:12:38 +02:00
feature(deep-exit): detect exit-triggering flag usage (#1544)
This commit is contained in:
committed by
GitHub
parent
d32d4a008f
commit
1bc57ac6f3
@@ -2,6 +2,7 @@ package rule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -9,23 +10,39 @@ func TestIsCallToExitFunction(t *testing.T) {
|
||||
tests := []struct {
|
||||
pkgName string
|
||||
functionName string
|
||||
functionArgs []ast.Expr
|
||||
expected bool
|
||||
}{
|
||||
{"os", "Exit", true},
|
||||
{"syscall", "Exit", true},
|
||||
{"log", "Fatal", true},
|
||||
{"log", "Fatalf", true},
|
||||
{"log", "Fatalln", true},
|
||||
{"log", "Panic", true},
|
||||
{"log", "Panicf", true},
|
||||
{"log", "Print", false},
|
||||
{"fmt", "Errorf", false},
|
||||
{"os", "Exit", nil, true},
|
||||
{"syscall", "Exit", nil, true},
|
||||
{"log", "Fatal", nil, true},
|
||||
{"log", "Fatalf", nil, true},
|
||||
{"log", "Fatalln", nil, true},
|
||||
{"log", "Panic", nil, true},
|
||||
{"log", "Panicf", nil, true},
|
||||
{"flag", "Parse", nil, true},
|
||||
{"flag", "NewFlagSet", []ast.Expr{
|
||||
nil,
|
||||
&ast.SelectorExpr{
|
||||
X: &ast.Ident{Name: "flag"},
|
||||
Sel: &ast.Ident{Name: "ExitOnError"},
|
||||
},
|
||||
}, true},
|
||||
{"log", "Print", nil, false},
|
||||
{"fmt", "Errorf", nil, false},
|
||||
{"flag", "NewFlagSet", []ast.Expr{
|
||||
nil,
|
||||
&ast.SelectorExpr{
|
||||
X: &ast.Ident{Name: "flag"},
|
||||
Sel: &ast.Ident{Name: "ContinueOnError"},
|
||||
},
|
||||
}, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(fmt.Sprintf("%s.%s", tt.pkgName, tt.functionName), func(t *testing.T) {
|
||||
if got := isCallToExitFunction(tt.pkgName, tt.functionName); got != tt.expected {
|
||||
t.Errorf("isCallToExitFunction(%s, %s) = %v; want %v", tt.pkgName, tt.functionName, got, tt.expected)
|
||||
if got := isCallToExitFunction(tt.pkgName, tt.functionName, tt.functionArgs); got != tt.expected {
|
||||
t.Errorf("isCallToExitFunction(%s, %s, %v) = %v; want %v", tt.pkgName, tt.functionName, tt.functionArgs, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user