]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
resources/page: Let GroupByParam return nil instead of error
authorJoe Mooring <joe.mooring@veriphor.com>
Sat, 8 Jun 2024 13:45:58 +0000 (06:45 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 8 Jun 2024 16:35:14 +0000 (18:35 +0200)
Closes #12578

hugolib/site_test.go
resources/page/pagegroup.go
resources/page/pagegroup_test.go

index 1de1d688a699a7e40b1da7b5414a44303bd8f5f2..37546824a11ca41c9c65082e81103ab9216693ad 100644 (file)
@@ -427,8 +427,8 @@ mainSections=["a", "b"]
 {{/* Behaviour before Hugo 0.112.0. */}}
 MainSections Params: {{ site.Params.mainSections }}|
 MainSections Site method: {{ site.MainSections }}|
-       
-       
+
+
        `
 
                b := Test(t, files)
@@ -478,8 +478,8 @@ disableKinds = ['RSS','sitemap','taxonomy','term']
 -- layouts/index.html --
 MainSections Params: {{ site.Params.mainSections }}|
 MainSections Site method: {{ site.MainSections }}|
-       
-       
+
+
        `
 
                b := Test(t, files)
@@ -787,9 +787,12 @@ func TestGroupedPages(t *testing.T) {
                t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 2, len(byparam[0].Pages))
        }
 
-       _, err = s.RegularPages().GroupByParam("not_exist")
-       if err == nil {
-               t.Errorf("GroupByParam didn't return an expected error")
+       byNonExistentParam, err := s.RegularPages().GroupByParam("not_exist")
+       if err != nil {
+               t.Errorf("GroupByParam returned an error when it shouldn't")
+       }
+       if len(byNonExistentParam) != 0 {
+               t.Errorf("PageGroup array has unexpected elements. Group length should be '%d', got '%d'", 0, len(byNonExistentParam))
        }
 
        byOnlyOneParam, err := s.RegularPages().GroupByParam("only_one")
index 7129fae17bc9353f46c271e651e5f885340e2a03..081708d625ef6d5fa5549c256cc99631c2923be4 100644 (file)
@@ -205,7 +205,7 @@ func (p Pages) GroupByParam(key string, order ...string) (PagesGroup, error) {
                }
        }
        if !tmp.IsValid() {
-               return nil, errors.New("there is no such param")
+               return nil, nil
        }
 
        for _, e := range p {
index 91f05b24a15a74e9a5795663c6b536f7c845fe49..5008aa72016329a6f093d3503ec0938c0edd1ff2 100644 (file)
@@ -142,15 +142,6 @@ func TestGroupByCalledWithEmptyPages(t *testing.T) {
        }
 }
 
-func TestGroupByParamCalledWithUnavailableKey(t *testing.T) {
-       t.Parallel()
-       pages := preparePageGroupTestPages(t)
-       _, err := pages.GroupByParam("UnavailableKey")
-       if err == nil {
-               t.Errorf("GroupByParam should return an error but didn't")
-       }
-}
-
 func TestReverse(t *testing.T) {
        t.Parallel()
        pages := preparePageGroupTestPages(t)
@@ -256,8 +247,8 @@ func TestGroupByParamCalledWithUnavailableParam(t *testing.T) {
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        _, err := pages.GroupByParam("unavailable_param")
-       if err == nil {
-               t.Errorf("GroupByParam should return an error but didn't")
+       if err != nil {
+               t.Errorf("GroupByParam returned an error when it shouldn't")
        }
 }