package main import ( "testing" "os" "github.com/stretchr/testify/assert" "flag" ) func TestConfig_ReadConfigDefaults(t *testing.T){ originalArgs := os.Args os.Args = []string{"shrt"} defer func(){ os.Args = originalArgs }() d := DefaultConfig() assert.Equal(t, &d, ReadConfig()) } func TestConfig_ReadConfig(t *testing.T){ input := []string{ "--host=host", "--port=port", "--db-file=db", "--title=title", "--apikey=apikey", "--domain=domain", } expected := &Config{ Host: "host", Port: "port", DB: "db", Title: "title", APIkey: "apikey", Domain: "domain", } cfg, err := readConfig(flag.NewFlagSet("", flag.ContinueOnError), input) assert.NoError(t, err) assert.Equal(t, expected, cfg) } func TestConfig_ReadConfigFromEnv(t *testing.T) { assert.NoError(t, os.Setenv("SHRT_DB_FILE", "db")) defer os.Unsetenv("SHRT_DB_FILE") assert.NoError(t, os.Setenv("SHRT_API_KEY", "apikey")) defer os.Unsetenv("SHRT_API_KEY") assert.NoError(t, os.Setenv("SHRT_HOST", "host")) defer os.Unsetenv("SHRT_HOST") assert.NoError(t, os.Setenv("SHRT_PORT", "port")) defer os.Unsetenv("SHRT_PORT") assert.NoError(t, os.Setenv("SHRT_DOMAIN", "domain")) defer os.Unsetenv("SHRT_DOMAIN") assert.NoError(t, os.Setenv("SHRT_TITLE", "title")) defer os.Unsetenv("SHRT_TITLE") expected := &Config{ Host: "host", Port: "port", DB: "db", Title: "title", APIkey: "apikey", Domain: "domain", } cfg, err := readConfig(flag.NewFlagSet("", flag.ContinueOnError), []string{}) assert.NoError(t, err) assert.Equal(t, expected, cfg) }