github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/terminal/tee_printer_test.go (about)

     1  package terminal_test
     2  
     3  import (
     4  	"os"
     5  
     6  	. "code.cloudfoundry.org/cli/cf/terminal"
     7  
     8  	io_helpers "code.cloudfoundry.org/cli/cf/util/testhelpers/io"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	"github.com/onsi/gomega/gbytes"
    12  )
    13  
    14  var _ = Describe("TeePrinter", func() {
    15  	var (
    16  		output  []string
    17  		printer *TeePrinter
    18  	)
    19  
    20  	Describe(".Print", func() {
    21  		var bucket *gbytes.Buffer
    22  
    23  		BeforeEach(func() {
    24  			bucket = gbytes.NewBuffer()
    25  
    26  			output = io_helpers.CaptureOutput(func() {
    27  				printer = NewTeePrinter(os.Stdout)
    28  				printer.SetOutputBucket(bucket)
    29  				printer.Print("Hello ")
    30  				printer.Print("Mom!")
    31  			})
    32  		})
    33  
    34  		It("should delegate to fmt.Print", func() {
    35  			Expect(output[0]).To(Equal("Hello Mom!"))
    36  		})
    37  
    38  		It("should save the output to the slice", func() {
    39  			Expect(bucket).To(gbytes.Say("Hello "))
    40  			Expect(bucket).To(gbytes.Say("Mom!"))
    41  		})
    42  
    43  		It("should decolorize text", func() {
    44  			io_helpers.CaptureOutput(func() {
    45  				printer = NewTeePrinter(os.Stdout)
    46  				printer.SetOutputBucket(bucket)
    47  				printer.Print("hi " + EntityNameColor("foo"))
    48  			})
    49  
    50  			Expect(bucket).To(gbytes.Say("hi foo"))
    51  		})
    52  	})
    53  
    54  	Describe(".Printf", func() {
    55  		var bucket *gbytes.Buffer
    56  
    57  		BeforeEach(func() {
    58  			bucket = gbytes.NewBuffer()
    59  
    60  			output = io_helpers.CaptureOutput(func() {
    61  				printer = NewTeePrinter(os.Stdout)
    62  				printer.SetOutputBucket(bucket)
    63  				printer.Printf("Hello %s", "everybody")
    64  			})
    65  		})
    66  
    67  		It("should delegate to fmt.Printf", func() {
    68  			Expect(output[0]).To(Equal("Hello everybody"))
    69  		})
    70  
    71  		It("should save the output to the slice", func() {
    72  			Expect(bucket).To(gbytes.Say("Hello everybody"))
    73  		})
    74  
    75  		It("should decolorize text", func() {
    76  			io_helpers.CaptureOutput(func() {
    77  				printer = NewTeePrinter(os.Stdout)
    78  				printer.SetOutputBucket(bucket)
    79  				printer.Printf("hi %s", EntityNameColor("foo"))
    80  			})
    81  
    82  			Expect(bucket).To(gbytes.Say("hi foo"))
    83  		})
    84  	})
    85  
    86  	Describe(".Println", func() {
    87  		var bucket *gbytes.Buffer
    88  		BeforeEach(func() {
    89  			bucket = gbytes.NewBuffer()
    90  
    91  			output = io_helpers.CaptureOutput(func() {
    92  				printer = NewTeePrinter(os.Stdout)
    93  				printer.SetOutputBucket(bucket)
    94  				printer.Println("Hello ", "everybody")
    95  			})
    96  		})
    97  
    98  		It("should delegate to fmt.Printf", func() {
    99  			Expect(output[0]).To(Equal("Hello everybody"))
   100  		})
   101  
   102  		It("should save the output to the slice", func() {
   103  			Expect(bucket).To(gbytes.Say("Hello everybody"))
   104  		})
   105  
   106  		It("should decolorize text", func() {
   107  			io_helpers.CaptureOutput(func() {
   108  				printer = NewTeePrinter(os.Stdout)
   109  				printer.SetOutputBucket(bucket)
   110  				printer.Println("hi " + EntityNameColor("foo"))
   111  			})
   112  
   113  			Expect(bucket).To(gbytes.Say("hi foo"))
   114  		})
   115  	})
   116  
   117  	Describe(".SetOutputBucket", func() {
   118  		It("sets the []string used to save the output", func() {
   119  			bucket := gbytes.NewBuffer()
   120  
   121  			output := io_helpers.CaptureOutput(func() {
   122  				printer = NewTeePrinter(os.Stdout)
   123  				printer.SetOutputBucket(bucket)
   124  				printer.Printf("Hello %s", "everybody")
   125  			})
   126  
   127  			Expect(bucket).To(gbytes.Say("Hello everybody"))
   128  			Expect(output).To(ContainElement("Hello everybody"))
   129  		})
   130  
   131  		It("disables the output saving when set to nil", func() {
   132  			output := io_helpers.CaptureOutput(func() {
   133  				printer = NewTeePrinter(os.Stdout)
   134  				printer.SetOutputBucket(nil)
   135  				printer.Printf("Hello %s", "everybody")
   136  			})
   137  			Expect(output).To(ContainElement("Hello everybody"))
   138  		})
   139  	})
   140  })