Skip to content

Commit

Permalink
Merge pull request #1237 from jonjohnsonjr/no-dupe-subpkg
Browse files Browse the repository at this point in the history
Disallow duplicate subpackage names
  • Loading branch information
jonjohnsonjr committed May 28, 2024
2 parents 427ebb1 + d85f49d commit c673c41
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,13 @@ func (cfg Configuration) validate() error {
return ErrInvalidConfiguration{Problem: err}
}

saw := map[string]int{}
for i, sp := range cfg.Subpackages {
if extant, ok := saw[sp.Name]; ok {
return fmt.Errorf("saw duplicate subpackage name %q (subpackages index: %d and %d)", sp.Name, extant, i)
}
saw[sp.Name] = i

if !packageNameRegex.MatchString(sp.Name) {
return ErrInvalidConfiguration{Problem: fmt.Errorf("subpackage name %q (subpackages index: %d) must match regex %q", sp.Name, i, packageNameRegex)}
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,33 @@ pipeline:
require.Equal(t, "/home/build/baz", cfg.Pipeline[1].Pipeline[0].Pipeline[1].WorkDir)
require.Equal(t, "/home/build/baz", cfg.Pipeline[1].Pipeline[0].Pipeline[2].WorkDir)
}

func TestDuplicateSubpackage(t *testing.T) {
ctx := slogtest.TestContextWithLogger(t)

fp := filepath.Join(os.TempDir(), "melange-test-applySubstitutionsInProvides")
if err := os.WriteFile(fp, []byte(`
package:
name: dupe-subpackage
version: 0.0.1
epoch: 8
description: example using a two subpackages with same name
data:
- name: I-am-a-range
items:
a: ""
b: ""
subpackages:
- name: subpackage
range: I-am-a-range
pipeline:
- runs: echo "I am a subpackage for ${{range.key}"
`), 0644); err != nil {
t.Fatal(err)
}
if _, err := ParseConfiguration(ctx, fp); err == nil {
t.Errorf("configuration should have failed to validate, got: %v", err)
}
}

0 comments on commit c673c41

Please sign in to comment.