You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
naza/pkg/fake/exit.go

49 lines
798 B
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 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 (191201771@qq.com)
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
}