diff options
author | Anthony HAMON <hamon.anth@gmail.com> | 2018-09-10 21:57:08 +0200 |
---|---|---|
committer | Anthony HAMON <hamon.anth@gmail.com> | 2018-09-11 21:56:17 +0200 |
commit | 5c204b28136d3f13baec64868e20a19685253fbb (patch) | |
tree | 3b64e5884f933f6b6603fed2ddaf8063512f6508 /pkg | |
parent | 7d862785073599b7887332a07d37f0ca4b867f7a (diff) |
commands/git: rewrite UsingGpg, add tests
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/commands/git.go | 41 | ||||
-rw-r--r-- | pkg/commands/git_test.go | 101 |
2 files changed, 120 insertions, 22 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index f7a80dc63..2945e1e92 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -59,11 +59,13 @@ func setupRepositoryAndWorktree(openGitRepository func(string) (*gogit.Repositor // GitCommand is our main git interface type GitCommand struct { - Log *logrus.Entry - OSCommand *OSCommand - Worktree *gogit.Worktree - Repo *gogit.Repository - Tr *i18n.Localizer + Log *logrus.Entry + OSCommand *OSCommand + Worktree *gogit.Worktree + Repo *gogit.Repository + Tr *i18n.Localizer + getGlobalGitConfig func(string) (string, error) + getLocalGitConfig func(string) (string, error) } // NewGitCommand it runs git commands @@ -92,11 +94,13 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) } return &GitCommand{ - Log: log, - OSCommand: osCommand, - Tr: tr, - Worktree: worktree, - Repo: repo, + Log: log, + OSCommand: osCommand, + Tr: tr, + Worktree: worktree, + Repo: repo, + getGlobalGitConfig: gitconfig.Global, + getLocalGitConfig: gitconfig.Local, }, nil } @@ -274,23 +278,22 @@ func (c *GitCommand) AbortMerge() error { 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 -func (c *GitCommand) UsingGpg() bool { - gpgsign, _ := gitconfig.Global("commit.gpgsign") +func (c *GitCommand) usingGpg() bool { + gpgsign, _ := c.getGlobalGitConfig("commit.gpgsign") if gpgsign == "" { - gpgsign, _ = gitconfig.Local("commit.gpgsign") + gpgsign, _ = c.getLocalGitConfig("commit.gpgsign") } - if gpgsign == "" { - return false - } - return true + value := strings.ToLower(gpgsign) + + return value == "true" || value == "1" || value == "yes" || value == "on" } // Commit commit to git func (c *GitCommand) Commit(g *gocui.Gui, message string) (*exec.Cmd, error) { 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 nil, c.OSCommand.RunCommand(command) diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 7bc7a8910..5d451174d 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -56,9 +56,11 @@ func newDummyLog() *logrus.Entry { func newDummyGitCommand() *GitCommand { return &GitCommand{ - Log: newDummyLog(), - OSCommand: newDummyOSCommand(), - Tr: i18n.NewLocalizer(newDummyLog()), + Log: newDummyLog(), + OSCommand: newDummyOSCommand(), + 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")) } +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) { gitCommand := newDummyGitCommand() assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh")) |