diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2019-03-12 19:56:04 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2019-03-16 10:20:27 +1100 |
commit | c034c88be4954a12057efdd8cd3659b867665725 (patch) | |
tree | 564affac0190d09cda7441a1ce8a3271f59fe20a /pkg/commands/git_test.go | |
parent | 72830efc45e005c63947f13dcf171ddd59f69506 (diff) |
display test name when running tests
Diffstat (limited to 'pkg/commands/git_test.go')
-rw-r--r-- | pkg/commands/git_test.go | 148 |
1 files changed, 146 insertions, 2 deletions
diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 9c2079b26..8c86fa252 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -1707,8 +1707,152 @@ func TestGitCommandRebaseBranch(t *testing.T) { gitCmd := NewDummyGitCommand() for _, s := range scenarios { - gitCmd.OSCommand.command = s.command - s.test(gitCmd.RebaseBranch(s.arg)) + t.Run(s.testName, func(t *testing.T) { + gitCmd.OSCommand.command = s.command + 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 { + t.Run(s.testName, func(t *testing.T) { + 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 { + t.Run(s.testName, func(t *testing.T) { + gitCmd.OSCommand.command = s.command + gitCmd.getLocalGitConfig = s.getLocalGitConfig + s.test(gitCmd.DiscardOldFileChanges(s.commits, s.commitIndex, s.fileName)) + }) } } |