github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/terminal/color_test.go (about) 1 package terminal_test 2 3 import ( 4 . "github.com/cloudfoundry/cli/cf/terminal" 5 . "github.com/onsi/ginkgo" 6 . "github.com/onsi/gomega" 7 "os" 8 "runtime" 9 ) 10 11 var _ = Describe("Terminal colors", func() { 12 BeforeEach(func() { 13 UserAskedForColors = "" 14 }) 15 16 JustBeforeEach(func() { 17 InitColorSupport() 18 }) 19 20 Describe("CF_COLOR", func() { 21 Context("On OSes that don't support colors", func() { 22 BeforeEach(func() { OsSupportsColors = false }) 23 24 Context("When the CF_COLOR env variable is specified", func() { 25 BeforeEach(func() { os.Setenv("CF_COLOR", "true") }) 26 itColorizes() 27 }) 28 29 Context("When the CF_COLOR env variable is not specified", func() { 30 BeforeEach(func() { os.Setenv("CF_COLOR", "") }) 31 itDoesntColorize() 32 33 Context("when the user DOES ask for colors", func() { 34 BeforeEach(func() { UserAskedForColors = "true" }) 35 itColorizes() 36 }) 37 }) 38 }) 39 40 Context("On OSes that support colors", func() { 41 BeforeEach(func() { OsSupportsColors = true }) 42 43 Context("When the CF_COLOR env variable is not specified", func() { 44 BeforeEach(func() { os.Setenv("CF_COLOR", "") }) 45 46 Context("And the terminal supports colors", func() { 47 BeforeEach(func() { TerminalSupportsColors = true }) 48 itColorizes() 49 50 Context("And user does not ask for color", func() { 51 BeforeEach(func() { UserAskedForColors = "false" }) 52 itDoesntColorize() 53 }) 54 55 Context("And user does ask for color", func() { 56 BeforeEach(func() { UserAskedForColors = "true" }) 57 itColorizes() 58 }) 59 }) 60 61 Context("And the terminal doesn't support colors", func() { 62 BeforeEach(func() { TerminalSupportsColors = false }) 63 itDoesntColorize() 64 65 Context("And user asked for color", func() { 66 BeforeEach(func() { UserAskedForColors = "true" }) 67 itColorizes() 68 }) 69 }) 70 }) 71 72 Context("When the CF_COLOR env variable is set to 'true'", func() { 73 BeforeEach(func() { os.Setenv("CF_COLOR", "true") }) 74 75 Context("And the terminal supports colors", func() { 76 BeforeEach(func() { TerminalSupportsColors = true }) 77 itColorizes() 78 79 Context("and the user asked for colors", func() { 80 BeforeEach(func() { UserAskedForColors = "true" }) 81 itColorizes() 82 }) 83 84 Context("and the user did not ask for colors", func() { 85 BeforeEach(func() { UserAskedForColors = "false" }) 86 itColorizes() 87 }) 88 }) 89 90 Context("Even if the terminal doesn't support colors", func() { 91 BeforeEach(func() { TerminalSupportsColors = false }) 92 itColorizes() 93 }) 94 }) 95 96 Context("When the CF_COLOR env variable is set to 'false', even if the terminal supports colors", func() { 97 BeforeEach(func() { 98 os.Setenv("CF_COLOR", "false") 99 TerminalSupportsColors = true 100 }) 101 102 itDoesntColorize() 103 104 Context("and the user asked for colors", func() { 105 BeforeEach(func() { UserAskedForColors = "true" }) 106 itDoesntColorize() 107 }) 108 }) 109 }) 110 }) 111 112 Describe("OsSupportsColors", func() { 113 It("Returns false on windows, and true otherwise", func() { 114 if runtime.GOOS == "windows" { 115 Expect(OsSupportsColors).To(BeFalse()) 116 } else { 117 Expect(OsSupportsColors).To(BeTrue()) 118 } 119 }) 120 }) 121 122 var ( 123 originalOsSupportsColors bool 124 originalTerminalSupportsColors bool 125 ) 126 127 BeforeEach(func() { 128 originalOsSupportsColors = OsSupportsColors 129 originalTerminalSupportsColors = TerminalSupportsColors 130 }) 131 132 AfterEach(func() { 133 OsSupportsColors = originalOsSupportsColors 134 TerminalSupportsColors = originalTerminalSupportsColors 135 os.Setenv("CF_COLOR", "false") 136 }) 137 }) 138 139 func itColorizes() { 140 It("colorizes", func() { 141 text := "Hello World" 142 colorizedText := ColorizeBold(text, 31) 143 Expect(colorizedText).To(Equal("\033[1;31mHello World\033[0m")) 144 }) 145 } 146 147 func itDoesntColorize() { 148 It("doesn't colorize", func() { 149 text := "Hello World" 150 colorizedText := ColorizeBold(text, 31) 151 Expect(colorizedText).To(Equal("Hello World")) 152 }) 153 }