Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/core/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"path/filepath"
"slices"
"strings"
)

// fileStorageMock implements an in-memory FileStorage for testing purposes.
Expand Down Expand Up @@ -61,7 +62,11 @@ func (fs *fileStorageMock) DirExists(path string) (bool, error) {
}

func (fs *fileStorageMock) IsDescendantOf(dir string, path string) (bool, error) {
panic("not implemented")
rel, err := filepath.Rel(dir, path)
if err != nil {
return false, err
}
return !filepath.IsAbs(rel) && !strings.HasPrefix(rel, ".."), nil
}

func (fs *fileStorageMock) Read(path string) ([]byte, error) {
Expand Down
15 changes: 14 additions & 1 deletion internal/core/notebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,20 @@ func (n *Notebook) FindCollections(kind CollectionKind, sorters []CollectionSort

// RelPath returns the path relative to the notebook root to the given path.
func (n *Notebook) RelPath(originalPath string) (string, error) {
path, err := n.fs.Abs(originalPath)
path := originalPath
if !filepath.IsAbs(path) {
isInsideNotebook, err := n.fs.IsDescendantOf(n.Path, n.fs.WorkingDir())
if err != nil {
return path, fmt.Errorf("%v: not a valid notebook path: %w", originalPath, err)
}
if isInsideNotebook {
path = filepath.Join(n.fs.WorkingDir(), path)
} else {
path = filepath.Join(n.Path, path)
}
}

path, err := n.fs.Abs(path)
if err != nil {
return path, fmt.Errorf("%v: not a valid notebook path: %w", originalPath, err)
}
Expand Down
68 changes: 68 additions & 0 deletions internal/core/notebook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package core

import (
"testing"

"github.com/zk-org/zk/internal/util"
"github.com/zk-org/zk/internal/util/test/assert"
)

// TestRelPathResolvesInsideNotebook tests that Notebook.RelPath() resolves
// relative paths relative to the notebook directory.
func TestRelPathResolvesInsideNotebook(t *testing.T) {
notebookPath := "/notebook"
workingDir := "/notebook"

fs := newFileStorageMock(workingDir, []string{notebookPath})

notebook := NewNotebook(notebookPath, NewDefaultConfig(), NotebookPorts{
FS: fs,
Logger: &util.NullLogger,
})

relPath, err := notebook.RelPath("inbox/note.md")

assert.Nil(t, err)
assert.Equal(t, relPath, "inbox/note.md")
}

// TestRelPathResolvesOutsideNotebook tests that Notebook.RelPath() resolves
// relative paths relative to the notebook directory, not the current working
// directory.
func TestRelPathResolvesOutsideNotebook(t *testing.T) {
notebookPath := "/notebook"
workingDir := "/other/dir"

fs := newFileStorageMock(workingDir, []string{notebookPath})

notebook := NewNotebook(notebookPath, NewDefaultConfig(), NotebookPorts{
FS: fs,
Logger: &util.NullLogger,
})

relPath, err := notebook.RelPath("inbox/note.md")

assert.Nil(t, err)
assert.Equal(t, relPath, "inbox/note.md")
}

// TestRelPathResolvesInsideNotebookSubdir tests that Notebook.RelPath() resolves
// relative paths relative to the current working directory when it is a child
// of the notebook root.
func TestRelPathResolvesInsideNotebookSubdir(t *testing.T) {
notebookPath := "/notebook"
workingDir := "/notebook/subdir"

fs := newFileStorageMock(workingDir, []string{notebookPath, workingDir})

notebook := NewNotebook(notebookPath, NewDefaultConfig(), NotebookPorts{
FS: fs,
Logger: &util.NullLogger,
})

// Relative path from /notebook/subdir should resolve to /notebook/subdir/note.md
relPath, err := notebook.RelPath("note.md")

assert.Nil(t, err)
assert.Equal(t, relPath, "subdir/note.md")
}
11 changes: 11 additions & 0 deletions tests/linked-by-hierarchical-path.tesh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Running from notebook directory.
$ cd full-sample
$ zk list -qf\{{title}} --linked-by inbox/er4k.md
>Channel
>Ownership in Rust

# Running from outside notebook with --notebook-dir.
$ cd ..
$ zk list --notebook-dir=full-sample -qf\{{title}} --linked-by inbox/er4k.md
>Channel
>Ownership in Rust
Loading