github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/updater/util/env_test.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package util
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  // testEnvFn returns value
    14  func testEnvFn(k, v string) func(e string) string {
    15  	return func(e string) string {
    16  		if k == e {
    17  			return v
    18  		}
    19  		return ""
    20  	}
    21  }
    22  
    23  func TestEnvDuration(t *testing.T) {
    24  	duration := envDuration(testEnvFn("TEST", "1s"), "TEST", time.Minute)
    25  	assert.Equal(t, time.Second, duration)
    26  	duration = envDuration(testEnvFn("TEST", ""), "TEST", time.Minute)
    27  	assert.Equal(t, time.Minute, duration)
    28  	duration = envDuration(testEnvFn("TEST", "invalid"), "TEST", time.Minute)
    29  	assert.Equal(t, time.Minute, duration)
    30  	duration = EnvDuration("TEST", time.Hour)
    31  	assert.Equal(t, time.Hour, duration)
    32  }
    33  
    34  func TestEnvBool(t *testing.T) {
    35  	b := envBool(testEnvFn("TEST", "true"), "TEST", false)
    36  	assert.True(t, b)
    37  	b = envBool(testEnvFn("TEST", "1"), "TEST", false)
    38  	assert.True(t, b)
    39  	b = envBool(testEnvFn("TEST", "false"), "TEST", true)
    40  	assert.False(t, b)
    41  	b = envBool(testEnvFn("TEST", "0"), "TEST", false)
    42  	assert.False(t, b)
    43  	b = envBool(testEnvFn("TEST", ""), "TEST", false)
    44  	assert.False(t, b)
    45  	b = envBool(testEnvFn("TEST", ""), "TEST", true)
    46  	assert.True(t, b)
    47  	b = EnvBool("TEST", true)
    48  	assert.True(t, b)
    49  }