Welcome to mirror list, hosted at ThFree Co, Russian Federation.

parser_test.go « conflict « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5b3eccff6879eef0df3944d6a3ae735355301aa7 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package conflict

import (
	"io"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v15/internal/helper/perm"
)

func TestFile_Resolve(t *testing.T) {
	for _, tt := range []struct {
		name         string
		path         string
		conflictFile io.Reader
		parseErr     error
		resolution   Resolution
		resolveErr   error
		expect       string
	}{
		{
			name: "ours",
			path: "conflict.txt",
			conflictFile: strings.NewReader(`# this file is very conflicted
<<<<<<< conflict.txt
we want this line
=======
but they want this line
>>>>>>> conflict.txt
we can both agree on this line though
`),
			resolution: Resolution{
				NewPath: "conflict.txt",
				OldPath: "conflict.txt",
				Sections: map[string]string{
					"dc1c302824bab8da29f7c06fec1c77cf16b975e6_2_2": "head",
				},
			},
			expect: `# this file is very conflicted
we want this line
we can both agree on this line though
`,
		},
		{
			name: "theirs",
			path: "conflict.txt",
			conflictFile: strings.NewReader(`# this file is very conflicted
<<<<<<< conflict.txt
we want this line
=======
but they want this line
>>>>>>> conflict.txt
we can both agree on this line though
`),
			resolution: Resolution{
				NewPath: "conflict.txt",
				OldPath: "conflict.txt",
				Sections: map[string]string{
					"dc1c302824bab8da29f7c06fec1c77cf16b975e6_2_2": "origin",
				},
			},
			expect: `# this file is very conflicted
but they want this line
we can both agree on this line though
`,
		},
		{
			name: "UnexpectedDelimiter",
			path: "conflict.txt",
			conflictFile: strings.NewReader(`# this file is very conflicted
<<<<<<< conflict.txt
we want this line
<<<<<<< conflict.txt
=======
but they want this line
>>>>>>> conflict.txt
we can both agree on this line though
`),
			parseErr: ErrUnexpectedDelimiter,
		},
		{
			name: "ErrMissingEndDelimiter",
			path: "conflict.txt",
			conflictFile: strings.NewReader(`# this file is very conflicted
<<<<<<< conflict.txt
we want this line
=======
but they want this line
we can both agree on this line though
`),
			parseErr: ErrMissingEndDelimiter,
		},
		{
			name:         "Conflict file under file limit",
			path:         "conflict.txt",
			conflictFile: strings.NewReader(strings.Repeat("x", fileLimit-2) + "\n"),
		},
		{
			name:         "ErrUnmergeableFile over file limit",
			path:         "conflict.txt",
			conflictFile: strings.NewReader(strings.Repeat("x", fileLimit+1)),
			parseErr:     ErrUnmergeableFile,
		},
		{
			name:         "ErrUnmergeableFile empty file",
			path:         "conflict.txt",
			conflictFile: strings.NewReader(""),
			parseErr:     ErrUnmergeableFile,
		},
	} {
		t.Run(tt.name, func(t *testing.T) {
			entry := Entry{
				Path:     tt.path,
				Mode:     uint(perm.SharedFile),
				Contents: []byte("something-with-trailing-newline\n"),
			}

			f, err := Parse(tt.conflictFile, &entry, &entry, &entry)
			require.Equal(t, tt.parseErr, err)

			actual, err := f.Resolve(tt.resolution)
			require.Equal(t, tt.resolveErr, err)
			require.Equal(t, tt.expect, string(actual))
		})
	}
}