Skip to content

Commit

Permalink
Merge pull request #299 from hairyhenderson/export-template-stdout
Browse files Browse the repository at this point in the history
Exporting the writer used when templates are sent to Stdout
  • Loading branch information
hairyhenderson authored Apr 20, 2018
2 parents ea4a99b + fcfc542 commit 0465ece
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
20 changes: 20 additions & 0 deletions gomplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gomplate

import (
"bytes"
"io"
"net/http/httptest"
"os"
"testing"
Expand All @@ -15,6 +16,15 @@ import (
"github.com/stretchr/testify/assert"
)

// like ioutil.NopCloser(), except for io.WriteClosers...
type nopWCloser struct {
io.Writer
}

func (n *nopWCloser) Close() error {
return nil
}

func testTemplate(g *gomplate, tmpl string) string {
var out bytes.Buffer
err := g.runTemplate(&tplate{name: "testtemplate", contents: tmpl, target: &out})
Expand Down Expand Up @@ -148,3 +158,13 @@ func TestCustomDelim(t *testing.T) {
}
assert.Equal(t, "hi", testTemplate(g, `[print "hi"]`))
}

func TestRunTemplates(t *testing.T) {
defer func() { Stdout = os.Stdout }()
buf := &bytes.Buffer{}
Stdout = &nopWCloser{buf}
config := &Config{Input: "foo"}
err := RunTemplates(config)
assert.NoError(t, err)
assert.Equal(t, "foo", buf.String())
}
6 changes: 4 additions & 2 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (

// for overriding in tests
var stdin io.ReadCloser = os.Stdin
var stdout io.WriteCloser = os.Stdout
var fs = afero.NewOsFs()

// Stdout allows overriding the writer to use when templates are written to stdout ("-").
var Stdout io.WriteCloser = os.Stdout

// tplate - models a gomplate template file...
type tplate struct {
name string
Expand Down Expand Up @@ -151,7 +153,7 @@ func inList(list []string, entry string) bool {

func openOutFile(filename string) (out io.WriteCloser, err error) {
if filename == "-" {
return stdout, nil
return Stdout, nil
}
return fs.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
}
Expand Down
19 changes: 5 additions & 14 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package gomplate

import (
"bytes"
"io"
"io/ioutil"
"os"
"testing"
Expand All @@ -14,14 +13,6 @@ import (
"github.com/stretchr/testify/assert"
)

// like ioutil.NopCloser(), except for io.WriteClosers...
type nopWCloser struct {
io.Writer
}

func (n *nopWCloser) Close() error {
return nil
}
func TestReadInput(t *testing.T) {
origfs := fs
defer func() { fs = origfs }()
Expand Down Expand Up @@ -60,12 +51,12 @@ func TestOpenOutFile(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, os.FileMode(0644), i.Mode())

defer func() { stdout = os.Stdout }()
stdout = &nopWCloser{&bytes.Buffer{}}
defer func() { Stdout = os.Stdout }()
Stdout = &nopWCloser{&bytes.Buffer{}}

f, err := openOutFile("-")
assert.NoError(t, err)
assert.Equal(t, stdout, f)
assert.Equal(t, Stdout, f)
}

func TestInList(t *testing.T) {
Expand Down Expand Up @@ -163,7 +154,7 @@ func TestGatherTemplates(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, templates, 1)
assert.Equal(t, "foo", templates[0].contents)
assert.Equal(t, stdout, templates[0].target)
assert.Equal(t, Stdout, templates[0].target)

templates, err = gatherTemplates(&Config{
InputFiles: []string{"foo"},
Expand All @@ -172,7 +163,7 @@ func TestGatherTemplates(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, templates, 1)
assert.Equal(t, "bar", templates[0].contents)
assert.NotEqual(t, stdout, templates[0].target)
assert.NotEqual(t, Stdout, templates[0].target)

templates, err = gatherTemplates(&Config{
InputDir: "in",
Expand Down

0 comments on commit 0465ece

Please sign in to comment.