hugolib: Improve nil handling in IsDescendant and IsAncestor
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 28 Nov 2018 11:36:59 +0000 (12:36 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 28 Nov 2018 11:36:59 +0000 (12:36 +0100)
Fixes #5461

hugolib/site_sections.go
hugolib/site_sections_test.go

index abcac1d5ce9cd662ae167159c8236fc77a227900..38f6a3b6fceb73e3fc3707837f6e5b6d5aa9fe56 100644 (file)
@@ -104,8 +104,11 @@ func (p *Page) InSection(other interface{}) (bool, error) {
 // IsDescendant returns whether the current page is a descendant of the given page.
 // Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
 func (p *Page) IsDescendant(other interface{}) (bool, error) {
+       if p == nil {
+               return false, nil
+       }
        pp, err := unwrapPage(other)
-       if err != nil {
+       if err != nil || pp == nil {
                return false, err
        }
 
@@ -119,8 +122,12 @@ func (p *Page) IsDescendant(other interface{}) (bool, error) {
 // IsAncestor returns whether the current page is an ancestor of the given page.
 // Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
 func (p *Page) IsAncestor(other interface{}) (bool, error) {
+       if p == nil {
+               return false, nil
+       }
+
        pp, err := unwrapPage(other)
-       if err != nil {
+       if err != nil || pp == nil {
                return false, err
        }
 
index 24bb6a91f853bd4aecb5e0a9a05c3b4775db59dd..1987d2bcb1e8bc1b9d2b398b53df627acbeae025 100644 (file)
@@ -238,6 +238,8 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
                        assert.Len(p.Sections(), 0)
                }},
                {"l1,l2,l3", func(p *Page) {
+                       var nilp *Page
+
                        assert.Equal("T3_-1", p.title)
                        assert.Len(p.Pages, 2)
                        assert.Equal("T2_-1", p.Parent().title)
@@ -247,6 +249,12 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
                        isDescendant, err := l1.IsDescendant(p)
                        assert.NoError(err)
                        assert.False(isDescendant)
+                       isDescendant, err = l1.IsDescendant(nil)
+                       assert.NoError(err)
+                       assert.False(isDescendant)
+                       isDescendant, err = nilp.IsDescendant(p)
+                       assert.NoError(err)
+                       assert.False(isDescendant)
                        isDescendant, err = p.IsDescendant(l1)
                        assert.NoError(err)
                        assert.True(isDescendant)
@@ -258,6 +266,12 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
                        assert.NoError(err)
                        assert.False(isAncestor)
                        assert.Equal(l1, p.FirstSection())
+                       isAncestor, err = p.IsAncestor(nil)
+                       assert.NoError(err)
+                       assert.False(isAncestor)
+                       isAncestor, err = nilp.IsAncestor(l1)
+                       assert.NoError(err)
+                       assert.False(isAncestor)
 
                }},
                {"perm a,link", func(p *Page) {