From aec787b9279399e54e6dd04dc4009addbf93fe89 Mon Sep 17 00:00:00 2001 From: Shiny Nematoda Date: Thu, 11 Apr 2024 14:52:37 +0000 Subject: [PATCH 1/2] fix code search template --- routers/web/repo/search.go | 2 +- templates/shared/search/code/results.tmpl | 4 ++-- templates/shared/search/code/search.tmpl | 8 ++------ templates/shared/searchfile.tmpl | 2 +- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/routers/web/repo/search.go b/routers/web/repo/search.go index 6e70428547..02df17a7e6 100644 --- a/routers/web/repo/search.go +++ b/routers/web/repo/search.go @@ -86,7 +86,7 @@ func Search(ctx *context.Context) { } } - ctx.Data["CodeIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled + ctx.Data["CodeIndexerDisabled"] = !setting.Indexer.RepoIndexerEnabled ctx.Data["Repo"] = ctx.Repo.Repository ctx.Data["SourcePath"] = ctx.Repo.Repository.Link() ctx.Data["SearchResults"] = searchResults diff --git a/templates/shared/search/code/results.tmpl b/templates/shared/search/code/results.tmpl index 022192a150..a98a662654 100644 --- a/templates/shared/search/code/results.tmpl +++ b/templates/shared/search/code/results.tmpl @@ -24,10 +24,10 @@ {{else}} {{.Filename}} {{end}} - {{ctx.Locale.Tr "repo.diff.view_file"}} + {{ctx.Locale.Tr "repo.diff.view_file"}}
- {{template "shared/searchfile" dict "RepoLink" $repo.Link "CodeIndexerDisabled" $.CodeIndexerDisabled "SearchResult" .}} + {{template "shared/searchfile" dict "RepoLink" $repo.Link "SearchResult" .}}
{{template "shared/searchbottom" dict "root" $ "result" .}} diff --git a/templates/shared/search/code/search.tmpl b/templates/shared/search/code/search.tmpl index 4a9d905b9d..581d1ea383 100644 --- a/templates/shared/search/code/search.tmpl +++ b/templates/shared/search/code/search.tmpl @@ -1,9 +1,5 @@
- {{if not $.CodeIndexerDisabled}} - {{template "shared/search/combo_fuzzy" dict "Value" .Keyword "Disabled" .CodeIndexerUnavailable "IsFuzzy" .IsFuzzy "Placeholder" (ctx.Locale.Tr "search.code_kind")}} - {{else}} - {{template "shared/search/combo" dict "Value" .Keyword "Placeholder" (ctx.Locale.Tr "search.code_kind")}} - {{end}} + {{template "shared/search/combo_fuzzy" dict "Value" .Keyword "Disabled" .CodeIndexerUnavailable "IsFuzzy" .IsFuzzy "Placeholder" (ctx.Locale.Tr "search.code_kind")}}
@@ -12,7 +8,7 @@

{{ctx.Locale.Tr "search.code_search_unavailable"}}

{{else}} - {{if not .CodeIndexerEnabled}} + {{if .CodeIndexerDisabled}}

{{ctx.Locale.Tr "search.code_search_by_git_grep"}}

diff --git a/templates/shared/searchfile.tmpl b/templates/shared/searchfile.tmpl index f2c1369555..280584e4d1 100644 --- a/templates/shared/searchfile.tmpl +++ b/templates/shared/searchfile.tmpl @@ -4,7 +4,7 @@ {{range .SearchResult.Lines}} - {{.Num}} + {{.Num}} {{.FormattedContent}} From a67ca43f75aca86a7d01f9e7917402b58452a783 Mon Sep 17 00:00:00 2001 From: Shiny Nematoda Date: Thu, 11 Apr 2024 15:42:59 +0000 Subject: [PATCH 2/2] add test --- templates/shared/search/code/search.tmpl | 2 +- tests/integration/explore_code_test.go | 25 +++++++++++++++++ tests/integration/repo_search_test.go | 34 +++++++++++++----------- 3 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 tests/integration/explore_code_test.go diff --git a/templates/shared/search/code/search.tmpl b/templates/shared/search/code/search.tmpl index 581d1ea383..37a23dc3d6 100644 --- a/templates/shared/search/code/search.tmpl +++ b/templates/shared/search/code/search.tmpl @@ -9,7 +9,7 @@ {{else}} {{if .CodeIndexerDisabled}} -
+

{{ctx.Locale.Tr "search.code_search_by_git_grep"}}

{{end}} diff --git a/tests/integration/explore_code_test.go b/tests/integration/explore_code_test.go new file mode 100644 index 0000000000..cf3939b093 --- /dev/null +++ b/tests/integration/explore_code_test.go @@ -0,0 +1,25 @@ +package integration + +import ( + "net/http" + "testing" + + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/test" + "code.gitea.io/gitea/tests" + + "github.com/stretchr/testify/assert" +) + +func TestExploreCodeSearchIndexer(t *testing.T) { + defer tests.PrepareTestEnv(t)() + defer test.MockVariableValue(&setting.Indexer.RepoIndexerEnabled, true)() + + req := NewRequest(t, "GET", "/explore/code") + resp := MakeRequest(t, req, http.StatusOK) + + doc := NewHTMLParser(t, resp.Body) + msg := doc.Find(".explore").Find(".ui.container").Find(".ui.message[data-test-tag=grep]") + + assert.EqualValues(t, 0, len(msg.Nodes)) +} diff --git a/tests/integration/repo_search_test.go b/tests/integration/repo_search_test.go index b6d2e24df6..e058851071 100644 --- a/tests/integration/repo_search_test.go +++ b/tests/integration/repo_search_test.go @@ -19,7 +19,7 @@ import ( ) func resultFilenames(t testing.TB, doc *HTMLDoc) []string { - filenameSelections := doc.doc.Find(".repository.search").Find(".repo-search-result").Find(".header").Find("span.file") + filenameSelections := doc.Find(".repository.search").Find(".repo-search-result").Find(".header").Find("span.file") result := make([]string, filenameSelections.Length()) filenameSelections.Each(func(i int, selection *goquery.Selection) { result[i] = selection.Text() @@ -46,7 +46,7 @@ func testSearchRepo(t *testing.T, indexer bool) { code_indexer.UpdateRepoIndexer(repo) } - testSearch(t, "/user2/repo1/search?q=Description&page=1", []string{"README.md"}) + testSearch(t, "/user2/repo1/search?q=Description&page=1", []string{"README.md"}, indexer) defer test.MockVariableValue(&setting.Indexer.IncludePatterns, setting.IndexerGlobFromString("**.txt"))() defer test.MockVariableValue(&setting.Indexer.ExcludePatterns, setting.IndexerGlobFromString("**/y/**"))() @@ -58,32 +58,36 @@ func testSearchRepo(t *testing.T, indexer bool) { code_indexer.UpdateRepoIndexer(repo) } - testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"}) - testSearch(t, "/user2/glob/search?q=loren&page=1&fuzzy=false", []string{"a.txt"}) + testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"}, indexer) + testSearch(t, "/user2/glob/search?q=loren&page=1&fuzzy=false", []string{"a.txt"}, indexer) if indexer { // fuzzy search: matches both file3 (x/b.txt) and file1 (a.txt) // when indexer is enabled - testSearch(t, "/user2/glob/search?q=file3&page=1", []string{"x/b.txt", "a.txt"}) - testSearch(t, "/user2/glob/search?q=file4&page=1", []string{"x/b.txt", "a.txt"}) - testSearch(t, "/user2/glob/search?q=file5&page=1", []string{"x/b.txt", "a.txt"}) + testSearch(t, "/user2/glob/search?q=file3&page=1", []string{"x/b.txt", "a.txt"}, indexer) + testSearch(t, "/user2/glob/search?q=file4&page=1", []string{"x/b.txt", "a.txt"}, indexer) + testSearch(t, "/user2/glob/search?q=file5&page=1", []string{"x/b.txt", "a.txt"}, indexer) } else { // fuzzy search: OR of all the keywords // when indexer is disabled - testSearch(t, "/user2/glob/search?q=file3+file1&page=1", []string{"a.txt", "x/b.txt"}) - testSearch(t, "/user2/glob/search?q=file4&page=1", []string{}) - testSearch(t, "/user2/glob/search?q=file5&page=1", []string{}) + testSearch(t, "/user2/glob/search?q=file3+file1&page=1", []string{"a.txt", "x/b.txt"}, indexer) + testSearch(t, "/user2/glob/search?q=file4&page=1", []string{}, indexer) + testSearch(t, "/user2/glob/search?q=file5&page=1", []string{}, indexer) } - testSearch(t, "/user2/glob/search?q=file3&page=1&fuzzy=false", []string{"x/b.txt"}) - testSearch(t, "/user2/glob/search?q=file4&page=1&fuzzy=false", []string{}) - testSearch(t, "/user2/glob/search?q=file5&page=1&fuzzy=false", []string{}) + testSearch(t, "/user2/glob/search?q=file3&page=1&fuzzy=false", []string{"x/b.txt"}, indexer) + testSearch(t, "/user2/glob/search?q=file4&page=1&fuzzy=false", []string{}, indexer) + testSearch(t, "/user2/glob/search?q=file5&page=1&fuzzy=false", []string{}, indexer) } -func testSearch(t *testing.T, url string, expected []string) { +func testSearch(t *testing.T, url string, expected []string, indexer bool) { req := NewRequest(t, "GET", url) resp := MakeRequest(t, req, http.StatusOK) - filenames := resultFilenames(t, NewHTMLParser(t, resp.Body)) + doc := NewHTMLParser(t, resp.Body) + msg := doc.Find(".repository").Find(".ui.container").Find(".ui.message[data-test-tag=grep]") + assert.EqualValues(t, indexer, len(msg.Nodes) == 0) + + filenames := resultFilenames(t, doc) assert.EqualValues(t, expected, filenames) }