Ensure subtests use the right t during setup

This commit is contained in:
William Martin 2023-12-01 17:30:03 +01:00
parent c165d5ccc0
commit 15ba536317
6 changed files with 44 additions and 45 deletions

View file

@ -260,7 +260,7 @@ func Test_loginRun_nontty(t *testing.T) {
opts *LoginOptions
env map[string]string
httpStubs func(*httpmock.Registry)
cfgStubs func(config.Config)
cfgStubs func(*testing.T, config.Config)
wantHosts string
wantErr string
wantStderr string
@ -396,7 +396,7 @@ func Test_loginRun_nontty(t *testing.T) {
Hostname: "github.com",
Token: "newUserToken",
},
cfgStubs: func(c config.Config) {
cfgStubs: func(t *testing.T, c config.Config) {
_, err := c.Authentication().Login("github.com", "monalisa", "abc123", "https", false)
require.NoError(t, err)
},
@ -429,7 +429,7 @@ func Test_loginRun_nontty(t *testing.T) {
cfg, readConfigs := config.NewIsolatedTestConfig(t)
if tt.cfgStubs != nil {
tt.cfgStubs(cfg)
tt.cfgStubs(t, cfg)
}
tt.opts.Config = func() (config.Config, error) {
return cfg, nil
@ -480,7 +480,7 @@ func Test_loginRun_Survey(t *testing.T) {
httpStubs func(*httpmock.Registry)
prompterStubs func(*prompter.PrompterMock)
runStubs func(*run.CommandStubber)
cfgStubs func(config.Config)
cfgStubs func(*testing.T, config.Config)
wantHosts string
wantErrOut *regexp.Regexp
wantSecureToken string
@ -685,7 +685,7 @@ func Test_loginRun_Survey(t *testing.T) {
return -1, prompter.NoSuchPromptErr(prompt)
}
},
cfgStubs: func(c config.Config) {
cfgStubs: func(t *testing.T, c config.Config) {
_, err := c.Authentication().Login("github.com", "monalisa", "abc123", "https", false)
require.NoError(t, err)
},
@ -728,7 +728,7 @@ func Test_loginRun_Survey(t *testing.T) {
cfg, readConfigs := config.NewIsolatedTestConfig(t)
if tt.cfgStubs != nil {
tt.cfgStubs(cfg)
tt.cfgStubs(t, cfg)
}
tt.opts.Config = func() (config.Config, error) {
return cfg, nil

View file

@ -28,7 +28,7 @@ func Test_setupGitRun(t *testing.T) {
name string
opts *SetupGitOptions
setupErr error
cfgStubs func(config.Config)
cfgStubs func(*testing.T, config.Config)
expectedHostsSetup []string
expectedErr string
expectedErrOut string
@ -53,8 +53,8 @@ func Test_setupGitRun(t *testing.T) {
opts: &SetupGitOptions{
Hostname: "foo",
},
cfgStubs: func(cfg config.Config) {
cfg.Authentication().Login("github.com", "test-user", "gho_ABCDEFG", "https", false)
cfgStubs: func(t *testing.T, cfg config.Config) {
login(t, cfg, "github.com", "test-user", "gho_ABCDEFG", "https", false)
},
expectedErr: "You are not logged into the GitHub host \"foo\"\n",
expectedErrOut: "",
@ -63,8 +63,8 @@ func Test_setupGitRun(t *testing.T) {
name: "error setting up git for hostname",
opts: &SetupGitOptions{},
setupErr: fmt.Errorf("broken"),
cfgStubs: func(cfg config.Config) {
cfg.Authentication().Login("github.com", "test-user", "gho_ABCDEFG", "https", false)
cfgStubs: func(t *testing.T, cfg config.Config) {
login(t, cfg, "github.com", "test-user", "gho_ABCDEFG", "https", false)
},
expectedErr: "failed to set up git credential helper: broken",
expectedErrOut: "",
@ -72,9 +72,9 @@ func Test_setupGitRun(t *testing.T) {
{
name: "no hostname option given. Setup git for each hostname in config",
opts: &SetupGitOptions{},
cfgStubs: func(cfg config.Config) {
cfg.Authentication().Login("ghe.io", "test-user", "gho_ABCDEFG", "https", false)
cfg.Authentication().Login("github.com", "test-user", "gho_ABCDEFG", "https", false)
cfgStubs: func(t *testing.T, cfg config.Config) {
login(t, cfg, "ghe.io", "test-user", "gho_ABCDEFG", "https", false)
login(t, cfg, "github.com", "test-user", "gho_ABCDEFG", "https", false)
},
expectedHostsSetup: []string{"github.com", "ghe.io"},
},
@ -83,8 +83,8 @@ func Test_setupGitRun(t *testing.T) {
opts: &SetupGitOptions{
Hostname: "ghe.io",
},
cfgStubs: func(cfg config.Config) {
cfg.Authentication().Login("ghe.io", "test-user", "gho_ABCDEFG", "https", false)
cfgStubs: func(t *testing.T, cfg config.Config) {
login(t, cfg, "ghe.io", "test-user", "gho_ABCDEFG", "https", false)
},
expectedHostsSetup: []string{"ghe.io"},
},
@ -101,7 +101,7 @@ func Test_setupGitRun(t *testing.T) {
cfg, _ := config.NewIsolatedTestConfig(t)
if tt.cfgStubs != nil {
tt.cfgStubs(cfg)
tt.cfgStubs(t, cfg)
}
if tt.opts.Config == nil {

View file

@ -79,7 +79,7 @@ func Test_statusRun(t *testing.T) {
name string
opts StatusOptions
httpStubs func(*httpmock.Registry)
cfgStubs func(config.Config)
cfgStubs func(*testing.T, config.Config)
wantErr error
wantOut string
wantErrOut string
@ -89,7 +89,7 @@ func Test_statusRun(t *testing.T) {
opts: StatusOptions{
Hostname: "github.com",
},
cfgStubs: func(c config.Config) {
cfgStubs: func(t *testing.T, c config.Config) {
login(t, c, "github.com", "monalisa", "abc123", "https")
},
httpStubs: func(reg *httpmock.Registry) {
@ -108,7 +108,7 @@ func Test_statusRun(t *testing.T) {
opts: StatusOptions{
Hostname: "ghe.io",
},
cfgStubs: func(c config.Config) {
cfgStubs: func(t *testing.T, c config.Config) {
login(t, c, "github.com", "monalisa", "gho_abc123", "https")
login(t, c, "ghe.io", "monalisa-ghe", "gho_abc123", "https")
},
@ -128,7 +128,7 @@ func Test_statusRun(t *testing.T) {
{
name: "missing scope",
opts: StatusOptions{},
cfgStubs: func(c config.Config) {
cfgStubs: func(t *testing.T, c config.Config) {
login(t, c, "ghe.io", "monalisa-ghe", "gho_abc123", "https")
},
httpStubs: func(reg *httpmock.Registry) {
@ -149,7 +149,7 @@ func Test_statusRun(t *testing.T) {
{
name: "bad token",
opts: StatusOptions{},
cfgStubs: func(c config.Config) {
cfgStubs: func(t *testing.T, c config.Config) {
login(t, c, "ghe.io", "monalisa-ghe", "gho_abc123", "https")
},
httpStubs: func(reg *httpmock.Registry) {
@ -168,7 +168,7 @@ func Test_statusRun(t *testing.T) {
{
name: "all good",
opts: StatusOptions{},
cfgStubs: func(c config.Config) {
cfgStubs: func(t *testing.T, c config.Config) {
login(t, c, "github.com", "monalisa", "gho_abc123", "https")
login(t, c, "ghe.io", "monalisa-ghe", "gho_abc123", "ssh")
},
@ -201,7 +201,7 @@ func Test_statusRun(t *testing.T) {
{
name: "server-to-server token",
opts: StatusOptions{},
cfgStubs: func(c config.Config) {
cfgStubs: func(t *testing.T, c config.Config) {
login(t, c, "github.com", "monalisa", "ghs_abc123", "https")
},
httpStubs: func(reg *httpmock.Registry) {
@ -221,7 +221,7 @@ func Test_statusRun(t *testing.T) {
{
name: "PAT V2 token",
opts: StatusOptions{},
cfgStubs: func(c config.Config) {
cfgStubs: func(t *testing.T, c config.Config) {
login(t, c, "github.com", "monalisa", "github_pat_abc123", "https")
},
httpStubs: func(reg *httpmock.Registry) {
@ -243,7 +243,7 @@ func Test_statusRun(t *testing.T) {
opts: StatusOptions{
ShowToken: true,
},
cfgStubs: func(c config.Config) {
cfgStubs: func(t *testing.T, c config.Config) {
login(t, c, "github.com", "monalisa", "gho_abc123", "https")
login(t, c, "ghe.io", "monalisa-ghe", "gho_xyz456", "https")
},
@ -274,7 +274,7 @@ func Test_statusRun(t *testing.T) {
opts: StatusOptions{
Hostname: "github.example.com",
},
cfgStubs: func(c config.Config) {
cfgStubs: func(t *testing.T, c config.Config) {
login(t, c, "github.com", "monalisa", "abc123", "https")
},
httpStubs: func(reg *httpmock.Registry) {},
@ -284,7 +284,7 @@ func Test_statusRun(t *testing.T) {
{
name: "multiple accounts on a host",
opts: StatusOptions{},
cfgStubs: func(c config.Config) {
cfgStubs: func(t *testing.T, c config.Config) {
login(t, c, "github.com", "monalisa", "gho_abc123", "https")
login(t, c, "github.com", "monalisa-2", "gho_abc123", "https")
},
@ -319,7 +319,7 @@ func Test_statusRun(t *testing.T) {
tt.opts.IO = ios
cfg, _ := config.NewIsolatedTestConfig(t)
if tt.cfgStubs != nil {
tt.cfgStubs(cfg)
tt.cfgStubs(t, cfg)
}
tt.opts.Config = func() (config.Config, error) {
return cfg, nil

View file

@ -86,7 +86,7 @@ func TestTokenRun(t *testing.T) {
name string
opts TokenOptions
env map[string]string
cfgStubs func(config.Config)
cfgStubs func(*testing.T, config.Config)
wantStdout string
wantErr bool
wantErrMsg string
@ -94,7 +94,7 @@ func TestTokenRun(t *testing.T) {
{
name: "token",
opts: TokenOptions{},
cfgStubs: func(cfg config.Config) {
cfgStubs: func(t *testing.T, cfg config.Config) {
login(t, cfg, "github.com", "test-user", "gho_ABCDEFG", "https", false)
},
wantStdout: "gho_ABCDEFG\n",
@ -104,7 +104,7 @@ func TestTokenRun(t *testing.T) {
opts: TokenOptions{
Hostname: "github.mycompany.com",
},
cfgStubs: func(cfg config.Config) {
cfgStubs: func(t *testing.T, cfg config.Config) {
login(t, cfg, "github.com", "test-user", "gho_ABCDEFG", "https", false)
login(t, cfg, "github.mycompany.com", "test-user", "gho_1234567", "https", false)
},
@ -119,7 +119,7 @@ func TestTokenRun(t *testing.T) {
{
name: "uses default host when one is not provided",
opts: TokenOptions{},
cfgStubs: func(cfg config.Config) {
cfgStubs: func(t *testing.T, cfg config.Config) {
login(t, cfg, "github.com", "test-user", "gho_ABCDEFG", "https", false)
login(t, cfg, "github.mycompany.com", "test-user", "gho_1234567", "https", false)
},
@ -139,7 +139,7 @@ func TestTokenRun(t *testing.T) {
cfg, _ := config.NewIsolatedTestConfig(t)
if tt.cfgStubs != nil {
tt.cfgStubs(cfg)
tt.cfgStubs(t, cfg)
}
tt.opts.Config = func() (config.Config, error) {
@ -162,7 +162,7 @@ func TestTokenRunSecureStorage(t *testing.T) {
tests := []struct {
name string
opts TokenOptions
cfgStubs func(config.Config)
cfgStubs func(*testing.T, config.Config)
wantStdout string
wantErr bool
wantErrMsg string
@ -170,7 +170,7 @@ func TestTokenRunSecureStorage(t *testing.T) {
{
name: "token",
opts: TokenOptions{},
cfgStubs: func(cfg config.Config) {
cfgStubs: func(t *testing.T, cfg config.Config) {
login(t, cfg, "github.com", "test-user", "gho_ABCDEFG", "https", true)
},
wantStdout: "gho_ABCDEFG\n",
@ -180,7 +180,7 @@ func TestTokenRunSecureStorage(t *testing.T) {
opts: TokenOptions{
Hostname: "mycompany.com",
},
cfgStubs: func(cfg config.Config) {
cfgStubs: func(t *testing.T, cfg config.Config) {
login(t, cfg, "mycompany.com", "test-user", "gho_1234567", "https", true)
},
wantStdout: "gho_1234567\n",
@ -201,7 +201,7 @@ func TestTokenRunSecureStorage(t *testing.T) {
cfg, _ := config.NewIsolatedTestConfig(t)
if tt.cfgStubs != nil {
tt.cfgStubs(cfg)
tt.cfgStubs(t, cfg)
}
tt.opts.Config = func() (config.Config, error) {

View file

@ -211,7 +211,7 @@ func TestRepoFork(t *testing.T) {
httpStubs func(*httpmock.Registry)
execStubs func(*run.CommandStubber)
promptStubs func(*prompter.MockPrompter)
cfgStubs func(config.Config)
cfgStubs func(*testing.T, config.Config)
remotes []*context.Remote
wantOut string
wantErrOut string
@ -254,7 +254,7 @@ func TestRepoFork(t *testing.T) {
Repo: ghrepo.New("OWNER", "REPO"),
},
},
cfgStubs: func(c config.Config) {
cfgStubs: func(_ *testing.T, c config.Config) {
c.Set("", "git_protocol", "")
},
httpStubs: forkPost,
@ -733,7 +733,7 @@ func TestRepoFork(t *testing.T) {
cfg, _ := config.NewIsolatedTestConfig(t)
if tt.cfgStubs != nil {
tt.cfgStubs(cfg)
tt.cfgStubs(t, cfg)
}
tt.opts.Config = func() (config.Config, error) {
return cfg, nil

View file

@ -11,12 +11,11 @@ func Test_CheckAuth(t *testing.T) {
tests := []struct {
name string
env map[string]string
cfgStubs func(config.Config)
cfgStubs func(*testing.T, config.Config)
expected bool
}{
{
name: "no known hosts, no env auth token",
cfgStubs: func(_ config.Config) {},
expected: false,
},
{
@ -26,7 +25,7 @@ func Test_CheckAuth(t *testing.T) {
},
{
name: "known host",
cfgStubs: func(c config.Config) {
cfgStubs: func(t *testing.T, c config.Config) {
_, err := c.Authentication().Login("github.com", "test-user", "test-token", "https", false)
require.NoError(t, err)
},
@ -43,7 +42,7 @@ func Test_CheckAuth(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
cfg, _ := config.NewIsolatedTestConfig(t)
if tt.cfgStubs != nil {
tt.cfgStubs(cfg)
tt.cfgStubs(t, cfg)
}
for k, v := range tt.env {