Skip to content

Commit 91bb107

Browse files
authored
test(sidekick): missing config tests (#1626)
1 parent e86f76f commit 91bb107

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

internal/sidekick/internal/config/config.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ func LoadConfig(language string, source, codec map[string]string) (*Config, erro
9090
Source: maps.Clone(source),
9191
Codec: maps.Clone(codec),
9292
}
93-
config, err := mergeConfigs(rootConfig, argsConfig)
94-
if err != nil {
95-
return nil, err
96-
}
97-
return config, nil
93+
return mergeConfigs(rootConfig, argsConfig), nil
9894
}
9995

10096
// LoadRootConfig loads the root configuration file.
@@ -124,10 +120,10 @@ func MergeConfigAndFile(rootConfig *Config, filename string) (*Config, error) {
124120
if err != nil {
125121
return nil, fmt.Errorf("error reading configuration %s: %w", filename, err)
126122
}
127-
return mergeConfigs(rootConfig, &local)
123+
return mergeConfigs(rootConfig, &local), nil
128124
}
129125

130-
func mergeConfigs(rootConfig, local *Config) (*Config, error) {
126+
func mergeConfigs(rootConfig, local *Config) *Config {
131127
merged := Config{
132128
General: GeneralConfig{
133129
Language: rootConfig.General.Language,
@@ -162,7 +158,7 @@ func mergeConfigs(rootConfig, local *Config) (*Config, error) {
162158
merged.Source[k] = v
163159
}
164160
// Ignore errors reading the top-level file.
165-
return &merged, nil
161+
return &merged
166162
}
167163

168164
// UpdateRootConfig updates the root configuration file with the latest SHA from GitHub.

internal/sidekick/internal/config/config_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ func TestLoadConfig(t *testing.T) {
8282
}
8383
}
8484

85+
func TestLoadConfigBadRoot(t *testing.T) {
86+
t.Chdir(t.TempDir())
87+
err := os.WriteFile(configName, []byte("bad-toml: [ a, 1, "), 0644)
88+
if err != nil {
89+
t.Fatal(err)
90+
}
91+
source := map[string]string{"root1": "rv1"}
92+
codec := map[string]string{"root2": "rv2"}
93+
if _, err = LoadConfig("root-language", source, codec); err == nil {
94+
t.Errorf("expected an error when loading a missing file")
95+
}
96+
}
97+
8598
func TestLoadRootConfigOnlyGeneral(t *testing.T) {
8699
tempFile, err := os.CreateTemp(t.TempDir(), "root-config-")
87100
if err != nil {
@@ -247,6 +260,39 @@ func TestMergeLocalForDocumentationOverrides(t *testing.T) {
247260
}
248261
}
249262

263+
func TestMergeConfigAndFileBadRead(t *testing.T) {
264+
root := Config{
265+
General: GeneralConfig{
266+
Language: "root-language",
267+
SpecificationFormat: "root-specification-format",
268+
IgnoredDirectories: []string{"a", "b"},
269+
},
270+
}
271+
272+
filename := path.Join(t.TempDir(), "file-does-not-exist")
273+
if _, err := MergeConfigAndFile(&root, filename); err == nil {
274+
t.Error("expected read error with missing file")
275+
}
276+
}
277+
278+
func TestMergeConfigAndFileBadContent(t *testing.T) {
279+
root := Config{
280+
General: GeneralConfig{
281+
Language: "root-language",
282+
SpecificationFormat: "root-specification-format",
283+
IgnoredDirectories: []string{"a", "b"},
284+
},
285+
}
286+
287+
filename := path.Join(t.TempDir(), configName)
288+
if err := os.WriteFile(filename, []byte("bad-toml = [ 1, 2"), 0644); err != nil {
289+
t.Fatal(err)
290+
}
291+
if _, err := MergeConfigAndFile(&root, filename); err == nil {
292+
t.Error("expected read error with bad contents")
293+
}
294+
}
295+
250296
func TestMergeIgnoreRootSourceAndServiceConfig(t *testing.T) {
251297
root := Config{
252298
General: GeneralConfig{

0 commit comments

Comments
 (0)