mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-09 11:32:29 +00:00
a7df0d5f41
1. refactor the legacy code and add more comments, remove the "+1/-1" tricks, clarify the BuildBlobExcerptDiffSection behavior 2. fix a line-counting bug (see screenshot below)
77 lines
1.8 KiB
Go
77 lines
1.8 KiB
Go
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"io"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBlob_Data(t *testing.T) {
|
|
output := "file2\n"
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
repo, err := OpenRepositoryLocal(t.Context(), bareRepo1Path)
|
|
require.NoError(t, err)
|
|
defer repo.Close()
|
|
|
|
testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375")
|
|
assert.NoError(t, err)
|
|
|
|
r, err := testBlob.DataAsync(t.Context())
|
|
assert.NoError(t, err)
|
|
require.NotNil(t, r)
|
|
|
|
data, err := io.ReadAll(r)
|
|
assert.NoError(t, r.Close())
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, output, string(data))
|
|
}
|
|
|
|
func Benchmark_Blob_Data(b *testing.B) {
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
repo, err := OpenRepositoryLocal(b.Context(), bareRepo1Path)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
defer repo.Close()
|
|
|
|
testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375")
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
|
|
for b.Loop() {
|
|
r, err := testBlob.DataAsync(b.Context())
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
io.ReadAll(r)
|
|
_ = r.Close()
|
|
}
|
|
}
|
|
|
|
func TestGetBlobLineCount(t *testing.T) {
|
|
size, count, err := getBlobLineCount(strings.NewReader(""), nil)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 0, size)
|
|
assert.Equal(t, 0, count)
|
|
|
|
size, count, err = getBlobLineCount(strings.NewReader("\n"), nil)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 1, size)
|
|
assert.Equal(t, 1, count)
|
|
|
|
size, count, err = getBlobLineCount(strings.NewReader("a\nb"), nil)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 3, size)
|
|
assert.Equal(t, 2, count)
|
|
}
|