瀏覽代碼

test: replace context.Background()/context.TODO() with t.Context()

Replace manual context creation with t.Context() which is automatically
cancelled when the test completes.

Go 1.24 modernization pattern.

Assisted-By: cagent
Signed-off-by: David Gageot <[email protected]>
David Gageot 2 天之前
父節點
當前提交
093205121c

+ 3 - 9
cmd/compose/options_test.go

@@ -18,7 +18,6 @@ package compose
 
 
 import (
 import (
 	"bytes"
 	"bytes"
-	"context"
 	"fmt"
 	"fmt"
 	"io"
 	"io"
 	"os"
 	"os"
@@ -244,16 +243,11 @@ services:
 	}
 	}
 
 
 	// Set up the context with necessary environment variables
 	// Set up the context with necessary environment variables
-	ctx := context.Background()
-	_ = os.Setenv("TEST_VAR", "test-value")
-	_ = os.Setenv("API_KEY", "123456")
-	defer func() {
-		_ = os.Unsetenv("TEST_VAR")
-		_ = os.Unsetenv("API_KEY")
-	}()
+	t.Setenv("TEST_VAR", "test-value")
+	t.Setenv("API_KEY", "123456")
 
 
 	// Extract variables from the model
 	// Extract variables from the model
-	info, noVariables, err := extractInterpolationVariablesFromModel(ctx, cli, projectOptions, []string{})
+	info, noVariables, err := extractInterpolationVariablesFromModel(t.Context(), cli, projectOptions, []string{})
 	require.NoError(t, err)
 	require.NoError(t, err)
 	require.False(t, noVariables)
 	require.False(t, noVariables)
 
 

+ 1 - 5
internal/desktop/client_test.go

@@ -17,7 +17,6 @@
 package desktop
 package desktop
 
 
 import (
 import (
-	"context"
 	"os"
 	"os"
 	"testing"
 	"testing"
 	"time"
 	"time"
@@ -34,9 +33,6 @@ func TestClientPing(t *testing.T) {
 		t.Skip("Skipping - COMPOSE_TEST_DESKTOP_ENDPOINT not defined")
 		t.Skip("Skipping - COMPOSE_TEST_DESKTOP_ENDPOINT not defined")
 	}
 	}
 
 
-	ctx, cancel := context.WithCancel(context.Background())
-	t.Cleanup(cancel)
-
 	client := NewClient(desktopEndpoint)
 	client := NewClient(desktopEndpoint)
 	t.Cleanup(func() {
 	t.Cleanup(func() {
 		_ = client.Close()
 		_ = client.Close()
@@ -44,7 +40,7 @@ func TestClientPing(t *testing.T) {
 
 
 	now := time.Now()
 	now := time.Now()
 
 
-	ret, err := client.Ping(ctx)
+	ret, err := client.Ping(t.Context())
 	require.NoError(t, err)
 	require.NoError(t, err)
 
 
 	serverTime := time.Unix(0, ret.ServerTime)
 	serverTime := time.Unix(0, ret.ServerTime)

+ 10 - 11
pkg/compose/convergence_test.go

@@ -17,7 +17,6 @@
 package compose
 package compose
 
 
 import (
 import (
-	"context"
 	"fmt"
 	"fmt"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
@@ -95,7 +94,7 @@ func TestServiceLinks(t *testing.T) {
 		c := testContainer("db", dbContainerName, false)
 		c := testContainer("db", dbContainerName, false)
 		apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]container.Summary{c}, nil)
 		apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]container.Summary{c}, nil)
 
 
-		links, err := tested.(*composeService).getLinks(context.Background(), testProject, s, 1)
+		links, err := tested.(*composeService).getLinks(t.Context(), testProject, s, 1)
 		assert.NilError(t, err)
 		assert.NilError(t, err)
 
 
 		assert.Equal(t, len(links), 3)
 		assert.Equal(t, len(links), 3)
@@ -118,7 +117,7 @@ func TestServiceLinks(t *testing.T) {
 		c := testContainer("db", dbContainerName, false)
 		c := testContainer("db", dbContainerName, false)
 
 
 		apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]container.Summary{c}, nil)
 		apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]container.Summary{c}, nil)
-		links, err := tested.(*composeService).getLinks(context.Background(), testProject, s, 1)
+		links, err := tested.(*composeService).getLinks(t.Context(), testProject, s, 1)
 		assert.NilError(t, err)
 		assert.NilError(t, err)
 
 
 		assert.Equal(t, len(links), 3)
 		assert.Equal(t, len(links), 3)
@@ -141,7 +140,7 @@ func TestServiceLinks(t *testing.T) {
 		c := testContainer("db", dbContainerName, false)
 		c := testContainer("db", dbContainerName, false)
 		apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]container.Summary{c}, nil)
 		apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]container.Summary{c}, nil)
 
 
-		links, err := tested.(*composeService).getLinks(context.Background(), testProject, s, 1)
+		links, err := tested.(*composeService).getLinks(t.Context(), testProject, s, 1)
 		assert.NilError(t, err)
 		assert.NilError(t, err)
 
 
 		assert.Equal(t, len(links), 3)
 		assert.Equal(t, len(links), 3)
@@ -165,7 +164,7 @@ func TestServiceLinks(t *testing.T) {
 		c := testContainer("db", dbContainerName, false)
 		c := testContainer("db", dbContainerName, false)
 		apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]container.Summary{c}, nil)
 		apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]container.Summary{c}, nil)
 
 
