1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-03 13:21:56 +02:00

commands/git : refactor test to Diff, refactor function

This commit is contained in:
Anthony HAMON 2018-09-18 21:09:51 +02:00
parent bdeb78c9a0
commit 360b7c1def
3 changed files with 50 additions and 119 deletions

View File

@ -506,15 +506,15 @@ func (c *GitCommand) Show(sha string) (string, error) {
// 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)

View File

@ -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"
@ -1609,96 +1608,63 @@ func TestGitCommandGetLog(t *testing.T) {
}
func TestGitCommandDiff(t *testing.T) {
gitCommand := newDummyGitCommand()
assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh"))
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
file *File
}
files := []*File{
scenarios := []scenario{
{
Name: "deleted_staged",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: true,
Deleted: true,
HasMergeConflicts: false,
DisplayString: " D deleted_staged",
"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: "file with space staged",
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "A \"file with space 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: "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",
},
{
Name: "modified_staged",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: true,
Deleted: false,
HasMergeConflicts: false,
DisplayString: " M modified_staged",
},
{
Name: "renamed_before -> renamed_after",
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: true,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "R renamed_before -> renamed_after",
},
{
Name: "untracked_unstaged",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "?? untracked_unstaged",
},
{
Name: "untracked_staged",
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "A untracked_staged",
},
{
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)
})
}
}

View File

@ -1,35 +0,0 @@
#!/bin/bash
set -ex; rm -rf repo; mkdir repo; cd repo
git init
git config user.email "test@example.com"
git config user.name "Lazygit Tester"
echo "deleted" > deleted_staged
echo "deleted_unstaged" > deleted_unstaged
echo "modified_staged" > modified_staged
echo "modified_unstaged" > modified_unstaged
echo "renamed" > renamed_before
git add .
git commit -m "files to delete"
rm deleted_staged
rm deleted_unstaged
rm renamed_before
echo "renamed" > renamed_after
echo "more" >> modified_staged
echo "more" >> modified_unstaged
echo "untracked_staged" > untracked_staged
echo "untracked_unstaged" > untracked_unstaged
echo "blah" > "file with space staged"
echo "blah" > "file with space unstaged"
echo "same name as branch" > master
git add deleted_staged
git add modified_staged
git add untracked_staged
git add "file with space staged"
git add renamed_before
git add renamed_after