From 7667fbdb5a71df954d5b5783359b51db5a59360b Mon Sep 17 00:00:00 2001 From: William Martin Date: Wed, 29 Nov 2023 17:06:37 +0100 Subject: [PATCH] Handle logout having no candidates --- pkg/cmd/auth/logout/logout.go | 4 +++- pkg/cmd/auth/logout/logout_test.go | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/auth/logout/logout.go b/pkg/cmd/auth/logout/logout.go index 5077cdae1..a400dfc93 100644 --- a/pkg/cmd/auth/logout/logout.go +++ b/pkg/cmd/auth/logout/logout.go @@ -110,7 +110,9 @@ func logoutRun(opts *LogoutOptions) error { } } - if len(candidates) == 1 { + if len(candidates) == 0 { + return errors.New("no user accounts matched that criteria") + } else if len(candidates) == 1 { hostname = candidates[0].host username = candidates[0].user } else if !opts.IO.CanPrompt() { diff --git a/pkg/cmd/auth/logout/logout_test.go b/pkg/cmd/auth/logout/logout_test.go index 57f40a59c..d3bfa6ca2 100644 --- a/pkg/cmd/auth/logout/logout_test.go +++ b/pkg/cmd/auth/logout/logout_test.go @@ -240,6 +240,17 @@ func Test_logoutRun_tty(t *testing.T) { }, wantErr: "not logged in as unknown-user on ghe.io", }, + { + name: "errors when user is specified but doesn't exist on any host", + opts: &LogoutOptions{ + Username: "unknown-user", + }, + cfgHosts: []hostUsers{ + {"github.com", []user{"monalisa"}}, + {"ghe.io", []user{"monalisa"}}, + }, + wantErr: "no user accounts matched that criteria", + }, } for _, tt := range tests {