Skip to content

Commit

Permalink
hugolib: Improve nil handling in IsDescendant and IsAncestor
Browse files Browse the repository at this point in the history
Fixes #5461
  • Loading branch information
bep committed Nov 28, 2018
1 parent 7540a62 commit b09a403
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 9 additions & 2 deletions hugolib/site_sections.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
14 changes: 14 additions & 0 deletions hugolib/site_sections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit b09a403

Please sign in to comment.