mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-13 06:20:24 +00:00
[v14.0/forgejo] fix(ui): add missing translation for code search when keyword is empty string (#10970)
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/10964
- `CodeSearchMode` was is now set when keyword is empty
- The default value for search mode should be exact, use fuzzy ONLY when fuzziness is enabled in settings
(cherry picked from commit da7ce17533)
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10970
Reviewed-by: 0ko <0ko@noreply.codeberg.org>
Co-authored-by: Shiny Nematoda <snematoda.751k2@aleeas.com>
Co-committed-by: Shiny Nematoda <snematoda.751k2@aleeas.com>
This commit is contained in:
parent
191b309486
commit
c3fe2a5e34
2 changed files with 50 additions and 4 deletions
|
|
@ -69,8 +69,9 @@ func Search(ctx *context.Context) {
|
|||
mode := ExactSearchMode
|
||||
if modeStr := ctx.FormString("mode"); len(modeStr) > 0 {
|
||||
mode = searchModeFromString(modeStr)
|
||||
} else if ctx.FormOptionalBool("fuzzy").ValueOrDefault(true) { // for backward compatibility in links
|
||||
mode = UnionSearchMode
|
||||
} else if ctx.FormOptionalBool("fuzzy").ValueOrDefault(true) &&
|
||||
setting.Indexer.RepoIndexerEnableFuzzy { // for backward compatibility in links
|
||||
mode = FuzzySearchMode
|
||||
}
|
||||
|
||||
ctx.Data["Keyword"] = keyword
|
||||
|
|
@ -85,6 +86,11 @@ func Search(ctx *context.Context) {
|
|||
}
|
||||
|
||||
if keyword == "" {
|
||||
if setting.Indexer.RepoIndexerEnabled {
|
||||
ctx.Data["CodeSearchMode"] = mode.ToIndexer().String()
|
||||
} else {
|
||||
ctx.Data["CodeSearchMode"] = mode.ToGitGrep().String()
|
||||
}
|
||||
ctx.HTML(http.StatusOK, tplSearch)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,6 +83,9 @@ func testSearchRepo(t *testing.T, indexer bool) {
|
|||
code_indexer.UpdateRepoIndexer(repo)
|
||||
}
|
||||
|
||||
testEmptySearch(t, indexer, true)
|
||||
testEmptySearch(t, indexer, false)
|
||||
|
||||
testSearch(t, "/user2/glob/search?q=", []string{}, indexer)
|
||||
testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"}, indexer)
|
||||
testSearch(t, "/user2/glob/search?q=loren&page=1&mode=exact", []string{"a.txt"}, indexer)
|
||||
|
|
@ -97,6 +100,30 @@ func testSearchRepo(t *testing.T, indexer bool) {
|
|||
testSearch(t, "/user2/glob/search?q=file5&page=1&mode=exact", []string{}, indexer)
|
||||
}
|
||||
|
||||
func testEmptySearch(t *testing.T, indexer, withFuzzy bool) {
|
||||
defer test.MockVariableValue(&setting.Indexer.RepoIndexerEnableFuzzy, withFuzzy)()
|
||||
req := NewRequest(t, "GET", "/user2/glob/search")
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
container := NewHTMLParser(t, resp.Body).
|
||||
Find(".repository").
|
||||
Find(".ui.container")
|
||||
|
||||
key := "search.exact"
|
||||
if withFuzzy && indexer {
|
||||
key = "search.fuzzy"
|
||||
}
|
||||
|
||||
expected := translation.NewLocale("en-US").TrString(key)
|
||||
menu := container.Find(".menu[data-test-tag=fuzzy-dropdown]")
|
||||
defaultOpt := menu.
|
||||
Parent().
|
||||
Find(".text").
|
||||
Text()
|
||||
|
||||
assert.Equal(t, expected, strings.TrimSpace(defaultOpt))
|
||||
}
|
||||
|
||||
func testSearch(t *testing.T, rawURL string, expected []string, indexer bool) {
|
||||
req := NewRequest(t, "GET", rawURL)
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
|
|
@ -131,10 +158,23 @@ func testSearch(t *testing.T, rawURL string, expected []string, indexer bool) {
|
|||
|
||||
// testDropdownOptions verifies additional properties of dropdown options
|
||||
func testDropdownOptions(t *testing.T, container *goquery.Selection, options []string, locale translation.Locale) {
|
||||
for _, option := range options {
|
||||
tr := make([]string, len(options))
|
||||
for i, option := range options {
|
||||
tr[i] = locale.TrString(fmt.Sprintf("search.%s", option))
|
||||
}
|
||||
|
||||
// assert that the default value (in a .text adjacent to the menu) is a valid option
|
||||
defaultOpt := container.
|
||||
Find(".menu[data-test-tag=fuzzy-dropdown]").
|
||||
Parent().
|
||||
Find(".text").
|
||||
Text()
|
||||
assert.Contains(t, tr, strings.TrimSpace(defaultOpt))
|
||||
|
||||
for i, option := range options {
|
||||
label := container.Find(fmt.Sprintf("label.item:has(input[value='%s'])", option))
|
||||
name := strings.TrimSpace(label.Text())
|
||||
assert.Equal(t, name, locale.TrString(fmt.Sprintf("search.%s", option)))
|
||||
assert.Equal(t, name, tr[i])
|
||||
|
||||
tooltip, exists := label.Attr("data-tooltip-content")
|
||||
assert.True(t, exists)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue