From c1b7a216312b4ce75bdf984fda6f59d870183bd3 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Thu, 13 Sep 2018 21:44:26 +0200 Subject: [PATCH] commands/git : move tests --- pkg/commands/git_test.go | 280 +++++++++++++++++++-------------------- 1 file changed, 140 insertions(+), 140 deletions(-) diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 466fa0c55..88ecaea66 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -825,146 +825,6 @@ func TestGitCommandUsingGpg(t *testing.T) { } } -func TestGitCommandCatFile(t *testing.T) { - gitCmd := newDummyGitCommand() - gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd { - assert.EqualValues(t, "cat", cmd) - assert.EqualValues(t, []string{"test.txt"}, args) - - return exec.Command("echo", "-n", "test") - } - - o, err := gitCmd.CatFile("test.txt") - assert.NoError(t, err) - assert.Equal(t, "test", o) -} - -func TestGitCommandStageFile(t *testing.T) { - gitCmd := newDummyGitCommand() - gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd { - assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"add", "test.txt"}, args) - - return exec.Command("echo") - } - - assert.NoError(t, gitCmd.StageFile("test.txt")) -} - -func TestGitCommandUnstageFile(t *testing.T) { - type scenario struct { - testName string - command func(string, ...string) *exec.Cmd - test func(error) - tracked bool - } - - scenarios := []scenario{ - { - "Remove an untracked file from staging", - func(cmd string, args ...string) *exec.Cmd { - assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"rm", "--cached", "test.txt"}, args) - - return exec.Command("echo") - }, - func(err error) { - assert.NoError(t, err) - }, - false, - }, - { - "Remove a tracked file from staging", - func(cmd string, args ...string) *exec.Cmd { - assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"reset", "HEAD", "test.txt"}, args) - - return exec.Command("echo") - }, - func(err error) { - assert.NoError(t, err) - }, - true, - }, - } - - for _, s := range scenarios { - t.Run(s.testName, func(t *testing.T) { - gitCmd := newDummyGitCommand() - gitCmd.OSCommand.command = s.command - s.test(gitCmd.UnStageFile("test.txt", s.tracked)) - }) - } -} - -func TestGitCommandIsInMergeState(t *testing.T) { - type scenario struct { - testName string - command func(string, ...string) *exec.Cmd - test func(bool, error) - } - - scenarios := []scenario{ - { - "An error occurred when running status command", - func(cmd string, args ...string) *exec.Cmd { - assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"status", "--untracked-files=all"}, args) - - return exec.Command("exit", "1") - }, - func(isInMergeState bool, err error) { - assert.Error(t, err) - assert.False(t, isInMergeState) - }, - }, - { - "Is not in merge state", - func(cmd string, args ...string) *exec.Cmd { - assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"status", "--untracked-files=all"}, args) - return exec.Command("echo") - }, - func(isInMergeState bool, err error) { - assert.False(t, isInMergeState) - assert.NoError(t, err) - }, - }, - { - "Command output contains conclude merge", - func(cmd string, args ...string) *exec.Cmd { - assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"status", "--untracked-files=all"}, args) - return exec.Command("echo", "'conclude merge'") - }, - func(isInMergeState bool, err error) { - assert.True(t, isInMergeState) - assert.NoError(t, err) - }, - }, - { - "Command output contains unmerged paths", - func(cmd string, args ...string) *exec.Cmd { - assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"status", "--untracked-files=all"}, args) - return exec.Command("echo", "'unmerged paths'") - }, - func(isInMergeState bool, err error) { - assert.True(t, isInMergeState) - assert.NoError(t, err) - }, - }, - } - - for _, s := range scenarios { - t.Run(s.testName, func(t *testing.T) { - gitCmd := newDummyGitCommand() - gitCmd.OSCommand.command = s.command - s.test(gitCmd.IsInMergeState()) - }) - } -} - func TestGitCommandCommit(t *testing.T) { type scenario struct { testName string @@ -1219,6 +1079,146 @@ func TestGitCommandSquashFixupCommit(t *testing.T) { } } +func TestGitCommandCatFile(t *testing.T) { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "cat", cmd) + assert.EqualValues(t, []string{"test.txt"}, args) + + return exec.Command("echo", "-n", "test") + } + + o, err := gitCmd.CatFile("test.txt") + assert.NoError(t, err) + assert.Equal(t, "test", o) +} + +func TestGitCommandStageFile(t *testing.T) { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"add", "test.txt"}, args) + + return exec.Command("echo") + } + + assert.NoError(t, gitCmd.StageFile("test.txt")) +} + +func TestGitCommandUnstageFile(t *testing.T) { + type scenario struct { + testName string + command func(string, ...string) *exec.Cmd + test func(error) + tracked bool + } + + scenarios := []scenario{ + { + "Remove an untracked file from staging", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"rm", "--cached", "test.txt"}, args) + + return exec.Command("echo") + }, + func(err error) { + assert.NoError(t, err) + }, + false, + }, + { + "Remove a tracked file from staging", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"reset", "HEAD", "test.txt"}, args) + + return exec.Command("echo") + }, + func(err error) { + assert.NoError(t, err) + }, + true, + }, + } + + for _, s := range scenarios { + t.Run(s.testName, func(t *testing.T) { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = s.command + s.test(gitCmd.UnStageFile("test.txt", s.tracked)) + }) + } +} + +func TestGitCommandIsInMergeState(t *testing.T) { + type scenario struct { + testName string + command func(string, ...string) *exec.Cmd + test func(bool, error) + } + + scenarios := []scenario{ + { + "An error occurred when running status command", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"status", "--untracked-files=all"}, args) + + return exec.Command("exit", "1") + }, + func(isInMergeState bool, err error) { + assert.Error(t, err) + assert.False(t, isInMergeState) + }, + }, + { + "Is not in merge state", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"status", "--untracked-files=all"}, args) + return exec.Command("echo") + }, + func(isInMergeState bool, err error) { + assert.False(t, isInMergeState) + assert.NoError(t, err) + }, + }, + { + "Command output contains conclude merge", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"status", "--untracked-files=all"}, args) + return exec.Command("echo", "'conclude merge'") + }, + func(isInMergeState bool, err error) { + assert.True(t, isInMergeState) + assert.NoError(t, err) + }, + }, + { + "Command output contains unmerged paths", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"status", "--untracked-files=all"}, args) + return exec.Command("echo", "'unmerged paths'") + }, + func(isInMergeState bool, err error) { + assert.True(t, isInMergeState) + assert.NoError(t, err) + }, + }, + } + + for _, s := range scenarios { + t.Run(s.testName, func(t *testing.T) { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = s.command + s.test(gitCmd.IsInMergeState()) + }) + } +} + func TestGitCommandDiff(t *testing.T) { gitCommand := newDummyGitCommand() assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh"))