shithub: hugo

Download patch

ref: 66b143a01d1c192619839b732ce188923ab15d60
parent: 4f93f8c670b26258dc7e3a613c38dbc86d8eda76
author: Bjørn Erik Pedersen <[email protected]>
date: Mon Apr 29 06:48:36 EDT 2019

tpl/compare: Fix nil compare in eq/ne for interface values

Fixes #5905

--- a/tpl/compare/compare.go
+++ b/tpl/compare/compare.go
@@ -20,6 +20,8 @@
 	"strconv"
 	"time"
 
+	"github.com/gohugoio/hugo/common/types"
+
 	"github.com/gohugoio/hugo/compare"
 )
 
@@ -88,7 +90,6 @@
 
 // Eq returns the boolean truth of arg1 == arg2.
 func (*Namespace) Eq(x, y interface{}) bool {
-
 	if e, ok := x.(compare.Eqer); ok {
 		return e.Eq(y)
 	}
@@ -98,6 +99,9 @@
 	}
 
 	normalize := func(v interface{}) interface{} {
+		if types.IsNil(v) {
+			return nil
+		}
 		vv := reflect.ValueOf(v)
 		switch vv.Kind() {
 		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
--- a/tpl/compare/compare_test.go
+++ b/tpl/compare/compare_test.go
@@ -27,6 +27,23 @@
 	"github.com/stretchr/testify/require"
 )
 
+type T struct {
+	NonEmptyInterfaceNil      I
+	NonEmptyInterfaceTypedNil I
+}
+
+type I interface {
+	Foo() string
+}
+
+func (t *T) Foo() string {
+	return "foo"
+}
+
+var testT = &T{
+	NonEmptyInterfaceTypedNil: (*T)(nil),
+}
+
 type tstEqerType1 string
 type tstEqerType2 string
 
@@ -183,7 +200,12 @@
 		{"0.37-DEV", hugo.MustParseVersion("0.37").Version(), -1},
 		{"0.36", hugo.MustParseVersion("0.37-DEV").Version(), -1},
 		{"0.37-DEV", hugo.MustParseVersion("0.37-DEV").Version(), 0},
+		// https://github.com/gohugoio/hugo/issues/5905
+		{nil, nil, 0},
+		{testT.NonEmptyInterfaceNil, nil, 0},
+		{testT.NonEmptyInterfaceTypedNil, nil, 0},
 	} {
+
 		result := funcUnderTest(test.left, test.right)
 		success := false
 
@@ -206,7 +228,7 @@
 		}
 
 		if !success {
-			t.Errorf("[%d][%s] %v compared to %v: %t", i, path.Base(runtime.FuncForPC(reflect.ValueOf(funcUnderTest).Pointer()).Name()), test.left, test.right, result)
+			t.Fatalf("[%d][%s] %v compared to %v: %t", i, path.Base(runtime.FuncForPC(reflect.ValueOf(funcUnderTest).Pointer()).Name()), test.left, test.right, result)
 		}
 	}
 }