Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to pkg/commands/git - Part 7 #291

Merged
merged 4 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions pkg/commands/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,26 +500,22 @@ func (c *GitCommand) Ignore(filename string) error {
}

// Show shows the diff of a commit
func (c *GitCommand) Show(sha string) string {
result, err := c.OSCommand.RunCommandWithOutput("git show --color " + sha)
if err != nil {
panic(err)
}
return result
func (c *GitCommand) Show(sha string) (string, error) {
return c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git show --color %s", sha))
}

// Diff returns the diff of a file
func (c *GitCommand) Diff(file *File) string {
cachedArg := ""
trackedArg := "--"
fileName := c.OSCommand.Quote(file.Name)
if file.HasStagedChanges && !file.HasUnstagedChanges {
cachedArg = "--cached"
}
trackedArg := "--"
if !file.Tracked && !file.HasStagedChanges {
trackedArg = "--no-index /dev/null"
}
command := fmt.Sprintf("%s %s %s %s", "git diff --color ", cachedArg, trackedArg, fileName)
command := fmt.Sprintf("git diff --color %s %s %s", cachedArg, trackedArg, fileName)

// for now we assume an error means the file was deleted
s, _ := c.OSCommand.RunCommandWithOutput(command)
Expand Down
179 changes: 100 additions & 79 deletions pkg/commands/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/test"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
gogit "gopkg.in/src-d/go-git.v4"
Expand Down Expand Up @@ -1421,6 +1420,19 @@ func TestGitCommandRemoveFile(t *testing.T) {
}
}

func TestGitCommandShow(t *testing.T) {
gitCmd := newDummyGitCommand()
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"show", "--color", "456abcde"}, args)

return exec.Command("echo")
}

_, err := gitCmd.Show("456abcde")
assert.NoError(t, err)
}

func TestGitCommandCheckout(t *testing.T) {
type scenario struct {
testName string
Expand Down Expand Up @@ -1553,97 +1565,106 @@ func TestGitCommandGetCommits(t *testing.T) {
}
}

func TestGitCommandDiff(t *testing.T) {
gitCommand := newDummyGitCommand()
assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh"))
func TestGitCommandGetLog(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
test func(string)
}

files := []*File{
{
Name: "deleted_staged",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: true,
Deleted: true,
HasMergeConflicts: false,
DisplayString: " D deleted_staged",
},
{
Name: "file with space staged",
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "A \"file with space staged\"",
},
{
Name: "file with space unstaged",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "?? file with space unstaged",
},
{
Name: "modified_unstaged",
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: true,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "M modified_unstaged",
},
scenarios := []scenario{
{
Name: "modified_staged",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: true,
Deleted: false,
HasMergeConflicts: false,
DisplayString: " M modified_staged",
"Retrieves logs",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)

return exec.Command("echo", "6f0b32f commands/git : add GetCommits tests refactor\n9d9d775 circle : remove new line")
},
func(output string) {
assert.EqualValues(t, "6f0b32f commands/git : add GetCommits tests refactor\n9d9d775 circle : remove new line\n", output)
},
},
{
Name: "renamed_before -> renamed_after",
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: true,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "R renamed_before -> renamed_after",
"An error occurred when retrieving logs",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
return exec.Command("test")
},
func(output string) {
assert.Empty(t, output)
},
},
}

for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := newDummyGitCommand()
gitCmd.OSCommand.command = s.command
s.test(gitCmd.GetLog())
})
}
}

func TestGitCommandDiff(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
file *File
}

scenarios := []scenario{
{
Name: "untracked_unstaged",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "?? untracked_unstaged",
"Default case",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"diff", "--color", "--", "test.txt"}, args)

return exec.Command("echo")
},
&File{
Name: "test.txt",
HasStagedChanges: false,
Tracked: true,
},
},
{
Name: "untracked_staged",
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "A untracked_staged",
"All changes staged",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"diff", "--color", "--cached", "--", "test.txt"}, args)

return exec.Command("echo")
},
&File{
Name: "test.txt",
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: true,
},
},
{
Name: "master",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "?? master",
"File not tracked and file has no staged changes",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"diff", "--color", "--no-index", "/dev/null", "test.txt"}, args)

return exec.Command("echo")
},
&File{
Name: "test.txt",
HasStagedChanges: false,
Tracked: false,
},
},
}

for _, file := range files {
t.Run(file.Name, func(t *testing.T) {
assert.NotContains(t, gitCommand.Diff(file), "error")
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := newDummyGitCommand()
gitCmd.OSCommand.command = s.command
gitCmd.Diff(s.file)
})
}
}
5 changes: 4 additions & 1 deletion pkg/gui/commits_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func (gui *Gui) handleCommitSelect(g *gocui.Gui, v *gocui.View) error {
}
return gui.renderString(g, "main", gui.Tr.SLocalize("NoCommitsThisBranch"))
}
commitText := gui.GitCommand.Show(commit.Sha)
commitText, err := gui.GitCommand.Show(commit.Sha)
if err != nil {
return err
}
return gui.renderString(g, "main", commitText)
}

Expand Down
35 changes: 0 additions & 35 deletions test/repos/lots_of_diffs.sh

This file was deleted.