summaryrefslogtreecommitdiffstats
path: root/pkg/commands
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2019-03-16 10:15:46 +1100
committerJesse Duffield <jessedduffield@gmail.com>2019-03-16 10:20:27 +1100
commitf5c8aac97d51e2173b7abf4441e6e997734c3593 (patch)
tree79ccdf903ea635acccb812e50234f0addb2bcfc7 /pkg/commands
parentb6447ebdbbc764bad99772b5cd5e3169143fb458 (diff)
add two more tests
Diffstat (limited to 'pkg/commands')
-rw-r--r--pkg/commands/git.go10
-rw-r--r--pkg/commands/git_test.go77
2 files changed, 82 insertions, 5 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go
index 944cd8788..af39ab2d2 100644
--- a/pkg/commands/git.go
+++ b/pkg/commands/git.go
@@ -798,8 +798,8 @@ func (c *GitCommand) CherryPickCommits(commits []*Commit) error {
}
// GetCommitFiles get the specified commit files
-func (c *GitCommand) GetCommitFiles(commitID string) ([]*CommitFile, error) {
- cmd := fmt.Sprintf("git show --pretty= --name-only %s", commitID)
+func (c *GitCommand) GetCommitFiles(commitSha string) ([]*CommitFile, error) {
+ cmd := fmt.Sprintf("git show --pretty= --name-only %s", commitSha)
files, err := c.OSCommand.RunCommandWithOutput(cmd)
if err != nil {
return nil, err
@@ -809,7 +809,7 @@ func (c *GitCommand) GetCommitFiles(commitID string) ([]*CommitFile, error) {
for _, file := range strings.Split(strings.TrimRight(files, "\n"), "\n") {
commitFiles = append(commitFiles, &CommitFile{
- Sha: commitID,
+ Sha: commitSha,
Name: file,
DisplayString: file,
})
@@ -819,8 +819,8 @@ func (c *GitCommand) GetCommitFiles(commitID string) ([]*CommitFile, error) {
}
// ShowCommitFile get the diff of specified commit file
-func (c *GitCommand) ShowCommitFile(commitID, file string) (string, error) {
- cmd := fmt.Sprintf("git show --color %s -- %s", commitID, file)
+func (c *GitCommand) ShowCommitFile(commitSha, fileName string) (string, error) {
+ cmd := fmt.Sprintf("git show --color %s -- %s", commitSha, fileName)
return c.OSCommand.RunCommandWithOutput(cmd)
}
diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go
index a80755184..8fbdc1642 100644
--- a/pkg/commands/git_test.go
+++ b/pkg/commands/git_test.go
@@ -1855,3 +1855,80 @@ func TestGitCommandDiscardOldFileChanges(t *testing.T) {
})
}
}
+
+// TestGitCommandShowCommitFile is a function.
+func TestGitCommandShowCommitFile(t *testing.T) {
+ type scenario struct {
+ testName string
+ commitSha string
+ fileName string
+ command func(string, ...string) *exec.Cmd
+ test func(string, error)
+ }
+
+ scenarios := []scenario{
+ {
+ "valid case",
+ "123456",
+ "hello.txt",
+ test.CreateMockCommand(t, []*test.CommandSwapper{
+ {
+ Expect: "git show --color 123456 -- hello.txt",
+ Replace: "echo -n hello",
+ },
+ }),
+ func(str string, err error) {
+ assert.NoError(t, err)
+ assert.Equal(t, "hello", str)
+ },
+ },
+ }
+
+ gitCmd := NewDummyGitCommand()
+
+ for _, s := range scenarios {
+ t.Run(s.testName, func(t *testing.T) {
+ gitCmd.OSCommand.command = s.command
+ s.test(gitCmd.ShowCommitFile(s.commitSha, s.fileName))
+ })
+ }
+}
+
+// TestGitCommandGetCommitFiles is a function.
+func TestGitCommandGetCommitFiles(t *testing.T) {
+ type scenario struct {
+ testName string
+ commitSha string
+ command func(string, ...string) *exec.Cmd
+ test func([]*CommitFile, error)
+ }
+
+ scenarios := []scenario{
+ {
+ "valid case",
+ "123456",
+ test.CreateMockCommand(t, []*test.CommandSwapper{
+ {
+ Expect: "git show --pretty= --name-only 123456",
+ Replace: "echo 'hello\nworld'",
+ },
+ }),
+ func(commitFiles []*CommitFile, err error) {
+ assert.NoError(t, err)
+ assert.Equal(t, []*CommitFile{
+ {Sha: "123456", Name: "hello", DisplayString: "hello"},
+ {Sha: "123456", Name: "world", DisplayString: "world"},
+ }, commitFiles)
+ },
+ },
+ }
+
+ gitCmd := NewDummyGitCommand()
+
+ for _, s := range scenarios {
+ t.Run(s.testName, func(t *testing.T) {
+ gitCmd.OSCommand.command = s.command
+ s.test(gitCmd.GetCommitFiles(s.commitSha))
+ })
+ }
+}