From 23ea61d325410f2cbfd61874078af4358b535ead Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Tue, 4 Mar 2025 13:39:38 -0500 Subject: [PATCH] Expand gh secret base repo tests Building on top of the work done in commands to account for GH_REPO environment variable, this commit expands existing tests around handling base repo functions to include new test scenarios. These tests fail in the same way as reported on the issue when run against `trunk` without the other branch's changes, demonstrating they will help avoid regression. --- pkg/cmd/secret/delete/delete_test.go | 17 +++++++++++++++-- pkg/cmd/secret/list/list_test.go | 16 ++++++++++++++-- pkg/cmd/secret/set/set_test.go | 17 +++++++++++++++-- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/secret/delete/delete_test.go b/pkg/cmd/secret/delete/delete_test.go index 377879cf2..2f6cad431 100644 --- a/pkg/cmd/secret/delete/delete_test.go +++ b/pkg/cmd/secret/delete/delete_test.go @@ -144,6 +144,7 @@ func TestNewCmdDeleteBaseRepoFuncs(t *testing.T) { tests := []struct { name string args string + env map[string]string prompterStubs func(*prompter.MockPrompter) wantRepo ghrepo.Interface wantErr error @@ -154,14 +155,22 @@ func TestNewCmdDeleteBaseRepoFuncs(t *testing.T) { wantRepo: ghrepo.New("owner", "repo"), }, { - name: "when there is no repo flag provided, and no prompting, the base func requiring no ambiguity is used", + name: "when GH_REPO env var is provided, the factory base repo func is used", + args: "SECRET_NAME", + env: map[string]string{ + "GH_REPO": "owner/repo", + }, + 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", wantErr: shared.AmbiguousBaseRepoError{ Remotes: remotes, }, }, { - name: "when there is no repo flag provided, and can prompt, the base func resolving ambiguity is used", + 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", prompterStubs: func(pm *prompter.MockPrompter) { pm.RegisterSelect( @@ -199,6 +208,10 @@ func TestNewCmdDeleteBaseRepoFuncs(t *testing.T) { }, } + for k, v := range tt.env { + t.Setenv(k, v) + } + argv, err := shlex.Split(tt.args) assert.NoError(t, err) diff --git a/pkg/cmd/secret/list/list_test.go b/pkg/cmd/secret/list/list_test.go index 601de2572..f4ecde73e 100644 --- a/pkg/cmd/secret/list/list_test.go +++ b/pkg/cmd/secret/list/list_test.go @@ -124,6 +124,7 @@ func TestNewCmdListBaseRepoFuncs(t *testing.T) { tests := []struct { name string args string + env map[string]string prompterStubs func(*prompter.MockPrompter) wantRepo ghrepo.Interface wantErr error @@ -134,14 +135,21 @@ func TestNewCmdListBaseRepoFuncs(t *testing.T) { wantRepo: ghrepo.New("owner", "repo"), }, { - name: "when there is no repo flag provided, and no prompting, the base func requiring no ambiguity is used", + name: "when GH_REPO env var is provided, the factory base repo func is used", + env: map[string]string{ + "GH_REPO": "owner/repo", + }, + 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: "", wantErr: shared.AmbiguousBaseRepoError{ Remotes: remotes, }, }, { - name: "when there is no repo flag provided, and can prompt, the base func resolving ambiguity is used", + name: "when there is no repo flag or GH_REPO env var provided, and can prompt, the base func resolving ambiguity is used", args: "", prompterStubs: func(pm *prompter.MockPrompter) { pm.RegisterSelect( @@ -179,6 +187,10 @@ func TestNewCmdListBaseRepoFuncs(t *testing.T) { }, } + for k, v := range tt.env { + t.Setenv(k, v) + } + argv, err := shlex.Split(tt.args) assert.NoError(t, err) diff --git a/pkg/cmd/secret/set/set_test.go b/pkg/cmd/secret/set/set_test.go index 99c8c6cbe..22c7aaab8 100644 --- a/pkg/cmd/secret/set/set_test.go +++ b/pkg/cmd/secret/set/set_test.go @@ -243,6 +243,7 @@ func TestNewCmdSetBaseRepoFuncs(t *testing.T) { tests := []struct { name string args string + env map[string]string prompterStubs func(*prompter.MockPrompter) wantRepo ghrepo.Interface wantErr error @@ -253,14 +254,22 @@ func TestNewCmdSetBaseRepoFuncs(t *testing.T) { wantRepo: ghrepo.New("owner", "repo"), }, { - name: "when there is no repo flag provided, and no prompting, the base func requiring no ambiguity is used", + name: "when GH_REPO env var is provided, the factory base repo func is used", + args: "SECRET_NAME", + env: map[string]string{ + "GH_REPO": "owner/repo", + }, + 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", wantErr: shared.AmbiguousBaseRepoError{ Remotes: remotes, }, }, { - name: "when there is no repo flag provided, and can prompt, the base func resolving ambiguity is used", + 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", prompterStubs: func(pm *prompter.MockPrompter) { pm.RegisterSelect( @@ -298,6 +307,10 @@ func TestNewCmdSetBaseRepoFuncs(t *testing.T) { }, } + for k, v := range tt.env { + t.Setenv(k, v) + } + argv, err := shlex.Split(tt.args) assert.NoError(t, err)