mirror of https://github.com/go-gitea/gitea.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.2 KiB
80 lines
2.2 KiB
// Copyright 2024 The Gitea Authors. All rights reserved. |
|
// SPDX-License-Identifier: MIT |
|
|
|
package git |
|
|
|
import ( |
|
"path/filepath" |
|
"testing" |
|
|
|
"github.com/stretchr/testify/assert" |
|
) |
|
|
|
func TestGrepSearch(t *testing.T) { |
|
repo, err := OpenRepository(t.Context(), filepath.Join(testReposDir, "language_stats_repo")) |
|
assert.NoError(t, err) |
|
defer repo.Close() |
|
|
|
res, err := GrepSearch(t.Context(), repo, "void", GrepOptions{}) |
|
assert.NoError(t, err) |
|
assert.Equal(t, []*GrepResult{ |
|
{ |
|
Filename: "java-hello/main.java", |
|
LineNumbers: []int{3}, |
|
LineCodes: []string{" public static void main(String[] args)"}, |
|
}, |
|
{ |
|
Filename: "main.vendor.java", |
|
LineNumbers: []int{3}, |
|
LineCodes: []string{" public static void main(String[] args)"}, |
|
}, |
|
}, res) |
|
|
|
res, err = GrepSearch(t.Context(), repo, "void", GrepOptions{PathspecList: []string{":(glob)java-hello/*"}}) |
|
assert.NoError(t, err) |
|
assert.Equal(t, []*GrepResult{ |
|
{ |
|
Filename: "java-hello/main.java", |
|
LineNumbers: []int{3}, |
|
LineCodes: []string{" public static void main(String[] args)"}, |
|
}, |
|
}, res) |
|
|
|
res, err = GrepSearch(t.Context(), repo, "void", GrepOptions{PathspecList: []string{":(glob,exclude)java-hello/*"}}) |
|
assert.NoError(t, err) |
|
assert.Equal(t, []*GrepResult{ |
|
{ |
|
Filename: "main.vendor.java", |
|
LineNumbers: []int{3}, |
|
LineCodes: []string{" public static void main(String[] args)"}, |
|
}, |
|
}, res) |
|
|
|
res, err = GrepSearch(t.Context(), repo, "void", GrepOptions{MaxResultLimit: 1}) |
|
assert.NoError(t, err) |
|
assert.Equal(t, []*GrepResult{ |
|
{ |
|
Filename: "java-hello/main.java", |
|
LineNumbers: []int{3}, |
|
LineCodes: []string{" public static void main(String[] args)"}, |
|
}, |
|
}, res) |
|
|
|
res, err = GrepSearch(t.Context(), repo, "void", GrepOptions{MaxResultLimit: 1, MaxLineLength: 39}) |
|
assert.NoError(t, err) |
|
assert.Equal(t, []*GrepResult{ |
|
{ |
|
Filename: "java-hello/main.java", |
|
LineNumbers: []int{3}, |
|
LineCodes: []string{" public static void main(String[] arg"}, |
|
}, |
|
}, res) |
|
|
|
res, err = GrepSearch(t.Context(), repo, "no-such-content", GrepOptions{}) |
|
assert.NoError(t, err) |
|
assert.Empty(t, res) |
|
|
|
res, err = GrepSearch(t.Context(), &Repository{Path: "no-such-git-repo"}, "no-such-content", GrepOptions{}) |
|
assert.Error(t, err) |
|
assert.Empty(t, res) |
|
}
|
|
|