From 1e52a86d42609fc23243c18d06d5de513ba9a805 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 31 May 2024 10:31:16 +0200 Subject: [PATCH] cli/command/context: add test-utility to create multiple contexts Signed-off-by: Sebastiaan van Stijn (cherry picked from commit 54291dd47a46049bff5bde9bba2d84ed783fb1cb) Signed-off-by: Sebastiaan van Stijn --- cli/command/context/list_test.go | 14 +++++++++----- cli/command/context/remove_test.go | 12 ++++-------- cli/command/context/update_test.go | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/cli/command/context/list_test.go b/cli/command/context/list_test.go index 98ac2e480c..1b264a6e85 100644 --- a/cli/command/context/list_test.go +++ b/cli/command/context/list_test.go @@ -8,6 +8,13 @@ import ( "gotest.tools/v3/golden" ) +func createTestContexts(t *testing.T, cli command.Cli, name ...string) { + t.Helper() + for _, n := range name { + createTestContext(t, cli, n) + } +} + func createTestContext(t *testing.T, cli command.Cli, name string) { t.Helper() @@ -21,9 +28,7 @@ func createTestContext(t *testing.T, cli command.Cli, name string) { func TestList(t *testing.T) { cli := makeFakeCli(t) - createTestContext(t, cli, "current") - createTestContext(t, cli, "other") - createTestContext(t, cli, "unset") + createTestContexts(t, cli, "current", "other", "unset") cli.SetCurrentContext("current") cli.OutBuffer().Reset() assert.NilError(t, runList(cli, &listOptions{})) @@ -32,8 +37,7 @@ func TestList(t *testing.T) { func TestListQuiet(t *testing.T) { cli := makeFakeCli(t) - createTestContext(t, cli, "current") - createTestContext(t, cli, "other") + createTestContexts(t, cli, "current", "other") cli.SetCurrentContext("current") cli.OutBuffer().Reset() assert.NilError(t, runList(cli, &listOptions{quiet: true})) diff --git a/cli/command/context/remove_test.go b/cli/command/context/remove_test.go index 35e27781b2..f42c4fe7eb 100644 --- a/cli/command/context/remove_test.go +++ b/cli/command/context/remove_test.go @@ -13,8 +13,7 @@ import ( func TestRemove(t *testing.T) { cli := makeFakeCli(t) - createTestContext(t, cli, "current") - createTestContext(t, cli, "other") + createTestContexts(t, cli, "current", "other") assert.NilError(t, RunRemove(cli, RemoveOptions{}, []string{"other"})) _, err := cli.ContextStore().GetMetadata("current") assert.NilError(t, err) @@ -24,8 +23,7 @@ func TestRemove(t *testing.T) { func TestRemoveNotAContext(t *testing.T) { cli := makeFakeCli(t) - createTestContext(t, cli, "current") - createTestContext(t, cli, "other") + createTestContexts(t, cli, "current", "other") err := RunRemove(cli, RemoveOptions{}, []string{"not-a-context"}) assert.ErrorContains(t, err, `context "not-a-context" does not exist`) @@ -35,8 +33,7 @@ func TestRemoveNotAContext(t *testing.T) { func TestRemoveCurrent(t *testing.T) { cli := makeFakeCli(t) - createTestContext(t, cli, "current") - createTestContext(t, cli, "other") + createTestContexts(t, cli, "current", "other") cli.SetCurrentContext("current") err := RunRemove(cli, RemoveOptions{}, []string{"current"}) assert.ErrorContains(t, err, `context "current" is in use, set -f flag to force remove`) @@ -50,8 +47,7 @@ func TestRemoveCurrentForce(t *testing.T) { assert.NilError(t, testCfg.Save()) cli := makeFakeCli(t, withCliConfig(testCfg)) - createTestContext(t, cli, "current") - createTestContext(t, cli, "other") + createTestContexts(t, cli, "current", "other") cli.SetCurrentContext("current") assert.NilError(t, RunRemove(cli, RemoveOptions{Force: true}, []string{"current"})) reloadedConfig, err := config.Load(configDir) diff --git a/cli/command/context/update_test.go b/cli/command/context/update_test.go index 5bad6dec2f..553f67fd1a 100644 --- a/cli/command/context/update_test.go +++ b/cli/command/context/update_test.go @@ -6,7 +6,7 @@ import ( "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/context/docker" "gotest.tools/v3/assert" - "gotest.tools/v3/assert/cmp" + is "gotest.tools/v3/assert/cmp" ) func TestUpdateDescriptionOnly(t *testing.T) { @@ -46,7 +46,7 @@ func TestUpdateDockerOnly(t *testing.T) { dc, err := command.GetDockerContext(c) assert.NilError(t, err) assert.Equal(t, dc.Description, "description of test") - assert.Check(t, cmp.Contains(c.Endpoints, docker.DockerEndpoint)) + assert.Check(t, is.Contains(c.Endpoints, docker.DockerEndpoint)) assert.Equal(t, c.Endpoints[docker.DockerEndpoint].(docker.EndpointMeta).Host, "tcp://some-host") }