diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 8c86fa252..a80755184 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -1796,7 +1796,7 @@ func TestGitCommandDiscardOldFileChanges(t *testing.T) { func(string) (string, error) { return "true", nil }, - []*Commit{&Commit{Name: "commit", Sha: "123456"}}, + []*Commit{{Name: "commit", Sha: "123456"}}, 0, "test999.txt", nil, @@ -1810,8 +1810,8 @@ func TestGitCommandDiscardOldFileChanges(t *testing.T) { return "", nil }, []*Commit{ - &Commit{Name: "commit", Sha: "123456"}, - &Commit{Name: "commit2", Sha: "abcdef"}, + {Name: "commit", Sha: "123456"}, + {Name: "commit2", Sha: "abcdef"}, }, 0, "test999.txt", @@ -1855,141 +1855,3 @@ func TestGitCommandDiscardOldFileChanges(t *testing.T) { }) } } - -// TestGitCommandCheckoutFile is a function. -func TestGitCommandCheckoutFile(t *testing.T) { - type scenario struct { - testName string - commitSha string - fileName string - command func(string, ...string) *exec.Cmd - test func(error) - } - - scenarios := []scenario{ - { - "typical case", - "11af912", - "test999.txt", - test.CreateMockCommand(t, []*test.CommandSwapper{ - { - Expect: "git checkout 11af912 test999.txt", - Replace: "echo", - }, - }), - func(err error) { - assert.NoError(t, err) - }, - }, - { - "returns error if there is one", - "11af912", - "test999.txt", - test.CreateMockCommand(t, []*test.CommandSwapper{ - { - Expect: "git checkout 11af912 test999.txt", - Replace: "test", - }, - }), - func(err error) { - assert.Error(t, err) - }, - }, - } - - gitCmd := NewDummyGitCommand() - - for _, s := range scenarios { - gitCmd.OSCommand.command = s.command - s.test(gitCmd.CheckoutFile(s.commitSha, s.fileName)) - } -} - -// TestGitCommandDiscardOldFileChanges is a function. -func TestGitCommandDiscardOldFileChanges(t *testing.T) { - type scenario struct { - testName string - getLocalGitConfig func(string) (string, error) - commits []*Commit - commitIndex int - fileName string - command func(string, ...string) *exec.Cmd - test func(error) - } - - scenarios := []scenario{ - { - "returns error when index outside of range of commits", - func(string) (string, error) { - return "", nil - }, - []*Commit{}, - 0, - "test999.txt", - nil, - func(err error) { - assert.Error(t, err) - }, - }, - { - "returns error when using gpg", - func(string) (string, error) { - return "true", nil - }, - []*Commit{&Commit{Name: "commit", Sha: "123456"}}, - 0, - "test999.txt", - nil, - func(err error) { - assert.Error(t, err) - }, - }, - { - "checks out file if it already existed", - func(string) (string, error) { - return "", nil - }, - []*Commit{ - &Commit{Name: "commit", Sha: "123456"}, - &Commit{Name: "commit2", Sha: "abcdef"}, - }, - 0, - "test999.txt", - test.CreateMockCommand(t, []*test.CommandSwapper{ - { - Expect: "git rebase --interactive --autostash 123456^", - Replace: "echo", - }, - { - Expect: "git cat-file -e HEAD^:test999.txt", - Replace: "echo", - }, - { - Expect: "git checkout HEAD^ test999.txt", - Replace: "echo", - }, - { - Expect: "git commit --amend --no-edit", - Replace: "echo", - }, - { - Expect: "git rebase --continue", - Replace: "echo", - }, - }), - func(err error) { - assert.NoError(t, err) - }, - }, - // test for when the file was created within the commit requires a refactor to support proper mocks - // currently we'd need to mock out the os.Remove function and that's gonna introduce tech debt - } - - gitCmd := NewDummyGitCommand() - - for _, s := range scenarios { - gitCmd.OSCommand.command = s.command - gitCmd.getLocalGitConfig = s.getLocalGitConfig - s.test(gitCmd.DiscardOldFileChanges(s.commits, s.commitIndex, s.fileName)) - } -}