test: make cmd testable

This commit is contained in:
Li Jie
2025-01-14 10:50:43 +08:00
parent 25a3e19384
commit 8749923f1a
9 changed files with 186 additions and 249 deletions

View File

@@ -0,0 +1,29 @@
package mockable
import (
"os"
)
var (
exitFunc = os.Exit
exitCode int
)
// EnableMock enables mocking of os.Exit
func EnableMock() {
exitCode = 0
exitFunc = func(code int) {
exitCode = code
panic("exit")
}
}
// Exit calls the current exit function
func Exit(code int) {
exitFunc(code)
}
// ExitCode returns the last exit code from a mocked Exit call
func ExitCode() int {
return exitCode
}