You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.6 KiB
Go

package main
import (
"testing"
"os"
"github.com/stretchr/testify/assert"
"flag"
)
func TestConfig_ReadConfigDefaults(t *testing.T){
originalArgs := os.Args
os.Args = []string{"shrty"}
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_ROOT_PATH", "root"))
defer os.Unsetenv("SHRTY_ROOT_PATH")
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")
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)
}
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)
}