diff --git a/pkg/lint/rules_test.go b/pkg/lint/rules_test.go index d2cef71f..be9d4be1 100644 --- a/pkg/lint/rules_test.go +++ b/pkg/lint/rules_test.go @@ -328,7 +328,7 @@ func TestLinter_Rules(t *testing.T) { file: "check-subpipeline-version-matches.yaml", minSeverity: SeverityWarning, want: EvalResult{ - File: "check-version-matches", + File: "check-subpipeline-version-matches", Errors: EvalRuleErrors{ { Rule: Rule{ diff --git a/pkg/lint/testdata/files/check-subpipeline-version-matches.yaml b/pkg/lint/testdata/files/check-subpipeline-version-matches.yaml index da963c5b..2b8c3626 100644 --- a/pkg/lint/testdata/files/check-subpipeline-version-matches.yaml +++ b/pkg/lint/testdata/files/check-subpipeline-version-matches.yaml @@ -1,5 +1,5 @@ package: - name: check-version-matches + name: check-subpipeline-version-matches version: 0.9.0 epoch: 0 description: "a package with an out of date comment" diff --git a/pkg/melange/melange.go b/pkg/melange/melange.go index f149d1af..a3265f60 100644 --- a/pkg/melange/melange.go +++ b/pkg/melange/melange.go @@ -144,7 +144,19 @@ func ReadAllPackagesFromRepo(ctx context.Context, dir string) (map[string]*Packa return p, fmt.Errorf("failed to read package config %s: %w", fi, err) } - p[packageConfig.Package.Name] = &Packages{ + // check that the package name matches the file name + name := packageConfig.Package.Name + fiBase := strings.TrimSuffix(filepath.Base(fi), filepath.Ext(fi)) + if name != fiBase { + return p, fmt.Errorf("package name does not match file name in '%s': '%s' != '%s'", fi, name, fiBase) + } + + // check that the package config name is unique + _, exists := p[name] + if exists { + return p, fmt.Errorf("package config names must be unique. Found a package called '%s' in '%s' and '%s'", name, fi, p[name].Filename) + } + p[name] = &Packages{ Config: *packageConfig, Filename: relativeFilename, Dir: dir, diff --git a/pkg/melange/melange_test.go b/pkg/melange/melange_test.go index 978e91e0..0ba7cff6 100644 --- a/pkg/melange/melange_test.go +++ b/pkg/melange/melange_test.go @@ -30,6 +30,12 @@ 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 name does not match file name in 'foo1.yaml': 'foo' != 'foo1") +} + func TestMelange_readPackageConfigForBar(t *testing.T) { ctx := context.Background() packages, err := ReadPackageConfigs(ctx, []string{"bar"}, filepath.Join("testdata", "melange_dir")) diff --git a/pkg/melange/testdata/duplicates_dir/foo.yaml b/pkg/melange/testdata/duplicates_dir/foo.yaml new file mode 100644 index 00000000..6d6fb81a --- /dev/null +++ b/pkg/melange/testdata/duplicates_dir/foo.yaml @@ -0,0 +1,3 @@ +package: + name: foo + version: 1.2.3 diff --git a/pkg/melange/testdata/duplicates_dir/foo2.yaml b/pkg/melange/testdata/duplicates_dir/foo2.yaml new file mode 100644 index 00000000..5b3a8e5e --- /dev/null +++ b/pkg/melange/testdata/duplicates_dir/foo2.yaml @@ -0,0 +1,3 @@ +package: + name: foo + version: 1.2.4