mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-07-12 06:59:24 +02:00
Some checks are pending
/ release (push) Waiting to run
testing-integration / test-unit (push) Waiting to run
testing-integration / test-sqlite (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/8261 Query string tokenizer could return a list containing empty tokens when the query string was `\` or `"` (probably in other scenarios as well). This seems undesirable and is what triggered #8260, but I'm posting this separately from that fix in case I'm wrong. Feel free to reject if so. The actual change in behavior is that now searching for `\` or `"` behaves the same as if the query were empty (the bleve/elastic code checks that the tokenizer actually returned, anything rather than just query being non-empty). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Co-authored-by: Danko Aleksejevs <danko@very.lv> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8398 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org> Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
295 lines
4.3 KiB
Go
295 lines
4.3 KiB
Go
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package internal
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type testIssueQueryStringOpt struct {
|
|
Keyword string
|
|
Results []Token
|
|
}
|
|
|
|
var testOpts = []testIssueQueryStringOpt{
|
|
{
|
|
Keyword: "Hello",
|
|
Results: []Token{
|
|
{
|
|
Term: "Hello",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "Hello World",
|
|
Results: []Token{
|
|
{
|
|
Term: "Hello",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
{
|
|
Term: "World",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "Hello World",
|
|
Results: []Token{
|
|
{
|
|
Term: "Hello",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
{
|
|
Term: "World",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: " Hello World ",
|
|
Results: []Token{
|
|
{
|
|
Term: "Hello",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
{
|
|
Term: "World",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "+Hello +World",
|
|
Results: []Token{
|
|
{
|
|
Term: "Hello",
|
|
Fuzzy: true,
|
|
Kind: BoolOptMust,
|
|
},
|
|
{
|
|
Term: "World",
|
|
Fuzzy: true,
|
|
Kind: BoolOptMust,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "+Hello World",
|
|
Results: []Token{
|
|
{
|
|
Term: "Hello",
|
|
Fuzzy: true,
|
|
Kind: BoolOptMust,
|
|
},
|
|
{
|
|
Term: "World",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "+Hello -World",
|
|
Results: []Token{
|
|
{
|
|
Term: "Hello",
|
|
Fuzzy: true,
|
|
Kind: BoolOptMust,
|
|
},
|
|
{
|
|
Term: "World",
|
|
Fuzzy: true,
|
|
Kind: BoolOptNot,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "\"Hello World\"",
|
|
Results: []Token{
|
|
{
|
|
Term: "Hello World",
|
|
Fuzzy: false,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "+\"Hello World\"",
|
|
Results: []Token{
|
|
{
|
|
Term: "Hello World",
|
|
Fuzzy: false,
|
|
Kind: BoolOptMust,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "-\"Hello World\"",
|
|
Results: []Token{
|
|
{
|
|
Term: "Hello World",
|
|
Fuzzy: false,
|
|
Kind: BoolOptNot,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "\"+Hello -World\"",
|
|
Results: []Token{
|
|
{
|
|
Term: "+Hello -World",
|
|
Fuzzy: false,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "\\+Hello", // \+Hello => +Hello
|
|
Results: []Token{
|
|
{
|
|
Term: "+Hello",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "\\\\Hello", // \\Hello => \Hello
|
|
Results: []Token{
|
|
{
|
|
Term: "\\Hello",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "\\\"Hello", // \"Hello => "Hello
|
|
Results: []Token{
|
|
{
|
|
Term: "\"Hello",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "\\",
|
|
Results: nil,
|
|
},
|
|
{
|
|
Keyword: "\"",
|
|
Results: nil,
|
|
},
|
|
{
|
|
Keyword: "Hello \\",
|
|
Results: []Token{
|
|
{
|
|
Term: "Hello",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "\"\"",
|
|
Results: nil,
|
|
},
|
|
{
|
|
Keyword: "\" World \"",
|
|
Results: []Token{
|
|
{
|
|
Term: " World ",
|
|
Fuzzy: false,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "\"\" World \"\"",
|
|
Results: []Token{
|
|
{
|
|
Term: "World",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Keyword: "Best \"Hello World\" Ever",
|
|
Results: []Token{
|
|
{
|
|
Term: "Best",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
{
|
|
Term: "Hello World",
|
|
Fuzzy: false,
|
|
Kind: BoolOptShould,
|
|
},
|
|
{
|
|
Term: "Ever",
|
|
Fuzzy: true,
|
|
Kind: BoolOptShould,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
func TestIssueQueryString(t *testing.T) {
|
|
var opt SearchOptions
|
|
for _, res := range testOpts {
|
|
t.Run(opt.Keyword, func(t *testing.T) {
|
|
opt.Keyword = res.Keyword
|
|
tokens, err := opt.Tokens()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, res.Results, tokens)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestToken_ParseIssueReference(t *testing.T) {
|
|
var tk Token
|
|
{
|
|
tk.Term = "123"
|
|
id, err := tk.ParseIssueReference()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(123), id)
|
|
}
|
|
{
|
|
tk.Term = "#123"
|
|
id, err := tk.ParseIssueReference()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(123), id)
|
|
}
|
|
{
|
|
tk.Term = "!123"
|
|
id, err := tk.ParseIssueReference()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(123), id)
|
|
}
|
|
{
|
|
tk.Term = "text"
|
|
_, err := tk.ParseIssueReference()
|
|
require.Error(t, err)
|
|
}
|
|
{
|
|
tk.Term = ""
|
|
_, err := tk.ParseIssueReference()
|
|
require.Error(t, err)
|
|
}
|
|
}
|