mirror of
https://github.com/goreleaser/goreleaser.git
synced 2026-06-19 23:24:39 +02:00
fix: better duplicate ID message
refs #1090 Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
+18
-9
@@ -4,23 +4,32 @@ package ids
|
||||
import "fmt"
|
||||
|
||||
// IDs is the IDs type
|
||||
type IDs map[string]int
|
||||
type IDs struct {
|
||||
ids map[string]int
|
||||
kind string
|
||||
}
|
||||
|
||||
// New IDs
|
||||
func New() IDs {
|
||||
return IDs(map[string]int{})
|
||||
func New(kind string) IDs {
|
||||
return IDs{
|
||||
ids: map[string]int{},
|
||||
kind: kind,
|
||||
}
|
||||
}
|
||||
|
||||
// Inc increment the counter of the given id
|
||||
func (ids IDs) Inc(id string) {
|
||||
ids[id]++
|
||||
func (i IDs) Inc(id string) {
|
||||
i.ids[id]++
|
||||
}
|
||||
|
||||
// Validate errors if there are any ids with counter > 1
|
||||
func (ids IDs) Validate() error {
|
||||
for id, cont := range ids {
|
||||
if cont > 1 {
|
||||
return fmt.Errorf("found %d items with the ID '%s', please fix your config", cont, id)
|
||||
func (i IDs) Validate() error {
|
||||
for id, count := range i.ids {
|
||||
if count > 1 {
|
||||
return fmt.Errorf(
|
||||
"found %d %s with the ID '%s', please fix your config",
|
||||
count, i.kind, id,
|
||||
)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -7,16 +7,16 @@ import (
|
||||
)
|
||||
|
||||
func TestIDs(t *testing.T) {
|
||||
var ids = New()
|
||||
var ids = New("foos")
|
||||
ids.Inc("foo")
|
||||
ids.Inc("bar")
|
||||
require.NoError(t, ids.Validate())
|
||||
}
|
||||
|
||||
func TestIDsError(t *testing.T) {
|
||||
var ids = New()
|
||||
var ids = New("builds")
|
||||
ids.Inc("foo")
|
||||
ids.Inc("bar")
|
||||
ids.Inc("foo")
|
||||
require.EqualError(t, ids.Validate(), "found 2 items with the ID 'foo', please fix your config")
|
||||
require.EqualError(t, ids.Validate(), "found 2 builds with the ID 'foo', please fix your config")
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func (Pipe) String() string {
|
||||
|
||||
// Default sets the pipe defaults
|
||||
func (Pipe) Default(ctx *context.Context) error {
|
||||
var ids = ids.New()
|
||||
var ids = ids.New("archives")
|
||||
if len(ctx.Config.Archives) == 0 {
|
||||
ctx.Config.Archives = append(ctx.Config.Archives, ctx.Config.Archive)
|
||||
if !reflect.DeepEqual(ctx.Config.Archive, config.Archive{}) {
|
||||
|
||||
@@ -678,5 +678,5 @@ func TestSeveralArchivesWithTheSameID(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
require.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'a', please fix your config")
|
||||
require.EqualError(t, Pipe{}.Default(ctx), "found 2 archives with the ID 'a', please fix your config")
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func (Pipe) Run(ctx *context.Context) error {
|
||||
|
||||
// Default sets the pipe defaults
|
||||
func (Pipe) Default(ctx *context.Context) error {
|
||||
var ids = ids.New()
|
||||
var ids = ids.New("builds")
|
||||
for i, build := range ctx.Config.Builds {
|
||||
ctx.Config.Builds[i] = buildWithDefaults(ctx, build)
|
||||
ids.Inc(ctx.Config.Builds[i].ID)
|
||||
|
||||
@@ -253,7 +253,7 @@ func TestDefaultBuildID(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'foo', please fix your config")
|
||||
assert.EqualError(t, Pipe{}.Default(ctx), "found 2 builds with the ID 'foo', please fix your config")
|
||||
var build = ctx.Config.Builds[0]
|
||||
assert.Equal(t, ctx.Config.ProjectName, build.ID)
|
||||
}
|
||||
@@ -273,7 +273,7 @@ func TestSeveralBuildsWithTheSameID(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'a', please fix your config")
|
||||
assert.EqualError(t, Pipe{}.Default(ctx), "found 2 builds with the ID 'a', please fix your config")
|
||||
}
|
||||
|
||||
func TestDefaultPartialBuilds(t *testing.T) {
|
||||
|
||||
@@ -41,7 +41,7 @@ func (Pipe) Default(ctx *context.Context) error {
|
||||
deprecate.Notice("nfpm")
|
||||
}
|
||||
}
|
||||
var ids = ids.New()
|
||||
var ids = ids.New("nfpms")
|
||||
for i := range ctx.Config.NFPMs {
|
||||
var fpm = &ctx.Config.NFPMs[i]
|
||||
if fpm.ID == "" {
|
||||
|
||||
@@ -356,5 +356,5 @@ func TestSeveralNFPMsWithTheSameID(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
require.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'a', please fix your config")
|
||||
require.EqualError(t, Pipe{}.Default(ctx), "found 2 nfpms with the ID 'a', please fix your config")
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ func (Pipe) Default(ctx *context.Context) error {
|
||||
deprecate.Notice("snapcraft")
|
||||
}
|
||||
}
|
||||
var ids = ids.New()
|
||||
var ids = ids.New("snapcrafts")
|
||||
for i := range ctx.Config.Snapcrafts {
|
||||
var snap = &ctx.Config.Snapcrafts[i]
|
||||
if snap.NameTemplate == "" {
|
||||
|
||||
@@ -402,5 +402,5 @@ func TestSeveralSnapssWithTheSameID(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
require.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'a', please fix your config")
|
||||
require.EqualError(t, Pipe{}.Default(ctx), "found 2 snapcrafts with the ID 'a', please fix your config")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user