github.com/BarDweller/libpak@v0.0.0-20230630201634-8dd5cfc15ec9/sherpa/env_var_test.go (about)

     1  /*
     2   * Copyright 2018-2020 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package sherpa_test
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	. "github.com/onsi/gomega"
    24  	"github.com/sclevine/spec"
    25  
    26  	"github.com/BarDweller/libpak/sherpa"
    27  )
    28  
    29  func testEnvVar(t *testing.T, context spec.G, it spec.S) {
    30  	var (
    31  		Expect = NewWithT(t).Expect
    32  	)
    33  
    34  	context("AppendToEnvVar", func() {
    35  
    36  		context("No Existing", func() {
    37  
    38  			it("append one", func() {
    39  				Expect(sherpa.AppendToEnvVar("TEST_KEY", "|", "test-value-2")).
    40  					To(Equal("test-value-2"))
    41  			})
    42  
    43  			it("appends multiple", func() {
    44  				Expect(sherpa.AppendToEnvVar("TEST_KEY", "|", "test-value-2", "test-value-3")).
    45  					To(Equal("test-value-2|test-value-3"))
    46  			})
    47  		})
    48  
    49  		context("With Existing", func() {
    50  			it.Before(func() {
    51  				Expect(os.Setenv("TEST_KEY", "test-value-1")).To(Succeed())
    52  			})
    53  
    54  			it.After(func() {
    55  				Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
    56  			})
    57  
    58  			it("append one", func() {
    59  				Expect(sherpa.AppendToEnvVar("TEST_KEY", "|", "test-value-2")).
    60  					To(Equal("test-value-1|test-value-2"))
    61  			})
    62  
    63  			it("appends multiple", func() {
    64  				Expect(sherpa.AppendToEnvVar("TEST_KEY", "|", "test-value-2", "test-value-3")).
    65  					To(Equal("test-value-1|test-value-2|test-value-3"))
    66  			})
    67  		})
    68  	})
    69  
    70  	context("GetEnvRequired", func() {
    71  		it.Before(func() {
    72  			Expect(os.Setenv("TEST_KEY", "test-value")).To(Succeed())
    73  		})
    74  
    75  		it.After(func() {
    76  			Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
    77  		})
    78  
    79  		it("returns value if set", func() {
    80  			Expect(sherpa.GetEnvRequired("TEST_KEY")).To(Equal("test-value"))
    81  		})
    82  
    83  		it("returns error if not set", func() {
    84  			_, err := sherpa.GetEnvRequired("ANOTHER_KEY")
    85  			Expect(err).To(MatchError("$ANOTHER_KEY must be set"))
    86  		})
    87  	})
    88  
    89  	context("GetEnvWithDefault", func() {
    90  		it.Before(func() {
    91  			Expect(os.Setenv("TEST_KEY", "test-value")).To(Succeed())
    92  		})
    93  
    94  		it.After(func() {
    95  			Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
    96  		})
    97  
    98  		it("returns value if set", func() {
    99  			Expect(sherpa.GetEnvWithDefault("TEST_KEY", "default-value")).To(Equal("test-value"))
   100  		})
   101  
   102  		it("returns default value if not set", func() {
   103  			Expect(sherpa.GetEnvWithDefault("ANOTHER_KEY", "default-value")).To(Equal("default-value"))
   104  		})
   105  	})
   106  
   107  	context("ResolveBoolErr", func() {
   108  		context("variable not set", func() {
   109  			it("returns false if not set", func() {
   110  				boolean, err := sherpa.ResolveBoolErr("TEST_KEY")
   111  				Expect(boolean).To(BeFalse())
   112  				Expect(err).ToNot(HaveOccurred())
   113  			})
   114  		})
   115  
   116  		context("variable is set to true value", func() {
   117  			it.After(func() {
   118  				Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
   119  			})
   120  
   121  			it("returns true", func() {
   122  				for _, form := range []string{"1", "t", "T", "TRUE", "true", "True", "\t1\n"} {
   123  					Expect(os.Setenv("TEST_KEY", form))
   124  					boolean, err := sherpa.ResolveBoolErr("TEST_KEY")
   125  					Expect(boolean).To(BeTrue())
   126  					Expect(err).ToNot(HaveOccurred())
   127  					Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
   128  				}
   129  			})
   130  		})
   131  
   132  		context("variable is set to non-true value", func() {
   133  			it.After(func() {
   134  				Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
   135  			})
   136  
   137  			it("returns false", func() {
   138  				for _, form := range []string{"0", "f", "F", "FALSE", "false", "False", "\tF\n"} {
   139  					Expect(os.Setenv("TEST_KEY", form))
   140  					boolean, err := sherpa.ResolveBoolErr("TEST_KEY")
   141  					Expect(boolean).To(BeFalse())
   142  					Expect(err).ToNot(HaveOccurred())
   143  					Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
   144  				}
   145  			})
   146  		})
   147  
   148  		context("variable is set to an invalid value", func() {
   149  			it.After(func() {
   150  				Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
   151  			})
   152  
   153  			it("returns false", func() {
   154  				Expect(os.Setenv("TEST_KEY", "foo"))
   155  				boolean, err := sherpa.ResolveBoolErr("TEST_KEY")
   156  				Expect(boolean).To(BeFalse())
   157  				Expect(err).To(MatchError("invalid value 'foo' for key 'TEST_KEY': " +
   158  					"expected one of [1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False]"))
   159  				Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
   160  			})
   161  		})
   162  
   163  	})
   164  
   165  	context("ResolveBool", func() {
   166  		context("variable is set to an invalid value", func() {
   167  			it.After(func() {
   168  				Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
   169  			})
   170  
   171  			it("returns false", func() {
   172  				Expect(os.Setenv("TEST_KEY", "foo"))
   173  				Expect(sherpa.ResolveBool("TEST_KEY")).To(BeFalse())
   174  				Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
   175  			})
   176  		})
   177  	})
   178  }