From 72830efc45e005c63947f13dcf171ddd59f69506 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 12 Mar 2019 19:51:38 +1100 Subject: add some tests --- pkg/commands/git.go | 4 ++ pkg/commands/git_test.go | 138 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) (limited to 'pkg/commands') diff --git a/pkg/commands/git.go b/pkg/commands/git.go index f447ec7ad..944cd8788 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -832,6 +832,10 @@ func (c *GitCommand) CheckoutFile(commitSha, fileName string) error { // DiscardOldFileChanges discards changes to a file from an old commit func (c *GitCommand) DiscardOldFileChanges(commits []*Commit, commitIndex int, fileName string) error { + if len(commits)-1 < commitIndex { + return errors.New("index outside of range of commits") + } + // we can make this GPG thing possible it just means we need to do this in two parts: // one where we handle the possibility of a credential request, and the other // where we continue the rebase diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index fb9bf9a1e..9c2079b26 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -1711,3 +1711,141 @@ func TestGitCommandRebaseBranch(t *testing.T) { s.test(gitCmd.RebaseBranch(s.arg)) } } + +// 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)) + } +} -- cgit v1.2.3