fuzzyworld

template / ci-testing / files / config_test.go

Branch ci-testing · default branch · all branches

config_test.go

6.5 KB · history · blame

1package main
2
3import (
4 "errors"
5 "os"
6 "path/filepath"
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10)
11
12// 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")
18 assert.Equal(t, len(expectedYaml), len(actualYaml), "Expected YAML and actual YAML are different lengths")
19 assert.Equal(t, expectedYaml, actualYaml)
20}
21
22func TestSaveLoadConfig(t *testing.T) {
23 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 }
29 defer os.Remove(configDir)
30
31 config := defaultConfig()
32
33 // save config
34 err = saveConfig(configPath, config)
35 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
42 compareObjectYaml(t, loadedConfig, config)
43
44 // test load non-existent config
45 _, err = loadConfig("./test/non-existent-file.yaml")
46 assert.NotNil(t, err, "Expected to fail to load config, but succeeded")
47
48 // test load invalid yaml
49 err = os.WriteFile("./invalid-yaml.yaml", []byte("invalid yaml"), 0644)
50 assert.Nil(t, err, "Error writing invalid yaml file")
51 _, err = loadConfig("./invalid-yaml.yaml")
52 assert.NotNil(t, err, "Expected to fail to parse config, but succeeded")
53 os.Remove("./invalid-yaml.yaml")
54
55 // test saving config to invalid path
56 err = saveConfig("invalid/path/invalid-config.yaml", config)
57 assert.NotNil(t, err, "Expected to fail to save config, but succeeded")
58
59 // remove config
60 os.Remove(configPath)
61
62 // test default config creation
63 _, err = os.Stat(configPath)
64 assert.True(t, errors.Is(err, os.ErrNotExist), "Expected config file to not exist, but it does")
65
66 // fail to get config
67 _, err = loadConfig(configPath)
68 assert.NotNil(t, err, "Expected to fail to load config, but succeeded")
69
70 // create default config
71 err = saveConfig(configPath, defaultConfig())
72 assert.Nil(t, err, "Error saving config")
73
74 // test config
75 compareObjectYaml(t, loadedConfig, config)
76
77 // verify config file exists
78 stat1, err := os.Stat(configPath)
79 assert.Nil(t, err, "Expected config file to exist, but it does not")
80
81 // get and create config
82 loadedConfig, err = loadConfig(configPath)
83 assert.Nil(t, err, "Error loading config")
84
85 // test config
86 compareObjectYaml(t, loadedConfig, config)
87
88 // verify config file wasn't updated
89 stat2, err := os.Stat(configPath)
90 assert.Nil(t, err, "Expected config file to exist, but it does not")
91 assert.Equal(t, stat1.ModTime(), stat2.ModTime(), "Expected config file to not be modified, but it was")
92}
93
94func TestValidateDefaultConfig(t *testing.T) {
95 config := defaultConfig()
96 err := validateConfig(&config)
97 if err != nil {
98 t.Errorf("Expected to validate config, but failed: %v", err)
99 }
100}
101
102func TestValidateConfig(t *testing.T) {
103 testFailure := func(config Config, errorText string) {
104 err := validateConfig(&config)
105 if err == nil {
106 t.Errorf("Expected to fail to validate config, but succeeded")
107 }
108 assert.Contains(t, err.Error(), errorText)
109 }
110
111 testFailure(Config{}, "no profiles found")
112 testFailure(Config{Profiles: []Profile{}}, "no profiles found")
113 testFailure(Config{Profiles: []Profile{
114 {
115 Name: "daily",
116 IsDefault: true,
117 NamePatterns: []ParameterizedText{"daily"},
118 File: File{
119 Template: "foo",
120 },
121 },
122 }}, "error validating profile")
123 testFailure(Config{Profiles: []Profile{
124 {
125 Name: "daily",
126 IsDefault: true,
127 NamePatterns: []ParameterizedText{"daily"},
128 Type: FileType,
129 Path: "foo",
130 File: File{
131 Template: "foo",
132 },
133 },
134 {
135 Name: "daily2",
136 IsDefault: true,
137 NamePatterns: []ParameterizedText{"daily"},
138 Type: FileType,
139 Path: "foo",
140 File: File{
141 Template: "foo",
142 },
143 },
144 }}, "multiple default profiles found")
145 testFailure(Config{Profiles: []Profile{
146 {
147 Name: "daily",
148 IsDefault: true,
149 NamePatterns: []ParameterizedText{"daily"},
150 Type: FileType,
151 Path: "foo",
152 File: File{
153 Template: "foo",
154 },
155 },
156 {
157 Name: "daily",
158 IsDefault: true,
159 NamePatterns: []ParameterizedText{"daily"},
160 Type: FileType,
161 Path: "foo",
162 File: File{
163 Template: "foo",
164 },
165 },
166 }}, "duplicate profile name 'daily'")
167}
168
169func TestLoadConfig(t *testing.T) {
170 bytes := []byte(`
171profiles:
172 - name:
173`)
174 err := os.WriteFile("./invalid-config.yaml", bytes, 0644)
175 assert.Nil(t, err, "Error writing invalid config file")
176 defer os.Remove("./invalid-config.yaml")
177
178 // load config
179 _, err = loadConfig("./invalid-config.yaml")
180 assert.NotNil(t, err, "Expected to fail to load config, but succeeded")
181 assert.Contains(t, err.Error(), "error validating profile : Profile name is required")
182}
183
184func TestSaveConfig(t *testing.T) {
185 config := Config{Profiles: []Profile{}}
186 err := saveConfig("./invalid-config.yaml", config)
187 assert.NotNil(t, err, "Expected to fail to save config, but succeeded")
188 assert.Contains(t, err.Error(), "no profiles found")
189}
190
191func TestMergeConfigs(t *testing.T) {
192 defaultConfig := defaultConfig()
193 testSuccess := func(config1 Config, config2 Config) Config {
194 config, err := mergeConfigs(config1, config2)
195 // compareObjectYaml(t, mergedConfig, expected)
196 assert.Nil(t, err, "Expected to merge configs, but failed")
197 return config
198 }
199 testFailure := func(config1 Config, config2 Config, errorText string) {
200 _, err := mergeConfigs(config1, config2)
201 assert.NotNil(t, err, "Expected to fail to merge configs, but succeeded")
202 assert.Contains(t, err.Error(), errorText)
203 }
204 testFailure(Config{}, Config{}, "no profiles found")
205 testFailure(Config{Profiles: []Profile{{}}}, Config{}, "error validating config a: error validating profile : Profile name is required")
206 testFailure(Config{Profiles: []Profile{defaultConfig.Profiles[0]}}, Config{}, "error validating config b: no profiles found")
207
208 profile1 := defaultConfig.Profiles[0]
209 profile2 := defaultConfig.Profiles[1]
210 testSuccess(Config{Profiles: []Profile{profile1}}, Config{Profiles: []Profile{profile2}})
211
212 profile1_updated := profile1
213 profile1_updated.Description += " This profile has been updated!"
214
215 result := testSuccess(Config{Profiles: []Profile{profile1}}, Config{Profiles: []Profile{profile1_updated}})
216 assert.Equal(t, 1, len(result.Profiles))
217 assert.Equal(t, profile1_updated.Name, result.Profiles[0].Name)
218}