gitech

Форк
0
/
blame_test.go 
146 строк · 3.8 Кб
1
// Copyright 2019 The Gitea Authors. All rights reserved.
2
// SPDX-License-Identifier: MIT
3

4
package git
5

6
import (
7
	"context"
8
	"testing"
9

10
	"github.com/stretchr/testify/assert"
11
)
12

13
func TestReadingBlameOutput(t *testing.T) {
14
	ctx, cancel := context.WithCancel(context.Background())
15
	defer cancel()
16

17
	t.Run("Without .git-blame-ignore-revs", func(t *testing.T) {
18
		repo, err := OpenRepository(ctx, "./tests/repos/repo5_pulls")
19
		assert.NoError(t, err)
20
		defer repo.Close()
21

22
		commit, err := repo.GetCommit("f32b0a9dfd09a60f616f29158f772cedd89942d2")
23
		assert.NoError(t, err)
24

25
		parts := []*BlamePart{
26
			{
27
				Sha: "72866af952e98d02a73003501836074b286a78f6",
28
				Lines: []string{
29
					"# test_repo",
30
					"Test repository for testing migration from github to gitea",
31
				},
32
			},
33
			{
34
				Sha:          "f32b0a9dfd09a60f616f29158f772cedd89942d2",
35
				Lines:        []string{"", "Do not make any changes to this repo it is used for unit testing"},
36
				PreviousSha:  "72866af952e98d02a73003501836074b286a78f6",
37
				PreviousPath: "README.md",
38
			},
39
		}
40

41
		for _, bypass := range []bool{false, true} {
42
			blameReader, err := CreateBlameReader(ctx, Sha1ObjectFormat, "./tests/repos/repo5_pulls", commit, "README.md", bypass)
43
			assert.NoError(t, err)
44
			assert.NotNil(t, blameReader)
45
			defer blameReader.Close()
46

47
			assert.False(t, blameReader.UsesIgnoreRevs())
48

49
			for _, part := range parts {
50
				actualPart, err := blameReader.NextPart()
51
				assert.NoError(t, err)
52
				assert.Equal(t, part, actualPart)
53
			}
54

55
			// make sure all parts have been read
56
			actualPart, err := blameReader.NextPart()
57
			assert.Nil(t, actualPart)
58
			assert.NoError(t, err)
59
		}
60
	})
61

62
	t.Run("With .git-blame-ignore-revs", func(t *testing.T) {
63
		repo, err := OpenRepository(ctx, "./tests/repos/repo6_blame")
64
		assert.NoError(t, err)
65
		defer repo.Close()
66

67
		full := []*BlamePart{
68
			{
69
				Sha:   "af7486bd54cfc39eea97207ca666aa69c9d6df93",
70
				Lines: []string{"line", "line"},
71
			},
72
			{
73
				Sha:          "45fb6cbc12f970b04eacd5cd4165edd11c8d7376",
74
				Lines:        []string{"changed line"},
75
				PreviousSha:  "af7486bd54cfc39eea97207ca666aa69c9d6df93",
76
				PreviousPath: "blame.txt",
77
			},
78
			{
79
				Sha:   "af7486bd54cfc39eea97207ca666aa69c9d6df93",
80
				Lines: []string{"line", "line", ""},
81
			},
82
		}
83

84
		cases := []struct {
85
			CommitID       string
86
			UsesIgnoreRevs bool
87
			Bypass         bool
88
			Parts          []*BlamePart
89
		}{
90
			{
91
				CommitID:       "544d8f7a3b15927cddf2299b4b562d6ebd71b6a7",
92
				UsesIgnoreRevs: true,
93
				Bypass:         false,
94
				Parts: []*BlamePart{
95
					{
96
						Sha:   "af7486bd54cfc39eea97207ca666aa69c9d6df93",
97
						Lines: []string{"line", "line", "changed line", "line", "line", ""},
98
					},
99
				},
100
			},
101
			{
102
				CommitID:       "544d8f7a3b15927cddf2299b4b562d6ebd71b6a7",
103
				UsesIgnoreRevs: false,
104
				Bypass:         true,
105
				Parts:          full,
106
			},
107
			{
108
				CommitID:       "45fb6cbc12f970b04eacd5cd4165edd11c8d7376",
109
				UsesIgnoreRevs: false,
110
				Bypass:         false,
111
				Parts:          full,
112
			},
113
			{
114
				CommitID:       "45fb6cbc12f970b04eacd5cd4165edd11c8d7376",
115
				UsesIgnoreRevs: false,
116
				Bypass:         false,
117
				Parts:          full,
118
			},
119
		}
120

121
		objectFormat, err := repo.GetObjectFormat()
122
		assert.NoError(t, err)
123
		for _, c := range cases {
124
			commit, err := repo.GetCommit(c.CommitID)
125
			assert.NoError(t, err)
126

127
			blameReader, err := CreateBlameReader(ctx, objectFormat, "./tests/repos/repo6_blame", commit, "blame.txt", c.Bypass)
128
			assert.NoError(t, err)
129
			assert.NotNil(t, blameReader)
130
			defer blameReader.Close()
131

132
			assert.Equal(t, c.UsesIgnoreRevs, blameReader.UsesIgnoreRevs())
133

134
			for _, part := range c.Parts {
135
				actualPart, err := blameReader.NextPart()
136
				assert.NoError(t, err)
137
				assert.Equal(t, part, actualPart)
138
			}
139

140
			// make sure all parts have been read
141
			actualPart, err := blameReader.NextPart()
142
			assert.Nil(t, actualPart)
143
			assert.NoError(t, err)
144
		}
145
	})
146
}
147

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.