forked from q191201771/naza
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e7252f
commit a78344e
Showing
3 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2019, Chef. All rights reserved. | ||
// https://github.com/q191201771/naza | ||
// | ||
// Use of this source code is governed by a MIT-style license | ||
// that can be found in the License file. | ||
// | ||
// Author: Chef ([email protected]) | ||
|
||
package fake | ||
|
||
import "os" | ||
|
||
var ( | ||
exit = os.Exit | ||
) | ||
|
||
type ExitResult struct { | ||
HasExit bool | ||
ExitCode int | ||
} | ||
|
||
var exitResult ExitResult | ||
|
||
// 正常情况下,调用 os.Exit,单元测试时,可配置为不调用 os.Exit | ||
func Exit(code int) { | ||
exit(code) | ||
} | ||
|
||
func WithFakeExit(fn func()) ExitResult { | ||
startFakeExit() | ||
fn() | ||
stopFakeExit() | ||
return exitResult | ||
} | ||
|
||
func startFakeExit() { | ||
exitResult.HasExit = false | ||
exitResult.ExitCode = 0 | ||
|
||
exit = func(code int) { | ||
exitResult.HasExit = true | ||
exitResult.ExitCode = code | ||
} | ||
} | ||
|
||
func stopFakeExit() { | ||
exit = os.Exit | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2019, Chef. All rights reserved. | ||
// https://github.com/q191201771/naza | ||
// | ||
// Use of this source code is governed by a MIT-style license | ||
// that can be found in the License file. | ||
// | ||
// Author: Chef ([email protected]) | ||
|
||
package fake_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/q191201771/naza/pkg/assert" | ||
"github.com/q191201771/naza/pkg/fake" | ||
) | ||
|
||
func TestWithFakeExit(t *testing.T) { | ||
var er fake.ExitResult | ||
er = fake.WithFakeExit(func() { | ||
fake.Exit(1) | ||
}) | ||
assert.Equal(t, true, er.HasExit) | ||
assert.Equal(t, 1, er.ExitCode) | ||
|
||
er = fake.WithFakeExit(func() { | ||
}) | ||
assert.Equal(t, false, er.HasExit) | ||
|
||
er = fake.WithFakeExit(func() { | ||
fake.Exit(2) | ||
}) | ||
assert.Equal(t, true, er.HasExit) | ||
assert.Equal(t, 2, er.ExitCode) | ||
} |