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