2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-06-09 16:27:47 +08:00
|
|
|
"path/filepath"
|
2018-05-27 22:40:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseConfigBasics(t *testing.T) {
|
2018-06-08 01:47:23 +08:00
|
|
|
os.Setenv("COMMENTO_ORIGIN", "https://commento.io")
|
2018-05-27 22:40:42 +08:00
|
|
|
|
|
|
|
if err := parseConfig(); err != nil {
|
|
|
|
t.Errorf("unexpected error when parsing config: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test feels kinda stupid, but whatever.
|
|
|
|
if os.Getenv("PORT") != "8080" {
|
|
|
|
t.Errorf("expected PORT=8080, but PORT=%s instead", os.Getenv("PORT"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-08 01:47:23 +08:00
|
|
|
os.Setenv("COMMENTO_PORT", "1886")
|
2018-05-27 22:40:42 +08:00
|
|
|
|
|
|
|
if err := parseConfig(); err != nil {
|
|
|
|
t.Errorf("unexpected error when parsing config: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("PORT") != "1886" {
|
|
|
|
t.Errorf("expected PORT=1886, but PORT=%s instead", os.Getenv("PORT"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-06-09 16:27:47 +08:00
|
|
|
|
|
|
|
func TestParseConfigStatic(t *testing.T) {
|
|
|
|
os.Setenv("COMMENTO_ORIGIN", "https://commento.io")
|
|
|
|
|
|
|
|
if err := parseConfig(); err != nil {
|
|
|
|
t.Errorf("unexpected error when parsing config: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
binPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot load binary path: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("STATIC") != binPath {
|
|
|
|
t.Errorf("COMMENTO_STATIC != %s when unset", binPath)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Setenv("COMMENTO_STATIC", "/usr/")
|
|
|
|
|
|
|
|
if err := parseConfig(); err != nil {
|
|
|
|
t.Errorf("unexpected error when parsing config: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("STATIC") != "/usr" {
|
|
|
|
t.Errorf("COMMENTO_STATIC != /usr when unset")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseConfigStaticDNE(t *testing.T) {
|
|
|
|
os.Setenv("COMMENTO_ORIGIN", "https://commento.io")
|
|
|
|
os.Setenv("COMMENTO_STATIC", "/does/not/exist/surely/")
|
|
|
|
|
|
|
|
if err := parseConfig(); err == nil {
|
|
|
|
t.Errorf("expected error not found when a non-existant directory is used")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseConfigStaticNotADirectory(t *testing.T) {
|
|
|
|
os.Setenv("COMMENTO_ORIGIN", "https://commento.io")
|
|
|
|
os.Setenv("COMMENTO_STATIC", os.Args[0])
|
|
|
|
|
|
|
|
if err := parseConfig(); err != errorNotADirectory {
|
|
|
|
t.Errorf("expected error not found when a file is used")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|