cli/pkg/cmd/auth/shared/git_credential_test.go
copilot-swe-agent[bot] dca59d52b6 Fix linting issues: canonicalheader, embeddedstructfieldcheck, iotamixing, makezero, testableexamples, wastedassign, usetesting, tparallel, unconvert, intrange, iface
- canonicalheader: fix cacheTTL header value to canonical form
- embeddedstructfieldcheck: move embedded fields before regular fields in 4 structs
- iotamixing: split const blocks mixing iota with non-iota constants
- makezero: use make([]T, 0, n) instead of make([]T, n) before appending
- testableexamples: add missing Output: comment to ExampleOption_UnwrapOrZero
- wastedassign: remove wasted initial assignments in 4 locations
- usetesting: replace os.MkdirTemp/os.Setenv with t.TempDir/t.Setenv in tests
- tparallel: add t.Parallel() to 8 top-level test functions
- unconvert: remove 16 unnecessary type conversions
- intrange: convert 3 for loops to use integer range syntax
- iface: consolidate identical EditPrompter and Prompt interfaces via type alias

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-24 11:05:52 +00:00

109 lines
3.5 KiB
Go

package shared
import (
"testing"
"github.com/cli/cli/v2/git"
"github.com/cli/cli/v2/internal/run"
"github.com/cli/cli/v2/pkg/cmd/auth/shared/gitcredentials"
)
func TestSetup_configureExisting(t *testing.T) {
cs, restoreRun := run.Stub()
defer restoreRun(t)
cs.Register(`git credential reject`, 0, "")
cs.Register(`git credential approve`, 0, "")
f := GitCredentialFlow{
helper: gitcredentials.Helper{Cmd: "osxkeychain"},
Updater: &gitcredentials.Updater{
GitClient: &git.Client{GitPath: "some/path/git"},
},
}
if err := f.Setup("example.com", "monalisa", "PASSWD"); err != nil {
t.Errorf("Setup() error = %v", err)
}
}
func TestGitCredentialsSetup_setOurs_GH(t *testing.T) {
cs, restoreRun := run.Stub()
defer restoreRun(t)
cs.Register(`git config --global --replace-all credential\.`, 0, "", func(args []string) {
if key := args[len(args)-2]; key != "credential.https://github.com.helper" {
t.Errorf("git config key was %q", key)
}
if val := args[len(args)-1]; val != "" {
t.Errorf("global credential helper configured to %q", val)
}
})
cs.Register(`git config --global --add credential\.`, 0, "", func(args []string) {
if key := args[len(args)-2]; key != "credential.https://github.com.helper" {
t.Errorf("git config key was %q", key)
}
if val := args[len(args)-1]; val != "!/path/to/gh auth git-credential" {
t.Errorf("global credential helper configured to %q", val)
}
})
cs.Register(`git config --global --replace-all credential\.`, 0, "", func(args []string) {
if key := args[len(args)-2]; key != "credential.https://gist.github.com.helper" {
t.Errorf("git config key was %q", key)
}
if val := args[len(args)-1]; val != "" {
t.Errorf("global credential helper configured to %q", val)
}
})
cs.Register(`git config --global --add credential\.`, 0, "", func(args []string) {
if key := args[len(args)-2]; key != "credential.https://gist.github.com.helper" {
t.Errorf("git config key was %q", key)
}
if val := args[len(args)-1]; val != "!/path/to/gh auth git-credential" {
t.Errorf("global credential helper configured to %q", val)
}
})
f := GitCredentialFlow{
helper: gitcredentials.Helper{},
HelperConfig: &gitcredentials.HelperConfig{
SelfExecutablePath: "/path/to/gh",
GitClient: &git.Client{GitPath: "some/path/git"},
},
}
if err := f.Setup("github.com", "monalisa", "PASSWD"); err != nil {
t.Errorf("Setup() error = %v", err)
}
}
func TestSetup_setOurs_nonGH(t *testing.T) {
cs, restoreRun := run.Stub()
defer restoreRun(t)
cs.Register(`git config --global --replace-all credential\.`, 0, "", func(args []string) {
if key := args[len(args)-2]; key != "credential.https://example.com.helper" {
t.Errorf("git config key was %q", key)
}
if val := args[len(args)-1]; val != "" {
t.Errorf("global credential helper configured to %q", val)
}
})
cs.Register(`git config --global --add credential\.`, 0, "", func(args []string) {
if key := args[len(args)-2]; key != "credential.https://example.com.helper" {
t.Errorf("git config key was %q", key)
}
if val := args[len(args)-1]; val != "!/path/to/gh auth git-credential" {
t.Errorf("global credential helper configured to %q", val)
}
})
f := GitCredentialFlow{
helper: gitcredentials.Helper{},
HelperConfig: &gitcredentials.HelperConfig{
SelfExecutablePath: "/path/to/gh",
GitClient: &git.Client{GitPath: "some/path/git"},
},
}
if err := f.Setup("example.com", "monalisa", "PASSWD"); err != nil {
t.Errorf("Setup() error = %v", err)
}
}