Skip to content

Commit 8845ccd

Browse files
committed
cli/command/registry: login: add unit test for flag validation
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent aadd787 commit 8845ccd

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

cli/command/registry/login_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"io"
78
"path/filepath"
89
"testing"
910
"time"
@@ -539,3 +540,51 @@ func TestIsOauthLoginDisabled(t *testing.T) {
539540
assert.Equal(t, disabled, tc.disabled)
540541
}
541542
}
543+
544+
func TestLoginValidateFlags(t *testing.T) {
545+
for _, tc := range []struct {
546+
name string
547+
args []string
548+
expectedErr string
549+
}{
550+
{
551+
name: "--password-stdin without --username",
552+
args: []string{"--password-stdin"},
553+
expectedErr: `Must provide --username with --password-stdin`,
554+
},
555+
{
556+
name: "--password-stdin with empty --username",
557+
args: []string{"--password-stdin", "--username", ""},
558+
expectedErr: `Must provide --username with --password-stdin`,
559+
},
560+
{
561+
name: "--username without value",
562+
args: []string{"--username"},
563+
expectedErr: `flag needs an argument: --username`,
564+
},
565+
{
566+
name: "conflicting options --password-stdin and --password",
567+
args: []string{"--password-stdin", "--password", ""},
568+
expectedErr: `Must provide --username with --password-stdin`,
569+
},
570+
{
571+
name: "--password without value",
572+
args: []string{"--password"},
573+
expectedErr: `flag needs an argument: --password`,
574+
},
575+
} {
576+
t.Run(tc.name, func(t *testing.T) {
577+
cmd := NewLoginCommand(test.NewFakeCli(&fakeClient{}))
578+
cmd.SetOut(io.Discard)
579+
cmd.SetErr(io.Discard)
580+
cmd.SetArgs(tc.args)
581+
582+
err := cmd.Execute()
583+
if tc.expectedErr != "" {
584+
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
585+
} else {
586+
assert.Check(t, is.Nil(err))
587+
}
588+
})
589+
}
590+
}

0 commit comments

Comments
 (0)