|
|
|
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",
|
|
|
|
"--root=root"
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &Config{
|
|
|
|
Host: "host",
|
|
|
|
Port: "port",
|
|
|
|
DB: "db",
|
|
|
|
Title: "title",
|
|
|
|
APIkey: "apikey",
|
|
|
|
Domain: "domain",
|
|
|
|
RootPath: "root",
|
|
|
|
}
|
|
|
|
|
|
|
|
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("SHRTY_DB_FILE", "db"))
|
|
|
|
defer os.Unsetenv("SHRTY_DB_FILE")
|
|
|
|
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")
|
|
|
|
assert.NoError(t, os.Setenv("SHRTY_ROOT_PATH", "root"))
|
|
|
|
defer os.Unsetenv("SHRTY_ROOT_PATH")
|
|
|
|
|
|
|
|
expected := &Config{
|
|
|
|
Host: "host",
|
|
|
|
Port: "port",
|
|
|
|
DB: "db",
|
|
|
|
Title: "title",
|
|
|
|
APIkey: "apikey",
|
|
|
|
Domain: "domain",
|
|
|
|
RootPath: "root",
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := readConfig(flag.NewFlagSet("", flag.ContinueOnError), []string{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, cfg)
|
|
|
|
}
|