-		links, err := tested.(*composeService).getLinks(context.Background(), testProject, s, 1)
+		links, err := tested.(*composeService).getLinks(t.Context(), testProject, s, 1)
 		assert.NilError(t, err)
 		assert.NilError(t, err)
 
 
 		assert.Equal(t, len(links), 4)
 		assert.Equal(t, len(links), 4)
@@ -202,7 +201,7 @@ func TestServiceLinks(t *testing.T) {
 		}
 		}
 		apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptionsOneOff).Return([]container.Summary{c}, nil)
 		apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptionsOneOff).Return([]container.Summary{c}, nil)
 
 
-		links, err := tested.(*composeService).getLinks(context.Background(), testProject, s, 1)
+		links, err := tested.(*composeService).getLinks(t.Context(), testProject, s, 1)
 		assert.NilError(t, err)
 		assert.NilError(t, err)
 
 
 		assert.Equal(t, len(links), 3)
 		assert.Equal(t, len(links), 3)
@@ -233,7 +232,7 @@ func TestWaitDependencies(t *testing.T) {
 			"db":    {Condition: ServiceConditionRunningOrHealthy},
 			"db":    {Condition: ServiceConditionRunningOrHealthy},
 			"redis": {Condition: ServiceConditionRunningOrHealthy},
 			"redis": {Condition: ServiceConditionRunningOrHealthy},
 		}
 		}
-		assert.NilError(t, tested.(*composeService).waitDependencies(context.Background(), &project, "", dependencies, nil, 0))
+		assert.NilError(t, tested.(*composeService).waitDependencies(t.Context(), &project, "", dependencies, nil, 0))
 	})
 	})
 	t.Run("should skip dependencies with condition service_started", func(t *testing.T) {
 	t.Run("should skip dependencies with condition service_started", func(t *testing.T) {
 		dbService := types.ServiceConfig{Name: "db", Scale: intPtr(1)}
 		dbService := types.ServiceConfig{Name: "db", Scale: intPtr(1)}
@@ -246,7 +245,7 @@ func TestWaitDependencies(t *testing.T) {
 			"db":    {Condition: types.ServiceConditionStarted, Required: true},
 			"db":    {Condition: types.ServiceConditionStarted, Required: true},
 			"redis": {Condition: types.ServiceConditionStarted, Required: true},
 			"redis": {Condition: types.ServiceConditionStarted, Required: true},
 		}
 		}
-		assert.NilError(t, tested.(*composeService).waitDependencies(context.Background(), &project, "", dependencies, nil, 0))
+		assert.NilError(t, tested.(*composeService).waitDependencies(t.Context(), &project, "", dependencies, nil, 0))
 	})
 	})
 }
 }
 
 
@@ -260,7 +259,7 @@ func TestIsServiceHealthy(t *testing.T) {
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 	cli.EXPECT().Client().Return(apiClient).AnyTimes()
 	cli.EXPECT().Client().Return(apiClient).AnyTimes()
 
 
-	ctx := context.Background()
+	ctx := t.Context()
 
 
 	t.Run("disabled healthcheck with fallback to running", func(t *testing.T) {
 	t.Run("disabled healthcheck with fallback to running", func(t *testing.T) {
 		containerID := "test-container-id"
 		containerID := "test-container-id"
@@ -475,7 +474,7 @@ func TestCreateMobyContainer(t *testing.T) {
 				Aliases:    []string{"bork-test-0"},
 				Aliases:    []string{"bork-test-0"},
 			}))
 			}))
 
 
-		_, err = tested.(*composeService).createMobyContainer(context.Background(), &project, service, "test", 0, nil, createOptions{
+		_, err = tested.(*composeService).createMobyContainer(t.Context(), &project, service, "test", 0, nil, createOptions{
 			Labels: make(types.Labels),
 			Labels: make(types.Labels),
 		})
 		})
 		assert.NilError(t, err)
 		assert.NilError(t, err)
@@ -561,7 +560,7 @@ func TestCreateMobyContainer(t *testing.T) {
 				NetworkSettings: &container.NetworkSettings{},
 				NetworkSettings: &container.NetworkSettings{},
 			}, nil)
 			}, nil)
 
 
-		_, err = tested.(*composeService).createMobyContainer(context.Background(), &project, service, "test", 0, nil, createOptions{
+		_, err = tested.(*composeService).createMobyContainer(t.Context(), &project, service, "test", 0, nil, createOptions{
 			Labels: make(types.Labels),
 			Labels: make(types.Labels),
 		})
 		})
 		assert.NilError(t, err)
 		assert.NilError(t, err)

+ 4 - 5
pkg/compose/create_test.go

