Skip to content

Commit

Permalink
mockExit in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hollesse committed Apr 16, 2024
1 parent 1fff1c9 commit b564002
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
18 changes: 9 additions & 9 deletions mob.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,22 +240,22 @@ 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 := ""
if isGit() {
projectRootDir = gitRootDir()
if !hasCommits() {
say.Error("Git repository does not have any commits yet. Please create an initial commit.")
exit(1)
Exit(1)
}
}

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1096,7 +1096,7 @@ func silentgit(args ...string) string {
say.Error(output)
say.Error(err.Error())
}
exit(1)
Exit(1)
}
return strings.TrimSpace(output)
}
Expand Down Expand Up @@ -1141,7 +1141,7 @@ func git(args ...string) {
say.Error(output)
say.Error(err.Error())
}
exit(1)
Exit(1)
} else {
say.Indented(commandString)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
28 changes: 27 additions & 1 deletion mob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
)

var (
tempDir string
tempDir string
originalExitFunction func(int)
)

type GitStatus = map[string]string
Expand Down Expand Up @@ -1405,28 +1406,33 @@ func TestStartNextPushManualCommits(t *testing.T) {

func TestStartBranchWithUncommitedChangesFixWithBranch(t *testing.T) {
output, _ := setup(t)
mockExit()

setWorkingDir(tempDir + "/local")

createFile(t, "uncommited.txt", "contentIrrelevant")
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")
createFile(t, "uncommited.txt", "contentIrrelevant")
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")

Expand All @@ -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")
Expand All @@ -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) {
Expand Down Expand Up @@ -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 = ""

Expand Down
16 changes: 8 additions & 8 deletions timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

func StartTimer(timerInMinutes string, configuration config.Configuration) {
if err := startTimer(configuration.Timer, configuration); err != nil {
exit(1)
Exit(1)
}
}

Expand All @@ -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 {
Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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 {
Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down

0 comments on commit b564002

Please sign in to comment.