|  | 
|  | 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. | 
|  | 2 | +// SPDX-License-Identifier: MIT | 
|  | 3 | + | 
|  | 4 | +package git | 
|  | 5 | + | 
|  | 6 | +import ( | 
|  | 7 | +	"io" | 
|  | 8 | +	"path/filepath" | 
|  | 9 | +	"testing" | 
|  | 10 | + | 
|  | 11 | +	"code.gitea.io/gitea/modules/test" | 
|  | 12 | +	"github.com/stretchr/testify/assert" | 
|  | 13 | +	"github.com/stretchr/testify/require" | 
|  | 14 | +) | 
|  | 15 | + | 
|  | 16 | +func TestCatFileBatch(t *testing.T) { | 
|  | 17 | +	defer test.MockVariableValue(&DefaultFeatures().SupportCatFileBatchCommand)() | 
|  | 18 | +	DefaultFeatures().SupportCatFileBatchCommand = false | 
|  | 19 | +	t.Run("LegacyCheck", testCatFileBatch) | 
|  | 20 | +	DefaultFeatures().SupportCatFileBatchCommand = true | 
|  | 21 | +	t.Run("BatchCommand", testCatFileBatch) | 
|  | 22 | +} | 
|  | 23 | + | 
|  | 24 | +func testCatFileBatch(t *testing.T) { | 
|  | 25 | +	batch, err := NewBatch(t.Context(), filepath.Join(testReposDir, "repo1_bare")) | 
|  | 26 | +	require.NoError(t, err) | 
|  | 27 | +	defer batch.Close() | 
|  | 28 | + | 
|  | 29 | +	t.Run("QueryInfo", func(t *testing.T) { | 
|  | 30 | +		_, err = batch.QueryInfo([]byte("e2129701f1a4d54dc44f03c93bca0a2aec7c5449\n")) | 
|  | 31 | +		require.NoError(t, err) | 
|  | 32 | + | 
|  | 33 | +		sha, typ, sz, err := ReadBatchLine(batch.InfoReader()) | 
|  | 34 | +		require.NoError(t, err) | 
|  | 35 | +		assert.Equal(t, "e2129701f1a4d54dc44f03c93bca0a2aec7c5449", string(sha)) | 
|  | 36 | +		assert.Equal(t, "blob", typ) | 
|  | 37 | +		assert.EqualValues(t, 6, sz) | 
|  | 38 | +	}) | 
|  | 39 | + | 
|  | 40 | +	t.Run("QueryContent", func(t *testing.T) { | 
|  | 41 | +		_, err = batch.QueryContent([]byte("e2129701f1a4d54dc44f03c93bca0a2aec7c5449\n")) | 
|  | 42 | +		require.NoError(t, err) | 
|  | 43 | + | 
|  | 44 | +		sha, typ, sz, err := ReadBatchLine(batch.ContentReader()) | 
|  | 45 | +		require.NoError(t, err) | 
|  | 46 | +		assert.Equal(t, "e2129701f1a4d54dc44f03c93bca0a2aec7c5449", string(sha)) | 
|  | 47 | +		assert.Equal(t, "blob", typ) | 
|  | 48 | +		assert.EqualValues(t, 6, sz) | 
|  | 49 | + | 
|  | 50 | +		content, err := io.ReadAll(io.LimitReader(batch.ContentReader(), sz)) | 
|  | 51 | +		require.NoError(t, err) | 
|  | 52 | +		require.Equal(t, "file1\n", string(content)) | 
|  | 53 | +	}) | 
|  | 54 | +} | 
0 commit comments