github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/net/warnings_collector_test.go (about) 1 package net_test 2 3 import ( 4 "os" 5 6 "github.com/cloudfoundry/cli/cf/net" 7 testnet "github.com/cloudfoundry/cli/testhelpers/net" 8 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 9 10 . "github.com/cloudfoundry/cli/testhelpers/matchers" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 ) 14 15 var _ = Describe("WarningsCollector", func() { 16 var ( 17 ui *testterm.FakeUI 18 oldRaiseErrorValue string 19 warningsCollector net.WarningsCollector 20 ) 21 22 BeforeEach(func() { 23 ui = new(testterm.FakeUI) 24 }) 25 26 Describe("PrintWarnings", func() { 27 BeforeEach(func() { 28 oldRaiseErrorValue = os.Getenv("CF_RAISE_ERROR_ON_WARNINGS") 29 }) 30 31 AfterEach(func() { 32 os.Setenv("CF_RAISE_ERROR_ON_WARNINGS", oldRaiseErrorValue) 33 }) 34 35 Context("when the CF_RAISE_ERROR_ON_WARNINGS environment variable is set", func() { 36 BeforeEach(func() { 37 os.Setenv("CF_RAISE_ERROR_ON_WARNINGS", "true") 38 }) 39 40 Context("when there are warnings", func() { 41 BeforeEach(func() { 42 warning_producer_one := testnet.NewWarningProducer([]string{"Hello", "Darling"}) 43 warning_producer_two := testnet.NewWarningProducer([]string{"Goodbye", "Sweetie"}) 44 warning_producer_three := testnet.NewWarningProducer(nil) 45 warningsCollector = net.NewWarningsCollector(ui, warning_producer_one, warning_producer_two, warning_producer_three) 46 }) 47 48 It("panics with an error that contains all the warnings", func() { 49 Expect(warningsCollector.PrintWarnings).To(Panic()) 50 }) 51 }) 52 53 Context("when there are no warnings", func() { 54 BeforeEach(func() { 55 warningsCollector = net.NewWarningsCollector(ui) 56 }) 57 58 It("does not panic", func() { 59 Expect(warningsCollector.PrintWarnings).NotTo(Panic()) 60 }) 61 62 }) 63 }) 64 65 Context("when the CF_RAISE_ERROR_ON_WARNINGS environment variable is not set", func() { 66 BeforeEach(func() { 67 os.Setenv("CF_RAISE_ERROR_ON_WARNINGS", "") 68 }) 69 70 It("does not panic", func() { 71 warning_producer_one := testnet.NewWarningProducer([]string{"Hello", "Darling"}) 72 warning_producer_two := testnet.NewWarningProducer([]string{"Goodbye", "Sweetie"}) 73 warning_producer_three := testnet.NewWarningProducer(nil) 74 warningsCollector := net.NewWarningsCollector(ui, warning_producer_one, warning_producer_two, warning_producer_three) 75 76 Expect(warningsCollector.PrintWarnings).NotTo(Panic()) 77 }) 78 79 It("does not print out duplicate warnings", func() { 80 warning_producer_one := testnet.NewWarningProducer([]string{"Hello Darling"}) 81 warning_producer_two := testnet.NewWarningProducer([]string{"Hello Darling"}) 82 warningsCollector := net.NewWarningsCollector(ui, warning_producer_one, warning_producer_two) 83 84 warningsCollector.PrintWarnings() 85 Expect(len(ui.Outputs)).To(Equal(1)) 86 Expect(ui.Outputs).To(ContainSubstrings([]string{"Hello Darling"})) 87 }) 88 }) 89 }) 90 91 })