cli/command/registry: TestLoginWithCredStoreCreds slight refactor

- also check errors that were previously not handled
- use the fakeCLI's buffer instead of creating one manually

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-12-03 18:38:27 +01:00
parent 575e373669
commit 297afb2a2b
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -1,10 +1,10 @@
package registry package registry
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"fmt" "fmt"
"path/filepath"
"testing" "testing"
"time" "time"
@ -55,8 +55,9 @@ func (c *fakeClient) RegistryLogin(_ context.Context, auth registrytypes.AuthCon
func TestLoginWithCredStoreCreds(t *testing.T) { func TestLoginWithCredStoreCreds(t *testing.T) {
testCases := []struct { testCases := []struct {
inputAuthConfig registrytypes.AuthConfig inputAuthConfig registrytypes.AuthConfig
expectedMsg string
expectedErr string expectedErr string
expectedMsg string
expectedErrMsg string
}{ }{
{ {
inputAuthConfig: registrytypes.AuthConfig{}, inputAuthConfig: registrytypes.AuthConfig{},
@ -66,20 +67,25 @@ func TestLoginWithCredStoreCreds(t *testing.T) {
inputAuthConfig: registrytypes.AuthConfig{ inputAuthConfig: registrytypes.AuthConfig{
Username: unknownUser, Username: unknownUser,
}, },
expectedErr: errUnknownUser,
expectedMsg: "Authenticating with existing credentials...\n", expectedMsg: "Authenticating with existing credentials...\n",
expectedErr: fmt.Sprintf("Login did not succeed, error: %s\n", errUnknownUser), expectedErrMsg: fmt.Sprintf("Login did not succeed, error: %s\n", errUnknownUser),
}, },
} }
ctx := context.Background() ctx := context.Background()
for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{}) cli := test.NewFakeCli(&fakeClient{})
errBuf := new(bytes.Buffer) cli.ConfigFile().Filename = filepath.Join(t.TempDir(), "config.json")
cli.SetErr(streams.NewOut(errBuf)) for _, tc := range testCases {
loginWithStoredCredentials(ctx, cli, tc.inputAuthConfig) _, err := loginWithStoredCredentials(ctx, cli, tc.inputAuthConfig)
outputString := cli.OutBuffer().String() if tc.expectedErrMsg != "" {
assert.Check(t, is.Equal(tc.expectedMsg, outputString)) assert.Check(t, is.Error(err, tc.expectedErr))
errorString := errBuf.String() } else {
assert.Check(t, is.Equal(tc.expectedErr, errorString)) assert.NilError(t, err)
}
assert.Check(t, is.Equal(tc.expectedMsg, cli.OutBuffer().String()))
assert.Check(t, is.Equal(tc.expectedErrMsg, cli.ErrBuffer().String()))
cli.ErrBuffer().Reset()
cli.OutBuffer().Reset()
} }
} }