mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-01-08 04:04:22 +02:00
4de31da4be
This fixes up some git and oscommand tests, and pulls some tests into commit_list_builder_test.go I've also made the NewDummyBlah functions public so that I didn't need to duplicate them across packages I've also given OSCommand a SetCommand() method for setting the command on the struct I've also created a file utils.go in the test package for creating convient 'CommandSwapper's, which basically enable you to assert a sequence of commands on the command line, and swap each one out for a different one to actually be executed
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package git
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"testing"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// NewDummyPatchParser constructs a new dummy patch parser for testing
|
|
func NewDummyPatchParser() *PatchParser {
|
|
return &PatchParser{
|
|
Log: commands.NewDummyLog(),
|
|
}
|
|
}
|
|
|
|
func TestParsePatch(t *testing.T) {
|
|
type scenario struct {
|
|
testName string
|
|
patchFilename string
|
|
shouldError bool
|
|
expectedStageableLines []int
|
|
expectedHunkStarts []int
|
|
}
|
|
|
|
scenarios := []scenario{
|
|
{
|
|
"Diff with one hunk",
|
|
"testdata/testPatchBefore.diff",
|
|
false,
|
|
[]int{8, 9, 10, 11},
|
|
[]int{4},
|
|
},
|
|
{
|
|
"Diff with two hunks",
|
|
"testdata/testPatchBefore2.diff",
|
|
false,
|
|
[]int{8, 9, 10, 11, 12, 13, 20, 21, 22, 23, 24, 25, 26, 27, 28, 33, 34, 35, 36, 37, 45, 46, 47, 48, 49, 50, 51, 52, 53},
|
|
[]int{4, 41},
|
|
},
|
|
{
|
|
"Unstaged file",
|
|
"testdata/addedFile.diff",
|
|
false,
|
|
[]int{6},
|
|
[]int{5},
|
|
},
|
|
}
|
|
|
|
for _, s := range scenarios {
|
|
t.Run(s.testName, func(t *testing.T) {
|
|
p := NewDummyPatchParser()
|
|
beforePatch, err := ioutil.ReadFile(s.patchFilename)
|
|
if err != nil {
|
|
panic("Cannot open file at " + s.patchFilename)
|
|
}
|
|
hunkStarts, stageableLines, err := p.ParsePatch(string(beforePatch))
|
|
if s.shouldError {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, s.expectedStageableLines, stageableLines)
|
|
assert.Equal(t, s.expectedHunkStarts, hunkStarts)
|
|
}
|
|
})
|
|
}
|
|
}
|