From bfc684d3f7031b159ee33665ee07695ba62898c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Fri, 16 May 2025 19:14:28 +0200 Subject: [PATCH] client/client_test: use gotest.tools-style asserts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Gronowski --- client/client_test.go | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index e1712010b0..f700887e7c 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -94,7 +94,7 @@ func TestNewClientWithOpsFromEnv(t *testing.T) { if tc.expectedError != "" { assert.Check(t, is.Error(err, tc.expectedError)) } else { - assert.Check(t, err) + assert.NilError(t, err) assert.Check(t, is.Equal(client.ClientVersion(), tc.expectedVersion)) } @@ -236,17 +236,13 @@ func TestNewClientWithOpsFromEnvSetsDefaultVersion(t *testing.T) { }) client, err := NewClientWithOpts(FromEnv) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) assert.Check(t, is.Equal(client.ClientVersion(), api.DefaultVersion)) const expected = "1.22" t.Setenv("DOCKER_API_VERSION", expected) client, err = NewClientWithOpts(FromEnv) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) assert.Check(t, is.Equal(client.ClientVersion(), expected)) } @@ -268,7 +264,7 @@ func TestNegotiateAPIVersionEmpty(t *testing.T) { // test downgrade client.NegotiateAPIVersionPing(types.Ping{}) - assert.Equal(t, client.ClientVersion(), expected) + assert.Check(t, is.Equal(client.ClientVersion(), expected)) } // TestNegotiateAPIVersion asserts that client.Client can @@ -333,7 +329,7 @@ func TestNegotiateAPIVersion(t *testing.T) { client, err := NewClientWithOpts(opts...) assert.NilError(t, err) client.NegotiateAPIVersionPing(types.Ping{APIVersion: tc.pingVersion}) - assert.Equal(t, tc.expectedVersion, client.ClientVersion()) + assert.Check(t, is.Equal(tc.expectedVersion, client.ClientVersion())) }) } } @@ -349,7 +345,7 @@ func TestNegotiateAPIVersionOverride(t *testing.T) { // test that we honored the env var client.NegotiateAPIVersionPing(types.Ping{APIVersion: "1.24"}) - assert.Equal(t, client.ClientVersion(), expected) + assert.Check(t, is.Equal(client.ClientVersion(), expected)) } // TestNegotiateAPIVersionConnectionFailure asserts that we do not modify the @@ -362,7 +358,7 @@ func TestNegotiateAPIVersionConnectionFailure(t *testing.T) { client.version = expected client.NegotiateAPIVersion(context.Background()) - assert.Equal(t, client.ClientVersion(), expected) + assert.Check(t, is.Equal(client.ClientVersion(), expected)) } func TestNegotiateAPIVersionAutomatic(t *testing.T) { @@ -383,19 +379,19 @@ func TestNegotiateAPIVersionAutomatic(t *testing.T) { // Client defaults to use api.DefaultVersion before version-negotiation. expected := api.DefaultVersion - assert.Equal(t, client.ClientVersion(), expected) + assert.Check(t, is.Equal(client.ClientVersion(), expected)) // First request should trigger negotiation pingVersion = "1.35" expected = "1.35" _, _ = client.Info(ctx) - assert.Equal(t, client.ClientVersion(), expected) + assert.Check(t, is.Equal(client.ClientVersion(), expected)) // Once successfully negotiated, subsequent requests should not re-negotiate pingVersion = "1.25" expected = "1.35" _, _ = client.Info(ctx) - assert.Equal(t, client.ClientVersion(), expected) + assert.Check(t, is.Equal(client.ClientVersion(), expected)) } // TestNegotiateAPIVersionWithEmptyVersion asserts that initializing a client @@ -406,7 +402,7 @@ func TestNegotiateAPIVersionWithEmptyVersion(t *testing.T) { const expected = "1.35" client.NegotiateAPIVersionPing(types.Ping{APIVersion: expected}) - assert.Equal(t, client.ClientVersion(), expected) + assert.Check(t, is.Equal(client.ClientVersion(), expected)) } // TestNegotiateAPIVersionWithFixedVersion asserts that initializing a client @@ -417,7 +413,7 @@ func TestNegotiateAPIVersionWithFixedVersion(t *testing.T) { assert.NilError(t, err) client.NegotiateAPIVersionPing(types.Ping{APIVersion: "1.31"}) - assert.Equal(t, client.ClientVersion(), customVersion) + assert.Check(t, is.Equal(client.ClientVersion(), customVersion)) } // TestCustomAPIVersion tests initializing the client with a custom @@ -462,12 +458,12 @@ func TestCustomAPIVersion(t *testing.T) { t.Run(tc.version, func(t *testing.T) { client, err := NewClientWithOpts(WithVersion(tc.version)) assert.NilError(t, err) - assert.Equal(t, client.ClientVersion(), tc.expected) + assert.Check(t, is.Equal(client.ClientVersion(), tc.expected)) t.Setenv(EnvOverrideAPIVersion, tc.expected) client, err = NewClientWithOpts(WithVersionFromEnv()) assert.NilError(t, err) - assert.Equal(t, client.ClientVersion(), tc.expected) + assert.Check(t, is.Equal(client.ClientVersion(), tc.expected)) }) } } @@ -530,15 +526,15 @@ func TestClientRedirect(t *testing.T) { for _, tc := range tests { t.Run(tc.httpMethod, func(t *testing.T) { req, err := http.NewRequest(tc.httpMethod, "/redirectme", nil) - assert.Check(t, err) + assert.NilError(t, err) resp, err := client.Do(req) assert.Check(t, is.Equal(resp.StatusCode, tc.statusCode)) if tc.expectedErr == nil { - assert.Check(t, err) + assert.NilError(t, err) } else { assert.Check(t, is.ErrorType(err, &url.Error{})) var urlError *url.Error - assert.Assert(t, errors.As(err, &urlError), "%T is not *url.Error", err) + assert.Check(t, errors.As(err, &urlError), "%T is not *url.Error", err) assert.Check(t, is.Equal(*urlError, *tc.expectedErr)) } })