github.com/ChicK00o/awgo@v0.29.4/config_test.go (about)

     1  // Copyright (c) 2018 Dean Jackson <deanishe@deanishe.net>
     2  // MIT Licence - http://opensource.org/licenses/MIT
     3  
     4  package aw
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  // TestConfigEnv verifies that Config holds the expected values.
    16  func TestConfigEnv(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	data := []struct {
    20  		name, x, key string
    21  	}{
    22  		{"Version", tVersion, EnvVarVersion},
    23  		{"Name", tName, EnvVarName},
    24  		{"BundleID", tBundleID, EnvVarBundleID},
    25  		{"UID", tUID, EnvVarUID},
    26  		{"ConfigVersion", tAlfredVersion, EnvVarAlfredVersion},
    27  		{"ConfigBuild", tAlfredBuild, EnvVarAlfredBuild},
    28  		{"Theme", tTheme, EnvVarTheme},
    29  		{"ThemeBackground", tThemeBackground, EnvVarThemeBG},
    30  		{"ThemeSelectionBackground", tThemeSelectionBackground, EnvVarThemeSelectionBG},
    31  		{"Preferences", tPreferences, EnvVarPreferences},
    32  		{"Localhash", tLocalhash, EnvVarLocalhash},
    33  		{"CacheDir", tCacheDir, EnvVarCacheDir},
    34  		{"CacheDir", tDataDir, EnvVarDataDir},
    35  	}
    36  
    37  	cfg := NewConfig(testEnv)
    38  
    39  	v := cfg.GetBool(EnvVarDebug)
    40  	assert.Equal(t, tDebug, v, "unexpected Debug")
    41  
    42  	for _, td := range data {
    43  		td := td // capture variable
    44  		t.Run(fmt.Sprintf("Config.Get(%v)", td.name), func(t *testing.T) {
    45  			t.Parallel()
    46  			assert.Equal(t, td.x, cfg.Get(td.key), "unexpected result")
    47  		})
    48  	}
    49  }
    50  
    51  func TestBundleID(t *testing.T) {
    52  	cfg := NewConfig()
    53  	x := "net.deanishe.awgo"
    54  	assert.Equal(t, x, cfg.getBundleID(), "unexpected bundle ID")
    55  
    56  	x = "net.deanishe.awgo2"
    57  	assert.Equal(t, x, cfg.getBundleID(x), "unexpected bundle ID")
    58  }
    59  
    60  // Basic usage of Config.Get. Returns an empty string if variable is unset.
    61  func ExampleConfig_Get() {
    62  	// Set some test variables
    63  	_ = os.Setenv("TEST_NAME", "Bob Smith")
    64  	_ = os.Setenv("TEST_ADDRESS", "7, Dreary Lane")
    65  
    66  	// New Config from environment
    67  	cfg := NewConfig()
    68  
    69  	fmt.Println(cfg.Get("TEST_NAME"))
    70  	fmt.Println(cfg.Get("TEST_ADDRESS"))
    71  	fmt.Println(cfg.Get("TEST_NONEXISTENT")) // unset variable
    72  
    73  	// GetString is a synonym
    74  	fmt.Println(cfg.GetString("TEST_NAME"))
    75  
    76  	// Output:
    77  	// Bob Smith
    78  	// 7, Dreary Lane
    79  	//
    80  	// Bob Smith
    81  
    82  	unsetEnv("TEST_NAME", "TEST_ADDRESS")
    83  }
    84  
    85  // The fallback value is returned if the variable is unset.
    86  func ExampleConfig_Get_fallback() {
    87  	// Set some test variables
    88  	_ = os.Setenv("TEST_NAME", "Bob Smith")
    89  	_ = os.Setenv("TEST_ADDRESS", "7, Dreary Lane")
    90  	_ = os.Setenv("TEST_EMAIL", "")
    91  
    92  	// New Config from environment
    93  	cfg := NewConfig()
    94  
    95  	fmt.Println(cfg.Get("TEST_NAME", "default name"))       // fallback ignored
    96  	fmt.Println(cfg.Get("TEST_ADDRESS", "default address")) // fallback ignored
    97  	fmt.Println(cfg.Get("TEST_EMAIL", "test@example.com"))  // fallback ignored (var is empty, not unset)
    98  	fmt.Println(cfg.Get("TEST_NONEXISTENT", "hi there!"))   // unset variable
    99  
   100  	// Output:
   101  	// Bob Smith
   102  	// 7, Dreary Lane
   103  	//
   104  	// hi there!
   105  
   106  	unsetEnv("TEST_NAME", "TEST_ADDRESS", "TEST_EMAIL")
   107  }
   108  
   109  // Getting int values with and without fallbacks.
   110  func ExampleConfig_GetInt() {
   111  	// Set some test variables
   112  	_ = os.Setenv("PORT", "3000")
   113  	_ = os.Setenv("PING_INTERVAL", "")
   114  
   115  	// New Config from environment
   116  	cfg := NewConfig()
   117  
   118  	fmt.Println(cfg.GetInt("PORT"))
   119  	fmt.Println(cfg.GetInt("PORT", 5000))        // fallback is ignored
   120  	fmt.Println(cfg.GetInt("PING_INTERVAL"))     // returns zero value
   121  	fmt.Println(cfg.GetInt("PING_INTERVAL", 60)) // returns fallback
   122  	// Output:
   123  	// 3000
   124  	// 3000
   125  	// 0
   126  	// 60
   127  
   128  	unsetEnv("PORT", "PING_INTERVAL")
   129  }
   130  
   131  // Strings are parsed to floats using strconv.ParseFloat().
   132  func ExampleConfig_GetFloat() {
   133  	// Set some test variables
   134  	_ = os.Setenv("TOTAL_SCORE", "172.3")
   135  	_ = os.Setenv("AVERAGE_SCORE", "7.54")
   136  
   137  	// New Config from environment
   138  	cfg := NewConfig()
   139  
   140  	fmt.Printf("%0.2f\n", cfg.GetFloat("TOTAL_SCORE"))
   141  	fmt.Printf("%0.1f\n", cfg.GetFloat("AVERAGE_SCORE"))
   142  	fmt.Println(cfg.GetFloat("NON_EXISTENT_SCORE", 120.5))
   143  	// Output:
   144  	// 172.30
   145  	// 7.5
   146  	// 120.5
   147  
   148  	unsetEnv("TOTAL_SCORE", "AVERAGE_SCORE")
   149  }
   150  
   151  // Durations are parsed using time.ParseDuration.
   152  func ExampleConfig_GetDuration() {
   153  	// Set some test variables
   154  	_ = os.Setenv("DURATION_NAP", "20m")
   155  	_ = os.Setenv("DURATION_EGG", "5m")
   156  	_ = os.Setenv("DURATION_BIG_EGG", "")
   157  	_ = os.Setenv("DURATION_MATCH", "1.5h")
   158  
   159  	// New Config from environment
   160  	cfg := NewConfig()
   161  
   162  	// returns time.Duration
   163  	fmt.Println(cfg.GetDuration("DURATION_NAP"))
   164  	fmt.Println(cfg.GetDuration("DURATION_EGG") * 2)
   165  	// fallback with unset variable
   166  	fmt.Println(cfg.GetDuration("DURATION_POWERNAP", time.Minute*45))
   167  	// or an empty one
   168  	fmt.Println(cfg.GetDuration("DURATION_BIG_EGG", time.Minute*10))
   169  	fmt.Println(cfg.GetDuration("DURATION_MATCH").Minutes())
   170  
   171  	// Output:
   172  	// 20m0s
   173  	// 10m0s
   174  	// 45m0s
   175  	// 10m0s
   176  	// 90
   177  
   178  	unsetEnv(
   179  		"DURATION_NAP",
   180  		"DURATION_EGG",
   181  		"DURATION_BIG_EGG",
   182  		"DURATION_MATCH",
   183  	)
   184  }
   185  
   186  // Strings are parsed using strconv.ParseBool().
   187  func ExampleConfig_GetBool() {
   188  	// Set some test variables
   189  	_ = os.Setenv("LIKE_PEAS", "t")
   190  	_ = os.Setenv("LIKE_CARROTS", "true")
   191  	_ = os.Setenv("LIKE_BEANS", "1")
   192  	_ = os.Setenv("LIKE_LIVER", "f")
   193  	_ = os.Setenv("LIKE_TOMATOES", "0")
   194  	_ = os.Setenv("LIKE_BVB", "false")
   195  	_ = os.Setenv("LIKE_BAYERN", "FALSE")
   196  
   197  	// New Config from environment
   198  	cfg := NewConfig()
   199  
   200  	// strconv.ParseBool() supports many formats
   201  	fmt.Println(cfg.GetBool("LIKE_PEAS"))
   202  	fmt.Println(cfg.GetBool("LIKE_CARROTS"))
   203  	fmt.Println(cfg.GetBool("LIKE_BEANS"))
   204  	fmt.Println(cfg.GetBool("LIKE_LIVER"))
   205  	fmt.Println(cfg.GetBool("LIKE_TOMATOES"))
   206  	fmt.Println(cfg.GetBool("LIKE_BVB"))
   207  	fmt.Println(cfg.GetBool("LIKE_BAYERN"))
   208  
   209  	// Fallback
   210  	fmt.Println(cfg.GetBool("LIKE_BEER", true))
   211  
   212  	// Output:
   213  	// true
   214  	// true
   215  	// true
   216  	// false
   217  	// false
   218  	// false
   219  	// false
   220  	// true
   221  
   222  	unsetEnv(
   223  		"LIKE_PEAS",
   224  		"LIKE_CARROTS",
   225  		"LIKE_BEANS",
   226  		"LIKE_LIVER",
   227  		"LIKE_TOMATOES",
   228  		"LIKE_BVB",
   229  		"LIKE_BAYERN",
   230  	)
   231  }
   232  
   233  func unsetEnv(keys ...string) {
   234  	for _, key := range keys {
   235  		panicOnErr(os.Unsetenv(key))
   236  	}
   237  }