Skip to content

Commit 9a41c73

Browse files
authored
Merge pull request #6796 from thaJeztah/login_cleanup_tests
cli/command/registry: remove uses of "gotest.tools/v3/fs"
2 parents e30ce84 + f5b6055 commit 9a41c73

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

cli/command/registry/login_test.go

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package registry
22

33
import (
4+
"bytes"
45
"context"
56
"errors"
67
"fmt"
@@ -10,6 +11,7 @@ import (
1011
"time"
1112

1213
"github.com/creack/pty"
14+
"github.com/docker/cli/cli/config/configfile"
1315
configtypes "github.com/docker/cli/cli/config/types"
1416
"github.com/docker/cli/cli/streams"
1517
"github.com/docker/cli/internal/prompt"
@@ -19,7 +21,6 @@ import (
1921
"github.com/moby/moby/client"
2022
"gotest.tools/v3/assert"
2123
is "gotest.tools/v3/assert/cmp"
22-
"gotest.tools/v3/fs"
2324
)
2425

2526
const (
@@ -71,8 +72,9 @@ func TestLoginWithCredStoreCreds(t *testing.T) {
7172
},
7273
}
7374
ctx := context.Background()
75+
tmpDir := t.TempDir()
7476
cli := test.NewFakeCli(&fakeClient{})
75-
cli.ConfigFile().Filename = filepath.Join(t.TempDir(), "config.json")
77+
cli.SetConfigFile(configfile.New(filepath.Join(tmpDir, "config.json")))
7678
for _, tc := range testCases {
7779
_, err := loginWithStoredCredentials(ctx, cli, tc.inputAuthConfig)
7880
if tc.expectedErrMsg != "" {
@@ -91,6 +93,7 @@ func TestRunLogin(t *testing.T) {
9193
testCases := []struct {
9294
doc string
9395
priorCredentials map[string]configtypes.AuthConfig
96+
stdIn string
9497
input loginOptions
9598
expectedCredentials map[string]configtypes.AuthConfig
9699
expectedErr string
@@ -286,20 +289,39 @@ func TestRunLogin(t *testing.T) {
286289
},
287290
},
288291
},
292+
{
293+
doc: "password stdin with line-endings",
294+
priorCredentials: map[string]configtypes.AuthConfig{},
295+
stdIn: "my password\r\n",
296+
input: loginOptions{
297+
serverAddress: "reg1",
298+
user: "my-username",
299+
passwordStdin: true,
300+
},
301+
expectedCredentials: map[string]configtypes.AuthConfig{
302+
"reg1": {
303+
Username: "my-username",
304+
Password: "my password",
305+
ServerAddress: "reg1",
306+
},
307+
},
308+
},
289309
}
290310

291311
for _, tc := range testCases {
292312
t.Run(tc.doc, func(t *testing.T) {
293-
tmpFile := fs.NewFile(t, "test-run-login")
294-
defer tmpFile.Remove()
313+
tmpDir := t.TempDir()
314+
cfg := configfile.New(filepath.Join(tmpDir, "config.json"))
295315
cli := test.NewFakeCli(&fakeClient{})
296-
configfile := cli.ConfigFile()
297-
configfile.Filename = tmpFile.Path()
316+
cli.SetConfigFile(cfg)
317+
if tc.input.passwordStdin {
318+
cli.SetIn(streams.NewIn(io.NopCloser(bytes.NewBufferString(tc.stdIn))))
319+
}
298320

299321
for _, priorCred := range tc.priorCredentials {
300-
assert.NilError(t, configfile.GetCredentialsStore(priorCred.ServerAddress).Store(priorCred))
322+
assert.NilError(t, cfg.GetCredentialsStore(priorCred.ServerAddress).Store(priorCred))
301323
}
302-
storedCreds, err := configfile.GetAllCredentials()
324+
storedCreds, err := cfg.GetAllCredentials()
303325
assert.NilError(t, err)
304326
assert.DeepEqual(t, storedCreds, tc.priorCredentials)
305327

@@ -310,7 +332,7 @@ func TestRunLogin(t *testing.T) {
310332
}
311333
assert.NilError(t, loginErr)
312334

313-
outputCreds, err := configfile.GetAllCredentials()
335+
outputCreds, err := cfg.GetAllCredentials()
314336
assert.Check(t, err)
315337
assert.DeepEqual(t, outputCreds, tc.expectedCredentials)
316338
})
@@ -356,11 +378,10 @@ func TestLoginNonInteractive(t *testing.T) {
356378
for _, registryAddr := range registries {
357379
for _, tc := range testCases {
358380
t.Run(tc.doc, func(t *testing.T) {
359-
tmpFile := fs.NewFile(t, "test-run-login")
360-
defer tmpFile.Remove()
381+
tmpDir := t.TempDir()
382+
cfg := configfile.New(filepath.Join(tmpDir, "config.json"))
361383
cli := test.NewFakeCli(&fakeClient{})
362-
cfg := cli.ConfigFile()
363-
cfg.Filename = tmpFile.Path()
384+
cli.SetConfigFile(cfg)
364385
options := loginOptions{
365386
serverAddress: registryAddr,
366387
}
@@ -419,11 +440,10 @@ func TestLoginNonInteractive(t *testing.T) {
419440
for _, registryAddr := range registries {
420441
for _, tc := range testCases {
421442
t.Run(tc.doc, func(t *testing.T) {
422-
tmpFile := fs.NewFile(t, "test-run-login")
423-
defer tmpFile.Remove()
443+
tmpDir := t.TempDir()
444+
cfg := configfile.New(filepath.Join(tmpDir, "config.json"))
424445
cli := test.NewFakeCli(&fakeClient{})
425-
cfg := cli.ConfigFile()
426-
cfg.Filename = tmpFile.Path()
446+
cli.SetConfigFile(cfg)
427447
serverAddress := registryAddr
428448
if serverAddress == "" {
429449
serverAddress = "https://index.docker.io/v1/"
@@ -465,17 +485,15 @@ func TestLoginTermination(t *testing.T) {
465485
_ = p.Close()
466486
})
467487

488+
tmpDir := t.TempDir()
489+
cfg := configfile.New(filepath.Join(tmpDir, "config.json"))
468490
cli := test.NewFakeCli(&fakeClient{}, func(fc *test.FakeCli) {
469491
fc.SetOut(streams.NewOut(tty))
470492
fc.SetIn(streams.NewIn(tty))
471493
})
472-
tmpFile := fs.NewFile(t, "test-login-termination")
473-
defer tmpFile.Remove()
474-
475-
configFile := cli.ConfigFile()
476-
configFile.Filename = tmpFile.Path()
494+
cli.SetConfigFile(cfg)
477495

478-
ctx, cancel := context.WithCancel(context.Background())
496+
ctx, cancel := context.WithCancel(t.Context())
479497
t.Cleanup(cancel)
480498

481499
runErr := make(chan error)

0 commit comments

Comments
 (0)