1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-15 00:15:32 +02:00

commands/git: rewrite UsingGpg, add tests

This commit is contained in:
Anthony HAMON
2018-09-10 21:57:08 +02:00
parent 7d86278507
commit 5c204b2813
2 changed files with 120 additions and 22 deletions

View File

@ -64,6 +64,8 @@ type GitCommand struct {
Worktree *gogit.Worktree Worktree *gogit.Worktree
Repo *gogit.Repository Repo *gogit.Repository
Tr *i18n.Localizer Tr *i18n.Localizer
getGlobalGitConfig func(string) (string, error)
getLocalGitConfig func(string) (string, error)
} }
// NewGitCommand it runs git commands // NewGitCommand it runs git commands
@ -97,6 +99,8 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer)
Tr: tr, Tr: tr,
Worktree: worktree, Worktree: worktree,
Repo: repo, Repo: repo,
getGlobalGitConfig: gitconfig.Global,
getLocalGitConfig: gitconfig.Local,
}, nil }, nil
} }
@ -274,23 +278,22 @@ func (c *GitCommand) AbortMerge() error {
return c.OSCommand.RunCommand("git merge --abort") return c.OSCommand.RunCommand("git merge --abort")
} }
// UsingGpg tells us whether the user has gpg enabled so that we can know // usingGpg tells us whether the user has gpg enabled so that we can know
// whether we need to run a subprocess to allow them to enter their password // whether we need to run a subprocess to allow them to enter their password
func (c *GitCommand) UsingGpg() bool { func (c *GitCommand) usingGpg() bool {
gpgsign, _ := gitconfig.Global("commit.gpgsign") gpgsign, _ := c.getGlobalGitConfig("commit.gpgsign")
if gpgsign == "" { if gpgsign == "" {
gpgsign, _ = gitconfig.Local("commit.gpgsign") gpgsign, _ = c.getLocalGitConfig("commit.gpgsign")
} }
if gpgsign == "" { value := strings.ToLower(gpgsign)
return false
} return value == "true" || value == "1" || value == "yes" || value == "on"
return true
} }
// Commit commit to git // Commit commit to git
func (c *GitCommand) Commit(g *gocui.Gui, message string) (*exec.Cmd, error) { func (c *GitCommand) Commit(g *gocui.Gui, message string) (*exec.Cmd, error) {
command := "git commit -m " + c.OSCommand.Quote(message) command := "git commit -m " + c.OSCommand.Quote(message)
if c.UsingGpg() { if c.usingGpg() {
return c.OSCommand.PrepareSubProcess(c.OSCommand.Platform.shell, c.OSCommand.Platform.shellArg, command), nil return c.OSCommand.PrepareSubProcess(c.OSCommand.Platform.shell, c.OSCommand.Platform.shellArg, command), nil
} }
return nil, c.OSCommand.RunCommand(command) return nil, c.OSCommand.RunCommand(command)

View File

@ -59,6 +59,8 @@ func newDummyGitCommand() *GitCommand {
Log: newDummyLog(), Log: newDummyLog(),
OSCommand: newDummyOSCommand(), OSCommand: newDummyOSCommand(),
Tr: i18n.NewLocalizer(newDummyLog()), Tr: i18n.NewLocalizer(newDummyLog()),
getGlobalGitConfig: func(string) (string, error) { return "", nil },
getLocalGitConfig: func(string) (string, error) { return "", nil },
} }
} }
@ -730,6 +732,99 @@ func TestGitCommandMerge(t *testing.T) {
assert.NoError(t, gitCmd.Merge("test")) assert.NoError(t, gitCmd.Merge("test"))
} }
func TestGitCommandUsingGpg(t *testing.T) {
type scenario struct {
testName string
getGlobalGitConfig func(string) (string, error)
getLocalGitConfig func(string) (string, error)
test func(bool)
}
scenarios := []scenario{
{
"Option global and local config commit.gpgsign is not set",
func(string) (string, error) {
return "", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.False(t, gpgEnabled)
},
},
{
"Option global config commit.gpgsign is not set, fallback on local config",
func(string) (string, error) {
return "", nil
},
func(string) (string, error) {
return "true", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
},
{
"Option commit.gpgsign is true",
func(string) (string, error) {
return "True", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
},
{
"Option commit.gpgsign is on",
func(string) (string, error) {
return "ON", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
},
{
"Option commit.gpgsign is yes",
func(string) (string, error) {
return "YeS", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
},
{
"Option commit.gpgsign is 1",
func(string) (string, error) {
return "1", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := newDummyGitCommand()
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig
gitCmd.getLocalGitConfig = s.getLocalGitConfig
s.test(gitCmd.usingGpg())
})
}
}
func TestGitCommandDiff(t *testing.T) { func TestGitCommandDiff(t *testing.T) {
gitCommand := newDummyGitCommand() gitCommand := newDummyGitCommand()
assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh")) assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh"))