github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/command/flag/environment_variable_test.go (about) 1 // +build !windows 2 3 package flag_test 4 5 import ( 6 "fmt" 7 "os" 8 "strings" 9 10 . "code.cloudfoundry.org/cli/command/flag" 11 flags "github.com/jessevdk/go-flags" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("EnvironmentVariable", func() { 17 var ( 18 envVar EnvironmentVariable 19 envList []string 20 ) 21 22 BeforeEach(func() { 23 envList = []string{ 24 "ENVIRONMENTVARIABLE_TEST_ABC", 25 "ENVIRONMENTVARIABLE_TEST_FOO_BAR", 26 "ENVIRONMENTVARIABLE_TEST_ACKBAR", 27 "ENVIRONMENTVARIABLE_TEST_abc", 28 } 29 30 var err error 31 for _, v := range envList { 32 err = os.Setenv(v, "") 33 Expect(err).NotTo(HaveOccurred()) 34 } 35 }) 36 37 AfterEach(func() { 38 var err error 39 for _, v := range envList { 40 err = os.Unsetenv(v) 41 Expect(err).ToNot(HaveOccurred()) 42 } 43 }) 44 45 Describe("Complete", func() { 46 When("the prefix is empty", func() { 47 It("returns no matches", func() { 48 Expect(envVar.Complete("")).To(BeEmpty()) 49 }) 50 }) 51 52 When("the prefix does not start with $", func() { 53 It("returns no matches", func() { 54 Expect(envVar.Complete("A$A")).To(BeEmpty()) 55 }) 56 }) 57 58 When("the prefix starts with $", func() { 59 When("only $ is specified", func() { 60 It("returns all environment variables", func() { 61 keyValPairs := os.Environ() 62 envVars := make([]string, len(keyValPairs)) 63 for i, keyValPair := range keyValPairs { 64 envVars[i] = fmt.Sprintf("$%s", strings.Split(keyValPair, "=")[0]) 65 } 66 67 matches := envVar.Complete("$") 68 Expect(matches).To(HaveLen(len(keyValPairs))) 69 for _, v := range envVars { 70 Expect(matches).To(ContainElement(flags.Completion{Item: v})) 71 } 72 }) 73 }) 74 75 When("additional characters are specified", func() { 76 When("there are matching environment variables", func() { 77 It("returns the matching environment variables", func() { 78 matches := envVar.Complete("$ENVIRONMENTVARIABLE_TEST_A") 79 Expect(matches).To(HaveLen(2)) 80 Expect(matches).To(ConsistOf( 81 flags.Completion{Item: "$ENVIRONMENTVARIABLE_TEST_ABC"}, 82 flags.Completion{Item: "$ENVIRONMENTVARIABLE_TEST_ACKBAR"}, 83 )) 84 }) 85 86 It("is case sensitive", func() { 87 matches := envVar.Complete("$ENVIRONMENTVARIABLE_TEST_a") 88 Expect(matches).To(HaveLen(1)) 89 Expect(matches).To(ConsistOf( 90 flags.Completion{Item: "$ENVIRONMENTVARIABLE_TEST_abc"}, 91 )) 92 }) 93 }) 94 95 When("there are no matching environment variables", func() { 96 It("returns no matches", func() { 97 Expect(envVar.Complete("$ZZZZ")).To(BeEmpty()) 98 }) 99 }) 100 }) 101 }) 102 }) 103 104 Describe("UnmarshalFlag", func() { 105 var ( 106 rawFlagValue string 107 executeErr error 108 ) 109 110 BeforeEach(func() { 111 envVar = "" 112 rawFlagValue = "SOME_FLAG" 113 }) 114 115 JustBeforeEach(func() { 116 executeErr = envVar.UnmarshalFlag(rawFlagValue) 117 }) 118 119 When("the env variable value prefix starts with the WorkAroundPrefix", func() { 120 BeforeEach(func() { 121 rawFlagValue = WorkAroundPrefix + "SOME_FLAG" 122 }) 123 124 It("removes the WorkAroundPrefix", func() { 125 Expect(executeErr).NotTo(HaveOccurred()) 126 Expect(string(envVar)).ToNot(ContainSubstring(WorkAroundPrefix)) 127 Expect(string(envVar)).To(Equal("SOME_FLAG")) 128 }) 129 }) 130 131 When("the env variable value does not start with the WorkAroundPrefix", func() { 132 It("unmarshals the value with no error", func() { 133 Expect(executeErr).NotTo(HaveOccurred()) 134 Expect(string(envVar)).To(Equal("SOME_FLAG")) 135 }) 136 }) 137 }) 138 })