fuzzyworld

template / files / internal / core / config_test.go

config_test.go

Blame: the commit that last changed each line · file · history

37160b89 2026-05-01 Pavel Safronov1package core
763c260d 2022-10-06 Pavel Safronov2
3import (
4 "errors"
5 "os"
748e3056 2022-11-19 Pavel Safronov6 "path/filepath"
763c260d 2022-10-06 Pavel Safronov7 "testing"
8
9 "github.com/stretchr/testify/assert"
10)
11
e86c26ae 2022-11-02 Pavel Safronov12// Compares YAML representation of two objects
13func compareObjectYaml(t *testing.T, expected, actual interface{}) {
14 expectedYaml, err := getYaml(expected)
15 assert.Nil(t, err, "Error marshalling expected")
16 actualYaml, err := getYaml(actual)
17 assert.Nil(t, err, "Error marshalling actual")
5e424f1b 2022-11-12 Pavel Safronov18 assert.Equal(t, len(expectedYaml), len(actualYaml), "Expected YAML and actual YAML are different lengths")
c140b8ee 2023-04-11 Pavel Safronov19 //assert.Equal(t, expectedYaml, actualYaml)
e86c26ae 2022-11-02 Pavel Safronov20}
21
763c260d 2022-10-06 Pavel Safronov22func TestSaveLoadConfig(t *testing.T) {
748e3056 2022-11-19 Pavel Safronov23 const configPath string = "./test/test-config.yaml"
24 configDir := filepath.Dir(configPath)
25 err := os.Mkdir(configDir, 0755)
26 if err != nil && !errors.Is(err, os.ErrExist) {
27 assert.Nil(t, err, "Error creating config directory")
28 }
83f12389 2026-01-15 Pavel Safronov29 defer func() { _ = os.Remove(configDir) }()
05f9e8ef 2022-10-06 Pavel Safronov30
763c260d 2022-10-06 Pavel Safronov31 config := defaultConfig()
32
33 // save config
748e3056 2022-11-19 Pavel Safronov34 err = saveConfig(configPath, config)
763c260d 2022-10-06 Pavel Safronov35 assert.Nil(t, err, "Error saving config")
36
37 // load config
38 loadedConfig, err := loadConfig(configPath)
39 assert.Nil(t, err, "Error loading config")
40
41 // test config
e86c26ae 2022-11-02 Pavel Safronov42 compareObjectYaml(t, loadedConfig, config)
763c260d 2022-10-06 Pavel Safronov43
44 // test load non-existent config
9573ca02 2022-11-16 Pavel Safronov45 _, err = loadConfig("./test/non-existent-file.yaml")
763c260d 2022-10-06 Pavel Safronov46 assert.NotNil(t, err, "Expected to fail to load config, but succeeded")
47
48 // test load invalid yaml
748e3056 2022-11-19 Pavel Safronov49 err = os.WriteFile("./invalid-yaml.yaml", []byte("invalid yaml"), 0644)
75a066b2 2022-11-16 Pavel Safronov50 assert.Nil(t, err, "Error writing invalid yaml file")
748e3056 2022-11-19 Pavel Safronov51 _, err = loadConfig("./invalid-yaml.yaml")
763c260d 2022-10-06 Pavel Safronov52 assert.NotNil(t, err, "Expected to fail to parse config, but succeeded")
83f12389 2026-01-15 Pavel Safronov53 err = os.Remove("./invalid-yaml.yaml")
54 assert.Nil(t, err, "Error removing invalid yaml file")
763c260d 2022-10-06 Pavel Safronov55
56 // test saving config to invalid path
57 err = saveConfig("invalid/path/invalid-config.yaml", config)
58 assert.NotNil(t, err, "Expected to fail to save config, but succeeded")
59
60 // remove config
83f12389 2026-01-15 Pavel Safronov61 err = os.Remove(configPath)
62 assert.Nil(t, err, "Error removing config file")
763c260d 2022-10-06 Pavel Safronov63
64 // test default config creation
65 _, err = os.Stat(configPath)
66 assert.True(t, errors.Is(err, os.ErrNotExist), "Expected config file to not exist, but it does")
67
ae89a42a 2022-11-02 Pavel Safronov68 // fail to get config
69 _, err = loadConfig(configPath)
70 assert.NotNil(t, err, "Expected to fail to load config, but succeeded")
71
72 // create default config
73 err = saveConfig(configPath, defaultConfig())
e86c26ae 2022-11-02 Pavel Safronov74 assert.Nil(t, err, "Error saving config")
763c260d 2022-10-06 Pavel Safronov75
76 // test config
e86c26ae 2022-11-02 Pavel Safronov77 compareObjectYaml(t, loadedConfig, config)
763c260d 2022-10-06 Pavel Safronov78
79 // verify config file exists
80 stat1, err := os.Stat(configPath)
81 assert.Nil(t, err, "Expected config file to exist, but it does not")
82
83 // get and create config
ae89a42a 2022-11-02 Pavel Safronov84 loadedConfig, err = loadConfig(configPath)
763c260d 2022-10-06 Pavel Safronov85 assert.Nil(t, err, "Error loading config")
86
87 // test config
e86c26ae 2022-11-02 Pavel Safronov88 compareObjectYaml(t, loadedConfig, config)
763c260d 2022-10-06 Pavel Safronov89
90 // verify config file wasn't updated
91 stat2, err := os.Stat(configPath)
92 assert.Nil(t, err, "Expected config file to exist, but it does not")
93 assert.Equal(t, stat1.ModTime(), stat2.ModTime(), "Expected config file to not be modified, but it was")
94}
95
96func TestValidateDefaultConfig(t *testing.T) {
97 config := defaultConfig()
b0586433 2022-10-21 Pavel Safronov98 err := validateConfig(&config)
763c260d 2022-10-06 Pavel Safronov99 if err != nil {
100 t.Errorf("Expected to validate config, but failed: %v", err)
101 }
102}
103
104func TestValidateConfig(t *testing.T) {
05f9e8ef 2022-10-06 Pavel Safronov105 testFailure := func(config Config, errorText string) {
b0586433 2022-10-21 Pavel Safronov106 err := validateConfig(&config)
763c260d 2022-10-06 Pavel Safronov107 if err == nil {
108 t.Errorf("Expected to fail to validate config, but succeeded")
109 }
05f9e8ef 2022-10-06 Pavel Safronov110 assert.Contains(t, err.Error(), errorText)
763c260d 2022-10-06 Pavel Safronov111 }
112
05f9e8ef 2022-10-06 Pavel Safronov113 testFailure(Config{}, "no profiles found")
114 testFailure(Config{Profiles: []Profile{}}, "no profiles found")
115 testFailure(Config{Profiles: []Profile{
116 {
117 Name: "daily",
118 IsDefault: true,
1a02ef4e 2022-10-21 Pavel Safronov119 NamePatterns: []ParameterizedText{"daily"},
873846b9 2022-12-09 Pavel Safronov120 FileTemplate: "foo",
05f9e8ef 2022-10-06 Pavel Safronov121 },
122 }}, "error validating profile")
763c260d 2022-10-06 Pavel Safronov123 testFailure(Config{Profiles: []Profile{
124 {
873846b9 2022-12-09 Pavel Safronov125 Name: "daily",
126 IsDefault: true,
127 NamePatterns: []ParameterizedText{"daily"},
128 Type: FileType,
129 TargetDirectory: "foo",
130 FileTemplate: "foo",
763c260d 2022-10-06 Pavel Safronov131 },
132 {
873846b9 2022-12-09 Pavel Safronov133 Name: "daily2",
134 IsDefault: true,
135 NamePatterns: []ParameterizedText{"daily"},
136 Type: FileType,
137 TargetDirectory: "foo",
138 FileTemplate: "foo",
763c260d 2022-10-06 Pavel Safronov139 },
05f9e8ef 2022-10-06 Pavel Safronov140 }}, "multiple default profiles found")
9573ca02 2022-11-16 Pavel Safronov141 testFailure(Config{Profiles: []Profile{
142 {
873846b9 2022-12-09 Pavel Safronov143 Name: "daily",
144 IsDefault: true,
145 NamePatterns: []ParameterizedText{"daily"},
146 Type: FileType,
147 TargetDirectory: "foo",
148 FileTemplate: "foo",
9573ca02 2022-11-16 Pavel Safronov149 },
150 {
873846b9 2022-12-09 Pavel Safronov151 Name: "daily",
152 IsDefault: true,
153 NamePatterns: []ParameterizedText{"daily"},
154 Type: FileType,
155 TargetDirectory: "foo",
156 FileTemplate: "foo",
9573ca02 2022-11-16 Pavel Safronov157 },
158 }}, "duplicate profile name 'daily'")
159}
160
161func TestLoadConfig(t *testing.T) {
162 bytes := []byte(`
163profiles:
164 - name:
165`)
748e3056 2022-11-19 Pavel Safronov166 err := os.WriteFile("./invalid-config.yaml", bytes, 0644)
75a066b2 2022-11-16 Pavel Safronov167 assert.Nil(t, err, "Error writing invalid config file")
83f12389 2026-01-15 Pavel Safronov168 defer func() { _ = os.Remove("./invalid-config.yaml") }()
9573ca02 2022-11-16 Pavel Safronov169
170 // load config
748e3056 2022-11-19 Pavel Safronov171 _, err = loadConfig("./invalid-config.yaml")
9573ca02 2022-11-16 Pavel Safronov172 assert.NotNil(t, err, "Expected to fail to load config, but succeeded")
873846b9 2022-12-09 Pavel Safronov173 assert.Contains(t, err.Error(), "error validating profile : profile name is required")
9573ca02 2022-11-16 Pavel Safronov174}
175
176func TestSaveConfig(t *testing.T) {
177 config := Config{Profiles: []Profile{}}
748e3056 2022-11-19 Pavel Safronov178 err := saveConfig("./invalid-config.yaml", config)
9573ca02 2022-11-16 Pavel Safronov179 assert.NotNil(t, err, "Expected to fail to save config, but succeeded")
180 assert.Contains(t, err.Error(), "no profiles found")
181}
182
183func TestMergeConfigs(t *testing.T) {
184 defaultConfig := defaultConfig()
185 testSuccess := func(config1 Config, config2 Config) Config {
186 config, err := mergeConfigs(config1, config2)
187 // compareObjectYaml(t, mergedConfig, expected)
188 assert.Nil(t, err, "Expected to merge configs, but failed")
189 return config
190 }
191 testFailure := func(config1 Config, config2 Config, errorText string) {
192 _, err := mergeConfigs(config1, config2)
193 assert.NotNil(t, err, "Expected to fail to merge configs, but succeeded")
194 assert.Contains(t, err.Error(), errorText)
195 }
196 testFailure(Config{}, Config{}, "no profiles found")
873846b9 2022-12-09 Pavel Safronov197 testFailure(Config{Profiles: []Profile{{}}}, Config{}, "error validating config a: error validating profile : profile name is required")
9573ca02 2022-11-16 Pavel Safronov198 testFailure(Config{Profiles: []Profile{defaultConfig.Profiles[0]}}, Config{}, "error validating config b: no profiles found")
199
200 profile1 := defaultConfig.Profiles[0]
201 profile2 := defaultConfig.Profiles[1]
202 testSuccess(Config{Profiles: []Profile{profile1}}, Config{Profiles: []Profile{profile2}})
203
204 profile1_updated := profile1
205 profile1_updated.Description += " This profile has been updated!"
206
207 result := testSuccess(Config{Profiles: []Profile{profile1}}, Config{Profiles: []Profile{profile1_updated}})
208 assert.Equal(t, 1, len(result.Profiles))
209 assert.Equal(t, profile1_updated.Name, result.Profiles[0].Name)
763c260d 2022-10-06 Pavel Safronov210}