Skip to content

Commit 9cb59e0

Browse files
Added config constraints & Created config package tests
1 parent 99804e7 commit 9cb59e0

File tree

7 files changed

+149
-11
lines changed

7 files changed

+149
-11
lines changed

cmd/updater/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type body struct {
1818
}
1919

2020
func main() {
21-
config, err := config.Load()
21+
config, err := config.Load("")
2222
if err != nil {
2323
log.Fatalf("Unable to load config: %s\n", err)
2424
}

cmd/watcher/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type updateRequestBody struct {
2020
}
2121

2222
func main() {
23-
config, err := config.Load()
23+
config, err := config.Load("")
2424
if err != nil {
2525
log.Fatalf("Unable to load config: %s\n", err)
2626
} else {

config/.env.test_default

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
NOTION_API_SECRET=65s$ye6&NWnu@dK%*utFd*dDa
2+
NOTION_PAGE_ID=Sob@3JRoXrB&#ffyXTkVCV9NY
3+
IGDB_CLIENT_ID=gqMAXshTpzhbfcT7$b$U7#mGh
4+
IGDB_SECRET=QurU4ihv7Sf7!WijUJ3s&gBa6
5+
UPDATER_HOST=
6+
UPDATER_PORT=
7+
WATCHER_TICK_DELAY=

config/.env.test_invalid

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
NOTION_API_SECRET=
2+
NOTION_PAGE_ID=
3+
IGDB_CLIENT_ID=
4+
IGDB_SECRET=
5+
UPDATER_HOST=
6+
UPDATER_PORT=
7+
WATCHER_TICK_DELAY=

config/.env.test_success

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
NOTION_API_SECRET=u3M3sAdjqkLm8JSJihaGu3GfscAGE2
2+
NOTION_PAGE_ID=sEQCpMkvVCRfd4JAzVjmsd4EGYCFNC
3+
IGDB_CLIENT_ID=pUnDQo4LzgjrYifQR5W7cAeaxACx92
4+
IGDB_SECRET=WjMpHfNk6Mt4b4FV8TFT2pDiwiUeKu
5+
UPDATER_HOST=76.388.29.99
6+
UPDATER_PORT=7654
7+
WATCHER_TICK_DELAY=99

config/config.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,24 @@ import (
99

1010
// Config holds all configurations loaded from .env file
1111
type Config struct {
12-
NotionAPISecret string `env:"NOTION_API_SECRET,required"`
13-
NotionPageID string `env:"NOTION_PAGE_ID,required"`
14-
IGDBClientID string `env:"IGDB_CLIENT_ID,required"`
15-
IGDBSecret string `env:"IGDB_SECRET,required"`
16-
UpdaterHost string `env:"UPDATER_HOST,required"`
17-
UpdaterPort int `env:"UPDATER_PORT,required"`
18-
WatcherTickDelay int `env:"WATCHER_TICK_DELAY,required"`
12+
NotionAPISecret string `env:"NOTION_API_SECRET,required,notEmpty"`
13+
NotionPageID string `env:"NOTION_PAGE_ID,required,notEmpty"`
14+
IGDBClientID string `env:"IGDB_CLIENT_ID,required,notEmpty"`
15+
IGDBSecret string `env:"IGDB_SECRET,required,notEmpty"`
16+
UpdaterHost string `env:"UPDATER_HOST,required" envDefault:"127.0.0.1"`
17+
UpdaterPort int `env:"UPDATER_PORT,required" envDefault:"8080"`
18+
WatcherTickDelay int `env:"WATCHER_TICK_DELAY,required" envDefault:"5"`
1919
}
2020

2121
// Load all configs from .env & returns the values (+ error if needed)
22-
func Load() (Config, error) {
22+
func Load(file string) (Config, error) {
2323
config := Config{}
2424

25-
err := godotenv.Load()
25+
if file == "" {
26+
file = ".env"
27+
}
28+
29+
err := godotenv.Load(file)
2630
if err != nil {
2731
return config, err
2832
}
@@ -35,6 +39,7 @@ func Load() (Config, error) {
3539
return config, nil
3640
}
3741

42+
// UpdaterURL returns URL to reach the updater process
3843
func (c *Config) UpdaterURL() string {
3944
return fmt.Sprintf("http://%s:%d/", c.UpdaterHost, c.UpdaterPort)
4045
}

config/config_test.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestLoadNotFoundFile(t *testing.T) {
11+
os.Clearenv()
12+
_, err := Load(".env.this_file_does_not_exist")
13+
if err == nil {
14+
t.Errorf("Unexpected Load() success: expected:failure, got:success")
15+
}
16+
}
17+
18+
func TestLoadErrors(t *testing.T) {
19+
expectedErrors := [4]string{
20+
`"NOTION_API_SECRET" should not be empty`,
21+
`"NOTION_PAGE_ID" should not be empty`,
22+
`"IGDB_CLIENT_ID" should not be empty`,
23+
`"IGDB_SECRET" should not be empty`,
24+
}
25+
26+
os.Clearenv()
27+
_, err := Load(".env.test_invalid")
28+
29+
for _, expected := range expectedErrors {
30+
if !strings.Contains(err.Error(), expected) {
31+
t.Errorf("Expected to find error '%s'", expected)
32+
}
33+
}
34+
}
35+
36+
func TestLoadDefaultValues(t *testing.T) {
37+
os.Clearenv()
38+
config, err := Load(".env.test_default")
39+
if err != nil {
40+
t.Errorf("Unexpected Load() error:'%s'", err)
41+
}
42+
43+
if assertConfig("UpdaterHost", config.UpdaterHost, "127.0.0.1") != nil {
44+
t.Error(err)
45+
}
46+
47+
if assertConfig("UpdaterPort", config.UpdaterPort, 8080) != nil {
48+
t.Error(err)
49+
}
50+
51+
if assertConfig("WatcherTickDelay", config.WatcherTickDelay, 5) != nil {
52+
t.Error(err)
53+
}
54+
}
55+
56+
func TestLoadSuccess(t *testing.T) {
57+
os.Clearenv()
58+
config, err := Load(".env.test_success")
59+
if err != nil {
60+
t.Errorf("Unexpected Load() error:'%s'", err)
61+
}
62+
63+
if assertConfig("NotionAPISecret", config.NotionAPISecret, "u3M3sAdjqkLm8JSJihaGu3GfscAGE2") != nil {
64+
t.Error(err)
65+
}
66+
67+
if assertConfig("NotionPageID", config.NotionPageID, "sEQCpMkvVCRfd4JAzVjmsd4EGYCFNC") != nil {
68+
t.Error(err)
69+
}
70+
71+
if assertConfig("IGDBClientID", config.IGDBClientID, "pUnDQo4LzgjrYifQR5W7cAeaxACx92") != nil {
72+
t.Error(err)
73+
}
74+
75+
if assertConfig("IGDBSecret", config.IGDBSecret, "WjMpHfNk6Mt4b4FV8TFT2pDiwiUeKu") != nil {
76+
t.Error(err)
77+
}
78+
79+
if assertConfig("UpdaterHost", config.UpdaterHost, "76.388.29.99") != nil {
80+
t.Error(err)
81+
}
82+
83+
if assertConfig("UpdaterPort", config.UpdaterPort, 7654) != nil {
84+
t.Error(err)
85+
}
86+
87+
if assertConfig("WatcherTickDelay", config.WatcherTickDelay, 99) != nil {
88+
t.Error(err)
89+
}
90+
}
91+
92+
func TestUpdaterURL(t *testing.T) {
93+
config := Config{
94+
UpdaterHost: "172.16.4.97",
95+
UpdaterPort: 9999,
96+
}
97+
98+
result := config.UpdaterURL()
99+
expected := "http://172.16.4.97:9999/"
100+
101+
if result != expected {
102+
t.Errorf("Unexpected UpdaterURL: expected:%s, got:%s", expected, result)
103+
}
104+
}
105+
106+
func assertConfig(key string, value any, expected any) (err error) {
107+
if value != expected {
108+
err = fmt.Errorf("Unexpected %s value: expected:%v, got:%v", key, expected, value)
109+
}
110+
111+
return
112+
}

0 commit comments

Comments
 (0)