github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/cf/terminal/color_test.go (about) 1 package terminal_test 2 3 import ( 4 "os" 5 6 "github.com/fatih/color" 7 . "github.com/liamawhite/cli-with-i18n/cf/terminal" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 ) 11 12 var _ = Describe("Terminal colors", func() { 13 BeforeEach(func() { 14 UserAskedForColors = "" 15 }) 16 17 JustBeforeEach(func() { 18 InitColorSupport() 19 }) 20 21 Describe("CF_COLOR", func() { 22 Context("All OSes support colors", func() { 23 Context("When the CF_COLOR env variable is not specified", func() { 24 BeforeEach(func() { os.Setenv("CF_COLOR", "") }) 25 26 Context("And the terminal supports colors", func() { 27 BeforeEach(func() { TerminalSupportsColors = true }) 28 itColorizes() 29 30 Context("And user does not ask for color", func() { 31 BeforeEach(func() { UserAskedForColors = "false" }) 32 itDoesntColorize() 33 }) 34 35 Context("And user does ask for color", func() { 36 BeforeEach(func() { UserAskedForColors = "true" }) 37 itColorizes() 38 }) 39 }) 40 41 Context("And the terminal doesn't support colors", func() { 42 BeforeEach(func() { TerminalSupportsColors = false }) 43 itDoesntColorize() 44 45 Context("And user asked for color", func() { 46 BeforeEach(func() { UserAskedForColors = "true" }) 47 itColorizes() 48 }) 49 }) 50 }) 51 52 Context("When the CF_COLOR env variable is set to 'true'", func() { 53 BeforeEach(func() { os.Setenv("CF_COLOR", "true") }) 54 55 Context("And the terminal supports colors", func() { 56 BeforeEach(func() { TerminalSupportsColors = true }) 57 itColorizes() 58 59 Context("and the user asked for colors", func() { 60 BeforeEach(func() { UserAskedForColors = "true" }) 61 itColorizes() 62 }) 63 64 Context("and the user did not ask for colors", func() { 65 BeforeEach(func() { UserAskedForColors = "false" }) 66 itColorizes() 67 }) 68 }) 69 70 Context("Even if the terminal doesn't support colors", func() { 71 BeforeEach(func() { TerminalSupportsColors = false }) 72 itColorizes() 73 }) 74 }) 75 76 Context("When the CF_COLOR env variable is set to 'false', even if the terminal supports colors", func() { 77 BeforeEach(func() { 78 os.Setenv("CF_COLOR", "false") 79 TerminalSupportsColors = true 80 }) 81 82 itDoesntColorize() 83 84 Context("and the user asked for colors", func() { 85 BeforeEach(func() { UserAskedForColors = "true" }) 86 itDoesntColorize() 87 }) 88 }) 89 }) 90 }) 91 92 var ( 93 originalTerminalSupportsColors bool 94 ) 95 96 BeforeEach(func() { 97 originalTerminalSupportsColors = TerminalSupportsColors 98 }) 99 100 AfterEach(func() { 101 TerminalSupportsColors = originalTerminalSupportsColors 102 os.Setenv("CF_COLOR", "false") 103 }) 104 }) 105 106 func itColorizes() { 107 It("colorizes", func() { 108 text := "Hello World" 109 colorizedText := ColorizeBold(text, 33) 110 colorizeYellow := color.New(color.FgYellow).Add(color.Bold).SprintFunc() 111 Expect(colorizedText).To(Equal(colorizeYellow(text))) 112 }) 113 } 114 115 func itDoesntColorize() { 116 It("doesn't colorize", func() { 117 text := "Hello World" 118 colorizedText := ColorizeBold(text, 33) 119 Expect(colorizedText).To(Equal("Hello World")) 120 }) 121 }