From b564002875373d529ee7889da743eb94fcffde3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joshua=20T=C3=B6pfer?= Date: Tue, 16 Apr 2024 13:24:23 +0200 Subject: [PATCH] mockExit in tests --- mob.go | 18 +++++++++--------- mob_test.go | 28 +++++++++++++++++++++++++++- timer.go | 16 ++++++++-------- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/mob.go b/mob.go index 6b31c525..575c41e3 100644 --- a/mob.go +++ b/mob.go @@ -240,14 +240,14 @@ func run(osArgs []string) { if versionString == "" { say.Error("'git' command was not found in PATH. It may be not installed. " + "To learn how to install 'git' refer to https://git-scm.com/book/en/v2/Getting-Started-Installing-Git.") - exit(1) + Exit(1) } currentVersion := parseGitVersion(versionString) if currentVersion.Less(parseGitVersion(minimumGitVersion)) { say.Error(fmt.Sprintf("'git' command version '%s' is lower than the required minimum version (%s). "+ "Please update your 'git' installation!", versionString, minimumGitVersion)) - exit(1) + Exit(1) } projectRootDir := "" @@ -255,7 +255,7 @@ func run(osArgs []string) { projectRootDir = gitRootDir() if !hasCommits() { say.Error("Git repository does not have any commits yet. Please create an initial commit.") - exit(1) + Exit(1) } } @@ -300,7 +300,7 @@ func execute(command string, parameter []string, configuration config.Configurat case "s", "start": err := start(configuration) if !isMobProgramming(configuration) || err != nil { - exit(1) + Exit(1) } if len(parameter) > 0 { timer := parameter[0] @@ -455,7 +455,7 @@ func injectCommandWithMessage(command string, message string) string { placeHolders := strings.Count(command, "%s") if placeHolders > 1 { say.Error(fmt.Sprintf("Too many placeholders (%d) in format command string: %s", placeHolders, command)) - exit(1) + Exit(1) } if placeHolders == 0 { return fmt.Sprintf("%s %s", command, message) @@ -1096,7 +1096,7 @@ func silentgit(args ...string) string { say.Error(output) say.Error(err.Error()) } - exit(1) + Exit(1) } return strings.TrimSpace(output) } @@ -1141,7 +1141,7 @@ func git(args ...string) { say.Error(output) say.Error(err.Error()) } - exit(1) + Exit(1) } else { say.Indented(commandString) } @@ -1158,7 +1158,7 @@ func gitIgnoreFailure(args ...string) error { if err != nil { if !isGit() { say.Error("expecting the current working directory to be a git repository.") - exit(1) + Exit(1) } else { say.Warning(commandString) say.Warning(output) @@ -1257,6 +1257,6 @@ func startCommand(name string, args ...string) (string, error) { return commandString, err } -var exit = func(code int) { +var Exit = func(code int) { os.Exit(code) } diff --git a/mob_test.go b/mob_test.go index 31ab58a2..d25442fa 100644 --- a/mob_test.go +++ b/mob_test.go @@ -16,7 +16,8 @@ import ( ) var ( - tempDir string + tempDir string + originalExitFunction func(int) ) type GitStatus = map[string]string @@ -1405,6 +1406,7 @@ func TestStartNextPushManualCommits(t *testing.T) { func TestStartBranchWithUncommitedChangesFixWithBranch(t *testing.T) { output, _ := setup(t) + mockExit() setWorkingDir(tempDir + "/local") @@ -1412,10 +1414,12 @@ func TestStartBranchWithUncommitedChangesFixWithBranch(t *testing.T) { runMob(t, tempDir+"/local", "start", "-b", "green") assertOutputContains(t, output, "mob start --branch green --include-uncommitted-changes") + resetExit() } func TestStartBranchEnvWithUncommitedChangesFixWithoutBranch(t *testing.T) { output, _ := setup(t) + mockExit() setWorkingDir(tempDir + "/local") t.Setenv("MOB_WIP_BRANCH_QUALIFIER", "red") @@ -1423,10 +1427,12 @@ func TestStartBranchEnvWithUncommitedChangesFixWithoutBranch(t *testing.T) { runMob(t, tempDir+"/local", "start") assertOutputContains(t, output, "mob start --include-uncommitted-changes") + resetExit() } func TestStartCreateBranchWithUncommitedChangesFixWithBranch(t *testing.T) { output, _ := setup(t) + mockExit() setWorkingDir(tempDir + "/local") @@ -1435,10 +1441,12 @@ func TestStartCreateBranchWithUncommitedChangesFixWithBranch(t *testing.T) { runMob(t, tempDir+"/local", "start", "--create", "-b", "green") assertOutputContains(t, output, "mob start --create --branch green --include-uncommitted-changes") + resetExit() } func TestStartCreateBranchEnvWithUncommitedChangesFixWithoutBranch(t *testing.T) { output, _ := setup(t) + mockExit() setWorkingDir(tempDir + "/local") os.Setenv("MOB_WIP_BRANCH_QUALIFIER", "red") @@ -1448,6 +1456,7 @@ func TestStartCreateBranchEnvWithUncommitedChangesFixWithoutBranch(t *testing.T) os.Unsetenv("MOB_WIP_BRANCH_QUALIFIER") assertOutputContains(t, output, "mob start --create --include-uncommitted-changes") + resetExit() } func TestStartNextPushManualCommitsFeatureBranch(t *testing.T) { @@ -1835,6 +1844,23 @@ func mockOpenInBrowser() { } } +func mockExit() { + originalExitFunction = Exit + Exit = func(code int) { + defer func() { + if r := recover(); r != nil { + fmt.Printf("exit(%d)\n", code) + } + }() + + panic(code) + } +} + +func resetExit() { + Exit = originalExitFunction +} + func createTestbed(t *testing.T, configuration config.Configuration) { workingDir = "" diff --git a/timer.go b/timer.go index ecfb1888..c7e1e78c 100644 --- a/timer.go +++ b/timer.go @@ -19,7 +19,7 @@ import ( func StartTimer(timerInMinutes string, configuration config.Configuration) { if err := startTimer(configuration.Timer, configuration); err != nil { - exit(1) + Exit(1) } } @@ -39,7 +39,7 @@ func startTimer(timerInMinutes string, configuration config.Configuration) error if !startRemoteTimer && !startLocalTimer { say.Error("No timer configured, not starting timer") - exit(1) + Exit(1) } if startRemoteTimer { @@ -48,7 +48,7 @@ func startTimer(timerInMinutes string, configuration config.Configuration) error if err != nil { say.Error("remote timer couldn't be started") say.Error(err.Error()) - exit(1) + Exit(1) } } @@ -58,7 +58,7 @@ func startTimer(timerInMinutes string, configuration config.Configuration) error if err != nil { say.Error(fmt.Sprintf("timer couldn't be started on your system (%s)", runtime.GOOS)) say.Error(err.Error()) - exit(1) + Exit(1) } } @@ -93,7 +93,7 @@ func getMobTimerRoom(configuration config.Configuration) string { func StartBreakTimer(timerInMinutes string, configuration config.Configuration) { if err := startBreakTimer(configuration.Timer, configuration); err != nil { - exit(1) + Exit(1) } } @@ -113,7 +113,7 @@ func startBreakTimer(timerInMinutes string, configuration config.Configuration) if !startRemoteTimer && !startLocalTimer { say.Error("No break timer configured, not starting break timer") - exit(1) + Exit(1) } if startRemoteTimer { @@ -123,7 +123,7 @@ func startBreakTimer(timerInMinutes string, configuration config.Configuration) if err != nil { say.Error("remote break timer couldn't be started") say.Error(err.Error()) - exit(1) + Exit(1) } } @@ -133,7 +133,7 @@ func startBreakTimer(timerInMinutes string, configuration config.Configuration) if err != nil { say.Error(fmt.Sprintf("break timer couldn't be started on your system (%s)", runtime.GOOS)) say.Error(err.Error()) - exit(1) + Exit(1) } }