Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail On Duplicate Config Names #1372

Merged
merged 8 commits into from
Jan 8, 2025
13 changes: 10 additions & 3 deletions pkg/melange/melange.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func ReadAllPackagesFromRepo(ctx context.Context, dir string) (map[string]*Packa

// guarantee a consistent sort order for test comparisons
sort.Strings(fileList)

for _, fi := range fileList {
data, err := os.ReadFile(fi)
if err != nil {
Expand Down Expand Up @@ -143,8 +143,15 @@ func ReadAllPackagesFromRepo(ctx context.Context, dir string) (map[string]*Packa
if err != nil {
return p, fmt.Errorf("failed to read package config %s: %w", fi, err)
}

p[packageConfig.Package.Name] = &Packages{

name := packageConfig.Package.Name

// check that the package config name is unique
_, exists := p[name]
if exists {
return p, fmt.Errorf("Package config names must be unique. Found duplicate '%s'", name)
imjasonh marked this conversation as resolved.
Show resolved Hide resolved
}
p[name] = &Packages{
Config: *packageConfig,
Filename: relativeFilename,
Dir: dir,
Expand Down
7 changes: 7 additions & 0 deletions pkg/melange/melange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ func TestMelange_readAllPackages(t *testing.T) {
assert.Equal(t, 4, len(packages))
}

func TestMelange_readAllPackagesErrorOnDuplicate(t *testing.T) {

ctx := context.Background()
_, err := ReadAllPackagesFromRepo(ctx, filepath.Join("testdata", "duplicates_dir"))
assert.Error(t, err, "Package config names must be unique. Found duplicate 'foo'")
}

func TestMelange_readPackageConfigForBar(t *testing.T) {
ctx := context.Background()
packages, err := ReadPackageConfigs(ctx, []string{"bar"}, filepath.Join("testdata", "melange_dir"))
Expand Down
3 changes: 3 additions & 0 deletions pkg/melange/testdata/duplicates_dir/foo1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package:
name: foo
version: 1.2.3
3 changes: 3 additions & 0 deletions pkg/melange/testdata/duplicates_dir/foo2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package:
name: foo
Copy link
Member

Choose a reason for hiding this comment

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

I think we might also want to fail if .package.name is not the same as the filename.

These are potentially different/separate issues, but it does I think violate some assumptions we've made, and is worth avoiding.

If we have that check, we also get uniqueness "for free" since there can't be two foo.yamls

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the .package.name field must match the filename, couldn't you make an argument that the field is redundant and should be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a check to error if the config name does not match the package name. This makes it impossible to test the package duplicate logic because, as you said, this gives us uniqueness for free, but I left the original check in just in case.

Copy link
Member

Choose a reason for hiding this comment

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

It probably is redundant, but I don't think we can easily remove it without causing lots and lots of downstream effects. I think it's fine to leave it, and just ensure it's the same as the filename, for now.

version: 1.2.4