Skip to content

Commit

Permalink
fix: package names can be of length two (#120)
Browse files Browse the repository at this point in the history
This commit adds support for package names with a minimum length of two.
Previously chisel only supported a minimum length of 3. The limit on the
slice name is kept unchanged.
  • Loading branch information
rebornplusplus authored Mar 8, 2024
1 parent fdb2e54 commit 60d8b38
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 3 deletions.
11 changes: 8 additions & 3 deletions internal/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,14 @@ func order(pkgs map[string]*Package, keys []SliceKey) ([]SliceKey, error) {
return order, nil
}

var fnameExp = regexp.MustCompile(`^([a-z0-9](?:-?[.a-z0-9+]){2,})\.yaml$`)
// fnameExp matches the slice definition file basename.
var fnameExp = regexp.MustCompile(`^([a-z0-9](?:-?[.a-z0-9+]){1,})\.yaml$`)

// snameExp matches only the slice name, without the leading package name.
var snameExp = regexp.MustCompile(`^([a-z](?:-?[a-z0-9]){2,})$`)
var knameExp = regexp.MustCompile(`^([a-z0-9](?:-?[.a-z0-9+]){2,})_([a-z](?:-?[a-z0-9]){2,})$`)

// knameExp matches the slice full name in pkg_slice format.
var knameExp = regexp.MustCompile(`^([a-z0-9](?:-?[.a-z0-9+]){1,})_([a-z](?:-?[a-z0-9]){2,})$`)

func ParseSliceKey(sliceKey string) (SliceKey, error) {
match := knameExp.FindStringSubmatch(sliceKey)
Expand Down Expand Up @@ -291,7 +296,7 @@ func readSlices(release *Release, baseDir, dirName string) error {
}
match := fnameExp.FindStringSubmatch(entry.Name())
if match == nil {
return fmt.Errorf("invalid slice definition filename: %q\")", entry.Name())
return fmt.Errorf("invalid slice definition filename: %q", entry.Name())
}

pkgName := match[1]
Expand Down
135 changes: 135 additions & 0 deletions internal/setup/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,54 @@ var setupTests = []setupTest{{
`,
},
relerror: `chisel.yaml: public key "extra-key" armor has incorrect ID: expected "9568570379BF1F43", got "854BAF1AA9D76600"`,
}, {
summary: "Short package name",
input: map[string]string{
"slices/mydir/jq.yaml": `
package: jq
slices:
bins:
contents:
/usr/bin/jq:
`,
},
release: &setup.Release{
DefaultArchive: "ubuntu",

Archives: map[string]*setup.Archive{
"ubuntu": {
Name: "ubuntu",
Version: "22.04",
Suites: []string{"jammy"},
Components: []string{"main", "universe"},
PubKeys: []*packet.PublicKey{testKey.PubKey},
},
},
Packages: map[string]*setup.Package{
"jq": {
Archive: "ubuntu",
Name: "jq",
Path: "slices/mydir/jq.yaml",
Slices: map[string]*setup.Slice{
"bins": {
Package: "jq",
Name: "bins",
Contents: map[string]setup.PathInfo{
"/usr/bin/jq": {Kind: "copy"},
},
},
},
},
},
},
}, {
summary: "Very short, invalid package name",
input: map[string]string{
"slices/mydir/a.yaml": `
package: a
`,
},
relerror: `invalid slice definition filename: "a.yaml"`,
}}

var defaultChiselYaml = `
Expand Down Expand Up @@ -1101,3 +1149,90 @@ func runParseReleaseTests(c *C, tests []setupTest) {
}
}
}

var sliceKeyTests = []struct {
input string
expected setup.SliceKey
err string
}{{
input: "foo_bar",
expected: setup.SliceKey{Package: "foo", Slice: "bar"},
}, {
input: "fo_bar",
expected: setup.SliceKey{Package: "fo", Slice: "bar"},
}, {
input: "1234_bar",
expected: setup.SliceKey{Package: "1234", Slice: "bar"},
}, {
input: "foo1.1-2-3_bar",
expected: setup.SliceKey{Package: "foo1.1-2-3", Slice: "bar"},
}, {
input: "foo-pkg_dashed-slice-name",
expected: setup.SliceKey{Package: "foo-pkg", Slice: "dashed-slice-name"},
}, {
input: "foo+_bar",
expected: setup.SliceKey{Package: "foo+", Slice: "bar"},
}, {
input: "foo_slice123",
expected: setup.SliceKey{Package: "foo", Slice: "slice123"},
}, {
input: "g++_bins",
expected: setup.SliceKey{Package: "g++", Slice: "bins"},
}, {
input: "a+_bar",
expected: setup.SliceKey{Package: "a+", Slice: "bar"},
}, {
input: "a._bar",
expected: setup.SliceKey{Package: "a.", Slice: "bar"},
}, {
input: "foo_ba",
err: `invalid slice reference: "foo_ba"`,
}, {
input: "f_bar",
err: `invalid slice reference: "f_bar"`,
}, {
input: "1234_789",
err: `invalid slice reference: "1234_789"`,
}, {
input: "foo_bar.x.y",
err: `invalid slice reference: "foo_bar.x.y"`,
}, {
input: "foo-_-bar",
err: `invalid slice reference: "foo-_-bar"`,
}, {
input: "foo_bar-",
err: `invalid slice reference: "foo_bar-"`,
}, {
input: "foo-_bar",
err: `invalid slice reference: "foo-_bar"`,
}, {
input: "-foo_bar",
err: `invalid slice reference: "-foo_bar"`,
}, {
input: "foo_bar_baz",
err: `invalid slice reference: "foo_bar_baz"`,
}, {
input: "a-_bar",
err: `invalid slice reference: "a-_bar"`,
}, {
input: "+++_bar",
err: `invalid slice reference: "\+\+\+_bar"`,
}, {
input: "..._bar",
err: `invalid slice reference: "\.\.\._bar"`,
}, {
input: "white space_no-whitespace",
err: `invalid slice reference: "white space_no-whitespace"`,
}}

func (s *S) TestParseSliceKey(c *C) {
for _, test := range sliceKeyTests {
key, err := setup.ParseSliceKey(test.input)
if test.err != "" {
c.Assert(err, ErrorMatches, test.err)
continue
}
c.Assert(err, IsNil)
c.Assert(key, DeepEquals, test.expected)
}
}

0 comments on commit 60d8b38

Please sign in to comment.