Skip to content

Commit 344abfa

Browse files
committed
Add config tests
1 parent 42bb8a5 commit 344abfa

File tree

4 files changed

+222
-10
lines changed

4 files changed

+222
-10
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ require (
1111
github.com/mattn/go-sqlite3 v1.14.24
1212
github.com/nats-io/nats-server/v2 v2.11.0
1313
github.com/nats-io/nats.go v1.39.1
14-
github.com/rqlite/gorqlite v0.0.0-20250128004930-114c7828b55a
15-
github.com/stretchr/testify v1.6.1
14+
github.com/stretchr/testify v1.10.0
1615
github.com/urfave/cli/v2 v2.27.5
1716
github.com/vmihailenco/msgpack/v5 v5.4.1
1817
)

go.sum

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,12 @@ github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
4242
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
4343
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4444
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
45-
github.com/rqlite/gorqlite v0.0.0-20250128004930-114c7828b55a h1:9O8zgGrMBuTsnA3yyFd+JWhFSflQwzSUEB4AMnFHKhU=
46-
github.com/rqlite/gorqlite v0.0.0-20250128004930-114c7828b55a/go.mod h1:xF/KoXmrRyahPfo5L7Szb5cAAUl53dMWBh9cMruGEZg=
4745
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
4846
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
4947
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5048
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
51-
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
52-
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
49+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
50+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
5351
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
5452
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
5553
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
@@ -70,6 +68,5 @@ golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0=
7068
golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
7169
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
7270
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
73-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7471
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
7572
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ func (c Config) NormalizePath(file string) string {
6363
}
6464

6565
// DefaultConfig returns a configuration with default values
66-
func DefaultConfig() Config {
66+
func DefaultConfig() *Config {
6767
cdir, err := os.UserHomeDir()
6868
if err != nil {
6969
panic(err)
7070
}
7171

72-
return Config{
72+
return &Config{
7373
Path: filepath.Join(cdir, "config.toml"),
7474
Main: MainConfig{
7575
NatsServerURL: "http://localhost:4222",
@@ -108,7 +108,7 @@ func LoadConfig(path string) (*Config, error) {
108108
config.Main.CACert = config.NormalizePath(config.Main.CACert)
109109
config.Store.DBPath = config.NormalizePath(config.Store.DBPath)
110110

111-
return &config, nil
111+
return config, nil
112112
}
113113

114114
func LoadConfigFromCLI(ctx *cli.Context) (*Config, error) {

internal/config/config_test.go

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package config_test
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/BurntSushi/toml"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/urfave/cli/v2"
11+
12+
"github.com/rubiojr/hashup/internal/config"
13+
)
14+
15+
func TestDefaultConfig(t *testing.T) {
16+
cfg := config.DefaultConfig()
17+
18+
homeDir, _ := os.UserHomeDir()
19+
expectedDBPath := filepath.Join(homeDir, ".local", "share", "hashup", "hashup.db")
20+
expectedCachePath := filepath.Join(homeDir, ".cache", "hashup", "cache")
21+
22+
assert.Equal(t, "http://localhost:4222", cfg.Main.NatsServerURL)
23+
assert.Equal(t, "HASHUP", cfg.Main.NatsStream)
24+
assert.Equal(t, "FILES", cfg.Main.NatsSubject)
25+
assert.Equal(t, 30, cfg.Store.StatsInterval)
26+
assert.Equal(t, expectedDBPath, cfg.Store.DBPath)
27+
assert.Equal(t, 3600, cfg.Scanner.ScanningInterval)
28+
assert.Equal(t, 5, cfg.Scanner.ScanningConcurrency)
29+
assert.Equal(t, expectedCachePath, cfg.Scanner.CachePath)
30+
}
31+
32+
func TestNormalizePath(t *testing.T) {
33+
homeDir, _ := os.UserHomeDir()
34+
cfg := config.Config{Path: "/some/config/path/config.toml"}
35+
36+
tests := []struct {
37+
name string
38+
input string
39+
expected string
40+
}{
41+
{"Empty path", "", ""},
42+
{"Relative path", "relative/path", "/some/config/path/relative/path"},
43+
{"Absolute path", "/absolute/path", "/absolute/path"},
44+
{"Home tilde path", "~/something", filepath.Join(homeDir, "something")},
45+
}
46+
47+
for _, tt := range tests {
48+
t.Run(tt.name, func(t *testing.T) {
49+
result := cfg.NormalizePath(tt.input)
50+
assert.Equal(t, tt.expected, result)
51+
})
52+
}
53+
}
54+
55+
func TestLoadConfig(t *testing.T) {
56+
// Create a temporary config file
57+
tempDir := t.TempDir()
58+
configPath := filepath.Join(tempDir, "test_config.toml")
59+
60+
testCfg := config.DefaultConfig()
61+
testCfg.Main.NatsServerURL = "nats://testserver:4222"
62+
testCfg.Store.StatsInterval = 60
63+
64+
f, err := os.Create(configPath)
65+
assert.NoError(t, err)
66+
defer f.Close()
67+
68+
err = toml.NewEncoder(f).Encode(testCfg)
69+
assert.NoError(t, err)
70+
f.Close()
71+
72+
// Test loading the config
73+
cfg, err := config.LoadConfig(configPath)
74+
assert.NoError(t, err)
75+
assert.Equal(t, "nats://testserver:4222", cfg.Main.NatsServerURL)
76+
assert.Equal(t, 60, cfg.Store.StatsInterval)
77+
78+
// Test loading non-existent config
79+
_, err = config.LoadConfig(filepath.Join(tempDir, "nonexistent.toml"))
80+
assert.Error(t, err)
81+
assert.Contains(t, err.Error(), "config file not found")
82+
}
83+
84+
func TestSaveConfig(t *testing.T) {
85+
tempDir := t.TempDir()
86+
configPath := filepath.Join(tempDir, "config.toml")
87+
88+
cfg := config.DefaultConfig()
89+
cfg.Main.NatsServerURL = "nats://customserver:4222"
90+
91+
err := config.SaveConfig(cfg, configPath)
92+
assert.NoError(t, err)
93+
94+
// Verify saved config can be loaded correctly
95+
loadedCfg, err := config.LoadConfig(configPath)
96+
assert.NoError(t, err)
97+
assert.Equal(t, "nats://customserver:4222", loadedCfg.Main.NatsServerURL)
98+
}
99+
100+
func TestLoadConfigFromCLI(t *testing.T) {
101+
t.Skip("broken")
102+
tempDir := t.TempDir()
103+
configPath := filepath.Join(tempDir, "cli_config.toml")
104+
105+
// Create a basic config file
106+
basicCfg := config.DefaultConfig()
107+
err := config.SaveConfig(basicCfg, configPath)
108+
assert.NoError(t, err)
109+
110+
tests := []struct {
111+
name string
112+
args []string
113+
validate func(*testing.T, *config.Config)
114+
}{
115+
{
116+
name: "Custom config file",
117+
args: []string{"--config", configPath},
118+
validate: func(t *testing.T, cfg *config.Config) {
119+
assert.Equal(t, configPath, cfg.Path)
120+
},
121+
},
122+
{
123+
name: "Custom encryption key",
124+
args: []string{"--config", configPath, "--encryption-key", "testkey123"},
125+
validate: func(t *testing.T, cfg *config.Config) {
126+
assert.Equal(t, "testkey123", cfg.Main.EncryptionKey)
127+
},
128+
},
129+
{
130+
name: "Custom DB path",
131+
args: []string{"--config", configPath, "--db-path", "/custom/db/path"},
132+
validate: func(t *testing.T, cfg *config.Config) {
133+
assert.Equal(t, "/custom/db/path", cfg.Store.DBPath)
134+
},
135+
},
136+
{
137+
name: "Custom stats interval",
138+
args: []string{"--config", configPath, "--stats-interval", "120"},
139+
validate: func(t *testing.T, cfg *config.Config) {
140+
assert.Equal(t, 120, cfg.Store.StatsInterval)
141+
},
142+
},
143+
{
144+
name: "Custom NATS URL",
145+
args: []string{"--config", configPath, "--nats-url", "nats://custom:4222"},
146+
validate: func(t *testing.T, cfg *config.Config) {
147+
assert.Equal(t, "nats://custom:4222", cfg.Main.NatsServerURL)
148+
},
149+
},
150+
}
151+
152+
for _, tt := range tests {
153+
t.Run(tt.name, func(t *testing.T) {
154+
app := cli.NewApp()
155+
ctx := cli.NewContext(app, nil, nil)
156+
157+
// Set up flags
158+
flags := map[string]any{
159+
"config": configPath,
160+
"encryption-key": "",
161+
"db-path": "",
162+
"stats-interval": 0,
163+
"nats-url": "",
164+
"stream": "",
165+
"client-cert": "",
166+
"client-key": "",
167+
"ca-cert": "",
168+
}
169+
170+
// Apply command line args
171+
for i := 0; i < len(tt.args); i += 2 {
172+
if i+1 < len(tt.args) {
173+
flagName := tt.args[i][2:] // Remove --
174+
flagValue := tt.args[i+1]
175+
flags[flagName] = flagValue
176+
}
177+
}
178+
179+
// Set values in the context
180+
for name, value := range flags {
181+
switch v := value.(type) {
182+
case string:
183+
ctx.Set(name, v)
184+
case int:
185+
if v != 0 {
186+
ctx.Set(name, string(rune(v)))
187+
}
188+
}
189+
}
190+
191+
cfg, err := config.LoadConfigFromCLI(ctx)
192+
assert.NoError(t, err)
193+
tt.validate(t, cfg)
194+
})
195+
}
196+
}
197+
198+
func TestDefaultPaths(t *testing.T) {
199+
homeDir, _ := os.UserHomeDir()
200+
201+
configDir, err := config.DefaultConfigDir()
202+
assert.NoError(t, err)
203+
assert.Equal(t, filepath.Join(homeDir, ".config", "hashup"), configDir)
204+
205+
dbPath := config.DefaultDBPath()
206+
assert.Equal(t, filepath.Join(homeDir, ".local", "share", "hashup", "hashup.db"), dbPath)
207+
208+
dbDir := config.DefaultDBDir()
209+
assert.Equal(t, filepath.Join(homeDir, ".local", "share", "hashup"), dbDir)
210+
211+
natsDir := config.DefaultNATSDataDir()
212+
assert.Equal(t, filepath.Join(homeDir, ".local", "share", "hashup", "nats"), natsDir)
213+
214+
cachePath := config.DefaultCachePath()
215+
assert.Equal(t, filepath.Join(homeDir, ".cache", "hashup", "cache"), cachePath)
216+
}

0 commit comments

Comments
 (0)