From 1e05268d617d2acadeb85439be670922907701e5 Mon Sep 17 00:00:00 2001 From: lucapette Date: Thu, 22 Jun 2017 15:40:25 +0200 Subject: [PATCH] Cover pipe templates too --- integration/cli_test.go | 44 +++++++++++++++++++ .../golden/broken-template-pipe.golden | 1 + .../golden/simple-template-pipe.golden | 1 + .../golden/unknown-function-pipe.golden | 1 + 4 files changed, 47 insertions(+) create mode 100644 integration/golden/broken-template-pipe.golden create mode 100644 integration/golden/simple-template-pipe.golden create mode 100644 integration/golden/unknown-function-pipe.golden diff --git a/integration/cli_test.go b/integration/cli_test.go index 79bd5d4..8fdbc5a 100644 --- a/integration/cli_test.go +++ b/integration/cli_test.go @@ -59,6 +59,14 @@ func (tf *testFile) write(content string) { } } +func (tf *testFile) asFile() *os.File { + file, err := os.Open(tf.path()) + if err != nil { + tf.t.Fatalf("could not open %s: %v", tf.name, err) + } + return file +} + func (tf *testFile) load() string { content, err := ioutil.ReadFile(tf.path()) if err != nil { @@ -235,6 +243,42 @@ func TestTemplatesWithCLIArgs(t *testing.T) { } } +func TestTemplatesWithPipe(t *testing.T) { + tests := []struct { + tmpl string + golden string + wantErr bool + }{ + {"simple.tmpl", "simple-template-pipe.golden", false}, + {"broken.tmpl", "broken-template-pipe.golden", true}, + {"unknown-function.tmpl", "unknown-function-pipe.golden", true}, + } + + for _, tt := range tests { + t.Run(tt.tmpl, func(t *testing.T) { + fixture := newFixture(t, tt.tmpl) + cmd := exec.Command(binaryPath) + cmd.Stdin = fixture.asFile() + output, err := cmd.CombinedOutput() + if (err != nil) != tt.wantErr { + t.Fatalf("%s\nexpected (err != nil) to be %v, but got %v. err: %v", output, tt.wantErr, err != nil, err) + } + + golden := newGoldenFile(t, tt.golden) + actual := string(output) + if *update { + golden.write(actual) + } + + expected := golden.load() + + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("diff: %v", diff(expected, actual)) + } + }) + } +} + func TestMain(m *testing.M) { err := os.Chdir("..") if err != nil { diff --git a/integration/golden/broken-template-pipe.golden b/integration/golden/broken-template-pipe.golden new file mode 100644 index 0000000..cb6d8f7 --- /dev/null +++ b/integration/golden/broken-template-pipe.golden @@ -0,0 +1 @@ +template: stdin:1: unexpected "}" in operand diff --git a/integration/golden/simple-template-pipe.golden b/integration/golden/simple-template-pipe.golden new file mode 100644 index 0000000..6281311 --- /dev/null +++ b/integration/golden/simple-template-pipe.golden @@ -0,0 +1 @@ +42--foo42--foo42--foo42--foo42--foo42--foo42--foo42--foo42--foo42--foo \ No newline at end of file diff --git a/integration/golden/unknown-function-pipe.golden b/integration/golden/unknown-function-pipe.golden new file mode 100644 index 0000000..42d2615 --- /dev/null +++ b/integration/golden/unknown-function-pipe.golden @@ -0,0 +1 @@ +template: stdin:1: function "Madeup" not defined