Skip to content

Commit

Permalink
fix: nested group rules (#495)
Browse files Browse the repository at this point in the history
Co-authored-by: tjex <tjex@tjex.net>
  • Loading branch information
dodgog and tjex authored Jan 26, 2025
1 parent 81f5daf commit 2f8be08
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 2 deletions.
12 changes: 10 additions & 2 deletions internal/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,29 @@ func (c Config) GroupConfigNamed(name string) (GroupConfig, error) {
// GroupNameForPath returns the name of the GroupConfig matching the given
// path, relative to the notebook.
func (c Config) GroupNameForPath(path string) (string, error) {
var longestMatch string
var matchedName string

for name, config := range c.Groups {
for _, groupPath := range config.Paths {
matches, err := filepath.Match(groupPath, path)
if err != nil {
return "", errors.Wrapf(err, "failed to match group %s to %s", name, path)
} else if matches {
// Early return if an exact match
return name, nil
}
if strings.HasPrefix(path, groupPath+"/") {
return name, nil
// If the match is partial, find the longest prefix overlap
if len(groupPath) > len(longestMatch) {
longestMatch = groupPath
matchedName = name
}
}
}
}

return "", nil
return matchedName, nil
}

// FormatConfig holds the configuration for document formats, such as Markdown.
Expand Down
28 changes: 28 additions & 0 deletions internal/core/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,34 @@ func TestParseComplete(t *testing.T) {
})
}

func TestGroupNameForPathApplyDeepestMatch(t *testing.T) {
config := Config{
Groups: map[string]GroupConfig{
"parent": {
Paths: []string{"dir1"},
},
"child": {
Paths: []string{"dir1/dir2"},
},
"other": {
Paths: []string{"other"},
},
},
}

name, err := config.GroupNameForPath("dir1/dir2/note.md")
assert.Nil(t, err)
assert.Equal(t, name, "child")

name, err = config.GroupNameForPath("dir1/note.md")
assert.Nil(t, err)
assert.Equal(t, name, "parent")

name, err = config.GroupNameForPath("other/note.md")
assert.Nil(t, err)
assert.Equal(t, name, "other")
}

func TestParseMergesGroupConfig(t *testing.T) {
conf, err := ParseConfig([]byte(`
[note]
Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/issue-490/.zk/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[note]
filename = "untitled"

[group.t1]
paths = ["dir1", "dir1/dir2/dir3"]

[group.t1.note]
extension = "md"
template = "template1.md"


# This group sits between the two directories of t1.
[group.t2]
paths = ["dir1/dir2"]

[group.t2.note]
extension = "md"
template = "template2.md"
1 change: 1 addition & 0 deletions tests/fixtures/issue-490/.zk/templates/template1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Template 1
1 change: 1 addition & 0 deletions tests/fixtures/issue-490/.zk/templates/template2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Template 2
Empty file.
25 changes: 25 additions & 0 deletions tests/issue-490.tesh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
$ cd issue-490

# This test ensures that group rules do not override other group
# rules that are applied to child directories.

# Test that group t1 rules are followed.
$ zk new dir1 --dry-run
>Template 1
2>{{working-dir}}/dir1/untitled.md

# Test that group t1 rules are followed.
$ zk new dir1/dir2/dir3 --dry-run
>Template 1
2>{{working-dir}}/dir1/dir2/dir3/untitled.md

# Test that group t2 rules are followed, signifying that nested group rules are
# functioning correctly.
$ zk new dir1/dir2 --dry-run
>Template 2
2>{{working-dir}}/dir1/dir2/untitled.md

# Test that explicit group override still works
$ zk new dir1/dir2 --group t1 --dry-run
>Template 1
2>{{working-dir}}/dir1/dir2/untitled.md

0 comments on commit 2f8be08

Please sign in to comment.