From c034c88be4954a12057efdd8cd3659b867665725 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 12 Mar 2019 19:56:04 +1100 Subject: display test name when running tests --- pkg/commands/git_test.go | 148 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 146 insertions(+), 2 deletions(-) (limited to 'pkg/commands') 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)) + }) } } -- cgit v1.2.3