Skip to content
This repository has been archived by the owner on Nov 26, 2023. It is now read-only.

Commit

Permalink
make SignalCancelError wrap context.Canceled
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmdm committed Oct 23, 2023
1 parent d7ccb57 commit 5cfe26a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.vscode

*.out

coverage
playground

*.out

23 changes: 23 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: "3"

tasks:
coverage:
cmds:
- rm -rf ./coverage && mkdir ./coverage
- go test -v -p 1 -count 1 -race -test.gocoverdir=./coverage -cover ./...
- go tool covdata percent -i=./coverage
- go tool covdata textfmt -i ./coverage -o ./coverage/results.out
- '{{if eq .CLI_ARGS "html"}} go tool cover -html ./coverage/results.out{{end}}'

lint:
cmds:
- golangci-lint run ./...


# Ironically the task runner is itself a dev dependency of the project.
# To install it run the following command:
#
# go install github.com/go-task/task/v3/cmd/task@latest
install-dev-deps:
cmds:
- go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.2
4 changes: 4 additions & 0 deletions signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (err SignalCancelError) Error() string {
return fmt.Sprintf("%v: received signal: %s", context.Canceled, err.Signal)
}

func (SignalCancelError) Unwrap() error {
return context.Canceled
}

func SignalCause(ctx context.Context) os.Signal {
if sigErr := (SignalCancelError{}); errors.As(context.Cause(ctx), &sigErr) {
return sigErr.Signal
Expand Down
23 changes: 19 additions & 4 deletions signal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ func TestWithCancelation(t *testing.T) {
require.NoError(t, err)
require.NoError(t, file.Close())

isCoverage := slices.ContainsFunc(os.Args, func(arg string) bool { return strings.HasPrefix(arg, "-test.gocoverdir=") })
coverDir := func() string {
args := append([]string{}, os.Args...)
slices.Reverse(args)
for _, arg := range args {
if dir, ok := strings.CutPrefix(arg, "-test.gocoverdir="); ok {
return dir
}
}
return ""
}()

build := func() *exec.Cmd {
args := []string{
Expand All @@ -31,8 +40,8 @@ func TestWithCancelation(t *testing.T) {
"-o", file.Name(),
}

if isCoverage {
args = append(args, "-coverpkg=./...")
if coverDir != "" {
args = append(args, "-cover", "-coverpkg=./...")
}

args = append(args, "./acceptance")
Expand All @@ -44,7 +53,8 @@ func TestWithCancelation(t *testing.T) {

acceptanceCMD := func() (cmd *exec.Cmd, stdout *bytes.Buffer) {
cmd = exec.Command(file.Name())
cmd.Env = append(cmd.Env, os.Environ()...)
// cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Env = append(cmd.Env, "GOCOVERDIR="+coverDir)

stdout = new(bytes.Buffer)
cmd.Stdout = stdout
Expand Down Expand Up @@ -123,6 +133,11 @@ func TestSignalCause(t *testing.T) {
}
}

func TestSignalErrorIsCanceled(t *testing.T) {
var e error = xcontext.SignalCancelError{Signal: syscall.SIGTERM}
require.True(t, errors.Is(e, context.Canceled))
}

func CommandStandardIO(name string, args ...string) *exec.Cmd {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
Expand Down

0 comments on commit 5cfe26a

Please sign in to comment.