Unittest and exec file proof on search

master v1.3
bel 2019-12-01 14:16:45 -07:00
parent 081d50328f
commit 807072a77f
2 changed files with 14 additions and 2 deletions

View File

@ -8,7 +8,6 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings"
) )
func (n *Notes) Search(phrase string) (string, error) { func (n *Notes) Search(phrase string) (string, error) {
@ -21,10 +20,13 @@ func (n *Notes) Search(phrase string) (string, error) {
if info.IsDir() { if info.IsDir() {
return nil return nil
} }
if splits := strings.Split(info.Name(), "."); len(splits) > 1 && !(strings.HasSuffix(info.Name(), ".md") || strings.HasSuffix(info.Name(), ".txt")) { if size := info.Size(); size < 1 || size > (5*1024*1024) {
return nil return nil
} }
ok, err := grepFile(walked, []byte(phrase)) ok, err := grepFile(walked, []byte(phrase))
if err != nil && err.Error() == "bufio.Scanner: token too long" {
err = nil
}
if err == nil && ok { if err == nil && ok {
p := filetree.NewPathFromLocal(path.Dir(walked)) p := filetree.NewPathFromLocal(path.Dir(walked))
files.Push(p, info) files.Push(p, info)

View File

@ -42,3 +42,13 @@ func TestSearch(t *testing.T) {
t.Fatal(v, result) t.Fatal(v, result)
} }
} }
func TestSearchBigFiles(t *testing.T) {
n := New()
n.root = "/usr/local/bin"
_, err := n.Search("this file")
if err != nil {
t.Fatal(err)
}
}