2024-11-28 09:47:10 +02:00
|
|
|
package rule
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2025-10-23 15:17:59 +01:00
|
|
|
"go/ast"
|
2024-11-28 09:47:10 +02:00
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestIsCallToExitFunction(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
pkgName string
|
|
|
|
|
functionName string
|
2025-10-23 15:17:59 +01:00
|
|
|
functionArgs []ast.Expr
|
2024-11-28 09:47:10 +02:00
|
|
|
expected bool
|
|
|
|
|
}{
|
2025-10-23 15:17:59 +01:00
|
|
|
{"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},
|
2024-11-28 09:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(fmt.Sprintf("%s.%s", tt.pkgName, tt.functionName), func(t *testing.T) {
|
2025-10-23 15:17:59 +01:00
|
|
|
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)
|
2024-11-28 09:47:10 +02:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|