1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-28 16:02:01 +03:00

add some more linters

This commit is contained in:
Jesse Duffield
2022-01-08 15:46:35 +11:00
parent 5d6d894286
commit 18f48a43d5
37 changed files with 104 additions and 60 deletions

View File

@ -46,6 +46,7 @@ func TestBranchGetCommitDifferences(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := NewBranchCommandsWithRunner(s.runner)
pushables, pullables := instance.GetCommitDifferences("HEAD", "@{u}")
@ -93,6 +94,7 @@ func TestBranchDeleteBranch(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := NewBranchCommandsWithRunner(s.runner)
@ -139,6 +141,7 @@ func TestBranchCheckout(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := NewBranchCommandsWithRunner(s.runner)
s.test(instance.Checkout("test", CheckoutOptions{Force: s.force}))
@ -218,6 +221,7 @@ func TestBranchCurrentBranchName(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := NewBranchCommandsWithRunner(s.runner)
s.test(instance.CurrentBranchName())

View File

@ -75,6 +75,7 @@ func TestCommitCommitObj(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
userConfig := config.GetDefaultConfig()
userConfig.Git.Commit.SignOff = s.configSignoff
@ -109,6 +110,7 @@ func TestCommitCreateFixupCommit(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildCommitCommands(commonDeps{runner: s.runner})
s.test(instance.CreateFixupCommit(s.sha))
@ -147,6 +149,7 @@ func TestCommitShowCmdObj(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
userConfig := config.GetDefaultConfig()
userConfig.Git.DiffContextSize = s.contextSize

View File

@ -27,7 +27,7 @@ func NewConfigCommands(
return &ConfigCommands{
Common: common,
gitConfig: gitConfig,
repo: repo,
// repo: repo,
}
}

View File

@ -42,6 +42,7 @@ func TestRebaseRebaseBranch(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildRebaseCommands(commonDeps{runner: s.runner})
s.test(instance.RebaseBranch(s.arg))
@ -62,6 +63,7 @@ func TestRebaseSkipEditorCommand(t *testing.T) {
`^GIT_EDITOR=.*$`,
"^LAZYGIT_CLIENT_COMMAND=EXIT_IMMEDIATELY$",
} {
regexStr := regexStr
foundMatch := utils.IncludesStringFunc(envVars, func(envVar string) bool {
return regexp.MustCompile(regexStr).MatchString(envVar)
})
@ -135,6 +137,7 @@ func TestRebaseDiscardOldFileChanges(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildRebaseCommands(commonDeps{
runner: s.runner,

View File

@ -68,6 +68,7 @@ func TestStashStashEntryCmdObj(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
userConfig := config.GetDefaultConfig()
userConfig.Git.DiffContextSize = s.contextSize

View File

@ -85,6 +85,7 @@ func TestSyncPush(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildSyncCommands(commonDeps{})
s.test(instance.PushCmdObj(s.opts))

View File

@ -53,6 +53,7 @@ func TestWorkingTreeUnstageFile(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
s.test(instance.UnStageFile([]string{"test.txt"}, s.reset))
@ -181,6 +182,7 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner, removeFile: s.removeFile})
err := instance.DiscardAllFileChanges(s.file)
@ -296,6 +298,7 @@ func TestWorkingTreeDiff(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
userConfig := config.GetDefaultConfig()
userConfig.Git.DiffContextSize = s.contextSize
@ -345,6 +348,7 @@ func TestWorkingTreeShowFileDiff(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
userConfig := config.GetDefaultConfig()
userConfig.Git.DiffContextSize = s.contextSize
@ -392,6 +396,7 @@ func TestWorkingTreeCheckoutFile(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
@ -445,6 +450,7 @@ func TestWorkingTreeApplyPatch(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
s.test(instance.ApplyPatch("test", "cached"))
@ -474,6 +480,7 @@ func TestWorkingTreeDiscardUnstagedFileChanges(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
s.test(instance.DiscardUnstagedFileChanges(s.file))
@ -501,6 +508,7 @@ func TestWorkingTreeDiscardAnyUnstagedFileChanges(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
s.test(instance.DiscardAnyUnstagedFileChanges())
@ -528,6 +536,7 @@ func TestWorkingTreeRemoveUntrackedFiles(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
s.test(instance.RemoveUntrackedFiles())
@ -557,6 +566,7 @@ func TestWorkingTreeResetHard(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
s.test(instance.ResetHard(s.ref))

View File

@ -104,6 +104,7 @@ func TestNavigateToRepoRootDirectory(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
s.test(navigateToRepoRootDirectory(s.stat, s.chdir))
})
@ -159,6 +160,7 @@ func TestSetupRepository(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
s.test(setupRepository(s.openGitRepository, s.errorStr))
})
@ -206,6 +208,7 @@ func TestNewGitCommand(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
s.setup()
s.test(NewGitCommand(utils.NewDummyCommon(), oscommands.NewDummyOSCommand(), git_config.NewFakeGitConfig(nil)))
@ -282,6 +285,7 @@ func TestFindDotGitDir(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
s.test(findDotGitDir(s.stat, s.readFile))
})

View File

@ -56,6 +56,7 @@ func TestGetRepoInfoFromURL(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
result, err := s.serviceDefinition.getRepoInfoFromURL(s.repoURL)
assert.NoError(t, err)
@ -222,6 +223,7 @@ func TestGetPullRequestURL(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
tr := i18n.EnglishTranslationSet()
log := &test.FakeFieldLogger{}

View File

@ -274,10 +274,7 @@ func (self *CommitLoader) getNormalRebasingCommits() ([]*models.Commit, error) {
return err
}
content := string(bytesContent)
commit, err := self.commitFromPatch(content)
if err != nil {
return err
}
commit := self.commitFromPatch(content)
commits = append([]*models.Commit{commit}, commits...)
return nil
})
@ -334,7 +331,7 @@ func (self *CommitLoader) getInteractiveRebasingCommits() ([]*models.Commit, err
// From: Lazygit Tester <test@example.com>
// Date: Wed, 5 Dec 2018 21:03:23 +1100
// Subject: second commit on master
func (self *CommitLoader) commitFromPatch(content string) (*models.Commit, error) {
func (self *CommitLoader) commitFromPatch(content string) *models.Commit {
lines := strings.Split(content, "\n")
sha := strings.Split(lines[0], " ")[1]
name := strings.TrimPrefix(lines[3], "Subject: ")
@ -342,7 +339,7 @@ func (self *CommitLoader) commitFromPatch(content string) (*models.Commit, error
Sha: sha,
Name: name,
Status: "rebasing",
}, nil
}
}
func (self *CommitLoader) setCommitMergedStatuses(refName string, commits []*models.Commit) ([]*models.Commit, error) {

View File

@ -186,6 +186,7 @@ func TestGetCommits(t *testing.T) {
}
for _, scenario := range scenarios {
scenario := scenario
t.Run(scenario.testName, func(t *testing.T) {
builder := &CommitLoader{
Common: utils.NewDummyCommon(),

View File

@ -186,6 +186,7 @@ func TestFileGetStatusFiles(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
cmd := oscommands.NewDummyCmdObjBuilder(s.runner)
gitConfig := git_config.NewFakeGitConfig(map[string]string{"status.showUntrackedFiles": "yes"})

View File

@ -48,6 +48,7 @@ func TestGetStashEntries(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
cmd := oscommands.NewDummyCmdObjBuilder(s.runner)

View File

@ -26,11 +26,13 @@ func (self *cmdObjRunner) runWithCredentialHandling(cmdObj ICmdObj) error {
case PROMPT:
return self.RunAndDetectCredentialRequest(cmdObj, self.guiIO.promptForCredentialFn)
case FAIL:
return self.RunAndDetectCredentialRequest(cmdObj, func(CredentialName) string { return "\n" })
return self.RunAndDetectCredentialRequest(cmdObj, func(CredentialType) string { return "\n" })
case NONE:
// we should never land here
return errors.New("runWithCredentialHandling called but cmdObj does not have a a credential strategy")
}
// we should never land here
return errors.New("runWithCredentialHandling called but cmdObj does not have a a credential strategy")
return errors.New("unexpected credential strategy")
}
func (self *cmdObjRunner) Run(cmdObj ICmdObj) error {

View File

@ -13,27 +13,27 @@ import (
"github.com/jesseduffield/lazygit/pkg/utils"
)
type CredentialName string
type CredentialType int
const (
Password CredentialName = "password"
Username = "username"
Passphrase = "passphrase"
Password CredentialType = iota
Username
Passphrase
)
// RunAndDetectCredentialRequest detect a username / password / passphrase question in a command
// promptUserForCredential is a function that gets executed when this function detect you need to fillin a password or passphrase
// The promptUserForCredential argument will be "username", "password" or "passphrase" and expects the user's password/passphrase or username back
func (self *cmdObjRunner) RunAndDetectCredentialRequest(cmdObj ICmdObj, promptUserForCredential func(CredentialName) string) error {
func (self *cmdObjRunner) RunAndDetectCredentialRequest(cmdObj ICmdObj, promptUserForCredential func(CredentialType) string) error {
ttyText := ""
err := self.RunCommandWithOutputLive(cmdObj, func(word string) string {
ttyText = ttyText + " " + word
prompts := map[string]CredentialName{
`.+'s password:`: "password",
`Password\s*for\s*'.+':`: "password",
`Username\s*for\s*'.+':`: "username",
`Enter\s*passphrase\s*for\s*key\s*'.+':`: "passphrase",
prompts := map[string]CredentialType{
`.+'s password:`: Password,
`Password\s*for\s*'.+':`: Password,
`Username\s*for\s*'.+':`: Username,
`Enter\s*passphrase\s*for\s*key\s*'.+':`: Passphrase,
}
for pattern, askFor := range prompts {

View File

@ -27,10 +27,10 @@ type guiIO struct {
// this allows us to request info from the user like username/password, in the event
// that a command requests it.
// the 'credential' arg is something like 'username' or 'password'
promptForCredentialFn func(credential CredentialName) string
promptForCredentialFn func(credential CredentialType) string
}
func NewGuiIO(log *logrus.Entry, logCommandFn func(string, bool), newCmdWriterFn func() io.Writer, promptForCredentialFn func(CredentialName) string) *guiIO {
func NewGuiIO(log *logrus.Entry, logCommandFn func(string, bool), newCmdWriterFn func() io.Writer, promptForCredentialFn func(CredentialType) string) *guiIO {
return &guiIO{
log: log,
logCommandFn: logCommandFn,
@ -44,6 +44,6 @@ func NewNullGuiIO(log *logrus.Entry) *guiIO {
log: log,
logCommandFn: func(string, bool) {},
newCmdWriterFn: func() io.Writer { return ioutil.Discard },
promptForCredentialFn: func(CredentialName) string { return "" },
promptForCredentialFn: func(CredentialType) string { return "" },
}
}

View File

@ -190,6 +190,7 @@ func TestOSCommandCreateTempFile(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
s.test(NewDummyOSCommand().CreateTempFile(s.filename, s.content))
})

View File

@ -22,7 +22,7 @@ func GetHeaderFromDiff(diff string) string {
func GetHunksFromDiff(diff string) []*PatchHunk {
hunks := []*PatchHunk{}
firstLineIdx := -1
var hunkLines []string
var hunkLines []string //nolint:prealloc
pastDiffHeader := false
for lineIdx, line := range strings.SplitAfter(diff, "\n") {

View File

@ -511,6 +511,7 @@ func TestModifyPatchForRange(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
result := ModifiedPatchForRange(nil, s.filename, s.diffText, s.firstLineIndex, s.lastLineIndex, s.reverse, false)
if !assert.Equal(t, s.expected, result) {
@ -538,6 +539,7 @@ func TestLineNumberOfLine(t *testing.T) {
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
result := s.hunk.LineNumberOfLine(s.idx)
if !assert.Equal(t, s.expected, result) {

View File

@ -98,7 +98,7 @@ func (l *PatchLine) render(selected bool, included bool) string {
return coloredString(style.FgCyan, match[1], selected, included) + coloredString(theme.DefaultTextColor, match[2], selected, false)
}
textStyle := theme.DefaultTextColor
var textStyle style.TextStyle
switch l.Kind {
case PATCH_HEADER:
textStyle = textStyle.SetBold()
@ -108,6 +108,8 @@ func (l *PatchLine) render(selected bool, included bool) string {
textStyle = style.FgRed
case COMMIT_SHA:
textStyle = style.FgYellow
default:
textStyle = theme.DefaultTextColor
}
return coloredString(textStyle, content, selected, included)