github.com/blend/go-sdk@v1.20220411.3/env/vars_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package env_test
     9  
    10  import (
    11  	"encoding/base64"
    12  	"os"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/blend/go-sdk/assert"
    17  	"github.com/blend/go-sdk/env"
    18  )
    19  
    20  func TestNewVarsFromEnvironment(t *testing.T) {
    21  	assert := assert.New(t)
    22  	assert.NotNil(env.New(env.OptEnviron(os.Environ()...)))
    23  }
    24  
    25  func TestVarsSet(t *testing.T) {
    26  	assert := assert.New(t)
    27  
    28  	vars := env.Vars{
    29  		"Foo": "baz",
    30  	}
    31  
    32  	vars.Set("Foo", "bar")
    33  	assert.Equal("bar", vars.String("Foo"))
    34  
    35  	vars.Set("NotFoo", "buzz")
    36  	assert.Equal("buzz", vars.String("NotFoo"))
    37  }
    38  
    39  func TestEnvBool(t *testing.T) {
    40  	assert := assert.New(t)
    41  
    42  	vars := env.Vars{
    43  		"true":  "true",
    44  		"1":     "1",
    45  		"yes":   "yes",
    46  		"false": "false",
    47  	}
    48  
    49  	assert.True(vars.Bool("true"))
    50  	assert.True(vars.Bool("1"))
    51  	assert.True(vars.Bool("yes"))
    52  	assert.False(vars.Bool("false"))
    53  	assert.False(vars.Bool("no"))
    54  
    55  	// Test Set False
    56  	assert.False(vars.Bool("false"))
    57  
    58  	// Test Unset Default
    59  	assert.False(vars.Bool("0"))
    60  
    61  	// Test Unset User Default
    62  	assert.True(vars.Bool("0", true))
    63  }
    64  
    65  func TestEnvInt(t *testing.T) {
    66  	assert := assert.New(t)
    67  
    68  	vars := env.Vars{
    69  		"One": "1",
    70  		"Two": "2",
    71  		"Foo": "Bar",
    72  	}
    73  
    74  	assert.Equal(1, vars.MustInt("One"))
    75  	assert.Equal(2, vars.MustInt("Two"))
    76  	_, err := vars.Int("Foo")
    77  	assert.NotNil(err)
    78  	assert.Zero(vars.MustInt("Baz"))
    79  	assert.Equal(4, vars.MustInt("Baz", 4))
    80  }
    81  
    82  func TestEnvInt64(t *testing.T) {
    83  	assert := assert.New(t)
    84  
    85  	vars := env.Vars{
    86  		"One": "1",
    87  		"Two": "2",
    88  		"Foo": "Bar",
    89  	}
    90  
    91  	assert.Equal(1, vars.MustInt64("One"))
    92  	assert.Equal(2, vars.MustInt64("Two"))
    93  	_, err := vars.Int64("Foo")
    94  	assert.NotNil(err)
    95  	assert.Zero(vars.MustInt64("Baz"))
    96  	assert.Equal(4, vars.MustInt64("Baz", 4))
    97  }
    98  
    99  func TestEnvBytes(t *testing.T) {
   100  	assert := assert.New(t)
   101  
   102  	vars := env.Vars{
   103  		"Foo": "abcdef",
   104  	}
   105  
   106  	assert.Equal("abcdef", string(vars.Bytes("Foo")))
   107  	assert.Nil(vars.Bytes("NotFoo"))
   108  	assert.Equal("Bar", string(vars.Bytes("NotFoo", []byte("Bar"))))
   109  }
   110  
   111  func TestEnvBase64(t *testing.T) {
   112  	assert := assert.New(t)
   113  
   114  	testValue := base64.StdEncoding.EncodeToString([]byte("this is a test"))
   115  	vars := env.Vars{
   116  		"Foo": string(testValue),
   117  		"Bar": "not_base64",
   118  	}
   119  
   120  	res, err := vars.Base64("Foo")
   121  	assert.Nil(err)
   122  	assert.Equal("this is a test", string(res))
   123  
   124  	res, err = vars.Base64("Bar")
   125  	assert.NotNil(err)
   126  	assert.Empty(res)
   127  }
   128  
   129  func TestEnvHasKey(t *testing.T) {
   130  	assert := assert.New(t)
   131  
   132  	vars := env.Vars{
   133  		"test1": "foo",
   134  		"test2": "bar",
   135  		"test3": "baz",
   136  		"test4": "buzz",
   137  	}
   138  
   139  	assert.True(vars.Has("test1"))
   140  	assert.False(vars.Has("notTest1"))
   141  }
   142  
   143  func TestEnvHasAnyKeys(t *testing.T) {
   144  	assert := assert.New(t)
   145  
   146  	vars := env.Vars{
   147  		"test1": "foo",
   148  		"test2": "bar",
   149  		"test3": "baz",
   150  		"test4": "buzz",
   151  	}
   152  
   153  	assert.True(vars.HasAny("test1"))
   154  	assert.True(vars.HasAny("test1", "test2", "test3", "test4"))
   155  	assert.True(vars.HasAny("test1", "test2", "test3", "notTest4"))
   156  	assert.False(vars.HasAny("notTest1", "notTest2"))
   157  	assert.False(vars.HasAny())
   158  }
   159  
   160  func TestEnvHasAllKeys(t *testing.T) {
   161  	assert := assert.New(t)
   162  
   163  	vars := env.Vars{
   164  		"test1": "foo",
   165  		"test2": "bar",
   166  		"test3": "baz",
   167  		"test4": "buzz",
   168  	}
   169  
   170  	assert.True(vars.HasAll("test1"))
   171  	assert.True(vars.HasAll("test1", "test2", "test3", "test4"))
   172  	assert.False(vars.HasAll("test1", "test2", "test3", "notTest4"))
   173  	assert.False(vars.HasAll())
   174  }
   175  
   176  func TestVarsKeys(t *testing.T) {
   177  	assert := assert.New(t)
   178  
   179  	vars := env.Vars{
   180  		"test1": "foo",
   181  		"test2": "bar",
   182  		"test3": "baz",
   183  		"test4": "buzz",
   184  	}
   185  
   186  	keys := vars.Vars()
   187  	assert.Len(keys, 4)
   188  	assert.Any(keys, func(v interface{}) bool { return v.(string) == "test1" })
   189  	assert.Any(keys, func(v interface{}) bool { return v.(string) == "test2" })
   190  	assert.Any(keys, func(v interface{}) bool { return v.(string) == "test3" })
   191  	assert.Any(keys, func(v interface{}) bool { return v.(string) == "test4" })
   192  }
   193  
   194  func TestEnvUnion(t *testing.T) {
   195  	assert := assert.New(t)
   196  
   197  	vars1 := env.Vars{
   198  		"test3": "baz",
   199  		"test4": "buzz",
   200  	}
   201  
   202  	vars2 := env.Vars{
   203  		"test1": "foo",
   204  		"test2": "bar",
   205  	}
   206  
   207  	union := vars1.Union(vars2)
   208  
   209  	assert.Len(union, 4)
   210  	assert.True(union.HasAll("test1", "test3"))
   211  }
   212  
   213  type readInto struct {
   214  	Test1   string        `env:"test1"`
   215  	Test2   int           `env:"test2"`
   216  	Test3   float64       `env:"test3"`
   217  	Dur     time.Duration `env:"dur"`
   218  	Sub     readIntoSub
   219  	Alias   alias   `env:"alias"`
   220  	Uint    uint    `env:"uint"`
   221  	Uint8   uint    `env:"uint8"`
   222  	Uint16  uint16  `env:"uint16"`
   223  	Uint32  uint32  `env:"uint32"`
   224  	Uint64  uint64  `env:"uint64"`
   225  	Int     int     `env:"int"`
   226  	Int8    int     `env:"int8"`
   227  	Int16   int16   `env:"int16"`
   228  	Int32   int32   `env:"int32"`
   229  	Int64   int64   `env:"int64"`
   230  	Float32 float32 `env:"float32"`
   231  	Float64 float32 `env:"float64"`
   232  
   233  	EmptyUint    uint    `env:"emptyuint"`
   234  	EmptyUint8   uint    `env:"emptyuint8"`
   235  	EmptyUint16  uint16  `env:"emptyuint16"`
   236  	EmptyUint32  uint32  `env:"emptyuint32"`
   237  	EmptyUint64  uint64  `env:"emptyuint64"`
   238  	EmptyInt     int     `env:"emptyint"`
   239  	EmptyInt8    int     `env:"emptyint8"`
   240  	EmptyInt16   int16   `env:"emptyint16"`
   241  	EmptyInt32   int32   `env:"emptyint32"`
   242  	EmptyInt64   int64   `env:"emptyint64"`
   243  	EmptyFloat32 float32 `env:"emptyfloat32"`
   244  	EmptyFloat64 float32 `env:"emptyfloat64"`
   245  }
   246  
   247  type readIntoSub struct {
   248  	Test4 string   `env:"test4"`
   249  	Test5 []string `env:"test5,csv"`
   250  	Test6 []byte   `env:"test6,base64"`
   251  	Test7 []byte   `env:"test7,bytes"`
   252  	Test8 *bool    `env:"test8"`
   253  }
   254  
   255  type alias string
   256  
   257  func TestEnvReadInto(t *testing.T) {
   258  	assert := assert.New(t)
   259  
   260  	vars1 := env.Vars{
   261  		"test1":   "foo",
   262  		"test2":   "1",
   263  		"test3":   "2.0",
   264  		"test4":   "bar",
   265  		"dur":     "4s",
   266  		"test5":   "bar0,bar1,bar2",
   267  		"test6":   string(base64.StdEncoding.EncodeToString([]byte("base64encoded"))),
   268  		"test7":   "alsoBytes",
   269  		"test8":   "true",
   270  		"alias":   "hello",
   271  		"uint":    "1",
   272  		"uint8":   "1",
   273  		"uint16":  "1",
   274  		"uint32":  "1",
   275  		"uint64":  "1",
   276  		"int":     "1",
   277  		"int8":    "1",
   278  		"int16":   "1",
   279  		"int32":   "1",
   280  		"int64":   "1",
   281  		"float32": "1",
   282  		"float64": "1",
   283  
   284  		"emptyuint":    "",
   285  		"emptyuint8":   "",
   286  		"emptyuint16":  "",
   287  		"emptyuint32":  "",
   288  		"emptyuint64":  "",
   289  		"emptyint":     "",
   290  		"emptyint8":    "",
   291  		"emptyint16":   "",
   292  		"emptyint32":   "",
   293  		"emptyint64":   "",
   294  		"emptyfloat32": "",
   295  		"emptyfloat64": "",
   296  	}
   297  
   298  	var obj readInto
   299  	err := vars1.ReadInto(&obj)
   300  	assert.Nil(err)
   301  	assert.Equal("foo", obj.Test1)
   302  	assert.Equal(1, obj.Test2)
   303  	assert.Equal(2.0, obj.Test3)
   304  	assert.Equal(4*time.Second, obj.Dur)
   305  	assert.Equal("bar", obj.Sub.Test4)
   306  	assert.NotEmpty(obj.Sub.Test5)
   307  	assert.Equal("bar0", obj.Sub.Test5[0])
   308  	assert.Equal("bar1", obj.Sub.Test5[1])
   309  	assert.Equal("bar2", obj.Sub.Test5[2])
   310  	assert.NotEmpty(obj.Sub.Test6)
   311  	assert.NotEmpty(obj.Sub.Test7)
   312  	assert.Equal(obj.Alias, vars1["alias"])
   313  
   314  	assert.NotZero(obj.Uint)
   315  	assert.NotZero(obj.Uint8)
   316  	assert.NotZero(obj.Uint16)
   317  	assert.NotZero(obj.Uint32)
   318  	assert.NotZero(obj.Uint64)
   319  	assert.NotZero(obj.Int)
   320  	assert.NotZero(obj.Int8)
   321  	assert.NotZero(obj.Int16)
   322  	assert.NotZero(obj.Int32)
   323  	assert.NotZero(obj.Int64)
   324  	assert.NotZero(obj.Float32)
   325  	assert.NotZero(obj.Float64)
   326  
   327  	assert.Zero(obj.EmptyUint)
   328  	assert.Zero(obj.EmptyUint8)
   329  	assert.Zero(obj.EmptyUint16)
   330  	assert.Zero(obj.EmptyUint32)
   331  	assert.Zero(obj.EmptyUint64)
   332  	assert.Zero(obj.EmptyInt)
   333  	assert.Zero(obj.EmptyInt8)
   334  	assert.Zero(obj.EmptyInt16)
   335  	assert.Zero(obj.EmptyInt32)
   336  	assert.Zero(obj.EmptyInt64)
   337  	assert.Zero(obj.EmptyFloat32)
   338  	assert.Zero(obj.EmptyFloat64)
   339  }
   340  
   341  func TestEnvDelete(t *testing.T) {
   342  	assert := assert.New(t)
   343  
   344  	vars := env.Vars{
   345  		"test": "foo",
   346  		"bar":  "baz",
   347  	}
   348  	assert.True(vars.Has("test"))
   349  	vars.Delete("test")
   350  	assert.False(vars.Has("test"))
   351  	assert.True(vars.Has("bar"))
   352  }
   353  
   354  func TestEnvCSV(t *testing.T) {
   355  	assert := assert.New(t)
   356  
   357  	vars := env.Vars{
   358  		"foo": "a,b,c",
   359  		"bar": "",
   360  	}
   361  
   362  	assert.Equal([]string{"a", "b", "c"}, vars.CSV("foo"))
   363  	assert.Equal([]string{"a", "b"}, vars.CSV("bar", "a", "b"))
   364  	assert.Equal([]string{"a", "b"}, vars.CSV("baz", "a", "b"))
   365  	assert.Nil(vars.CSV("baz"))
   366  }