]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl/collections: Fix where ... not in with empty slice
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 21 Apr 2025 09:53:38 +0000 (11:53 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 21 Apr 2025 13:17:20 +0000 (15:17 +0200)
Fixes #13621

tpl/collections/collections_integration_test.go
tpl/collections/where.go
tpl/collections/where_test.go

index cc60770f915980c1b019be2416b79a528bf68cf5..b60aaea87b086d39c8c383ae8e7e14b9e51739b9 100644 (file)
@@ -278,3 +278,23 @@ disableKinds = ['rss','sitemap', 'taxonomy', 'term', 'page']
 
        b.AssertFileContentExact("public/index.html", "0: /a3_b1.html\n\n1: /b2.html\n\n2: /a1.html\n\n3: /a2.html\n$")
 }
+
+// Issue 13621.
+func TestWhereNotInEmptySlice(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+-- layouts/home.html --
+{{- $pages := where site.RegularPages "Kind" "not in" (slice) -}}
+Len: {{  $pages | len }}|
+-- layouts/all.html --
+All|{{ .Title }}|
+-- content/p1.md --
+
+`
+
+       b := hugolib.Test(t, files)
+
+       b.AssertFileContent("public/index.html", "Len: 1|")
+}
index b15cfe78142fbca2f11bb350c11b50838fc552ec..ee49d0bbbed4e05b58b18b663dfc556021e98941 100644 (file)
@@ -138,6 +138,9 @@ func (ns *Namespace) checkCondition(v, mv reflect.Value, op string) (bool, error
                }
 
                if mv.Len() == 0 {
+                       if op == "not in" {
+                               return true, nil
+                       }
                        return false, nil
                }
 
index 60f97e607a8b8b91c81d8e3363ca4acdb9c4a97a..ecf748f936f4f023684d2d789795ea10a5f6043b 100644 (file)
@@ -761,6 +761,7 @@ func TestCheckCondition(t *testing.T) {
                        expect{true, false},
                },
                {reflect.ValueOf(123), reflect.ValueOf([]int{45, 678}), "not in", expect{true, false}},
+               {reflect.ValueOf(123), reflect.ValueOf([]int{}), "not in", expect{true, false}},
                {reflect.ValueOf("foo"), reflect.ValueOf([]string{"bar", "baz"}), "not in", expect{true, false}},
                {
                        reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),