2024-03-24 17:05:00 +01:00
|
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2024-04-11 13:34:53 +02:00
|
|
|
"bytes"
|
2024-03-24 17:05:00 +01:00
|
|
|
"context"
|
2024-04-11 13:34:53 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
2024-03-24 17:05:00 +01:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 21:42:06 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-03-24 17:05:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGrepSearch(t *testing.T) {
|
|
|
|
repo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "language_stats_repo"))
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2024-03-24 17:05:00 +01:00
|
|
|
defer repo.Close()
|
|
|
|
|
|
|
|
res, err := GrepSearch(context.Background(), repo, "void", GrepOptions{})
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2024-03-24 17:05:00 +01:00
|
|
|
assert.Equal(t, []*GrepResult{
|
|
|
|
{
|
|
|
|
Filename: "java-hello/main.java",
|
|
|
|
LineNumbers: []int{3},
|
|
|
|
LineCodes: []string{" public static void main(String[] args)"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Filename: "main.vendor.java",
|
|
|
|
LineNumbers: []int{3},
|
|
|
|
LineCodes: []string{" public static void main(String[] args)"},
|
|
|
|
},
|
|
|
|
}, res)
|
|
|
|
|
2024-03-25 14:25:22 +01:00
|
|
|
res, err = GrepSearch(context.Background(), repo, "void", GrepOptions{MaxResultLimit: 1})
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2024-03-25 14:25:22 +01:00
|
|
|
assert.Equal(t, []*GrepResult{
|
|
|
|
{
|
|
|
|
Filename: "java-hello/main.java",
|
|
|
|
LineNumbers: []int{3},
|
|
|
|
LineCodes: []string{" public static void main(String[] args)"},
|
|
|
|
},
|
|
|
|
}, res)
|
|
|
|
|
2024-03-24 17:05:00 +01:00
|
|
|
res, err = GrepSearch(context.Background(), repo, "no-such-content", GrepOptions{})
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, res)
|
2024-03-24 17:05:00 +01:00
|
|
|
|
|
|
|
res, err = GrepSearch(context.Background(), &Repository{Path: "no-such-git-repo"}, "no-such-content", GrepOptions{})
|
2024-07-30 21:42:06 +02:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Empty(t, res)
|
2024-03-24 17:05:00 +01:00
|
|
|
}
|
2024-04-11 13:34:53 +02:00
|
|
|
|
|
|
|
func TestGrepLongFiles(t *testing.T) {
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
|
|
|
err := InitRepository(DefaultContext, tmpDir, false, Sha1ObjectFormat.Name())
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2024-04-11 13:34:53 +02:00
|
|
|
|
|
|
|
gitRepo, err := openRepositoryWithDefaultContext(tmpDir)
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2024-04-11 13:34:53 +02:00
|
|
|
defer gitRepo.Close()
|
|
|
|
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, os.WriteFile(path.Join(tmpDir, "README.md"), bytes.Repeat([]byte{'a'}, 65*1024), 0o666))
|
2024-04-11 13:34:53 +02:00
|
|
|
|
|
|
|
err = AddChanges(tmpDir, true)
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2024-04-11 13:34:53 +02:00
|
|
|
|
|
|
|
err = CommitChanges(tmpDir, CommitChangesOptions{Message: "Long file"})
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2024-04-11 13:34:53 +02:00
|
|
|
|
|
|
|
res, err := GrepSearch(context.Background(), gitRepo, "a", GrepOptions{})
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2024-04-11 13:34:53 +02:00
|
|
|
assert.Len(t, res, 1)
|
|
|
|
assert.Len(t, res[0].LineCodes[0], 65*1024)
|
|
|
|
}
|