github.com/cloudfoundry/cli@v7.1.0+incompatible/util/ui/log_message_test.go (about)

     1  package ui_test
     2  
     3  import (
     4  	"time"
     5  
     6  	"code.cloudfoundry.org/cli/util/configv3"
     7  	. "code.cloudfoundry.org/cli/util/ui"
     8  	"code.cloudfoundry.org/cli/util/ui/uifakes"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  )
    13  
    14  var _ = Describe("Log Message", func() {
    15  	var (
    16  		ui         *UI
    17  		fakeConfig *uifakes.FakeConfig
    18  		out        *Buffer
    19  		errBuff    *Buffer
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		fakeConfig = new(uifakes.FakeConfig)
    24  		fakeConfig.ColorEnabledReturns(configv3.ColorEnabled)
    25  
    26  		var err error
    27  		ui, err = NewUI(fakeConfig)
    28  		Expect(err).NotTo(HaveOccurred())
    29  
    30  		out = NewBuffer()
    31  		ui.Out = out
    32  		ui.OutForInteration = out
    33  		errBuff = NewBuffer()
    34  		ui.Err = errBuff
    35  	})
    36  
    37  	It("sets the TimezoneLocation to the local timezone", func() {
    38  		location := time.Now().Location()
    39  		Expect(ui.TimezoneLocation).To(Equal(location))
    40  	})
    41  
    42  	Describe("DisplayLogMessage", func() {
    43  		var message *uifakes.FakeLogMessage
    44  
    45  		BeforeEach(func() {
    46  			var err error
    47  			ui.TimezoneLocation, err = time.LoadLocation("America/Los_Angeles")
    48  			Expect(err).NotTo(HaveOccurred())
    49  
    50  			message = new(uifakes.FakeLogMessage)
    51  			message.MessageReturns("This is a log message\r\n")
    52  			message.TypeReturns("OUT")
    53  			message.TimestampReturns(time.Unix(1468969692, 0)) // "2016-07-19T16:08:12-07:00"
    54  			message.SourceTypeReturns("APP/PROC/WEB")
    55  			message.SourceInstanceReturns("12")
    56  		})
    57  
    58  		Context("with header", func() {
    59  			Context("single line log message", func() {
    60  				It("prints out a single line to STDOUT", func() {
    61  					ui.DisplayLogMessage(message, true)
    62  					Expect(out).To(Say(`2016-07-19T16:08:12.00-0700 \[APP/PROC/WEB/12\] OUT This is a log message\n`))
    63  				})
    64  			})
    65  
    66  			Context("multi-line log message", func() {
    67  				BeforeEach(func() {
    68  					var err error
    69  					ui.TimezoneLocation, err = time.LoadLocation("America/Los_Angeles")
    70  					Expect(err).NotTo(HaveOccurred())
    71  
    72  					message.MessageReturns("This is a log message\nThis is also a log message")
    73  				})
    74  
    75  				It("prints out mutliple lines to STDOUT", func() {
    76  					ui.DisplayLogMessage(message, true)
    77  					Expect(out).To(Say(`2016-07-19T16:08:12.00-0700 \[APP/PROC/WEB/12\] OUT This is a log message\n`))
    78  					Expect(out).To(Say(`2016-07-19T16:08:12.00-0700 \[APP/PROC/WEB/12\] OUT This is also a log message\n`))
    79  				})
    80  			})
    81  		})
    82  
    83  		Context("without header", func() {
    84  			Context("single line log message", func() {
    85  				It("prints out a single line to STDOUT", func() {
    86  					ui.DisplayLogMessage(message, false)
    87  					Expect(out).To(Say("This is a log message\n"))
    88  				})
    89  			})
    90  
    91  			Context("multi-line log message", func() {
    92  				BeforeEach(func() {
    93  					var err error
    94  					ui.TimezoneLocation, err = time.LoadLocation("America/Los_Angeles")
    95  					Expect(err).NotTo(HaveOccurred())
    96  
    97  					message.MessageReturns("This is a log message\nThis is also a log message")
    98  				})
    99  
   100  				It("prints out mutliple lines to STDOUT", func() {
   101  					ui.DisplayLogMessage(message, false)
   102  					Expect(out).To(Say("This is a log message\n"))
   103  					Expect(out).To(Say("This is also a log message\n"))
   104  				})
   105  			})
   106  		})
   107  
   108  		Context("error log lines", func() {
   109  			BeforeEach(func() {
   110  				message.TypeReturns("ERR")
   111  			})
   112  			It("colors the line red", func() {
   113  				ui.DisplayLogMessage(message, false)
   114  				Expect(out).To(Say("\x1b\\[31mThis is a log message\x1b\\[0m\n"))
   115  			})
   116  		})
   117  	})
   118  })