summaryrefslogtreecommitdiffstats
path: root/modules/git/repo_base_test.go
blob: 323b28f476f7663bc16135fef4c9d4c8a324cda9 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later

package git

import (
	"bufio"
	"context"
	"testing"

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

// This unit test relies on the implementation detail of CatFileBatch.
func TestCatFileBatch(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	repo, err := OpenRepository(ctx, "./tests/repos/repo1_bare")
	require.NoError(t, err)
	defer repo.Close()

	var wr WriteCloserError
	var r *bufio.Reader
	var cancel1 func()
	t.Run("Request cat file batch", func(t *testing.T) {
		assert.Nil(t, repo.batch)
		wr, r, cancel1, err = repo.CatFileBatch(ctx)
		require.NoError(t, err)
		assert.NotNil(t, repo.batch)
		assert.Equal(t, repo.batch.Writer, wr)
		assert.True(t, repo.batchInUse)
	})

	t.Run("Request temporary cat file batch", func(t *testing.T) {
		wr, r, cancel, err := repo.CatFileBatch(ctx)
		require.NoError(t, err)
		assert.NotEqual(t, repo.batch.Writer, wr)

		t.Run("Check temporary cat file batch", func(t *testing.T) {
			_, err = wr.Write([]byte("95bb4d39648ee7e325106df01a621c530863a653" + "\n"))
			require.NoError(t, err)

			sha, typ, size, err := ReadBatchLine(r)
			require.NoError(t, err)
			assert.Equal(t, "commit", typ)
			assert.EqualValues(t, []byte("95bb4d39648ee7e325106df01a621c530863a653"), sha)
			assert.EqualValues(t, 144, size)
		})

		cancel()
		assert.True(t, repo.batchInUse)
	})

	t.Run("Check cached cat file batch", func(t *testing.T) {
		_, err = wr.Write([]byte("95bb4d39648ee7e325106df01a621c530863a653" + "\n"))
		require.NoError(t, err)

		sha, typ, size, err := ReadBatchLine(r)
		require.NoError(t, err)
		assert.Equal(t, "commit", typ)
		assert.EqualValues(t, []byte("95bb4d39648ee7e325106df01a621c530863a653"), sha)
		assert.EqualValues(t, 144, size)
	})

	t.Run("Cancel cached cat file batch", func(t *testing.T) {
		cancel1()
		assert.False(t, repo.batchInUse)
		assert.NotNil(t, repo.batch)
	})

	t.Run("Request cached cat file batch", func(t *testing.T) {
		wr, _, _, err := repo.CatFileBatch(ctx)
		require.NoError(t, err)
		assert.NotNil(t, repo.batch)
		assert.Equal(t, repo.batch.Writer, wr)
		assert.True(t, repo.batchInUse)

		t.Run("Close git repo", func(t *testing.T) {
			require.NoError(t, repo.Close())
			assert.Nil(t, repo.batch)
		})

		_, err = wr.Write([]byte("95bb4d39648ee7e325106df01a621c530863a653" + "\n"))
		require.Error(t, err)
	})
}

// This unit test relies on the implementation detail of CatFileBatchCheck.
func TestCatFileBatchCheck(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	repo, err := OpenRepository(ctx, "./tests/repos/repo1_bare")
	require.NoError(t, err)
	defer repo.Close()

	var wr WriteCloserError
	var r *bufio.Reader
	var cancel1 func()
	t.Run("Request cat file batch check", func(t *testing.T) {
		assert.Nil(t, repo.check)
		wr, r, cancel1, err = repo.CatFileBatchCheck(ctx)
		require.NoError(t, err)
		assert.NotNil(t, repo.check)
		assert.Equal(t, repo.check.Writer, wr)
		assert.True(t, repo.checkInUse)
	})

	t.Run("Request temporary cat file batch check", func(t *testing.T) {
		wr, r, cancel, err := repo.CatFileBatchCheck(ctx)
		require.NoError(t, err)
		assert.NotEqual(t, repo.check.Writer, wr)

		t.Run("Check temporary cat file batch check", func(t *testing.T) {
			_, err = wr.Write([]byte("test" + "\n"))
			require.NoError(t, err)

			sha, typ, size, err := ReadBatchLine(r)
			require.NoError(t, err)
			assert.Equal(t, "tag", typ)
			assert.EqualValues(t, []byte("3ad28a9149a2864384548f3d17ed7f38014c9e8a"), sha)
			assert.EqualValues(t, 807, size)
		})

		cancel()
		assert.True(t, repo.checkInUse)
	})

	t.Run("Check cached cat file batch check", func(t *testing.T) {
		_, err = wr.Write([]byte("test" + "\n"))
		require.NoError(t, err)

		sha, typ, size, err := ReadBatchLine(r)
		require.NoError(t, err)
		assert.Equal(t, "tag", typ)
		assert.EqualValues(t, []byte("3ad28a9149a2864384548f3d17ed7f38014c9e8a"), sha)
		assert.EqualValues(t, 807, size)
	})

	t.Run("Cancel cached cat file batch check", func(t *testing.T) {
		cancel1()
		assert.False(t, repo.checkInUse)
		assert.NotNil(t, repo.check)
	})

	t.Run("Request cached cat file batch check", func(t *testing.T) {
		wr, _, _, err := repo.CatFileBatchCheck(ctx)
		require.NoError(t, err)
		assert.NotNil(t, repo.check)
		assert.Equal(t, repo.check.Writer, wr)
		assert.True(t, repo.checkInUse)

		t.Run("Close git repo", func(t *testing.T) {
			require.NoError(t, repo.Close())
			assert.Nil(t, repo.check)
		})

		_, err = wr.Write([]byte("test" + "\n"))
		require.Error(t, err)
	})
}