2017-05-11 04:34:47 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-10-31 14:28:54 +02:00
|
|
|
"bytes"
|
2017-05-18 14:02:44 +02:00
|
|
|
"fmt"
|
2021-11-21 16:10:08 +02:00
|
|
|
"os"
|
2017-05-11 04:34:47 +02:00
|
|
|
"strings"
|
2017-05-11 04:35:44 +02:00
|
|
|
"testing"
|
2017-05-11 04:34:47 +02:00
|
|
|
|
2020-10-06 14:48:04 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-05-11 04:34:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepo(t *testing.T) {
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Equal(
|
2017-10-15 21:46:21 +02:00
|
|
|
t,
|
|
|
|
"goreleaser/godownloader",
|
|
|
|
Repo{Owner: "goreleaser", Name: "godownloader"}.String(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyRepoNameAndOwner(t *testing.T) {
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Empty(t, Repo{}.String())
|
2017-05-11 04:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadReader(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
conf := `
|
2019-12-27 19:51:40 +02:00
|
|
|
nfpms:
|
|
|
|
- homepage: http://goreleaser.github.io
|
2017-05-11 04:34:47 +02:00
|
|
|
`
|
|
|
|
buf := strings.NewReader(conf)
|
|
|
|
prop, err := LoadReader(buf)
|
|
|
|
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "http://goreleaser.github.io", prop.NFPMs[0].Homepage, "yaml did not load correctly")
|
2017-05-11 04:34:47 +02:00
|
|
|
}
|
2017-05-18 14:02:02 +02:00
|
|
|
|
2024-06-10 00:58:24 +02:00
|
|
|
func TestArrayEmptyVsNil(t *testing.T) {
|
|
|
|
conf := `
|
|
|
|
builds: []
|
|
|
|
# blobs:
|
|
|
|
`
|
|
|
|
buf := strings.NewReader(conf)
|
|
|
|
prop, err := LoadReader(buf)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, prop.Builds)
|
|
|
|
require.Empty(t, prop.Builds)
|
|
|
|
require.Nil(t, prop.Blobs)
|
|
|
|
require.Empty(t, prop.Blobs)
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:02:02 +02:00
|
|
|
type errorReader struct{}
|
|
|
|
|
2023-03-20 03:17:18 +02:00
|
|
|
func (errorReader) Read(_ []byte) (n int, err error) {
|
2017-05-18 14:02:02 +02:00
|
|
|
return 1, fmt.Errorf("error")
|
|
|
|
}
|
2021-04-25 19:20:49 +02:00
|
|
|
|
2017-05-18 14:02:02 +02:00
|
|
|
func TestLoadBadReader(t *testing.T) {
|
|
|
|
_, err := LoadReader(errorReader{})
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Error(t, err)
|
2017-05-18 14:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFile(t *testing.T) {
|
2021-11-21 16:10:08 +02:00
|
|
|
f, err := os.CreateTemp(t.TempDir(), "config")
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
2021-04-25 16:34:40 +02:00
|
|
|
require.NoError(t, f.Close())
|
2024-04-23 04:44:28 +02:00
|
|
|
_, err = Load(f.Name())
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
2017-05-18 14:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileNotFound(t *testing.T) {
|
|
|
|
_, err := Load("/nope/no-way.yml")
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Error(t, err)
|
2017-05-18 14:02:02 +02:00
|
|
|
}
|
2017-07-08 17:05:57 +02:00
|
|
|
|
|
|
|
func TestInvalidFields(t *testing.T) {
|
|
|
|
_, err := Load("testdata/invalid_config.yml")
|
2024-05-25 19:09:49 +02:00
|
|
|
require.EqualError(t, err, "yaml: unmarshal errors:\n line 3: field invalid_yaml not found in type config.Build")
|
2017-07-08 17:05:57 +02:00
|
|
|
}
|
2017-08-04 03:25:25 +02:00
|
|
|
|
|
|
|
func TestInvalidYaml(t *testing.T) {
|
|
|
|
_, err := Load("testdata/invalid.yml")
|
2024-05-25 19:09:49 +02:00
|
|
|
require.EqualError(t, err, "yaml: line 2: did not find expected node content")
|
2017-08-04 03:25:25 +02:00
|
|
|
}
|
2018-01-26 18:35:06 +02:00
|
|
|
|
|
|
|
func TestConfigWithAnchors(t *testing.T) {
|
|
|
|
_, err := Load("testdata/anchor.yaml")
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
2018-01-26 18:35:06 +02:00
|
|
|
}
|
2023-10-31 14:28:54 +02:00
|
|
|
|
|
|
|
func TestVersion(t *testing.T) {
|
|
|
|
t.Run("allow no version", func(t *testing.T) {
|
|
|
|
_, err := LoadReader(bytes.NewReader(nil))
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2024-05-25 19:09:49 +02:00
|
|
|
t.Run("do not allow no version with errors", func(t *testing.T) {
|
|
|
|
_, err := LoadReader(strings.NewReader("nope: nope"))
|
|
|
|
require.Error(t, err)
|
|
|
|
require.ErrorIs(t, err, VersionError{0})
|
|
|
|
})
|
2023-10-31 14:28:54 +02:00
|
|
|
t.Run("allow v0", func(t *testing.T) {
|
|
|
|
_, err := LoadReader(strings.NewReader("version: 0"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2024-05-25 19:09:49 +02:00
|
|
|
t.Run("do not allow v0 with errors", func(t *testing.T) {
|
|
|
|
_, err := LoadReader(strings.NewReader("version: 0\nnope: nope"))
|
|
|
|
require.Error(t, err)
|
|
|
|
require.ErrorIs(t, err, VersionError{0})
|
|
|
|
})
|
2023-10-31 14:28:54 +02:00
|
|
|
t.Run("allow v1", func(t *testing.T) {
|
|
|
|
_, err := LoadReader(strings.NewReader("version: 1"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2024-05-25 19:09:49 +02:00
|
|
|
t.Run("do not allow v1 with errors", func(t *testing.T) {
|
|
|
|
_, err := LoadReader(strings.NewReader("version: 1\nnope: nope"))
|
2023-10-31 14:28:54 +02:00
|
|
|
require.Error(t, err)
|
2024-05-25 19:09:49 +02:00
|
|
|
require.ErrorIs(t, err, VersionError{1})
|
|
|
|
})
|
|
|
|
t.Run("allow v2", func(t *testing.T) {
|
|
|
|
_, err := LoadReader(strings.NewReader("version: 2\nbuilds: []"))
|
|
|
|
require.NoError(t, err)
|
2023-10-31 14:28:54 +02:00
|
|
|
})
|
|
|
|
}
|