grep: add tests

This commit is contained in:
Shiny Nematoda 2024-05-07 15:15:05 +05:30
parent 46a5b13292
commit 514b7810e1
4 changed files with 44 additions and 8 deletions

View file

@ -69,10 +69,10 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
len(setting.Indexer.ExcludePatterns)+
len(opts.PathSpec))
for _, expr := range append(setting.Indexer.IncludePatterns, opts.PathSpec...) {
files = append(files, ":(glob)"+expr.Pattern())
files = append(files, ":"+expr.Pattern())
}
for _, expr := range setting.Indexer.ExcludePatterns {
files = append(files, ":(glob,exclude)"+expr.Pattern())
files = append(files, ":^"+expr.Pattern())
}
cmd.AddDynamicArguments(cmp.Or(opts.RefName, "HEAD")).AddDashesAndList(files...)

View file

@ -11,7 +11,7 @@
{{if $description}}<span class="description">{{$description | RenderCodeBlock}}</span>{{else}}<span class="no-description text-italic">{{ctx.Locale.Tr "repo.no_desc"}}</span>{{end}}
{{if .Repository.Website}}<a class="link" href="{{.Repository.Website}}">{{.Repository.Website}}</a>{{end}}
</div>
<form class="ignore-dirty" action="{{.RepoLink}}/search/{{if .CodeIndexerDisabled}}{{.BranchNameSubURL}}{{end}}" method="get">
<form class="ignore-dirty" action="{{.RepoLink}}/search/{{if .CodeIndexerDisabled}}{{.BranchNameSubURL}}{{end}}" method="get" data-test-tag="codesearch">
<div class="ui small action input">
<input name="q" value="{{.Keyword}}" placeholder="{{ctx.Locale.Tr "search.code_kind"}}">
{{template "shared/search/button"}}

View file

@ -12,6 +12,7 @@ import (
code_indexer "code.gitea.io/gitea/modules/indexer/code"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/routers"
"code.gitea.io/gitea/tests"
"github.com/PuerkitoBio/goquery"
@ -38,6 +39,7 @@ func TestSearchRepoNoIndexer(t *testing.T) {
func testSearchRepo(t *testing.T, indexer bool) {
defer tests.PrepareTestEnv(t)()
defer test.MockVariableValue(&setting.Indexer.RepoIndexerEnabled, indexer)()
defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "repo1")
assert.NoError(t, err)
@ -48,6 +50,13 @@ func testSearchRepo(t *testing.T, indexer bool) {
testSearch(t, "/user2/repo1/search?q=Description&page=1", []string{"README.md"}, indexer)
req := NewRequest(t, "HEAD", "/user2/repo1/search/branch/"+repo.DefaultBranch)
if indexer {
MakeRequest(t, req, http.StatusNotFound)
} else {
MakeRequest(t, req, http.StatusOK)
}
defer test.MockVariableValue(&setting.Indexer.IncludePatterns, setting.IndexerGlobFromString("**.txt"))()
defer test.MockVariableValue(&setting.Indexer.ExcludePatterns, setting.IndexerGlobFromString("**/y/**"))()

View file

@ -190,10 +190,7 @@ func TestViewRepoWithSymlinks(t *testing.T) {
// TestViewAsRepoAdmin tests PR #2167
func TestViewAsRepoAdmin(t *testing.T) {
for user, expectedNoDescription := range map[string]bool{
"user2": true,
"user4": false,
} {
for _, user := range []string{"user2", "user4"} {
defer tests.PrepareTestEnv(t)()
session := loginUser(t, user)
@ -206,7 +203,7 @@ func TestViewAsRepoAdmin(t *testing.T) {
repoTopics := htmlDoc.doc.Find("#repo-topics").Children()
repoSummary := htmlDoc.doc.Find(".repository-summary").Children()
assert.Equal(t, expectedNoDescription, noDescription.HasClass("no-description"))
assert.True(t, noDescription.HasClass("no-description"))
assert.True(t, repoTopics.HasClass("repo-topic"))
assert.True(t, repoSummary.HasClass("repository-menu"))
}
@ -995,3 +992,33 @@ func TestViewRepoOpenWith(t *testing.T) {
testOpenWith([]string{"test://"})
})
}
func TestRepoCodeSearchForm(t *testing.T) {
defer tests.PrepareTestEnv(t)()
testSearchForm := func(t *testing.T, indexer bool) {
defer test.MockVariableValue(&setting.Indexer.RepoIndexerEnabled, indexer)()
req := NewRequest(t, "GET", "/user2/repo1/src/branch/master")
resp := MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
action, exists := htmlDoc.doc.Find("form[data-test-tag=codesearch]").Attr("action")
assert.True(t, exists)
branchSubURL := "/branch/master"
if indexer {
assert.NotContains(t, action, branchSubURL)
} else {
assert.Contains(t, action, branchSubURL)
}
}
t.Run("indexer disabled", func(t *testing.T) {
testSearchForm(t, false)
})
t.Run("indexer enabled", func(t *testing.T) {
testSearchForm(t, true)
})
}