Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/cmd/secret/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
RunE: func(cmd *cobra.Command, args []string) error {
// If the user specified a repo directly, then we're using the OverrideBaseRepoFunc set by EnableRepoOverride
// So there's no reason to use the specialised BaseRepoFunc that requires remote disambiguation.
if cmd.Flags().Changed("repo") || os.Getenv("GH_REPO") != "" {
opts.BaseRepo = f.BaseRepo
} else {
opts.BaseRepo = f.BaseRepo
isRepoUserProvided := cmd.Flags().Changed("repo") || os.Getenv("GH_REPO") != ""
if !isRepoUserProvided {
// If they haven't specified a repo directly, then we will wrap the BaseRepoFunc in one that errors if
// there might be multiple valid remotes.
opts.BaseRepo = shared.RequireNoAmbiguityBaseRepoFunc(opts.BaseRepo, f.Remotes)
Expand Down
34 changes: 27 additions & 7 deletions pkg/cmd/secret/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestNewCmdDelete(t *testing.T) {
}

func TestNewCmdDeleteBaseRepoFuncs(t *testing.T) {
remotes := ghContext.Remotes{
multipleRemotes := ghContext.Remotes{
&ghContext.Remote{
Remote: &git.Remote{
Name: "origin",
Expand All @@ -141,17 +141,28 @@ func TestNewCmdDeleteBaseRepoFuncs(t *testing.T) {
},
}

singleRemote := ghContext.Remotes{
&ghContext.Remote{
Remote: &git.Remote{
Name: "origin",
},
Repo: ghrepo.New("owner", "repo"),
},
}

tests := []struct {
name string
args string
env map[string]string
remotes ghContext.Remotes
prompterStubs func(*prompter.MockPrompter)
wantRepo ghrepo.Interface
wantErr error
}{
{
name: "when there is a repo flag provided, the factory base repo func is used",
args: "SECRET_NAME --repo owner/repo",
remotes: multipleRemotes,
wantRepo: ghrepo.New("owner", "repo"),
},
{
Expand All @@ -160,18 +171,27 @@ func TestNewCmdDeleteBaseRepoFuncs(t *testing.T) {
env: map[string]string{
"GH_REPO": "owner/repo",
},
remotes: multipleRemotes,
wantRepo: ghrepo.New("owner", "repo"),
},
{
name: "when there is no repo flag or GH_REPO env var provided, and no prompting, the base func requiring no ambiguity is used",
args: "SECRET_NAME",
name: "when there is no repo flag or GH_REPO env var provided, and no prompting, the base func requiring no ambiguity is used",
args: "SECRET_NAME",
remotes: multipleRemotes,
wantErr: shared.AmbiguousBaseRepoError{
Remotes: remotes,
Remotes: multipleRemotes,
},
},
{
name: "when there is no repo flag or GH_REPO env var provided, and can prompt, the base func resolving ambiguity is used",
args: "SECRET_NAME",
name: "when there is no repo flag or GH_REPO env provided, and there is a single remote, the factory base repo func is used",
args: "SECRET_NAME",
remotes: singleRemote,
wantRepo: ghrepo.New("owner", "repo"),
},
{
name: "when there is no repo flag or GH_REPO env var provided, and can prompt, the base func resolving ambiguity is used",
args: "SECRET_NAME",
remotes: multipleRemotes,
prompterStubs: func(pm *prompter.MockPrompter) {
pm.RegisterSelect(
"Select a repo",
Expand Down Expand Up @@ -204,7 +224,7 @@ func TestNewCmdDeleteBaseRepoFuncs(t *testing.T) {
},
Prompter: pm,
Remotes: func() (ghContext.Remotes, error) {
return remotes, nil
return tt.remotes, nil
},
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/secret/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
RunE: func(cmd *cobra.Command, args []string) error {
// If the user specified a repo directly, then we're using the OverrideBaseRepoFunc set by EnableRepoOverride
// So there's no reason to use the specialised BaseRepoFunc that requires remote disambiguation.
if cmd.Flags().Changed("repo") || os.Getenv("GH_REPO") != "" {
opts.BaseRepo = f.BaseRepo
} else {
opts.BaseRepo = f.BaseRepo
isRepoUserProvided := cmd.Flags().Changed("repo") || os.Getenv("GH_REPO") != ""
if !isRepoUserProvided {
// If they haven't specified a repo directly, then we will wrap the BaseRepoFunc in one that errors if
// there might be multiple valid remotes.
opts.BaseRepo = shared.RequireNoAmbiguityBaseRepoFunc(opts.BaseRepo, f.Remotes)
Expand Down
34 changes: 27 additions & 7 deletions pkg/cmd/secret/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func Test_NewCmdList(t *testing.T) {
}

func TestNewCmdListBaseRepoFuncs(t *testing.T) {
remotes := ghContext.Remotes{
multipleRemotes := ghContext.Remotes{
&ghContext.Remote{
Remote: &git.Remote{
Name: "origin",
Expand All @@ -121,36 +121,56 @@ func TestNewCmdListBaseRepoFuncs(t *testing.T) {
},
}

singleRemote := ghContext.Remotes{
&ghContext.Remote{
Remote: &git.Remote{
Name: "origin",
},
Repo: ghrepo.New("owner", "repo"),
},
}

tests := []struct {
name string
args string
env map[string]string
remotes ghContext.Remotes
prompterStubs func(*prompter.MockPrompter)
wantRepo ghrepo.Interface
wantErr error
}{
{
name: "when there is a repo flag provided, the factory base repo func is used",
args: "--repo owner/repo",
remotes: multipleRemotes,
wantRepo: ghrepo.New("owner", "repo"),
},
{
name: "when GH_REPO env var is provided, the factory base repo func is used",
env: map[string]string{
"GH_REPO": "owner/repo",
},
remotes: multipleRemotes,
wantRepo: ghrepo.New("owner", "repo"),
},
{
name: "when there is no repo flag or GH_REPO env var provided, and no prompting, the base func requiring no ambiguity is used",
args: "",
name: "when there is no repo flag or GH_REPO env var provided, and no prompting, the base func requiring no ambiguity is used",
args: "",
remotes: multipleRemotes,
wantErr: shared.AmbiguousBaseRepoError{
Remotes: remotes,
Remotes: multipleRemotes,
},
},
{
name: "when there is no repo flag or GH_REPO env var provided, and can prompt, the base func resolving ambiguity is used",
args: "",
name: "when there is no repo flag or GH_REPO env provided, and there is a single remote, the factory base repo func is used",
args: "",
remotes: singleRemote,
wantRepo: ghrepo.New("owner", "repo"),
},
{
name: "when there is no repo flag or GH_REPO env var provided, and can prompt, the base func resolving ambiguity is used",
args: "",
remotes: multipleRemotes,
prompterStubs: func(pm *prompter.MockPrompter) {
pm.RegisterSelect(
"Select a repo",
Expand Down Expand Up @@ -183,7 +203,7 @@ func TestNewCmdListBaseRepoFuncs(t *testing.T) {
},
Prompter: pm,
Remotes: func() (ghContext.Remotes, error) {
return remotes, nil
return tt.remotes, nil
},
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/secret/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
RunE: func(cmd *cobra.Command, args []string) error {
// If the user specified a repo directly, then we're using the OverrideBaseRepoFunc set by EnableRepoOverride
// So there's no reason to use the specialised BaseRepoFunc that requires remote disambiguation.
if cmd.Flags().Changed("repo") || os.Getenv("GH_REPO") != "" {
opts.BaseRepo = f.BaseRepo
} else {
opts.BaseRepo = f.BaseRepo
isRepoUserProvided := cmd.Flags().Changed("repo") || os.Getenv("GH_REPO") != ""
if !isRepoUserProvided {
// If they haven't specified a repo directly, then we will wrap the BaseRepoFunc in one that errors if
// there might be multiple valid remotes.
opts.BaseRepo = shared.RequireNoAmbiguityBaseRepoFunc(opts.BaseRepo, f.Remotes)
Expand Down
34 changes: 27 additions & 7 deletions pkg/cmd/secret/set/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func TestNewCmdSet(t *testing.T) {
}

func TestNewCmdSetBaseRepoFuncs(t *testing.T) {
remotes := ghContext.Remotes{
multipleRemotes := ghContext.Remotes{
&ghContext.Remote{
Remote: &git.Remote{
Name: "origin",
Expand All @@ -240,17 +240,28 @@ func TestNewCmdSetBaseRepoFuncs(t *testing.T) {
},
}

singleRemote := ghContext.Remotes{
&ghContext.Remote{
Remote: &git.Remote{
Name: "origin",
},
Repo: ghrepo.New("owner", "repo"),
},
}

tests := []struct {
name string
args string
env map[string]string
remotes ghContext.Remotes
prompterStubs func(*prompter.MockPrompter)
wantRepo ghrepo.Interface
wantErr error
}{
{
name: "when there is a repo flag provided, the factory base repo func is used",
args: "SECRET_NAME --repo owner/repo",
remotes: multipleRemotes,
wantRepo: ghrepo.New("owner", "repo"),
},
{
Expand All @@ -259,18 +270,27 @@ func TestNewCmdSetBaseRepoFuncs(t *testing.T) {
env: map[string]string{
"GH_REPO": "owner/repo",
},
remotes: multipleRemotes,
wantRepo: ghrepo.New("owner", "repo"),
},
{
name: "when there is no repo flag or GH_REPO env var provided, and no prompting, the base func requiring no ambiguity is used",
args: "SECRET_NAME",
name: "when there is no repo flag or GH_REPO env var provided, and no prompting, the base func requiring no ambiguity is used",
args: "SECRET_NAME",
remotes: multipleRemotes,
wantErr: shared.AmbiguousBaseRepoError{
Remotes: remotes,
Remotes: multipleRemotes,
},
},
{
name: "when there is no repo flag or GH_REPO env var provided, and can prompt, the base func resolving ambiguity is used",
args: "SECRET_NAME",
name: "when there is no repo flag or GH_REPO env provided, and there is a single remote, the factory base repo func is used",
args: "SECRET_NAME",
remotes: singleRemote,
wantRepo: ghrepo.New("owner", "repo"),
},
{
name: "when there is no repo flag or GH_REPO env var provided, and can prompt, the base func resolving ambiguity is used",
args: "SECRET_NAME",
remotes: multipleRemotes,
prompterStubs: func(pm *prompter.MockPrompter) {
pm.RegisterSelect(
"Select a repo",
Expand Down Expand Up @@ -303,7 +323,7 @@ func TestNewCmdSetBaseRepoFuncs(t *testing.T) {
},
Prompter: pm,
Remotes: func() (ghContext.Remotes, error) {
return remotes, nil
return tt.remotes, nil
},
}

Expand Down