summaryrefslogtreecommitdiffstats
path: root/pkg/git/patch_parser_test.go
blob: 6670aaea276b8eb90c375eb22e73003095bb1438 (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
package git

import (
	"io/ioutil"
	"testing"

	"github.com/stretchr/testify/assert"
)

func newDummyPatchParser() *PatchParser {
	return &PatchParser{
		Log: 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)
			}
		})
	}
}