Impl FTS and some main tests
parent
60dc6bc876
commit
259a8efc70
2
TODO
2
TODO
|
|
@ -14,7 +14,7 @@ FTS
|
||||||
|
|
||||||
main test -
|
main test -
|
||||||
- create,
|
- create,
|
||||||
- header
|
x header
|
||||||
- text box
|
- text box
|
||||||
- submit
|
- submit
|
||||||
- submit target
|
- submit target
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,171 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"local/notes-server/config"
|
||||||
|
"local/notes-server/server"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAll(t *testing.T) {
|
||||||
|
makeFiles(t)
|
||||||
|
defer os.RemoveAll(config.Root)
|
||||||
|
s := makeServer(t)
|
||||||
|
defer s.Close()
|
||||||
|
testServer(t, s.URL)
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeFiles(t *testing.T) {
|
||||||
|
d, err := ioutil.TempDir(os.TempDir(), "pattern*")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
config.Root = d
|
||||||
|
for _, dir := range []string{"dirA", "dirB"} {
|
||||||
|
if err := os.MkdirAll(path.Join(d, dir), os.ModePerm); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, file := range []string{"fileA", "fileB"} {
|
||||||
|
content := fmt.Sprintf("hello from %s/%s/%s", d, dir, file)
|
||||||
|
err := ioutil.WriteFile(
|
||||||
|
path.Join(d, dir, file),
|
||||||
|
[]byte(content),
|
||||||
|
os.ModePerm,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeServer(t *testing.T) *httptest.Server {
|
||||||
|
s := server.New()
|
||||||
|
if err := s.Routes(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return httptest.NewServer(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testServer(t *testing.T, url string) {
|
||||||
|
testCreate(t, url)
|
||||||
|
testEdit(t, url)
|
||||||
|
testDir(t, url)
|
||||||
|
testFile(t, url)
|
||||||
|
testNavRootDir(t, url)
|
||||||
|
testNavRootFile(t, url)
|
||||||
|
testNavDirDir(t, url)
|
||||||
|
testNavDirFile(t, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testCreate(t *testing.T, url string) {
|
||||||
|
for _, path := range []string{"dirX/fileX", "fileX"} {
|
||||||
|
resp, err := http.Get(url + "/create/" + path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatal(resp.StatusCode)
|
||||||
|
}
|
||||||
|
b, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
s := string(b)
|
||||||
|
log.Println(string(s))
|
||||||
|
if ok := assertHasMultilink(s, path); !ok {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if ok := assertHasForm(s, "/submit/"+path); !ok {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if ok := assertHasTextArea(s); !ok {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if ok := assertHasSubmit(s); !ok {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testEdit(t *testing.T, url string) {
|
||||||
|
for _, path := range []string{"dirX/fileX", "fileX"} {
|
||||||
|
resp, err := http.Get(url + "/edit/" + path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatal(resp.StatusCode)
|
||||||
|
}
|
||||||
|
b, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
s := string(b)
|
||||||
|
if ok := assertHasMultilink(s, path); !ok {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if ok := assertHasForm(s); !ok {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if ok := assertHasTextArea(s); !ok {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if ok := assertHasSubmit(s); !ok {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testDir(t *testing.T, url string) {
|
||||||
|
t.Error("not impl")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testFile(t *testing.T, url string) {
|
||||||
|
t.Error("not impl")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testNavRootDir(t *testing.T, url string) {
|
||||||
|
t.Error("not impl")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testNavRootFile(t *testing.T, url string) {
|
||||||
|
t.Error("not impl")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testNavDirDir(t *testing.T, url string) {
|
||||||
|
t.Error("not impl")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testNavDirFile(t *testing.T, url string) {
|
||||||
|
t.Error("not impl")
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertHasMultilink(body string, segments ...string) bool {
|
||||||
|
if !strings.Contains(body, `/<a href="/notes">notes</a>/`) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, segment := range segments {
|
||||||
|
regexp := regexp.MustCompile(`<a href="[^"]*` + segment + `">`)
|
||||||
|
if !regexp.MatchString(body) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertHasForm(body string, action ...string) bool {
|
||||||
|
return strings.Contains(body, `<form`) && (len(action) == 0 || strings.Contains(body, `action="`))
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertHasTextArea(body string) bool {
|
||||||
|
return strings.Contains(body, `<textarea`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertHasSubmit(body string) bool {
|
||||||
|
return strings.Contains(body, `<button`) && strings.Contains(body, `type="submit"`)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
package notes
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
package notes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"local/notes-server/filetree"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (n *Notes) Search(phrase string) (string, error) {
|
||||||
|
files := filetree.NewFiles()
|
||||||
|
err := filepath.Walk(n.root,
|
||||||
|
func(path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ok, err := grepFile(path, []byte(phrase))
|
||||||
|
if ok {
|
||||||
|
p := filetree.NewPathFromLocal(path)
|
||||||
|
files.Push(p, info)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return filetree.Paths(*files).List(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func grepFile(file string, pat []byte) (bool, error) {
|
||||||
|
f, err := os.Open(file)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
scanner := bufio.NewScanner(f)
|
||||||
|
for scanner.Scan() {
|
||||||
|
if bytes.Contains(scanner.Bytes(), pat) {
|
||||||
|
return true, scanner.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, scanner.Err()
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
package notes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSearch(t *testing.T) {
|
||||||
|
d, err := ioutil.TempDir(os.TempDir(), "pattern*")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(d)
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
f, err := ioutil.TempFile(d, fmt.Sprintf("file_%d", i))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(f, "this file is number %d", i)
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
n := New()
|
||||||
|
n.root = d
|
||||||
|
|
||||||
|
result, err := n.Search("this file")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if v := len(strings.Split(result, "\n")); v < 7 {
|
||||||
|
t.Fatal(v, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err = n.Search("4")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if v := len(strings.Split(result, "\n")); v > 4 {
|
||||||
|
t.Fatal(v, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue