aboutsummaryrefslogtreecommitdiffstats
path: root/config
diff options
context:
space:
mode:
Diffstat (limited to 'config')
-rw-r--r--config/config.go26
-rw-r--r--config/config_test.go126
2 files changed, 141 insertions, 11 deletions
diff --git a/config/config.go b/config/config.go
index f9dd386..32e4b07 100644
--- a/config/config.go
+++ b/config/config.go
@@ -1,9 +1,9 @@
package config
import (
- "gopkg.in/yaml.v2"
"io/ioutil"
- "log"
+
+ "gopkg.in/yaml.v2"
)
type Settings struct {
@@ -16,22 +16,26 @@ type Settings struct {
var Config Settings
-func Init(filename string) {
+func Init(filename string) error {
if filename != "" {
- readConfig(filename)
+ if err := readConfig(filename); err != nil {
+ return err
+ }
}
addDefaults()
+ return nil
}
-func readConfig(filename string) {
- file, e := ioutil.ReadFile(filename)
- if e != nil {
- log.Fatal("Can not read config file\n", e)
+func readConfig(filename string) error {
+ file, err := ioutil.ReadFile(filename)
+ if err != nil {
+ return err
}
- e = yaml.Unmarshal(file, &Config)
- if e != nil {
- log.Fatal("Config read error\n", e)
+ err = yaml.Unmarshal(file, &Config)
+ if err != nil {
+ return err
}
+ return nil
}
func addDefaults() {
diff --git a/config/config_test.go b/config/config_test.go
new file mode 100644
index 0000000..0e700f2
--- /dev/null
+++ b/config/config_test.go
@@ -0,0 +1,126 @@
+package config
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func TestInitEmpty(t *testing.T) {
+ // Reset global
+ Config = Settings{}
+
+ err := Init("")
+ if err != nil {
+ t.Fatalf("Init with empty string should not error: %v", err)
+ }
+
+ // Defaults should be set
+ if Config.DBFile != "neko.db" {
+ t.Errorf("expected default DBFile 'neko.db', got %q", Config.DBFile)
+ }
+ if Config.Port != 4994 {
+ t.Errorf("expected default Port 4994, got %d", Config.Port)
+ }
+ if Config.CrawlMinutes != 60 {
+ t.Errorf("expected default CrawlMinutes 60, got %d", Config.CrawlMinutes)
+ }
+}
+
+func TestInitWithValidFile(t *testing.T) {
+ Config = Settings{}
+
+ dir := t.TempDir()
+ configPath := filepath.Join(dir, "config.yaml")
+ content := []byte("database: test.db\nhttp: 8080\npassword: secret\nminutes: 30\nimageproxy: true\n")
+ if err := os.WriteFile(configPath, content, 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ err := Init(configPath)
+ if err != nil {
+ t.Fatalf("Init should not error with valid file: %v", err)
+ }
+
+ if Config.DBFile != "test.db" {
+ t.Errorf("expected DBFile 'test.db', got %q", Config.DBFile)
+ }
+ if Config.Port != 8080 {
+ t.Errorf("expected Port 8080, got %d", Config.Port)
+ }
+ if Config.DigestPassword != "secret" {
+ t.Errorf("expected password 'secret', got %q", Config.DigestPassword)
+ }
+ if Config.CrawlMinutes != 30 {
+ t.Errorf("expected CrawlMinutes 30, got %d", Config.CrawlMinutes)
+ }
+ if !Config.ProxyImages {
+ t.Error("expected ProxyImages true")
+ }
+}
+
+func TestInitWithMissingFile(t *testing.T) {
+ Config = Settings{}
+ err := Init("/nonexistent/config.yaml")
+ if err == nil {
+ t.Fatal("Init with missing file should return error")
+ }
+}
+
+func TestInitWithInvalidYAML(t *testing.T) {
+ Config = Settings{}
+
+ dir := t.TempDir()
+ configPath := filepath.Join(dir, "bad.yaml")
+ content := []byte("{{{{not valid yaml at all")
+ if err := os.WriteFile(configPath, content, 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ err := Init(configPath)
+ if err == nil {
+ t.Fatal("Init with invalid YAML should return error")
+ }
+}
+
+func TestAddDefaultsNoOverride(t *testing.T) {
+ // When values are already set, addDefaults should not overwrite
+ Config = Settings{
+ DBFile: "custom.db",
+ Port: 9999,
+ CrawlMinutes: 120,
+ }
+ addDefaults()
+
+ if Config.DBFile != "custom.db" {
+ t.Errorf("addDefaults should not override DBFile, got %q", Config.DBFile)
+ }
+ if Config.Port != 9999 {
+ t.Errorf("addDefaults should not override Port, got %d", Config.Port)
+ }
+ if Config.CrawlMinutes != 120 {
+ t.Errorf("addDefaults should not override CrawlMinutes, got %d", Config.CrawlMinutes)
+ }
+}
+
+func TestReadConfigValid(t *testing.T) {
+ Config = Settings{}
+
+ dir := t.TempDir()
+ configPath := filepath.Join(dir, "config.yaml")
+ content := []byte("database: mydb.db\nhttp: 5000\n")
+ if err := os.WriteFile(configPath, content, 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ err := readConfig(configPath)
+ if err != nil {
+ t.Fatalf("readConfig should not error: %v", err)
+ }
+ if Config.DBFile != "mydb.db" {
+ t.Errorf("expected DBFile 'mydb.db', got %q", Config.DBFile)
+ }
+ if Config.Port != 5000 {
+ t.Errorf("expected Port 5000, got %d", Config.Port)
+ }
+}