mirror of
https://github.com/goreleaser/nfpm
synced 2024-05-18 20:46:19 +02:00
fix: better validations
This commit is contained in:
parent
c52d07bcc8
commit
672aa2bd65
|
@ -31,6 +31,8 @@ func accept(t *testing.T, name, conf, format, dockerfile string) {
|
|||
var info nfpm.Info
|
||||
err = yaml.Unmarshal(bts, &info)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, nfpm.Validate(info))
|
||||
|
||||
pkg, err := nfpm.Get(format)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
|
|
@ -19,3 +19,9 @@ func TestComplexDeb(t *testing.T) {
|
|||
accept(t, "complex_deb_386", "complex.386.yaml", "deb", "deb.386.complex.dockerfile")
|
||||
})
|
||||
}
|
||||
|
||||
func TestMinDeb(t *testing.T) {
|
||||
t.Run("amd64", func(t *testing.T) {
|
||||
accept(t, "min_deb", "min.yaml", "deb", "deb.min.dockerfile")
|
||||
})
|
||||
}
|
||||
|
|
|
@ -19,3 +19,9 @@ func TestComplexRPM(t *testing.T) {
|
|||
accept(t, "complex_rpm_386", "complex.386.yaml", "rpm", "rpm.386.complex.dockerfile")
|
||||
})
|
||||
}
|
||||
|
||||
func TestMinRPM(t *testing.T) {
|
||||
t.Run("amd64", func(t *testing.T) {
|
||||
accept(t, "min_rpm", "min.yaml", "rpm", "rpm.min.dockerfile")
|
||||
})
|
||||
}
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
FROM ubuntu
|
||||
ARG package
|
||||
COPY ${package} /tmp/foo.deb
|
||||
RUN dpkg -i /tmp/foo.deb
|
||||
RUN dpkg -r foo
|
|
@ -0,0 +1,6 @@
|
|||
name: foo
|
||||
arch: amd64
|
||||
version: 1.2.3
|
||||
license: MIT
|
||||
files:
|
||||
../testdata/fake: "/usr/local/bin/fake"
|
|
@ -0,0 +1,5 @@
|
|||
FROM fedora
|
||||
ARG package
|
||||
COPY ${package} /tmp/foo.rpm
|
||||
RUN rpm -ivh /tmp/foo.rpm
|
||||
RUN rpm -e foo
|
|
@ -68,6 +68,9 @@ func doPackage(config, target string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := nfpm.Validate(info); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("using %s packager...\n", format)
|
||||
pkg, err := nfpm.Get(format)
|
||||
if err != nil {
|
||||
|
|
17
nfpm.go
17
nfpm.go
|
@ -59,6 +59,23 @@ type Info struct {
|
|||
ConfigFiles map[string]string `yaml:"config_files,omitempty"`
|
||||
}
|
||||
|
||||
// Validate the given Info and returns an error if it is invalid.
|
||||
func Validate(info Info) error {
|
||||
if info.Name == "" {
|
||||
return fmt.Errorf("package name cannot be empty")
|
||||
}
|
||||
if info.Arch == "" {
|
||||
return fmt.Errorf("package arch must be provided")
|
||||
}
|
||||
if info.Version == "" {
|
||||
return fmt.Errorf("package version must be provided")
|
||||
}
|
||||
if len(info.Files)+len(info.ConfigFiles) == 0 {
|
||||
return fmt.Errorf("no files were provided")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WithDefaults set some sane defaults into the given Info
|
||||
func WithDefaults(info Info) Info {
|
||||
if info.Bindir == "" {
|
||||
|
|
42
nfpm_test.go
42
nfpm_test.go
|
@ -5,6 +5,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRegister(t *testing.T) {
|
||||
|
@ -50,6 +51,47 @@ func TestDefaults(t *testing.T) {
|
|||
assert.Equal(t, info, got)
|
||||
}
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
require.NoError(t, Validate(Info{
|
||||
Name: "as",
|
||||
Arch: "asd",
|
||||
Version: "1.2.3",
|
||||
Files: map[string]string{
|
||||
"asa": "asd",
|
||||
},
|
||||
}))
|
||||
require.NoError(t, Validate(Info{
|
||||
Name: "as",
|
||||
Arch: "asd",
|
||||
Version: "1.2.3",
|
||||
ConfigFiles: map[string]string{
|
||||
"asa": "asd",
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
func TestValidateError(t *testing.T) {
|
||||
for err, info := range map[string]Info{
|
||||
"package name cannot be empty": Info{},
|
||||
"package arch must be provided": Info{
|
||||
Name: "fo",
|
||||
},
|
||||
"package version must be provided": Info{
|
||||
Name: "as",
|
||||
Arch: "asd",
|
||||
},
|
||||
"no files were provided": Info{
|
||||
Name: "as",
|
||||
Arch: "asd",
|
||||
Version: "1.2.3",
|
||||
},
|
||||
} {
|
||||
t.Run(err, func(t *testing.T) {
|
||||
require.EqualError(t, Validate(info), err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type fakePackager struct{}
|
||||
|
||||
func (*fakePackager) Package(info Info, w io.Writer) error {
|
||||
|
|
|
@ -278,10 +278,14 @@ Name: {{ .Info.Name }}
|
|||
Summary: {{ first_line .Info.Description }}
|
||||
Version: {{ .Info.Version }}
|
||||
Release: 1
|
||||
License: {{ .Info.License }}
|
||||
{{- with .Info.License }}
|
||||
License: {{ . }}
|
||||
{{- end }}
|
||||
Group: Development/Tools
|
||||
SOURCE0 : %{name}-%{version}.tar.gz
|
||||
URL: {{ .Info.Homepage }}
|
||||
{{- with .Info.Homepage }}
|
||||
URL: {{ . }}
|
||||
{{- end }}
|
||||
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
|
||||
|
||||
{{ range $index, $element := .Info.Replaces }}
|
||||
|
|
Loading…
Reference in New Issue