From 60d8b38f6ea4f5ba3801eaca185aa03d627d4398 Mon Sep 17 00:00:00 2001 From: Rafid Bin Mostofa Date: Fri, 8 Mar 2024 16:33:22 +0600 Subject: [PATCH] fix: package names can be of length two (#120) 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. --- internal/setup/setup.go | 11 ++- internal/setup/setup_test.go | 135 +++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 3 deletions(-) diff --git a/internal/setup/setup.go b/internal/setup/setup.go index fe142d92..b3faa492 100644 --- a/internal/setup/setup.go +++ b/internal/setup/setup.go @@ -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) @@ -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] diff --git a/internal/setup/setup_test.go b/internal/setup/setup_test.go index 9a92089b..02fd4887 100644 --- a/internal/setup/setup_test.go +++ b/internal/setup/setup_test.go @@ -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 = ` @@ -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) + } +}