forkjo/services/repository/files/search_test.go
Shiny Nematoda 3b2f28ff1c [PORT] Fix wrong line number in code search result (gitea#29260) (#2619)
Port [Fix wrong line number in code search result (gitea#29260)](https://github.com/go-gitea/gitea/pull/29260)

PS: also added [`-e`](https://git-scm.com/docs/git-grep#Documentation/git-grep.txt--e) before passing the keyword (my bad)

Co-authored-by: yp05327 <576951401@qq.com>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2619
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Shiny Nematoda <snematoda.751k2@aleeas.com>
Co-committed-by: Shiny Nematoda <snematoda.751k2@aleeas.com>
2024-03-10 15:35:30 +00:00

50 lines
1.2 KiB
Go

package files
import (
"testing"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/services/contexttest"
"github.com/stretchr/testify/assert"
)
func TestNewRepoGrep(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
t.Run("with result", func(t *testing.T) {
res, err := NewRepoGrep(ctx, ctx.Repo.Repository, "Description")
assert.NoError(t, err)
expected := []*Result{
{
RepoID: 0,
Filename: "README.md",
CommitID: "master",
UpdatedUnix: 0,
Language: "Markdown",
Color: "#083fa1",
Lines: []ResultLine{
{Num: 2, FormattedContent: ""},
{Num: 3, FormattedContent: "Description for repo1"},
},
},
}
assert.EqualValues(t, res, expected)
})
t.Run("empty result", func(t *testing.T) {
res, err := NewRepoGrep(ctx, ctx.Repo.Repository, "keyword that does not match in the repo")
assert.NoError(t, err)
assert.EqualValues(t, res, []*Result{})
})
}