-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ctrl+c handling in core/commandline #2788
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package commandline | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"testing" | ||
"time" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func TestSignalHandling(t *testing.T) { | ||
shellCommands := []*cli.Command{ | ||
{ | ||
Name: "test", | ||
Usage: "test command", | ||
Action: func(c *cli.Context) error { | ||
return nil | ||
}, | ||
}, | ||
} | ||
|
||
app := &cli.App{ | ||
Commands: []*cli.Command{ | ||
GenerateShellCommand(shellCommands), | ||
}, | ||
} | ||
|
||
// Set up a context that we can cancel | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
go func() { | ||
time.Sleep(1 * time.Second) | ||
p, err := os.FindProcess(os.Getpid()) | ||
if err != nil { | ||
t.Errorf("Failed to find process: %v", err) | ||
return | ||
} | ||
if err := p.Signal(syscall.SIGINT); err != nil { | ||
t.Errorf("Failed to send SIGINT: %v", err) | ||
} | ||
}() | ||
|
||
err := app.RunContext(ctx, []string{"app", "shell"}) | ||
if err != nil { | ||
t.Fatalf("app.RunContext failed: %v", err) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification Ensure proper cleanup and context handling in
To improve the test, consider adding assertions or logs to confirm that the application performs the expected cleanup when receiving a Analysis chainEnsure proper cleanup and context handling in
Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify that the SIGINT handling correctly stops the application and cleans up resources.
# Test: Search for logs or state changes indicating proper SIGINT handling.
rg --type go "SIGINT"
Length of output: 517 |
||
} | ||
|
||
func TestMain(m *testing.M) { | ||
// Call signal.Notify so that the test process does not exit on SIGINT | ||
sigs := make(chan os.Signal, 1) | ||
signal.Notify(sigs, syscall.SIGINT) | ||
|
||
go func() { | ||
sig := <-sigs | ||
fmt.Printf("Received signal: %v", sig) | ||
}() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enhance signal handling and context management in
GenerateShellCommand
.context.WithCancel
and the setup of signal handling withsignal.Notify
for SIGINT and SIGTERM are well-implemented. This ensures that the command can gracefully handle termination signals.