From e2aea65170985d7428c468ce0591557ad95af884 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Sat, 7 May 2016 16:34:53 -0500 Subject: [PATCH] helpers: Remove ToReader funcs Remove StringToReader and BytesToReader in favor of using the stdlib directly. --- helpers/general.go | 10 ---------- helpers/general_test.go | 21 ++++----------------- hugolib/handler_file.go | 5 +++-- source/file_test.go | 9 +++++---- transform/chain_test.go | 7 ++++--- transform/livereloadinject_test.go | 4 ++-- 6 files changed, 18 insertions(+), 38 deletions(-) diff --git a/helpers/general.go b/helpers/general.go index 8d627831c..731452d9d 100644 --- a/helpers/general.go +++ b/helpers/general.go @@ -130,16 +130,6 @@ func ReaderToString(lines io.Reader) string { return b.String() } -// StringToReader does the opposite of ReaderToString. -func StringToReader(in string) io.Reader { - return strings.NewReader(in) -} - -// BytesToReader does the opposite of ReaderToBytes. -func BytesToReader(in []byte) io.Reader { - return bytes.NewReader(in) -} - // ReaderContains reports whether subslice is within r. func ReaderContains(r io.Reader, subslice []byte) bool { diff --git a/helpers/general_test.go b/helpers/general_test.go index e735620ca..089bb9b1a 100644 --- a/helpers/general_test.go +++ b/helpers/general_test.go @@ -14,10 +14,11 @@ package helpers import ( - "github.com/stretchr/testify/assert" "reflect" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestGuessType(t *testing.T) { @@ -62,20 +63,6 @@ func TestFirstUpper(t *testing.T) { } } -func TestBytesToReader(t *testing.T) { - asBytes := ReaderToBytes(strings.NewReader("Hello World!")) - asReader := BytesToReader(asBytes) - assert.Equal(t, []byte("Hello World!"), asBytes) - assert.Equal(t, asBytes, ReaderToBytes(asReader)) -} - -func TestStringToReader(t *testing.T) { - asString := ReaderToString(strings.NewReader("Hello World!")) - assert.Equal(t, "Hello World!", asString) - asReader := StringToReader(asString) - assert.Equal(t, asString, ReaderToString(asReader)) -} - var containsTestText = (`На берегу пустынных волн Стоял он, дум великих полн, И вдаль глядел. Пред ним широко @@ -136,7 +123,7 @@ var containsAdditionalTestData = []struct { func TestReaderContains(t *testing.T) { for i, this := range append(containsBenchTestData, containsAdditionalTestData...) { - result := ReaderContains(StringToReader(this.v1), this.v2) + result := ReaderContains(strings.NewReader(this.v1), this.v2) if result != this.expect { t.Errorf("[%d] got %t but expected %t", i, result, this.expect) } @@ -150,7 +137,7 @@ func BenchmarkReaderContains(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { for i, this := range containsBenchTestData { - result := ReaderContains(StringToReader(this.v1), this.v2) + result := ReaderContains(strings.NewReader(this.v1), this.v2) if result != this.expect { b.Errorf("[%d] got %t but expected %t", i, result, this.expect) } diff --git a/hugolib/handler_file.go b/hugolib/handler_file.go index b0ea4fdd7..def0408a4 100644 --- a/hugolib/handler_file.go +++ b/hugolib/handler_file.go @@ -14,8 +14,9 @@ package hugolib import ( + "bytes" + "github.com/dchest/cssmin" - "github.com/spf13/hugo/helpers" "github.com/spf13/hugo/source" "github.com/spf13/hugo/tpl" ) @@ -48,6 +49,6 @@ type cssHandler struct{ basicFileHandler } func (h cssHandler) Extensions() []string { return []string{"css"} } func (h cssHandler) FileConvert(f *source.File, s *Site) HandledResult { x := cssmin.Minify(f.Bytes()) - s.writeDestFile(f.Path(), helpers.BytesToReader(x)) + s.writeDestFile(f.Path(), bytes.NewReader(x)) return HandledResult{file: f} } diff --git a/source/file_test.go b/source/file_test.go index 43165b2c3..bbf51f748 100644 --- a/source/file_test.go +++ b/source/file_test.go @@ -14,9 +14,10 @@ package source import ( - "github.com/spf13/hugo/helpers" - "github.com/stretchr/testify/assert" + "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestFileUniqueID(t *testing.T) { @@ -28,11 +29,11 @@ func TestFileUniqueID(t *testing.T) { } func TestFileString(t *testing.T) { - assert.Equal(t, "abc", NewFileWithContents("a", helpers.StringToReader("abc")).String()) + assert.Equal(t, "abc", NewFileWithContents("a", strings.NewReader("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("abc"), NewFileWithContents("a", strings.NewReader("abc")).Bytes()) assert.Equal(t, []byte(""), NewFile("a").Bytes()) } diff --git a/transform/chain_test.go b/transform/chain_test.go index a0ae358d6..5200baa18 100644 --- a/transform/chain_test.go +++ b/transform/chain_test.go @@ -15,11 +15,12 @@ package transform import ( "bytes" - "github.com/spf13/hugo/helpers" - "github.com/stretchr/testify/assert" "path/filepath" "strings" "testing" + + "github.com/spf13/hugo/helpers" + "github.com/stretchr/testify/assert" ) const ( @@ -148,7 +149,7 @@ func TestChaingMultipleTransformers(t *testing.T) { tr := NewChain(f1, f2, f3, f4) out := new(bytes.Buffer) - if err := tr.Apply(out, helpers.StringToReader("Test: f4 f3 f1 f2 f1 The End."), []byte("")); err != nil { + if err := tr.Apply(out, strings.NewReader("Test: f4 f3 f1 f2 f1 The End."), []byte("")); err != nil { t.Errorf("Multi transformer chain returned an error: %s", err) } diff --git a/transform/livereloadinject_test.go b/transform/livereloadinject_test.go index 5aea8689c..cf618e9ee 100644 --- a/transform/livereloadinject_test.go +++ b/transform/livereloadinject_test.go @@ -16,7 +16,7 @@ package transform import ( "bytes" "fmt" - "github.com/spf13/hugo/helpers" + "strings" "testing" ) @@ -27,7 +27,7 @@ func TestLiveReloadInject(t *testing.T) { func doTestLiveReloadInject(t *testing.T, bodyEndTag string) { out := new(bytes.Buffer) - in := helpers.StringToReader(bodyEndTag) + in := strings.NewReader(bodyEndTag) tr := NewChain(LiveReloadInject) tr.Apply(out, in, []byte("path"))