diff --git a/README.md b/README.md index 3e362ef..b45120b 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,8 @@ All functions return a predefined error type, and it is highly recommended these ## Context support All calls into this library support go's `context` functionality. -Therefore, blocking calls can time out according to the caller's needs, and the returned error should be checked to see if a timeout occurred (`ErrExecTimeout`). +Therefore, blocking calls can time out according to the caller's needs. +Use `errors.Is(err, ErrExecTimeout)` to check for context cancellation or timeout, and `errors.Is(err, context.Canceled)` or `errors.Is(err, context.DeadlineExceeded)` to distinguish the cause. ## Simple example diff --git a/execute_context_linux_test.go b/execute_context_linux_test.go new file mode 100644 index 0000000..c48bdf6 --- /dev/null +++ b/execute_context_linux_test.go @@ -0,0 +1,69 @@ +//go:build linux + +package systemctl + +import ( + "context" + "errors" + "os" + "path/filepath" + "testing" + "time" +) + +func TestExecuteContextErrors(t *testing.T) { + fakeSystemctl := filepath.Join(t.TempDir(), "systemctl") + // Replace the shell so cancellation cannot leave a child holding the pipes. + if err := os.WriteFile(fakeSystemctl, []byte("#!/bin/sh\nprintf 'Failed to connect to bus' >&2\nexec sleep 30\n"), 0o755); err != nil { + t.Fatal(err) + } + original := systemctl + systemctl = fakeSystemctl + t.Cleanup(func() { systemctl = original }) + + tests := []struct { + name string + context func() (context.Context, context.CancelFunc) + want error + }{ + { + name: "already canceled", + context: func() (context.Context, context.CancelFunc) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + return ctx, cancel + }, + want: context.Canceled, + }, + { + name: "already expired", + context: func() (context.Context, context.CancelFunc) { + return context.WithDeadline(context.Background(), time.Now().Add(-time.Second)) + }, + want: context.DeadlineExceeded, + }, + { + name: "deadline during execution", + context: func() (context.Context, context.CancelFunc) { + return context.WithTimeout(context.Background(), 200*time.Millisecond) + }, + want: context.DeadlineExceeded, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + ctx, cancel := test.context() + defer cancel() + _, _, code, err := execute(ctx, []string{"start", "example.service"}) + if !errors.Is(err, test.want) { + t.Errorf("execute() error = %v, want %v", err, test.want) + } + if !errors.Is(err, ErrExecTimeout) { + t.Errorf("execute() error = %v, want ErrExecTimeout", err) + } + if code == 0 { + t.Error("execute() exit code = 0, want failure") + } + }) + } +} diff --git a/util.go b/util.go index 4ff98f8..9645686 100644 --- a/util.go +++ b/util.go @@ -44,6 +44,12 @@ func execute(ctx context.Context, args []string) (string, string, int, error) { code = 1 } + // Context cancellation takes precedence over stderr classification. A killed + // process may have emitted a diagnostic unrelated to why it was terminated. + if err != nil && ctx.Err() != nil { + return output, warnings, code, errors.Join(ErrExecTimeout, ctx.Err(), err) + } + if code == killed { return output, warnings, code, ErrExecTimeout }