@@ -17,7 +17,6 @@
 package compose
 package compose
 
 
 import (
 import (
-	"context"
 	"os"
 	"os"
 	"path/filepath"
 	"path/filepath"
 	"sort"
 	"sort"
@@ -164,7 +163,7 @@ func TestBuildContainerMountOptions(t *testing.T) {
 	}
 	}
 	mock.EXPECT().ImageInspect(gomock.Any(), "myProject-myService").AnyTimes().Return(image.InspectResponse{}, nil)
 	mock.EXPECT().ImageInspect(gomock.Any(), "myProject-myService").AnyTimes().Return(image.InspectResponse{}, nil)
 
 
-	mounts, err := s.buildContainerMountOptions(context.TODO(), project, project.Services["myService"], inherit)
+	mounts, err := s.buildContainerMountOptions(t.Context(), project, project.Services["myService"], inherit)
 	sort.Slice(mounts, func(i, j int) bool {
 	sort.Slice(mounts, func(i, j int) bool {
 		return mounts[i].Target < mounts[j].Target
 		return mounts[i].Target < mounts[j].Target
 	})
 	})
@@ -176,7 +175,7 @@ func TestBuildContainerMountOptions(t *testing.T) {
 	assert.Equal(t, mounts[2].VolumeOptions.Subpath, "etc")
 	assert.Equal(t, mounts[2].VolumeOptions.Subpath, "etc")
 	assert.Equal(t, mounts[3].Target, "\\\\.\\pipe\\docker_engine")
 	assert.Equal(t, mounts[3].Target, "\\\\.\\pipe\\docker_engine")
 
 
-	mounts, err = s.buildContainerMountOptions(context.TODO(), project, project.Services["myService"], inherit)
+	mounts, err = s.buildContainerMountOptions(t.Context(), project, project.Services["myService"], inherit)
 	sort.Slice(mounts, func(i, j int) bool {
 	sort.Slice(mounts, func(i, j int) bool {
 		return mounts[i].Target < mounts[j].Target
 		return mounts[i].Target < mounts[j].Target
 	})
 	})
@@ -435,7 +434,7 @@ volumes:
 	}
 	}
 	for _, tt := range tests {
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 		t.Run(tt.name, func(t *testing.T) {
-			p, err := composeloader.LoadWithContext(context.TODO(), composetypes.ConfigDetails{
+			p, err := composeloader.LoadWithContext(t.Context(), composetypes.ConfigDetails{
 				ConfigFiles: []composetypes.ConfigFile{
 				ConfigFiles: []composetypes.ConfigFile{
 					{
 					{
 						Filename: "test",
 						Filename: "test",
@@ -448,7 +447,7 @@ volumes:
 			})
 			})
 			assert.NilError(t, err)
 			assert.NilError(t, err)
 			s := &composeService{}
 			s := &composeService{}
-			binds, mounts, err := s.buildContainerVolumes(context.TODO(), *p, p.Services["test"], nil)
+			binds, mounts, err := s.buildContainerVolumes(t.Context(), *p, p.Services["test"], nil)
 			assert.NilError(t, err)
 			assert.NilError(t, err)
 			assert.DeepEqual(t, tt.binds, binds)
 			assert.DeepEqual(t, tt.binds, binds)
 			assert.DeepEqual(t, tt.mounts, mounts)
 			assert.DeepEqual(t, tt.mounts, mounts)

+ 4 - 13
pkg/compose/dependencies_test.go

@@ -71,9 +71,6 @@ func TestTraversalWithMultipleParents(t *testing.T) {
 		project.Services[name] = svc
 		project.Services[name] = svc
 	}
 	}
 
 
-	ctx, cancel := context.WithCancel(context.Background())
-	t.Cleanup(cancel)
-
 	svc := make(chan string, 10)
 	svc := make(chan string, 10)
 	seen := make(map[string]int)
 	seen := make(map[string]int)
 	done := make(chan struct{})
 	done := make(chan struct{})
@@ -84,7 +81,7 @@ func TestTraversalWithMultipleParents(t *testing.T) {
 		done <- struct{}{}
 		done <- struct{}{}
 	}()
 	}()
 
 
-	err := InDependencyOrder(ctx, &project, func(ctx context.Context, service string) error {
+	err := InDependencyOrder(t.Context(), &project, func(ctx context.Context, service string) error {
 		svc <- service
 		svc <- service
 		return nil
 		return nil
 	})
 	})
@@ -99,11 +96,8 @@ func TestTraversalWithMultipleParents(t *testing.T) {
 }
 }
 
 
 func TestInDependencyUpCommandOrder(t *testing.T) {
 func TestInDependencyUpCommandOrder(t *testing.T) {
-	ctx, cancel := context.WithCancel(context.Background())
-	t.Cleanup(cancel)
-
 	var order []string
 	var order []string
-	err := InDependencyOrder(ctx, createTestProject(), func(ctx context.Context, service string) error {
+	err := InDependencyOrder(t.Context(), createTestProject(), func(ctx context.Context, service string) error {
 		order = append(order, service)
 		order = append(order, service)
 		return nil
 		return nil
 	})
 	})
@@ -112,11 +106,8 @@ func TestInDependencyUpCommandOrder(t *testing.T) {
 }
 }
 
 
 func TestInDependencyReverseDownCommandOrder(t *testing.T) {
 func TestInDependencyReverseDownCommandOrder(t *testing.T) {
-	ctx, cancel := context.WithCancel(context.Background())
-	t.Cleanup(cancel)
-
 	var order []string
 	var order []string
-	err := InReverseDependencyOrder(ctx, createTestProject(), func(ctx context.Context, service string) error {
+	err := InReverseDependencyOrder(t.Context(), createTestProject(), func(ctx context.Context, service string) error {
 		order = append(order, service)
 		order = append(order, service)
 		return nil
 		return nil
 	})
 	})
@@ -429,7 +420,7 @@ func TestWith_RootNodesAndUp(t *testing.T) {
 				return nil
 				return nil
 			})
 			})
 			WithRootNodesAndDown(tt.nodes)(gt)
 			WithRootNodesAndDown(tt.nodes)(gt)
-			err := gt.visit(context.TODO(), graph)
+			err := gt.visit(t.Context(), graph)
 			assert.NilError(t, err)
 			assert.NilError(t, err)
 			sort.Strings(visited)
 			sort.Strings(visited)
 			assert.DeepEqual(t, tt.want, visited)
 			assert.DeepEqual(t, tt.want, visited)

+ 8 - 9
pkg/compose/down_test.go

@@ -17,7 +17,6 @@
 package compose
 package compose
 
 
 import (
 import (
-	"context"
 	"fmt"
 	"fmt"
 	"os"
 	"os"
 	"strings"
 	"strings"
@@ -90,7 +89,7 @@ func TestDown(t *testing.T) {
 	api.EXPECT().NetworkRemove(gomock.Any(), "abc123").Return(nil)
 	api.EXPECT().NetworkRemove(gomock.Any(), "abc123").Return(nil)
 	api.EXPECT().NetworkRemove(gomock.Any(), "def456").Return(nil)
 	api.EXPECT().NetworkRemove(gomock.Any(), "def456").Return(nil)
 
 
-	err = tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{})
+	err = tested.Down(t.Context(), strings.ToLower(testProject), compose.DownOptions{})
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 }
 }
 
 
@@ -139,7 +138,7 @@ func TestDownWithGivenServices(t *testing.T) {
 	api.EXPECT().NetworkInspect(gomock.Any(), "abc123", gomock.Any()).Return(network.Inspect{ID: "abc123"}, nil)
 	api.EXPECT().NetworkInspect(gomock.Any(), "abc123", gomock.Any()).Return(network.Inspect{ID: "abc123"}, nil)
 	api.EXPECT().NetworkRemove(gomock.Any(), "abc123").Return(nil)
 	api.EXPECT().NetworkRemove(gomock.Any(), "abc123").Return(nil)
 
 
-	err = tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{
+	err = tested.Down(t.Context(), strings.ToLower(testProject), compose.DownOptions{
 		Services: []string{"service1", "not-running-service"},
 		Services: []string{"service1", "not-running-service"},
 	})
 	})
 	assert.NilError(t, err)
 	assert.NilError(t, err)
@@ -175,7 +174,7 @@ func TestDownWithSpecifiedServiceButTheServicesAreNotRunning(t *testing.T) {
 			{ID: "def456", Name: "myProject_default", Labels: map[string]string{compose.NetworkLabel: "default"}},
 			{ID: "def456", Name: "myProject_default", Labels: map[string]string{compose.NetworkLabel: "default"}},
 		}, nil)
 		}, nil)
 
 
-	err = tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{
+	err = tested.Down(t.Context(), strings.ToLower(testProject), compose.DownOptions{
 		Services: []string{"not-running-service1", "not-running-service2"},
 		Services: []string{"not-running-service1", "not-running-service2"},
 	})
 	})
 	assert.NilError(t, err)
 	assert.NilError(t, err)
