github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/flag/environment_variable_test.go (about)

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