Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pkg/ctxutil/spec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build !integration

package ctxutil_test

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/github/gh-aw/pkg/ctxutil"
)

// TestSpec_PublicAPI_OrBackground validates the documented behavior of
// OrBackground as described in the ctxutil README.md specification.
func TestSpec_PublicAPI_OrBackground(t *testing.T) {
type key string
const testKey key = "spec-key"

t.Run("returns context.Background() when ctx is nil", func(t *testing.T) {
var nilCtx context.Context

got := ctxutil.OrBackground(nilCtx)
assert.NotNil(t, got, "OrBackground should never return a nil context")
assert.Equal(t, context.Background(), got, "OrBackground should fall back to context.Background() for a nil input")
})

t.Run("returns the context unchanged when ctx is non-nil", func(t *testing.T) {
ctx := context.WithValue(context.Background(), testKey, "value")

got := ctxutil.OrBackground(ctx)

assert.Equal(t, ctx, got, "OrBackground should return a non-nil context unchanged")
assert.Equal(t, "value", got.Value(testKey), "OrBackground should preserve values carried by the input context")
})

t.Run("preserves cancellation of the input context", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

got := ctxutil.OrBackground(ctx)
cancel()

assert.ErrorIs(t, got.Err(), context.Canceled, "OrBackground should not detach the input context from its cancellation")
})
}
Loading