diff options
-rw-r--r-- | helpers/general.go | 6 | ||||
-rw-r--r-- | source/file_test.go | 25 |
2 files changed, 31 insertions, 0 deletions
diff --git a/helpers/general.go b/helpers/general.go index e0fe99b6f..a0aa20c78 100644 --- a/helpers/general.go +++ b/helpers/general.go @@ -75,6 +75,9 @@ func GuessType(in string) string { // ReaderToBytes takes an io.Reader argument, reads from it // and returns bytes. func ReaderToBytes(lines io.Reader) []byte { + if lines == nil { + return []byte{} + } b := bp.GetBuffer() defer bp.PutBuffer(b) @@ -87,6 +90,9 @@ func ReaderToBytes(lines io.Reader) []byte { // ReaderToString is the same as ReaderToBytes, but returns a string. func ReaderToString(lines io.Reader) string { + if lines == nil { + return "" + } b := bp.GetBuffer() defer bp.PutBuffer(b) b.ReadFrom(lines) diff --git a/source/file_test.go b/source/file_test.go new file mode 100644 index 000000000..6a4e456c6 --- /dev/null +++ b/source/file_test.go @@ -0,0 +1,25 @@ +package source + +import ( + "github.com/spf13/hugo/helpers" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestFileUniqueID(t *testing.T) { + f1 := File{uniqueID: "123"} + f2 := NewFile("a") + + assert.Equal(t, "123", f1.UniqueID()) + assert.Equal(t, "0cc175b9c0f1b6a831c399e269772661", f2.UniqueID()) +} + +func TestFileString(t *testing.T) { + assert.Equal(t, "abc", NewFileWithContents("a", helpers.StringToReader("abc")).String()) + assert.Equal(t, "", NewFile("a").String()) +} + +func TestFileBytes(t *testing.T) { + assert.Equal(t, []byte("abc"), NewFileWithContents("a", helpers.StringToReader("abc")).Bytes()) + assert.Equal(t, []byte(""), NewFile("a").Bytes()) +} |