summaryrefslogtreecommitdiffstats
path: root/pkg/git/patch_modifier_test.go
blob: 72c3f62a79e3c080166944e92a62dbf1f7f05da7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package git

import (
	"io/ioutil"
	"testing"

	"github.com/jesseduffield/lazygit/pkg/commands"
	"github.com/stretchr/testify/assert"
)

// NewDummyPatchModifier constructs a new dummy patch modifier for testing
func NewDummyPatchModifier() *PatchModifier {
	return &PatchModifier{
		Log: commands.NewDummyLog(),
	}
}

func TestModifyPatchForLine(t *testing.T) {
	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",
		},
		{
			"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",
		},
	}

	for _, s := range scenarios {
		t.Run(s.testName, func(t *testing.T) {
			p := NewDummyPatchModifier()
			beforePatch, err := ioutil.ReadFile(s.patchFilename)
			if err != nil {
				panic("Cannot open file at " + s.patchFilename)
			}
			afterPatch, err := p.ModifyPatchForLine(string(beforePatch), s.lineNumber)
			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)
			}
		})
	}
}