@@ -227,7 +226,7 @@ func TestDownRemoveOrphans(t *testing.T) {
 	api.EXPECT().NetworkInspect(gomock.Any(), "abc123", gomock.Any()).Return(network.Inspect{ID: "abc123"}, nil)
 	api.EXPECT().NetworkInspect(gomock.Any(), "abc123", gomock.Any()).Return(network.Inspect{ID: "abc123"}, nil)
 	api.EXPECT().NetworkRemove(gomock.Any(), "abc123").Return(nil)
 	api.EXPECT().NetworkRemove(gomock.Any(), "abc123").Return(nil)
 
 
-	err = tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{RemoveOrphans: true})
+	err = tested.Down(t.Context(), strings.ToLower(testProject), compose.DownOptions{RemoveOrphans: true})
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 }
 }
 
 
@@ -259,7 +258,7 @@ func TestDownRemoveVolumes(t *testing.T) {
 
 
 	api.EXPECT().VolumeRemove(gomock.Any(), "myProject_volume", true).Return(nil)
 	api.EXPECT().VolumeRemove(gomock.Any(), "myProject_volume", true).Return(nil)
 
 
-	err = tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{Volumes: true})
+	err = tested.Down(t.Context(), strings.ToLower(testProject), compose.DownOptions{Volumes: true})
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 }
 }
 
 
@@ -346,7 +345,7 @@ func TestDownRemoveImages(t *testing.T) {
 
 
 	t.Log("-> docker compose down --rmi=local")
 	t.Log("-> docker compose down --rmi=local")
 	opts.Images = "local"
 	opts.Images = "local"
-	err = tested.Down(context.Background(), strings.ToLower(testProject), opts)
+	err = tested.Down(t.Context(), strings.ToLower(testProject), opts)
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 
 
 	otherImagesToBeRemoved := []string{
 	otherImagesToBeRemoved := []string{
@@ -361,7 +360,7 @@ func TestDownRemoveImages(t *testing.T) {
 
 
 	t.Log("-> docker compose down --rmi=all")
 	t.Log("-> docker compose down --rmi=all")
 	opts.Images = "all"
 	opts.Images = "all"
-	err = tested.Down(context.Background(), strings.ToLower(testProject), opts)
+	err = tested.Down(t.Context(), strings.ToLower(testProject), opts)
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 }
 }
 
 
@@ -406,7 +405,7 @@ func TestDownRemoveImages_NoLabel(t *testing.T) {
 
 
 	api.EXPECT().ImageRemove(gomock.Any(), "testproject-service1:latest", image.RemoveOptions{}).Return(nil, nil)
 	api.EXPECT().ImageRemove(gomock.Any(), "testproject-service1:latest", image.RemoveOptions{}).Return(nil, nil)
 
 
-	err = tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{Images: "local"})
+	err = tested.Down(t.Context(), strings.ToLower(testProject), compose.DownOptions{Images: "local"})
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 }
 }
 
 

