Skip to content

Commit

Permalink
fix: config tests
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Apr 29, 2024
1 parent bc5e1de commit 471e1cd
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
9 changes: 5 additions & 4 deletions cmd/testdata/invalid.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
version: 2
builds:
- id: a
- id: b
- id: c
- id: a
- id: a
- id: b
- id: c
- id: a
3 changes: 2 additions & 1 deletion pkg/config/config_homebrew_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ brews:

t.Run("mixed", func(t *testing.T) {
conf := `
version: 2
brews:
- name: foo
dependencies:
Expand All @@ -67,6 +68,6 @@ brews:
buf := strings.NewReader(conf)
_, err := LoadReader(buf)

require.EqualError(t, err, "yaml: unmarshal errors:\n line 6: field namer not found in type config.homebrewDependency")
require.EqualError(t, err, "yaml: unmarshal errors:\n line 7: field namer not found in type config.homebrewDependency")
})
}
12 changes: 8 additions & 4 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func TestFileNotFound(t *testing.T) {

func TestInvalidFields(t *testing.T) {
_, err := Load("testdata/invalid_config.yml")
require.EqualError(t, err, "yaml: unmarshal errors:\n line 2: field invalid_yaml not found in type config.Build")
require.EqualError(t, err, "yaml: unmarshal errors:\n line 3: field invalid_yaml not found in type config.Build")
}

func TestInvalidYaml(t *testing.T) {
_, err := Load("testdata/invalid.yml")
require.EqualError(t, err, "yaml: line 1: did not find expected node content")
require.EqualError(t, err, "yaml: line 2: did not find expected node content")
}

func TestConfigWithAnchors(t *testing.T) {
Expand All @@ -86,9 +86,13 @@ func TestVersion(t *testing.T) {
_, err := LoadReader(strings.NewReader("version: 1"))
require.NoError(t, err)
})
t.Run("do not allow v2", func(t *testing.T) {
t.Run("allow v2", func(t *testing.T) {
_, err := LoadReader(strings.NewReader("version: 2"))
require.NoError(t, err)
})
t.Run("do not allow v3", func(t *testing.T) {
_, err := LoadReader(strings.NewReader("version: 3\nnope: 1"))
require.Error(t, err)
require.ErrorIs(t, err, VersionError{2})
require.ErrorIs(t, err, VersionError{3})
})
}
6 changes: 3 additions & 3 deletions pkg/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type VersionError struct {
func (e VersionError) Error() string {
return fmt.Sprintf(
"only configurations files on %s are supported, yours is %s, please update your configuration",
logext.Keyword("version: 1"),
logext.Keyword("version: 2"),
logext.Keyword(fmt.Sprintf("version: %d", e.current)),
)
}
Expand All @@ -42,15 +42,15 @@ func LoadReader(fd io.Reader) (config Project, err error) {
}

var versioned Versioned
_ = yaml.Unmarshal(data, &versioned)
verr := yaml.Unmarshal(data, &versioned)

validVersion := versioned.Version == 2
if !validVersion {
log.Warn(VersionError{versioned.Version}.Error())
}

err = yaml.UnmarshalStrict(data, &config)
if err != nil && !validVersion {
if err != nil && !validVersion && verr == nil {
return config, VersionError{versioned.Version}
}
return config, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/testdata/anchor.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
project_name: &anchor_name This string will appear as the value of two keys.
build:
binary: *anchor_name
builds:
- binary: *anchor_name
1 change: 1 addition & 0 deletions pkg/config/testdata/invalid.yml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
version: 2
this_is_not_valid: [
6 changes: 3 additions & 3 deletions pkg/config/testdata/invalid_config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
build:
invalid_yaml: 1

version: 2
builds:
- invalid_yaml: 1

0 comments on commit 471e1cd

Please sign in to comment.