From 104f5049a72ca03ca3127747f8a75558b3e551e4 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Tue, 15 Oct 2013 05:51:49 -0700 Subject: [PATCH] provisioner/shell: fix EOF detection Fixes #507 The scanUnixLine function was erroneously returning empty lines when EOF was reached. This commit adds a test for the problem and changes the scanUnixLine function to elide the trailing \n if no content was read. --- provisioner/shell/unix_reader.go | 3 +++ provisioner/shell/unix_reader_test.go | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/provisioner/shell/unix_reader.go b/provisioner/shell/unix_reader.go index 0a19c0692..52b83eddd 100644 --- a/provisioner/shell/unix_reader.go +++ b/provisioner/shell/unix_reader.go @@ -54,5 +54,8 @@ func (r *UnixReader) Read(p []byte) (n int, err error) { // token returned will be "one\n". func scanUnixLine(data []byte, atEOF bool) (advance int, token []byte, err error) { advance, token, err = bufio.ScanLines(data, atEOF) + if advance == 0 { + return + } return advance, append(token, "\n"...), err } diff --git a/provisioner/shell/unix_reader_test.go b/provisioner/shell/unix_reader_test.go index 4f39eb00f..4aa1f9ea9 100644 --- a/provisioner/shell/unix_reader_test.go +++ b/provisioner/shell/unix_reader_test.go @@ -18,24 +18,24 @@ func TestUnixReader(t *testing.T) { input := "one\r\ntwo\n\r\nthree\r\n" expected := "one\ntwo\n\nthree\n" - r := &UnixReader{ - Reader: bytes.NewReader([]byte(input)), - } - - result := new(bytes.Buffer) - if _, err := io.Copy(result, r); err != nil { - t.Fatalf("err: %s", err) - } - - if result.String() != expected { - t.Fatalf("bad: %#v", result.String()) - } + unixReaderTest(t, input, expected) } func TestUnixReader_unixOnly(t *testing.T) { input := "\none\n\ntwo\nthree\n\n" expected := "\none\n\ntwo\nthree\n\n" + unixReaderTest(t, input, expected) +} + +func TestUnixReader_readsLastLine(t *testing.T) { + input := "one\ntwo" + expected := "one\ntwo\n" + + unixReaderTest(t, input, expected) +} + +func unixReaderTest(t *testing.T, input string, expected string) { r := &UnixReader{ Reader: bytes.NewReader([]byte(input)), }