fuzzyworld

template / windows / files / config_test.go

Branch windows · default branch · all branches

config_test.go

6.3 KB · history · blame

1package main
2
3import (
4 "errors"
5 "os"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9)
10
11const configPath string = "./test/test-config.yaml"
12
13// Compares YAML representation of two objects
14func compareObjectYaml(t *testing.T, expected, actual interface{}) {
15 expectedYaml, err := getYaml(expected)
16 assert.Nil(t, err, "Error marshalling expected")
17 actualYaml, err := getYaml(actual)
18 assert.Nil(t, err, "Error marshalling actual")
19 assert.Equal(t, len(expectedYaml), len(actualYaml), "Expected YAML and actual YAML are different lengths")
20 assert.Equal(t, expectedYaml, actualYaml)
21}
22
23func TestSaveLoadConfig(t *testing.T) {
24
25 config := defaultConfig()
26
27 // save config
28 err := saveConfig(configPath, config)
29 assert.Nil(t, err, "Error saving config")
30
31 // load config
32 loadedConfig, err := loadConfig(configPath)
33 assert.Nil(t, err, "Error loading config")
34
35 // test config
36 compareObjectYaml(t, loadedConfig, config)
37
38 // test load non-existent config
39 _, err = loadConfig("./test/non-existent-file.yaml")
40 assert.NotNil(t, err, "Expected to fail to load config, but succeeded")
41
42 // test load invalid yaml
43 err = os.WriteFile("./test/invalid-yaml.yaml", []byte("invalid yaml"), 0644)
44 assert.Nil(t, err, "Error writing invalid yaml file")
45 _, err = loadConfig("./test/invalid-yaml.yaml")
46 assert.NotNil(t, err, "Expected to fail to parse config, but succeeded")
47
48 // test saving config to invalid path
49 err = saveConfig("invalid/path/invalid-config.yaml", config)
50 assert.NotNil(t, err, "Expected to fail to save config, but succeeded")
51
52 // remove config
53 os.Remove(configPath)
54
55 // test default config creation
56 _, err = os.Stat(configPath)
57 assert.True(t, errors.Is(err, os.ErrNotExist), "Expected config file to not exist, but it does")
58
59 // fail to get config
60 _, err = loadConfig(configPath)
61 assert.NotNil(t, err, "Expected to fail to load config, but succeeded")
62
63 // create default config
64 err = saveConfig(configPath, defaultConfig())
65 assert.Nil(t, err, "Error saving config")
66
67 // test config
68 compareObjectYaml(t, loadedConfig, config)
69
70 // verify config file exists
71 stat1, err := os.Stat(configPath)
72 assert.Nil(t, err, "Expected config file to exist, but it does not")
73
74 // get and create config
75 loadedConfig, err = loadConfig(configPath)
76 assert.Nil(t, err, "Error loading config")
77
78 // test config
79 compareObjectYaml(t, loadedConfig, config)
80
81 // verify config file wasn't updated
82 stat2, err := os.Stat(configPath)
83 assert.Nil(t, err, "Expected config file to exist, but it does not")
84 assert.Equal(t, stat1.ModTime(), stat2.ModTime(), "Expected config file to not be modified, but it was")
85}
86
87func TestValidateDefaultConfig(t *testing.T) {
88 config := defaultConfig()
89 err := validateConfig(&config)
90 if err != nil {
91 t.Errorf("Expected to validate config, but failed: %v", err)
92 }
93}
94
95func TestValidateConfig(t *testing.T) {
96 testFailure := func(config Config, errorText string) {
97 err := validateConfig(&config)
98 if err == nil {
99 t.Errorf("Expected to fail to validate config, but succeeded")
100 }
101 assert.Contains(t, err.Error(), errorText)
102 }
103
104 testFailure(Config{}, "no profiles found")
105 testFailure(Config{Profiles: []Profile{}}, "no profiles found")
106 testFailure(Config{Profiles: []Profile{
107 {
108 Name: "daily",
109 IsDefault: true,
110 NamePatterns: []ParameterizedText{"daily"},
111 File: File{
112 Template: "foo",
113 },
114 },
115 }}, "error validating profile")
116 testFailure(Config{Profiles: []Profile{
117 {
118 Name: "daily",
119 IsDefault: true,
120 NamePatterns: []ParameterizedText{"daily"},
121 Type: FileType,
122 Path: "foo",
123 File: File{
124 Template: "foo",
125 },
126 },
127 {
128 Name: "daily2",
129 IsDefault: true,
130 NamePatterns: []ParameterizedText{"daily"},
131 Type: FileType,
132 Path: "foo",
133 File: File{
134 Template: "foo",
135 },
136 },
137 }}, "multiple default profiles found")
138 testFailure(Config{Profiles: []Profile{
139 {
140 Name: "daily",
141 IsDefault: true,
142 NamePatterns: []ParameterizedText{"daily"},
143 Type: FileType,
144 Path: "foo",
145 File: File{
146 Template: "foo",
147 },
148 },
149 {
150 Name: "daily",
151 IsDefault: true,
152 NamePatterns: []ParameterizedText{"daily"},
153 Type: FileType,
154 Path: "foo",
155 File: File{
156 Template: "foo",
157 },
158 },
159 }}, "duplicate profile name 'daily'")
160}
161
162func TestLoadConfig(t *testing.T) {
163 bytes := []byte(`
164profiles:
165 - name:
166`)
167 err := os.WriteFile("./test/invalid-config.yaml", bytes, 0644)
168 assert.Nil(t, err, "Error writing invalid config file")
169
170 // load config
171 _, err = loadConfig("./test/invalid-config.yaml")
172 assert.NotNil(t, err, "Expected to fail to load config, but succeeded")
173 assert.Contains(t, err.Error(), "error validating profile : Profile name is required")
174}
175
176func TestSaveConfig(t *testing.T) {
177 config := Config{Profiles: []Profile{}}
178 err := saveConfig("./test/invalid-config.yaml", config)
179 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")
197 testFailure(Config{Profiles: []Profile{Profile{}}}, Config{}, "error validating config a: error validating profile : Profile name is required")
198 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)
210}