Skip to content

Commit 2bec93f

Browse files
chavacavamgechev
authored andcommitted
fixes issue 290 (#294)
1 parent 757182a commit 2bec93f

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

fixtures/deep-exit.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
package fixtures
22

33
import (
4-
"syscall"
54
"log"
65
"os"
6+
"syscall"
7+
"testing"
78
)
89

910
func foo0() {
10-
os.Exit(1) // MATCH /calls to os.Exit only in main() or init() functions/
11+
os.Exit(1) // MATCH /calls to os.Exit only in main() or init() functions/
1112
}
1213

1314
func init() {
14-
log.Fatal("v ...interface{}")
15+
log.Fatal("v ...interface{}")
1516
}
1617

1718
func foo() {
18-
log.Fatalf(1) // MATCH /calls to log.Fatalf only in main() or init() functions/
19+
log.Fatalf(1) // MATCH /calls to log.Fatalf only in main() or init() functions/
1920
}
2021

2122
func main() {
22-
log.Fatalln("v ...interface{}")
23+
log.Fatalln("v ...interface{}")
2324
}
2425

2526
func bar() {
26-
log.Fatal(1) // MATCH /calls to log.Fatal only in main() or init() functions/
27+
log.Fatal(1) // MATCH /calls to log.Fatal only in main() or init() functions/
2728
}
2829

2930
func bar2() {
30-
bar()
31-
syscall.Exit(1) // MATCH /calls to syscall.Exit only in main() or init() functions/
31+
bar()
32+
syscall.Exit(1) // MATCH /calls to syscall.Exit only in main() or init() functions/
33+
}
34+
35+
func TestMain(m *testing.M) {
36+
// must match because this is not a test file
37+
os.Exit(m.Run()) // MATCH /calls to os.Exit only in main() or init() functions/
3238
}

fixtures/deep-exit_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package fixtures
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestMain(m *testing.M) {
9+
// call flag.Parse() here if TestMain uses flags
10+
os.Exit(m.Run())
11+
}

rule/deep-exit.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (r *DeepExitRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
3030
},
3131
}
3232

33-
w := lintDeepExit{onFailure, exitFunctions, false}
33+
w := lintDeepExit{onFailure, exitFunctions, file.IsTest()}
3434
ast.Walk(w, file.AST)
3535
return failures
3636
}
@@ -43,16 +43,15 @@ func (r *DeepExitRule) Name() string {
4343
type lintDeepExit struct {
4444
onFailure func(lint.Failure)
4545
exitFunctions map[string]map[string]bool
46-
ignore bool
46+
isTestFile bool
4747
}
4848

4949
func (w lintDeepExit) Visit(node ast.Node) ast.Visitor {
50-
if stmt, ok := node.(*ast.FuncDecl); ok {
51-
w.updateIgnore(stmt)
52-
return w
53-
}
50+
if fd, ok := node.(*ast.FuncDecl); ok {
51+
if w.mustIgnore(fd) {
52+
return nil // skip analysis of this function
53+
}
5454

55-
if w.ignore {
5655
return w
5756
}
5857

@@ -88,7 +87,8 @@ func (w lintDeepExit) Visit(node ast.Node) ast.Visitor {
8887
return w
8988
}
9089

91-
func (w *lintDeepExit) updateIgnore(fd *ast.FuncDecl) {
90+
func (w *lintDeepExit) mustIgnore(fd *ast.FuncDecl) bool {
9291
fn := fd.Name.Name
93-
w.ignore = (fn == "init" || fn == "main")
92+
93+
return fn == "init" || fn == "main" || (w.isTestFile && fn == "TestMain")
9494
}

test/deep-exit_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ import (
88

99
func TestDeepExit(t *testing.T) {
1010
testRule(t, "deep-exit", &rule.DeepExitRule{})
11+
testRule(t, "deep-exit_test", &rule.DeepExitRule{})
1112
}

0 commit comments

Comments
 (0)