|
|
@ -9,7 +9,7 @@ import (
|
|
|
|
|
|
|
|
|
|
|
|
func TestConfig_ReadConfigDefaults(t *testing.T){
|
|
|
|
func TestConfig_ReadConfigDefaults(t *testing.T){
|
|
|
|
originalArgs := os.Args
|
|
|
|
originalArgs := os.Args
|
|
|
|
os.Args = []string{"shrt"}
|
|
|
|
os.Args = []string{"shrty"}
|
|
|
|
defer func(){ os.Args = originalArgs }()
|
|
|
|
defer func(){ os.Args = originalArgs }()
|
|
|
|
|
|
|
|
|
|
|
|
d := DefaultConfig()
|
|
|
|
d := DefaultConfig()
|
|
|
@ -43,6 +43,8 @@ func TestConfig_ReadConfig(t *testing.T){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestConfig_ReadConfigFromEnv(t *testing.T) {
|
|
|
|
func TestConfig_ReadConfigFromEnv(t *testing.T) {
|
|
|
|
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_ROOT_PATH", "root"))
|
|
|
|
|
|
|
|
defer os.Unsetenv("SHRTY_ROOT_PATH")
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_DB_FILE", "db"))
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_DB_FILE", "db"))
|
|
|
|
defer os.Unsetenv("SHRTY_DB_FILE")
|
|
|
|
defer os.Unsetenv("SHRTY_DB_FILE")
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_API_KEY", "apikey"))
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_API_KEY", "apikey"))
|
|
|
@ -55,8 +57,6 @@ func TestConfig_ReadConfigFromEnv(t *testing.T) {
|
|
|
|
defer os.Unsetenv("SHRTY_DOMAIN")
|
|
|
|
defer os.Unsetenv("SHRTY_DOMAIN")
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_TITLE", "title"))
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_TITLE", "title"))
|
|
|
|
defer os.Unsetenv("SHRTY_TITLE")
|
|
|
|
defer os.Unsetenv("SHRTY_TITLE")
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_ROOT_PATH", "root"))
|
|
|
|
|
|
|
|
defer os.Unsetenv("SHRTY_ROOT_PATH")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expected := &Config{
|
|
|
|
expected := &Config{
|
|
|
|
Host: "host",
|
|
|
|
Host: "host",
|
|
|
@ -72,3 +72,33 @@ func TestConfig_ReadConfigFromEnv(t *testing.T) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, cfg)
|
|
|
|
assert.Equal(t, expected, cfg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestConfig_ReadConfigWithRootpath(t *testing.T) {
|
|
|
|
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_ROOT_PATH", "root"))
|
|
|
|
|
|
|
|
defer os.Unsetenv("SHRTY_ROOT_PATH")
|
|
|
|
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_API_KEY", "apikey"))
|
|
|
|
|
|
|
|
defer os.Unsetenv("SHRTY_API_KEY")
|
|
|
|
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_HOST", "host"))
|
|
|
|
|
|
|
|
defer os.Unsetenv("SHRTY_HOST")
|
|
|
|
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_PORT", "port"))
|
|
|
|
|
|
|
|
defer os.Unsetenv("SHRTY_PORT")
|
|
|
|
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_DOMAIN", "domain"))
|
|
|
|
|
|
|
|
defer os.Unsetenv("SHRTY_DOMAIN")
|
|
|
|
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_TITLE", "title"))
|
|
|
|
|
|
|
|
defer os.Unsetenv("SHRTY_TITLE")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expected := &Config{
|
|
|
|
|
|
|
|
RootPath:"root",
|
|
|
|
|
|
|
|
Host: "host",
|
|
|
|
|
|
|
|
Port: "port",
|
|
|
|
|
|
|
|
Title: "title",
|
|
|
|
|
|
|
|
APIkey: "apikey",
|
|
|
|
|
|
|
|
Domain: "domain",
|
|
|
|
|
|
|
|
DB: "root/shrty.db",
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cfg, err := readConfig(flag.NewFlagSet("", flag.ContinueOnError), []string{})
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, expected, cfg)
|
|
|
|
|
|
|
|
}
|