2018-12-02 17:29:17 +11:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
2019-03-02 13:08:09 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
2018-12-02 17:29:17 +11:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-03-02 13:08:09 +11:00
|
|
|
// NewDummyPatchModifier constructs a new dummy patch modifier for testing
|
|
|
|
func NewDummyPatchModifier() *PatchModifier {
|
2018-12-02 17:29:17 +11:00
|
|
|
return &PatchModifier{
|
2019-03-02 13:08:09 +11:00
|
|
|
Log: commands.NewDummyLog(),
|
2018-12-02 17:29:17 +11:00
|
|
|
}
|
|
|
|
}
|
2019-03-02 13:08:09 +11:00
|
|
|
|
2018-12-05 19:33:46 +11:00
|
|
|
func TestModifyPatchForLine(t *testing.T) {
|
2018-12-02 17:29:17 +11:00
|
|
|
type scenario struct {
|
|
|
|
testName string
|
|
|
|
patchFilename string
|
|
|
|
lineNumber int
|
|
|
|
shouldError bool
|
|
|
|
expectedPatchFilename string
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
"Removing one line",
|
|
|
|
"testdata/testPatchBefore.diff",
|
|
|
|
8,
|
|
|
|
false,
|
|
|
|
"testdata/testPatchAfter1.diff",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Adding one line",
|
|
|
|
"testdata/testPatchBefore.diff",
|
|
|
|
10,
|
|
|
|
false,
|
|
|
|
"testdata/testPatchAfter2.diff",
|
|
|
|
},
|
2018-12-05 19:33:46 +11:00
|
|
|
{
|
|
|
|
"Adding one line in top hunk in diff with multiple hunks",
|
|
|
|
"testdata/testPatchBefore2.diff",
|
|
|
|
20,
|
|
|
|
false,
|
|
|
|
"testdata/testPatchAfter3.diff",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Adding one line in top hunk in diff with multiple hunks",
|
|
|
|
"testdata/testPatchBefore2.diff",
|
|
|
|
53,
|
|
|
|
false,
|
|
|
|
"testdata/testPatchAfter4.diff",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"adding unstaged file with a single line",
|
|
|
|
"testdata/addedFile.diff",
|
|
|
|
6,
|
|
|
|
false,
|
|
|
|
"testdata/addedFile.diff",
|
|
|
|
},
|
2018-12-02 17:29:17 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
2019-03-02 13:08:09 +11:00
|
|
|
p := NewDummyPatchModifier()
|
2018-12-02 17:29:17 +11:00
|
|
|
beforePatch, err := ioutil.ReadFile(s.patchFilename)
|
|
|
|
if err != nil {
|
|
|
|
panic("Cannot open file at " + s.patchFilename)
|
|
|
|
}
|
2018-12-05 19:33:46 +11:00
|
|
|
afterPatch, err := p.ModifyPatchForLine(string(beforePatch), s.lineNumber)
|
2018-12-02 17:29:17 +11:00
|
|
|
if s.shouldError {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
expected, err := ioutil.ReadFile(s.expectedPatchFilename)
|
|
|
|
if err != nil {
|
|
|
|
panic("Cannot open file at " + s.expectedPatchFilename)
|
|
|
|
}
|
|
|
|
assert.Equal(t, string(expected), afterPatch)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|