* refactor: 收拢核心管线、强化错误处理、优化热路径与更新体积 * fix: preserve download recovery and bound PBZX decoding Keep VM diagnostics available during plugin initialization failures and verify settings through the unified driver lifecycle.
96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
package archtest
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/token"
|
|
"testing"
|
|
)
|
|
|
|
// TestNoSilentRecover forbids bare `recover()` and `_ = recover()` statements
|
|
// in production code so panics are never silently swallowed without logging or
|
|
// surfacing an error.
|
|
func TestNoSilentRecover(t *testing.T) {
|
|
for _, s := range sources(t) {
|
|
if s.isTest() {
|
|
continue
|
|
}
|
|
ast.Inspect(s.file, func(n ast.Node) bool {
|
|
switch stmt := n.(type) {
|
|
case *ast.ExprStmt:
|
|
if isRecoverCall(stmt.X) {
|
|
pos := fileSet.Position(stmt.Pos())
|
|
t.Errorf("%s:%d: bare recover() silently swallows panics; bind and log/report it", s.rel, pos.Line)
|
|
}
|
|
case *ast.AssignStmt:
|
|
if len(stmt.Rhs) == 1 && isRecoverCall(stmt.Rhs[0]) && allBlankIdents(stmt.Lhs) {
|
|
pos := fileSet.Position(stmt.Pos())
|
|
t.Errorf("%s:%d: `_ = recover()` silently swallows panics; bind and log/report it", s.rel, pos.Line)
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
}
|
|
|
|
func isRecoverCall(expr ast.Expr) bool {
|
|
call, ok := expr.(*ast.CallExpr)
|
|
if !ok {
|
|
return false
|
|
}
|
|
ident, ok := call.Fun.(*ast.Ident)
|
|
return ok && ident.Name == "recover"
|
|
}
|
|
|
|
func allBlankIdents(exprs []ast.Expr) bool {
|
|
if len(exprs) == 0 {
|
|
return false
|
|
}
|
|
for _, e := range exprs {
|
|
id, ok := e.(*ast.Ident)
|
|
if !ok || id.Name != "_" {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// TestNoSilentFatalPersistenceDiscards guards against `_ = <call>` on critical
|
|
// persistence/rollback primitives where dropping an error silently corrupts
|
|
// on-disk state.
|
|
func TestNoSilentFatalPersistenceDiscards(t *testing.T) {
|
|
guarded := map[string]bool{
|
|
"atomicfile.Write": true,
|
|
"atomicfile.WriteJSON": true,
|
|
"plugin.SaveArgLedger": true,
|
|
}
|
|
for _, s := range sources(t) {
|
|
if s.isTest() {
|
|
continue
|
|
}
|
|
ast.Inspect(s.file, func(n ast.Node) bool {
|
|
assign, ok := n.(*ast.AssignStmt)
|
|
if !ok || assign.Tok != token.ASSIGN || len(assign.Rhs) != 1 || !allBlankIdents(assign.Lhs) {
|
|
return true
|
|
}
|
|
call, ok := assign.Rhs[0].(*ast.CallExpr)
|
|
if !ok {
|
|
return true
|
|
}
|
|
sel, ok := call.Fun.(*ast.SelectorExpr)
|
|
if !ok {
|
|
return true
|
|
}
|
|
pkg, ok := sel.X.(*ast.Ident)
|
|
if !ok {
|
|
return true
|
|
}
|
|
key := pkg.Name + "." + sel.Sel.Name
|
|
if guarded[key] {
|
|
pos := fileSet.Position(assign.Pos())
|
|
t.Errorf("%s:%d: discarding error from %s with `_ =` is forbidden; handle or log the error", s.rel, pos.Line, key)
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
}
|