Skip to content

Commit

Permalink
fix: Fix file collision detection with per-file packagers (#303)
Browse files Browse the repository at this point in the history
* fix: Fix file collision detection with per-file packagers

* fix typo
  • Loading branch information
erikgeiser authored Feb 26, 2021
1 parent 4a81c34 commit 6192474
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
3 changes: 2 additions & 1 deletion files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ func appendWithUniqueDestination(slice []*Content, elems ...*Content) ([]*Conten
}

for _, elem := range elems {
if present, ok := alreadyPresent[elem.Destination]; ok {
present, ok := alreadyPresent[elem.Destination]
if ok && (present.Packager == "" || elem.Packager == "" || present.Packager == elem.Packager) {
return nil, fmt.Errorf(
"cannot add %s because %s is already present at the same destination (%s): %w",
elem.Source, present.Source, present.Destination, ErrContentCollision)
Expand Down
36 changes: 29 additions & 7 deletions files/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,33 @@ contents:
}

func TestCollision(t *testing.T) {
configuredFiles := []*files.Content{
{Source: "../testdata/whatever.conf", Destination: "/samedestination"},
{Source: "../testdata/whatever2.conf", Destination: "/samedestination"},
}

_, err := files.ExpandContentGlobs(configuredFiles, true)
require.ErrorIs(t, err, files.ErrContentCollision)
t.Run("collision between files for all packagers", func(t *testing.T) {
configuredFiles := []*files.Content{
{Source: "../testdata/whatever.conf", Destination: "/samedestination"},
{Source: "../testdata/whatever2.conf", Destination: "/samedestination"},
}

_, err := files.ExpandContentGlobs(configuredFiles, true)
require.ErrorIs(t, err, files.ErrContentCollision)
})

t.Run("no collision due to different per-file packagers", func(t *testing.T) {
configuredFiles := []*files.Content{
{Source: "../testdata/whatever.conf", Destination: "/samedestination", Packager: "foo"},
{Source: "../testdata/whatever2.conf", Destination: "/samedestination", Packager: "bar"},
}

_, err := files.ExpandContentGlobs(configuredFiles, true)
require.NoError(t, err)
})

t.Run("collision between file for all packagers and file for specific packager", func(t *testing.T) {
configuredFiles := []*files.Content{
{Source: "../testdata/whatever.conf", Destination: "/samedestination", Packager: "foo"},
{Source: "../testdata/whatever2.conf", Destination: "/samedestination", Packager: ""},
}

_, err := files.ExpandContentGlobs(configuredFiles, true)
require.ErrorIs(t, err, files.ErrContentCollision)
})
}

1 comment on commit 6192474

@vercel
Copy link

@vercel vercel bot commented on 6192474 Feb 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.