From 4f67ae08757f3b880691d2496e62cb9c696523af Mon Sep 17 00:00:00 2001 From: Emanuel Bennici Date: Fri, 20 Dec 2024 14:53:36 +0100 Subject: [PATCH] fix: Handle nil value in CleanupNetwork (#2928) The godoc of `CleanupNetwork` states that a `nil` network will result in a no-op. --- network/network_test.go | 5 +++++ testing.go | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/network/network_test.go b/network/network_test.go index bbe5d45c7c..8b83056f43 100644 --- a/network/network_test.go +++ b/network/network_test.go @@ -440,3 +440,8 @@ func TestWithNewNetworkContextTimeout(t *testing.T) { require.Empty(t, req.Networks) require.Empty(t, req.NetworkAliases) } + +func TestCleanupWithNil(t *testing.T) { + var network *testcontainers.DockerNetwork + testcontainers.CleanupNetwork(t, network) +} diff --git a/testing.go b/testing.go index 35ce4f0a39..8502f018d9 100644 --- a/testing.go +++ b/testing.go @@ -83,7 +83,9 @@ func CleanupNetwork(tb testing.TB, network Network) { tb.Helper() tb.Cleanup(func() { - noErrorOrIgnored(tb, network.Remove(context.Background())) + if !isNil(network) { + noErrorOrIgnored(tb, network.Remove(context.Background())) + } }) }