+ 2 - 4
pkg/compose/images_test.go

@@ -17,7 +17,6 @@
 package compose
 package compose
 
 
 import (
 import (
-	"context"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
 	"time"
 	"time"
@@ -40,7 +39,6 @@ func TestImages(t *testing.T) {
 	tested, err := NewComposeService(cli)
 	tested, err := NewComposeService(cli)
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 
 
-	ctx := context.Background()
 	args := filters.NewArgs(projectFilter(strings.ToLower(testProject)))
 	args := filters.NewArgs(projectFilter(strings.ToLower(testProject)))
 	listOpts := container.ListOptions{All: true, Filters: args}
 	listOpts := container.ListOptions{All: true, Filters: args}
 	api.EXPECT().ServerVersion(gomock.Any()).Return(types.Version{APIVersion: "1.96"}, nil).AnyTimes()
 	api.EXPECT().ServerVersion(gomock.Any()).Return(types.Version{APIVersion: "1.96"}, nil).AnyTimes()
@@ -56,9 +54,9 @@ func TestImages(t *testing.T) {
 	c2 := containerDetail("service1", "456", "running", "bar:2")
 	c2 := containerDetail("service1", "456", "running", "bar:2")
 	c2.Ports = []container.Port{{PublicPort: 80, PrivatePort: 90, IP: "localhost"}}
 	c2.Ports = []container.Port{{PublicPort: 80, PrivatePort: 90, IP: "localhost"}}
 	c3 := containerDetail("service2", "789", "exited", "foo:1")
 	c3 := containerDetail("service2", "789", "exited", "foo:1")
-	api.EXPECT().ContainerList(ctx, listOpts).Return([]container.Summary{c1, c2, c3}, nil)
+	api.EXPECT().ContainerList(t.Context(), listOpts).Return([]container.Summary{c1, c2, c3}, nil)
 
 
-	images, err := tested.Images(ctx, strings.ToLower(testProject), compose.ImagesOptions{})
+	images, err := tested.Images(t.Context(), strings.ToLower(testProject), compose.ImagesOptions{})
 
 
 	expected := map[string]compose.ImageSummary{
 	expected := map[string]compose.ImageSummary{
 		"123": {
 		"123": {

+ 4 - 6
pkg/compose/kill_test.go

@@ -45,8 +45,7 @@ func TestKillAll(t *testing.T) {
 
 
 	name := strings.ToLower(testProject)
 	name := strings.ToLower(testProject)
 
 
-	ctx := context.Background()
-	api.EXPECT().ContainerList(ctx, container.ListOptions{
+	api.EXPECT().ContainerList(t.Context(), container.ListOptions{
 		Filters: filters.NewArgs(projectFilter(name), hasConfigHashLabel()),
 		Filters: filters.NewArgs(projectFilter(name), hasConfigHashLabel()),
 	}).Return(
 	}).Return(
 		[]container.Summary{testContainer("service1", "123", false), testContainer("service1", "456", false), testContainer("service2", "789", false)}, nil)
 		[]container.Summary{testContainer("service1", "123", false), testContainer("service1", "456", false), testContainer("service2", "789", false)}, nil)
@@ -64,7 +63,7 @@ func TestKillAll(t *testing.T) {
 	api.EXPECT().ContainerKill(anyCancellableContext(), "456", "").Return(nil)
 	api.EXPECT().ContainerKill(anyCancellableContext(), "456", "").Return(nil)
 	api.EXPECT().ContainerKill(anyCancellableContext(), "789", "").Return(nil)
 	api.EXPECT().ContainerKill(anyCancellableContext(), "789", "").Return(nil)
 
 
-	err = tested.Kill(ctx, name, compose.KillOptions{})
+	err = tested.Kill(t.Context(), name, compose.KillOptions{})
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 }
 }
 
 
@@ -82,8 +81,7 @@ func TestKillSignal(t *testing.T) {
 		Filters: filters.NewArgs(projectFilter(name), serviceFilter(serviceName), hasConfigHashLabel()),
 		Filters: filters.NewArgs(projectFilter(name), serviceFilter(serviceName), hasConfigHashLabel()),
 	}
 	}
 
 
-	ctx := context.Background()
-	api.EXPECT().ContainerList(ctx, listOptions).Return([]container.Summary{testContainer(serviceName, "123", false)}, nil)
+	api.EXPECT().ContainerList(t.Context(), listOptions).Return([]container.Summary{testContainer(serviceName, "123", false)}, nil)
 	api.EXPECT().VolumeList(
 	api.EXPECT().VolumeList(
 		gomock.Any(),
 		gomock.Any(),
 		volume.ListOptions{
 		volume.ListOptions{
@@ -96,7 +94,7 @@ func TestKillSignal(t *testing.T) {
 		}, nil)
 		}, nil)
 	api.EXPECT().ContainerKill(anyCancellableContext(), "123", "SIGTERM").Return(nil)
 	api.EXPECT().ContainerKill(anyCancellableContext(), "123", "SIGTERM").Return(nil)
 
 
-	err = tested.Kill(ctx, name, compose.KillOptions{Services: []string{serviceName}, Signal: "SIGTERM"})
+	err = tested.Kill(t.Context(), name, compose.KillOptions{Services: []string{serviceName}, Signal: "SIGTERM"})
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 }
 }
 
 

+ 4 - 7
pkg/compose/logs_test.go

@@ -17,7 +17,6 @@
 package compose
 package compose
 
 
 import (
 import (
-	"context"
 	"io"
 	"io"
 	"strings"
 	"strings"
 	"sync"
 	"sync"
@@ -44,8 +43,7 @@ func TestComposeService_Logs_Demux(t *testing.T) {
 
 
 	name := strings.ToLower(testProject)
 	name := strings.ToLower(testProject)
 
 
-	ctx := context.Background()
-	api.EXPECT().ContainerList(ctx, containerType.ListOptions{
+	api.EXPECT().ContainerList(t.Context(), containerType.ListOptions{
 		All:     true,
 		All:     true,
 		Filters: filters.NewArgs(oneOffFilter(false), projectFilter(name), hasConfigHashLabel()),
 		Filters: filters.NewArgs(oneOffFilter(false), projectFilter(name), hasConfigHashLabel()),
 	}).Return(
 	}).Return(
@@ -87,7 +85,7 @@ func TestComposeService_Logs_Demux(t *testing.T) {
 	}
 	}
 
 
 	consumer := &testLogConsumer{}
 	consumer := &testLogConsumer{}
-	err = tested.Logs(ctx, name, consumer, opts)
+	err = tested.Logs(t.Context(), name, consumer, opts)
 	require.NoError(t, err)
 	require.NoError(t, err)
 
 
 	require.Equal(
 	require.Equal(
@@ -114,8 +112,7 @@ func TestComposeService_Logs_ServiceFiltering(t *testing.T) {
 
 
 	name := strings.ToLower(testProject)
 	name := strings.ToLower(testProject)
 
 
-	ctx := context.Background()
-	api.EXPECT().ContainerList(ctx, containerType.ListOptions{
+	api.EXPECT().ContainerList(t.Context(), containerType.ListOptions{
 		All:     true,
 		All:     true,
 		Filters: filters.NewArgs(oneOffFilter(false), projectFilter(name), hasConfigHashLabel()),
 		Filters: filters.NewArgs(oneOffFilter(false), projectFilter(name), hasConfigHashLabel()),
 	}).Return(
 	}).Return(
@@ -157,7 +154,7 @@ func TestComposeService_Logs_ServiceFiltering(t *testing.T) {
 	opts := compose.LogOptions{
 	opts := compose.LogOptions{
 		Project: proj,
 		Project: proj,
 	}
 	}
-	err = tested.Logs(ctx, name, consumer, opts)
+	err = tested.Logs(t.Context(), name, consumer, opts)
 	require.NoError(t, err)
 	require.NoError(t, err)
 
 
 	require.Equal(t, []string{"hello c1"}, consumer.LogsForContainer("c1"))
 	require.Equal(t, []string{"hello c1"}, consumer.LogsForContainer("c1"))

+ 2 - 4
pkg/compose/ps_test.go

@@ -17,7 +17,6 @@
 package compose
 package compose
 
 
 import (
 import (
-	"context"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
 
 
@@ -37,7 +36,6 @@ func TestPs(t *testing.T) {
 	tested, err := NewComposeService(cli)
 	tested, err := NewComposeService(cli)
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 
 
-	ctx := context.Background()
 	args := filters.NewArgs(projectFilter(strings.ToLower(testProject)), hasConfigHashLabel())
 	args := filters.NewArgs(projectFilter(strings.ToLower(testProject)), hasConfigHashLabel())
 	args.Add("label", "com.docker.compose.oneoff=False")
 	args.Add("label", "com.docker.compose.oneoff=False")
 	listOpts := containerType.ListOptions{Filters: args, All: false}
 	listOpts := containerType.ListOptions{Filters: args, All: false}
@@ -45,12 +43,12 @@ func TestPs(t *testing.T) {
 	c2, inspect2 := containerDetails("service1", "456", containerType.StateRunning, "", 0)
 	c2, inspect2 := containerDetails("service1", "456", containerType.StateRunning, "", 0)
 	c2.Ports = []containerType.Port{{PublicPort: 80, PrivatePort: 90, IP: "localhost"}}
 	c2.Ports = []containerType.Port{{PublicPort: 80, PrivatePort: 90, IP: "localhost"}}
 	c3, inspect3 := containerDetails("service2", "789", containerType.StateExited, "", 130)
 	c3, inspect3 := containerDetails("service2", "789", containerType.StateExited, "", 130)
-	api.EXPECT().ContainerList(ctx, listOpts).Return([]containerType.Summary{c1, c2, c3}, nil)
+	api.EXPECT().ContainerList(t.Context(), listOpts).Return([]containerType.Summary{c1, c2, c3}, nil)
 	api.EXPECT().ContainerInspect(anyCancellableContext(), "123").Return(inspect1, nil)
 	api.EXPECT().ContainerInspect(anyCancellableContext(), "123").Return(inspect1, nil)
 	api.EXPECT().ContainerInspect(anyCancellableContext(), "456").Return(inspect2, nil)
 	api.EXPECT().ContainerInspect(anyCancellableContext(), "456").Return(inspect2, nil)
 	api.EXPECT().ContainerInspect(anyCancellableContext(), "789").Return(inspect3, nil)
 	api.EXPECT().ContainerInspect(anyCancellableContext(), "789").Return(inspect3, nil)
 
 
-	containers, err := tested.Ps(ctx, strings.ToLower(testProject), compose.PsOptions{})
+	containers, err := tested.Ps(t.Context(), strings.ToLower(testProject), compose.PsOptions{})
 
 
 	expected := []compose.ContainerSummary{
 	expected := []compose.ContainerSummary{
 		{
 		{

+ 2 - 3
pkg/compose/publish_test.go

@@ -17,7 +17,6 @@
 package compose
 package compose
 
 
 import (
 import (
-	"context"
 	"slices"
 	"slices"
 	"testing"
 	"testing"
 
 
@@ -32,7 +31,7 @@ import (
 )
 )
 
 
 func Test_createLayers(t *testing.T) {
 func Test_createLayers(t *testing.T) {
-	project, err := loader.LoadWithContext(context.TODO(), types.ConfigDetails{
+	project, err := loader.LoadWithContext(t.Context(), types.ConfigDetails{
 		WorkingDir:  "testdata/publish/",
 		WorkingDir:  "testdata/publish/",
 		Environment: types.Mapping{},
 		Environment: types.Mapping{},
 		ConfigFiles: []types.ConfigFile{
 		ConfigFiles: []types.ConfigFile{
@@ -45,7 +44,7 @@ func Test_createLayers(t *testing.T) {
 	project.ComposeFiles = []string{"testdata/publish/compose.yaml"}
 	project.ComposeFiles = []string{"testdata/publish/compose.yaml"}
 
 
 	service := &composeService{}
 	service := &composeService{}
-	layers, err := service.createLayers(context.TODO(), project, api.PublishOptions{
+	layers, err := service.createLayers(t.Context(), project, api.PublishOptions{
 		WithEnvironment: true,
 		WithEnvironment: true,
 	})
 	})
 	assert.NilError(t, err)
 	assert.NilError(t, err)

+ 1 - 3
pkg/compose/stop_test.go

@@ -17,7 +17,6 @@
 package compose
 package compose
 
 
 import (
 import (
-	"context"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
 	"time"
 	"time"
@@ -41,7 +40,6 @@ func TestStopTimeout(t *testing.T) {
 	tested, err := NewComposeService(cli)
 	tested, err := NewComposeService(cli)
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 
 
-	ctx := context.Background()
 	api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt(false)).Return(
 	api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt(false)).Return(
 		[]container.Summary{
 		[]container.Summary{
 			testContainer("service1", "123", false),
 			testContainer("service1", "123", false),
@@ -63,7 +61,7 @@ func TestStopTimeout(t *testing.T) {
 	api.EXPECT().ContainerStop(gomock.Any(), "456", stopConfig).Return(nil)
 	api.EXPECT().ContainerStop(gomock.Any(), "456", stopConfig).Return(nil)
 	api.EXPECT().ContainerStop(gomock.Any(), "789", stopConfig).Return(nil)
 	api.EXPECT().ContainerStop(gomock.Any(), "789", stopConfig).Return(nil)
 
 
-	err = tested.Stop(ctx, strings.ToLower(testProject), compose.StopOptions{
+	err = tested.Stop(t.Context(), strings.ToLower(testProject), compose.StopOptions{
 		Timeout: &timeout,
 		Timeout: &timeout,
 	})
 	})
 	assert.NilError(t, err)
 	assert.NilError(t, err)

+ 2 - 5
pkg/compose/viz_test.go

@@ -17,7 +17,6 @@
 package compose
 package compose
 
 
 import (
 import (
-	"context"
 	"strconv"
 	"strconv"
 	"testing"
 	"testing"
 
 
@@ -119,10 +118,8 @@ func TestViz(t *testing.T) {
 	tested, err := NewComposeService(cli)
 	tested, err := NewComposeService(cli)
 	require.NoError(t, err)
 	require.NoError(t, err)
 
 
-	ctx := context.Background()
-
 	t.Run("viz (no ports, networks or image)", func(t *testing.T) {
 	t.Run("viz (no ports, networks or image)", func(t *testing.T) {
-		graphStr, err := tested.Viz(ctx, &project, compose.VizOptions{
+		graphStr, err := tested.Viz(t.Context(), &project, compose.VizOptions{
 			Indentation:      "  ",
 			Indentation:      "  ",
 			IncludePorts:     false,
 			IncludePorts:     false,
 			IncludeImageName: false,
 			IncludeImageName: false,
@@ -181,7 +178,7 @@ func TestViz(t *testing.T) {
 	})
 	})
 
 
 	t.Run("viz (with ports, networks and image)", func(t *testing.T) {
 	t.Run("viz (with ports, networks and image)", func(t *testing.T) {
-		graphStr, err := tested.Viz(ctx, &project, compose.VizOptions{
+		graphStr, err := tested.Viz(t.Context(), &project, compose.VizOptions{
 			Indentation:      "\t",
 			Indentation:      "\t",
 			IncludePorts:     true,
 			IncludePorts:     true,
 			IncludeImageName: true,
 			IncludeImageName: true,

+ 4 - 7
pkg/compose/volumes_test.go

@@ -17,7 +17,6 @@
 package compose
 package compose
 
 
 import (
 import (
-	"context"
 	"testing"
 	"testing"
 
 
 	"github.com/docker/docker/api/types/container"
 	"github.com/docker/docker/api/types/container"
@@ -58,7 +57,6 @@ func TestVolumes(t *testing.T) {
 		},
 		},
 	}
 	}
 
 
-	ctx := context.Background()
 	args := filters.NewArgs(projectFilter(testProject))
 	args := filters.NewArgs(projectFilter(testProject))
 	listOpts := container.ListOptions{Filters: args}
 	listOpts := container.ListOptions{Filters: args}
 	volumeListArgs := filters.NewArgs(projectFilter(testProject))
 	volumeListArgs := filters.NewArgs(projectFilter(testProject))
@@ -68,20 +66,19 @@ func TestVolumes(t *testing.T) {
 	}
 	}
 	containerReturn := []container.Summary{c1, c2}
 	containerReturn := []container.Summary{c1, c2}
 
 
-	// Mock API calls
-	mockApi.EXPECT().ContainerList(ctx, listOpts).Times(2).Return(containerReturn, nil)
-	mockApi.EXPECT().VolumeList(ctx, volumeListOpts).Times(2).Return(volumeReturn, nil)
+	mockApi.EXPECT().ContainerList(t.Context(), listOpts).Times(2).Return(containerReturn, nil)
+	mockApi.EXPECT().VolumeList(t.Context(), volumeListOpts).Times(2).Return(volumeReturn, nil)
 
 
 	// Test without service filter - should return all project volumes
 	// Test without service filter - should return all project volumes
 	volumeOptions := api.VolumesOptions{}
 	volumeOptions := api.VolumesOptions{}
-	volumes, err := tested.Volumes(ctx, testProject, volumeOptions)
+	volumes, err := tested.Volumes(t.Context(), testProject, volumeOptions)
 	expected := []api.VolumesSummary{vol1, vol2, vol3}
 	expected := []api.VolumesSummary{vol1, vol2, vol3}
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 	assert.DeepEqual(t, volumes, expected)
 	assert.DeepEqual(t, volumes, expected)
 
 
 	// Test with service filter - should only return volumes used by service1
 	// Test with service filter - should only return volumes used by service1
 	volumeOptions = api.VolumesOptions{Services: []string{"service1"}}
 	volumeOptions = api.VolumesOptions{Services: []string{"service1"}}
-	volumes, err = tested.Volumes(ctx, testProject, volumeOptions)
+	volumes, err = tested.Volumes(t.Context(), testProject, volumeOptions)
 	expected = []api.VolumesSummary{vol1, vol2}
 	expected = []api.VolumesSummary{vol1, vol2}
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 	assert.DeepEqual(t, volumes, expected)
 	assert.DeepEqual(t, volumes, expected)

+ 1 - 1
pkg/compose/watch_test.go

@@ -95,7 +95,7 @@ func TestWatch_Sync(t *testing.T) {
 	//
 	//
 	cli.EXPECT().Client().Return(apiClient).AnyTimes()
 	cli.EXPECT().Client().Return(apiClient).AnyTimes()
 
 
-	ctx, cancelFunc := context.WithCancel(context.Background())
+	ctx, cancelFunc := context.WithCancel(t.Context())
 	t.Cleanup(cancelFunc)
 	t.Cleanup(cancelFunc)
 
 
 	proj := types.Project{
 	proj := types.Project{

+ 1 - 1
pkg/e2e/cancel_test.go

@@ -39,7 +39,7 @@ func TestComposeCancel(t *testing.T) {
 	t.Run("metrics on cancel Compose build", func(t *testing.T) {
 	t.Run("metrics on cancel Compose build", func(t *testing.T) {
 		const buildProjectPath = "fixtures/build-infinite/compose.yaml"
 		const buildProjectPath = "fixtures/build-infinite/compose.yaml"
 
 
-		ctx, cancel := context.WithCancel(context.Background())
+		ctx, cancel := context.WithCancel(t.Context())
 		defer cancel()
 		defer cancel()
 
 
 		// require a separate groupID from the process running tests, in order to simulate ctrl+C from a terminal.
 		// require a separate groupID from the process running tests, in order to simulate ctrl+C from a terminal.

+ 1 - 1
pkg/e2e/up_test.go

@@ -75,7 +75,7 @@ func TestUpDependenciesNotStopped(t *testing.T) {
 		"app",
 		"app",
 	)
 	)
 
 
-	ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
+	ctx, cancel := context.WithTimeout(t.Context(), 15*time.Second)
 	t.Cleanup(cancel)
 	t.Cleanup(cancel)
 
 
 	cmd, err := StartWithNewGroupID(ctx, testCmd, upOut, nil)
 	cmd, err := StartWithNewGroupID(ctx, testCmd, upOut, nil)

+ 1 - 1
pkg/watch/debounce_test.go

@@ -27,7 +27,7 @@ import (
 func Test_BatchDebounceEvents(t *testing.T) {
 func Test_BatchDebounceEvents(t *testing.T) {
 	ch := make(chan FileEvent)
 	ch := make(chan FileEvent)
 	clock := clockwork.NewFakeClock()
 	clock := clockwork.NewFakeClock()
-	ctx, stop := context.WithCancel(context.Background())
+	ctx, stop := context.WithCancel(t.Context())
 	t.Cleanup(stop)
 	t.Cleanup(stop)
 
 
 	eventBatchCh := BatchDebounceEvents(ctx, clock, ch)
 	eventBatchCh := BatchDebounceEvents(ctx, clock, ch)

+ 1 - 1
pkg/watch/notify_test.go

@@ -501,7 +501,7 @@ type notifyFixture struct {
 
 
 func newNotifyFixture(t *testing.T) *notifyFixture {
 func newNotifyFixture(t *testing.T) *notifyFixture {
 	out := bytes.NewBuffer(nil)
 	out := bytes.NewBuffer(nil)
-	ctx, cancel := context.WithCancel(context.Background())
+	ctx, cancel := context.WithCancel(t.Context())
 	nf := &notifyFixture{
 	nf := &notifyFixture{
 		ctx:            ctx,
 		ctx:            ctx,
 		cancel:         cancel,
 		cancel:         cancel,