-
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: exit on
ctrl+c
in interactive printers by default (#593)
- Loading branch information
Showing
6 changed files
with
117 additions
and
10 deletions.
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
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,54 @@ | ||
package internal | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestNewCancelationSignal(t *testing.T) { | ||
// Mock DefaultExitFunc | ||
exitCalled := false | ||
exitCode := 0 | ||
DefaultExitFunc = func(code int) { | ||
exitCalled = true | ||
exitCode = code | ||
} | ||
defer func() { DefaultExitFunc = os.Exit }() // Reset after tests | ||
|
||
// Scenario 1: Testing cancel function | ||
cancel, exit := NewCancelationSignal(nil) | ||
if exitCalled { | ||
t.Error("Exit function should not be called immediately after NewCancelationSignal") | ||
} | ||
|
||
// Scenario 2: Testing exit function without cancel | ||
exit() | ||
if exitCalled { | ||
t.Error("Exit function should not be called when cancel is not set") | ||
} | ||
|
||
// Scenario 3: Testing cancel then exit with no interruptFunc | ||
cancel() | ||
exit() | ||
if !exitCalled || exitCode != 1 { | ||
t.Errorf("Expected Exit(1) to be called, exitCalled: %v, exitCode: %d", exitCalled, exitCode) | ||
} | ||
|
||
// Reset for next scenario | ||
exitCalled = false | ||
exitCode = 0 | ||
|
||
// Scenario 4: Testing cancel then exit with interruptFunc | ||
interruptCalled := false | ||
cancel, exit = NewCancelationSignal(func() { | ||
interruptCalled = true | ||
}) | ||
cancel() | ||
exit() | ||
if interruptCalled == false { | ||
t.Error("Expected interruptFunc to be called") | ||
} | ||
if exitCalled { | ||
t.Error("Exit should not be called when interruptFunc is provided") | ||
} | ||
} |
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,14 @@ | ||
package internal | ||
|
||
import "os" | ||
|
||
// ExitFuncType is the type of function used to exit the program. | ||
type ExitFuncType func(int) | ||
|
||
// DefaultExitFunc is the default function used to exit the program. | ||
var DefaultExitFunc ExitFuncType = os.Exit | ||
|
||
// Exit calls the current exit function. | ||
func Exit(code int) { | ||
DefaultExitFunc(code) | ||
} |
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,23 @@ | ||
package internal_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/pterm/pterm/internal" | ||
) | ||
|
||
func TestExit(t *testing.T) { | ||
var lastExitCode int | ||
internal.DefaultExitFunc = func(code int) { | ||
lastExitCode = code | ||
} | ||
|
||
defer func() { internal.DefaultExitFunc = os.Exit }() | ||
|
||
internal.Exit(1) | ||
|
||
if lastExitCode != 1 { | ||
t.Errorf("Expected exit code 1, got %d", lastExitCode) | ||
} | ||
} |