github.com/YousefHaggyHeroku/pack@v1.5.5/internal/logging/log_writer_test.go (about)

     1  package logging_test
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/heroku/color"
    10  	"github.com/sclevine/spec"
    11  	"github.com/sclevine/spec/report"
    12  
    13  	ilogging "github.com/YousefHaggyHeroku/pack/internal/logging"
    14  	h "github.com/YousefHaggyHeroku/pack/testhelpers"
    15  )
    16  
    17  const (
    18  	timeFmt  = "2006/01/02 15:04:05.000000"
    19  	testTime = "2019/05/15 01:01:01.000000"
    20  )
    21  
    22  func TestLogWriter(t *testing.T) {
    23  	spec.Run(t, "LogWriter", testLogWriter, spec.Parallel(), spec.Report(report.Terminal{}))
    24  }
    25  
    26  func testLogWriter(t *testing.T, when spec.G, it spec.S) {
    27  	var (
    28  		writer  *ilogging.LogWriter
    29  		outCons *color.Console
    30  		fOut    func() string
    31  
    32  		clockFunc = func() time.Time {
    33  			clock, _ := time.Parse(timeFmt, testTime)
    34  			return clock
    35  		}
    36  	)
    37  
    38  	it.Before(func() {
    39  		outCons, fOut = h.MockWriterAndOutput()
    40  	})
    41  
    42  	when("wantTime is true", func() {
    43  		it("has time", func() {
    44  			writer = ilogging.NewLogWriter(outCons, clockFunc, true)
    45  			writer.Write([]byte("test\n"))
    46  			h.AssertEq(t, fOut(), "2019/05/15 01:01:01.000000 test\n")
    47  		})
    48  	})
    49  
    50  	when("wantTime is false", func() {
    51  		it("doesn't have time", func() {
    52  			writer = ilogging.NewLogWriter(outCons, clockFunc, false)
    53  			writer.Write([]byte("test\n"))
    54  			h.AssertEq(t, fOut(), "test\n")
    55  		})
    56  	})
    57  
    58  	when("#Fd", func() {
    59  		when("out is a file", func() {
    60  			it("returns a Fd", func() {
    61  				file, err := ioutil.TempFile("", "testFile")
    62  				h.AssertNil(t, err)
    63  				writer = ilogging.NewLogWriter(file, clockFunc, false)
    64  				h.AssertNotEq(t, int(writer.Fd()), 0)
    65  			})
    66  		})
    67  
    68  		when("out is just a Writer", func() {
    69  			it("returns 0", func() {
    70  				var out *bytes.Buffer
    71  				writer = ilogging.NewLogWriter(out, clockFunc, true)
    72  				h.AssertEq(t, int(writer.Fd()), 0)
    73  			})
    74  		})
    75  	})
    76  
    77  	when("color is enabled", func() {
    78  		it("doesn't strip color", func() {
    79  			color.Disable(false)
    80  
    81  			writer = ilogging.NewLogWriter(outCons, clockFunc, false)
    82  			writer.Write([]byte(color.HiBlueString(("test"))))
    83  			h.AssertEq(t, fOut(), "\x1b[94mtest\x1b[0m")
    84  		})
    85  	})
    86  
    87  	when("color is disabled", func() {
    88  		it("strips color out", func() {
    89  			color.Disable(true)
    90  			defer color.Disable(false)
    91  
    92  			writer = ilogging.NewLogWriter(outCons, clockFunc, false)
    93  			writer.Write([]byte(color.HiBlueString(("test"))))
    94  
    95  			output := fOut()
    96  			h.AssertEq(t, output, "test")
    97  			h.AssertNotEq(t, output, "\x1b[94mtest\x1b[0m")
    98  		})
    99  
   100  		it("doesn't strip time out", func() {
   101  			color.Disable(true)
   102  			defer color.Disable(false)
   103  
   104  			writer = ilogging.NewLogWriter(outCons, clockFunc, true)
   105  			writer.Write([]byte(color.HiBlueString(("test"))))
   106  
   107  			output := fOut()
   108  			h.AssertEq(t, output, "2019/05/15 01:01:01.000000 test")
   109  		})
   110  	})
   111  }