Skip to content

Commit b1f9430

Browse files
committed
fix: four one-line bugs found in code review
- Fix pop/shift filters being swapped (pop returned first element, shift returned last) - Fix ReadCollections silently dropping errors (returned nil instead of err) - Fix groupPagesBy ignoring its getter argument, causing site.tags to contain categories instead of tags - Fix readDataFiles stopping at first subdirectory (used break instead of continue), skipping data files after it alphabetically
1 parent 0ca90ed commit b1f9430

9 files changed

Lines changed: 59 additions & 7 deletions

File tree

filters/filters.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ func AddJekyllFilters(e *liquid.Engine, c *config.Config) {
5454
return append(array, evaluator.MustConvertItem(item, array))
5555
})
5656
e.RegisterFilter("pop", requireNonEmptyArray(func(array []interface{}) interface{} {
57-
return array[0]
57+
return array[len(array)-1]
5858
}))
5959
e.RegisterFilter("shift", requireNonEmptyArray(func(array []interface{}) interface{} {
60-
return array[len(array)-1]
60+
return array[0]
6161
}))
6262
e.RegisterFilter("unshift", func(array []interface{}, item interface{}) interface{} {
6363
return append([]interface{}{evaluator.MustConvertItem(item, array)}, array...)

filters/filters_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ var filterTests = []struct{ in, expected string }{
3737
{`{{ site.members | where_exp: "item", "item.name contains 'Al'" | map: "name" | join }}`, "Alonzo Alan"},
3838

3939
{`{{ page.tags | push: 'Spokane' | join }}`, "Seattle Tacoma Spokane"},
40-
{`{{ page.tags | pop }}`, "Seattle"},
41-
{`{{ page.tags | shift }}`, "Tacoma"},
40+
{`{{ page.tags | pop }}`, "Tacoma"},
41+
{`{{ page.tags | shift }}`, "Seattle"},
4242
{`{{ page.tags | unshift: "Olympia" | join }}`, "Olympia Seattle Tacoma"},
4343

4444
// strings
@@ -113,6 +113,20 @@ func TestFilters(t *testing.T) {
113113
}
114114
}
115115

116+
func TestPopShiftFilters(t *testing.T) {
117+
// pop returns the last element, shift returns the first
118+
bindings := liquid.Bindings{
119+
"arr": []string{"alpha", "beta", "gamma"},
120+
}
121+
requireTemplateRender(t, `{{ arr | pop }}`, bindings, "gamma")
122+
requireTemplateRender(t, `{{ arr | shift }}`, bindings, "alpha")
123+
124+
// single-element array
125+
single := liquid.Bindings{"arr": []string{"only"}}
126+
requireTemplateRender(t, `{{ arr | pop }}`, single, "only")
127+
requireTemplateRender(t, `{{ arr | shift }}`, single, "only")
128+
}
129+
116130
func TestSampleFilter(t *testing.T) {
117131
engine := liquid.NewEngine()
118132
cfg := config.Default()

site/data.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (s *Site) readDataFiles() error {
2121
}
2222
for _, f := range files {
2323
if f.IsDir() {
24-
break
24+
continue
2525
}
2626
var (
2727
filename = filepath.Join(dataDir, f.Name())

site/drop_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,40 @@ func TestSite_ToLiquid_related_posts(t *testing.T) {
5959
require.Len(t, posts, 1)
6060
}
6161

62+
func TestSite_readDataFiles_skips_directories(t *testing.T) {
63+
// Regression test: readDataFiles should skip directories and continue
64+
// reading subsequent files (previously used break instead of continue)
65+
site, err := FromDirectory("testdata/site1", config.Flags{})
66+
require.NoError(t, err)
67+
require.NoError(t, site.Read())
68+
69+
// The _data dir has: alpha.json, subdir/, zulu.json
70+
// Both alpha and zulu should be loaded despite subdir/ between them
71+
require.Contains(t, site.data, "alpha", "data file before directory should be loaded")
72+
require.Contains(t, site.data, "zulu", "data file after directory should be loaded")
73+
}
74+
75+
func TestSite_ToLiquid_tags_vs_categories(t *testing.T) {
76+
drop := readTestSiteDrop(t)
77+
78+
// tags and categories should be distinct groupings
79+
tags, ok := drop["tags"].(map[string][]Page)
80+
require.True(t, ok, fmt.Sprintf("tags has type %T", drop["tags"]))
81+
82+
categories, ok := drop["categories"].(map[string][]Page)
83+
require.True(t, ok, fmt.Sprintf("categories has type %T", drop["categories"]))
84+
85+
// The test post has tags: ["go", "jekyll"] and categories: ["dev"]
86+
// tags should contain "go" and "jekyll" keys
87+
require.Contains(t, tags, "go", "tags should contain 'go'")
88+
require.Contains(t, tags, "jekyll", "tags should contain 'jekyll'")
89+
require.NotContains(t, tags, "dev", "tags should not contain category 'dev'")
90+
91+
// categories should contain "dev" key
92+
require.Contains(t, categories, "dev", "categories should contain 'dev'")
93+
require.NotContains(t, categories, "go", "categories should not contain tag 'go'")
94+
}
95+
6296
func TestSite_ToLiquid_static_files(t *testing.T) {
6397
drop := readTestSiteDrop(t)
6498
files, ok := drop["static_files"].([]*pages.StaticFile)

site/posts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (s *Site) setPostVariables() {
3333
func (s *Site) groupPagesBy(getter func(Page) []string) map[string][]Page {
3434
categories := map[string][]Page{}
3535
for _, p := range s.Pages() {
36-
for _, k := range p.Categories() {
36+
for _, k := range getter(p) {
3737
ps, found := categories[k]
3838
if !found {
3939
ps = []Page{}

site/read.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,5 @@ func (s *Site) ReadCollections() (err error) {
151151
return cols[i].Name < cols[j].Name
152152
})
153153
s.Collections = cols
154-
return nil
154+
return err
155155
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"key": "value_a"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"key": "value_z"}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
---
2+
tags: ["go", "jekyll"]
3+
categories: ["dev"]
24
---

0 commit comments

Comments
 (0)