github.com/hashicorp/packer@v1.14.3/provisioner/shell/unix_reader_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package shell
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  	"testing"
    10  )
    11  
    12  func TestUnixReader_impl(t *testing.T) {
    13  	var raw interface{}
    14  	raw = new(UnixReader)
    15  	if _, ok := raw.(io.Reader); !ok {
    16  		t.Fatal("should be reader")
    17  	}
    18  }
    19  
    20  func TestUnixReader(t *testing.T) {
    21  	input := "one\r\ntwo\n\r\nthree\r\n"
    22  	expected := "one\ntwo\n\nthree\n"
    23  
    24  	unixReaderTest(t, input, expected)
    25  }
    26  
    27  func TestUnixReader_unixOnly(t *testing.T) {
    28  	input := "\none\n\ntwo\nthree\n\n"
    29  	expected := "\none\n\ntwo\nthree\n\n"
    30  
    31  	unixReaderTest(t, input, expected)
    32  }
    33  
    34  func TestUnixReader_readsLastLine(t *testing.T) {
    35  	input := "one\ntwo"
    36  	expected := "one\ntwo\n"
    37  
    38  	unixReaderTest(t, input, expected)
    39  }
    40  
    41  func unixReaderTest(t *testing.T, input string, expected string) {
    42  	r := &UnixReader{
    43  		Reader: bytes.NewReader([]byte(input)),
    44  	}
    45  
    46  	result := new(bytes.Buffer)
    47  	if _, err := io.Copy(result, r); err != nil {
    48  		t.Fatalf("err: %s", err)
    49  	}
    50  
    51  	if result.String() != expected {
    52  		t.Fatalf("bad: %#v", result.String())
    53  	}
    54  }