From 3079cd163fbd6865cc968f3db7e9e22a58137162 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 21 Nov 2019 12:28:30 -0700 Subject: [PATCH] Fix absolute and relative without parent dir paths work --- .gitignore | 1 + filetree/path.go | 6 +++++- filetree/path_test.go | 6 ++++++ main_test.go | 26 ++++++++++++++++++-------- public/A/D/G/A/b | 1 + public/A/abc | 1 + public/A/asdf | 1 + public/A/x | 12 ++++++++++++ public/B/y | 5 +++++ public/B/z | 8 ++++++++ public/D/E/F/g | 1 + 11 files changed, 59 insertions(+), 9 deletions(-) create mode 100755 public/A/D/G/A/b create mode 100755 public/A/abc create mode 100755 public/A/asdf create mode 100755 public/A/x create mode 100755 public/B/y create mode 100755 public/B/z create mode 100755 public/D/E/F/g diff --git a/.gitignore b/.gitignore index 29de071..5722ea5 100755 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ gollum +public **.sw* **/**.sw* *.sw* diff --git a/filetree/path.go b/filetree/path.go index 8621fa1..cd4eab2 100755 --- a/filetree/path.go +++ b/filetree/path.go @@ -16,7 +16,11 @@ type Path struct { } func NewPathFromLocal(p string) Path { - splits := strings.SplitN(p, path.Base(config.Root)+"/", 2) + root := config.Root + "/" + if strings.HasPrefix(root, "./") { + root = root[2:] + } + splits := strings.SplitN(p, root, 2) href := splits[0] if len(splits) > 1 && (splits[0] == "" || splits[0] == "/") { href = splits[1] diff --git a/filetree/path_test.go b/filetree/path_test.go index b08a52b..b466ca9 100755 --- a/filetree/path_test.go +++ b/filetree/path_test.go @@ -32,6 +32,12 @@ func TestNewPathFromLocal(t *testing.T) { href string local string }{ + { + in: "/wiki/wiki/b/a.md", + root: "/wiki/wiki", + href: "/notes/b/a.md", + local: "/wiki/wiki/b/a.md", + }, { in: "/wiki/b/a.md", root: "/wiki", diff --git a/main_test.go b/main_test.go index 68b9e21..8b0e07d 100644 --- a/main_test.go +++ b/main_test.go @@ -16,16 +16,26 @@ import ( ) func TestAll(t *testing.T) { - makeFiles(t) - defer os.RemoveAll(config.Root) - log.Println(config.Root) - s := makeServer(t) - defer s.Close() - testServer(t, s.URL) + for _, basedir := range []string{os.TempDir(), "./tempDir"} { + os.MkdirAll(basedir, os.ModePerm) + makeFiles(t, basedir) + if basedir[0] == '.' && config.Root[0] != '.' { + config.Root = "./" + config.Root + } + defer os.RemoveAll(config.Root) + log.Println(config.Root) + t.Log("trying with root", config.Root) + s := makeServer(t) + defer s.Close() + testServer(t, s.URL) + if basedir[0] == '.' { + os.RemoveAll(basedir) + } + } } -func makeFiles(t *testing.T) { - d, err := ioutil.TempDir(os.TempDir(), "pattern*") +func makeFiles(t *testing.T, basedir string) { + d, err := ioutil.TempDir(basedir, "pattern*") if err != nil { t.Fatal(err) } diff --git a/public/A/D/G/A/b b/public/A/D/G/A/b new file mode 100755 index 0000000..5e40c08 --- /dev/null +++ b/public/A/D/G/A/b @@ -0,0 +1 @@ +asdf \ No newline at end of file diff --git a/public/A/abc b/public/A/abc new file mode 100755 index 0000000..5e40c08 --- /dev/null +++ b/public/A/abc @@ -0,0 +1 @@ +asdf \ No newline at end of file diff --git a/public/A/asdf b/public/A/asdf new file mode 100755 index 0000000..5e40c08 --- /dev/null +++ b/public/A/asdf @@ -0,0 +1 @@ +asdf \ No newline at end of file diff --git a/public/A/x b/public/A/x new file mode 100755 index 0000000..24dd6a1 --- /dev/null +++ b/public/A/x @@ -0,0 +1,12 @@ +# A.x + +| hello | world | +|-------|-------| +| cont | ent. | + +## A.X + +1 +2 + +3 \ No newline at end of file diff --git a/public/B/y b/public/B/y new file mode 100755 index 0000000..a23a765 --- /dev/null +++ b/public/B/y @@ -0,0 +1,5 @@ +## B.y + +| hello | world | +|-------|-------| +| cont | ent. | diff --git a/public/B/z b/public/B/z new file mode 100755 index 0000000..61c1394 --- /dev/null +++ b/public/B/z @@ -0,0 +1,8 @@ +## B.z + +| hello | world | +|-------|-------| +| cont | ent. | + + +HI diff --git a/public/D/E/F/g b/public/D/E/F/g new file mode 100755 index 0000000..32f95c0 --- /dev/null +++ b/public/D/E/F/g @@ -0,0 +1 @@ +hi \ No newline at end of file