Skip to content

Commit a9b7d8d

Browse files
committed
refactor: refactor the exit way for unit test
1 parent 43e90f6 commit a9b7d8d

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

commit-msg.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111
func logAndExit(state msgState, v ...interface{}) {
1212
if state <= Merge {
1313
log.Printf(state.Hint(), v...)
14-
os.Exit(0)
1514
} else if state <= FileMissing {
16-
log.Fatalf(state.Hint(), v...)
15+
log.Printf(state.Hint(), v...)
1716
} else {
1817
log.Printf(state.Hint(), v...)
19-
log.Fatalf(Lang.Rule, Types)
18+
log.Printf(Lang.Rule, Types)
2019
}
20+
panic(state)
2121
}
2222

2323
func getMsg(path string) string {
@@ -134,5 +134,18 @@ func main() {
134134

135135
msg := getMsg(msgFile)
136136

137+
defer func() {
138+
err := recover()
139+
state, ok := err.(msgState)
140+
if !ok {
141+
panic(err)
142+
}
143+
144+
if state <= Merge {
145+
os.Exit(0)
146+
} else {
147+
os.Exit(int(state))
148+
}
149+
}()
137150
validateMsg(msg)
138151
}

commit-msg_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,15 @@ func TestGetMsg(t *testing.T) {
1010
t.Error("Failed!")
1111
}
1212
}
13+
14+
func TestValidateSample(t *testing.T) {
15+
defer func() {
16+
err := recover()
17+
state, ok := err.(msgState)
18+
if !ok || state > Merge {
19+
t.Errorf("Failed! %v", err)
20+
}
21+
}()
22+
23+
validateMsg(getMsg("testcase/sample.txt"))
24+
}

0 commit comments

Comments
 (0)