From 070152800176e51383a3f1c8c3d96b6018339aa2 Mon Sep 17 00:00:00 2001 From: Rafid Bin Mostofa Date: Thu, 8 Feb 2024 15:30:23 +0600 Subject: [PATCH 01/10] feat: package names can be of length two 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. Fixes #119. --- internal/setup/setup.go | 11 +++- internal/setup/setup_test.go | 110 +++++++++++++++++++++++++++++++++++ 2 files changed, 118 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..7b0bd5d4 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,65 @@ func runParseReleaseTests(c *C, tests []setupTest) { } } } + +type sliceKeyTest struct { + input string + expected setup.SliceKey + err string +} + +var sliceKeyTests = []sliceKeyTest{{ + input: "foo_bar", + expected: setup.SliceKey{Package: "foo", Slice: "bar"}, +}, { + input: "fo_bar", + expected: setup.SliceKey{Package: "fo", Slice: "bar"}, +}, { + input: "foo_ba", + err: `invalid slice reference: "foo_ba"`, +}, { + input: "f_bar", + err: `invalid slice reference: "f_bar"`, +}, { + input: "1234_bar", + expected: setup.SliceKey{Package: "1234", Slice: "bar"}, +}, { + input: "1234_789", + err: `invalid slice reference: "1234_789"`, +}, { + input: "foo1.1-2-3_bar", + expected: setup.SliceKey{Package: "foo1.1-2-3", Slice: "bar"}, +}, { + input: "chicken_bar.b.q", + err: `invalid slice reference: "chicken_bar.b.q"`, +}, { + input: "foo-pkg_dashed-slice-name", + expected: setup.SliceKey{Package: "foo-pkg", Slice: "dashed-slice-name"}, +}, { + 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_slice123", + expected: setup.SliceKey{Package: "foo", Slice: "slice123"}, +}} + +func (s *S) TestParseSliceKey(c *C) { + for _, test := range sliceKeyTests { + key, err := setup.ParseSliceKey(test.input) + if err != nil || test.err != "" { + c.Assert(err, ErrorMatches, test.err) + continue + } + c.Assert(err, IsNil) + c.Assert(key, DeepEquals, test.expected) + } +} From 5a275f4f5373a8c5c17ec7a3ab5687c4017cd40c Mon Sep 17 00:00:00 2001 From: Rafid Bin Mostofa Date: Fri, 9 Feb 2024 09:48:12 +0600 Subject: [PATCH 02/10] test: inline struct definition in slice key tests --- internal/setup/setup_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/internal/setup/setup_test.go b/internal/setup/setup_test.go index 7b0bd5d4..85f23f09 100644 --- a/internal/setup/setup_test.go +++ b/internal/setup/setup_test.go @@ -1150,13 +1150,11 @@ func runParseReleaseTests(c *C, tests []setupTest) { } } -type sliceKeyTest struct { +var sliceKeyTests = []struct{ input string expected setup.SliceKey err string -} - -var sliceKeyTests = []sliceKeyTest{{ +}{{ input: "foo_bar", expected: setup.SliceKey{Package: "foo", Slice: "bar"}, }, { From 8d85c30fe4e7b4823e308e7345161f8b71d2fc4f Mon Sep 17 00:00:00 2001 From: Rafid Bin Mostofa Date: Fri, 9 Feb 2024 09:49:52 +0600 Subject: [PATCH 03/10] test: fix formatting --- internal/setup/setup_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/setup/setup_test.go b/internal/setup/setup_test.go index 85f23f09..f8be4c48 100644 --- a/internal/setup/setup_test.go +++ b/internal/setup/setup_test.go @@ -1150,7 +1150,7 @@ func runParseReleaseTests(c *C, tests []setupTest) { } } -var sliceKeyTests = []struct{ +var sliceKeyTests = []struct { input string expected setup.SliceKey err string From 9d5fdbeea3616195dddf6eb6b679890212ffe57f Mon Sep 17 00:00:00 2001 From: Rafid Bin Mostofa Date: Wed, 14 Feb 2024 12:37:11 +0600 Subject: [PATCH 04/10] test: simplify error comparison logic --- internal/setup/setup_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/setup/setup_test.go b/internal/setup/setup_test.go index f8be4c48..e4f2acc3 100644 --- a/internal/setup/setup_test.go +++ b/internal/setup/setup_test.go @@ -1193,6 +1193,9 @@ var sliceKeyTests = []struct { }, { input: "-foo_bar", err: `invalid slice reference: "-foo_bar"`, +}, { + input: "foo+_bar", + expected: setup.SliceKey{Package: "foo+", Slice: "bar"}, }, { input: "foo_slice123", expected: setup.SliceKey{Package: "foo", Slice: "slice123"}, @@ -1201,7 +1204,7 @@ var sliceKeyTests = []struct { func (s *S) TestParseSliceKey(c *C) { for _, test := range sliceKeyTests { key, err := setup.ParseSliceKey(test.input) - if err != nil || test.err != "" { + if test.err != "" { c.Assert(err, ErrorMatches, test.err) continue } From 604a00b331ed4d79a5f399569f1a8479832df9bd Mon Sep 17 00:00:00 2001 From: Rafid Bin Mostofa Date: Wed, 14 Feb 2024 12:40:19 +0600 Subject: [PATCH 05/10] refactor: fix formatting issues --- internal/setup/setup_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/setup/setup_test.go b/internal/setup/setup_test.go index e4f2acc3..966a6d8c 100644 --- a/internal/setup/setup_test.go +++ b/internal/setup/setup_test.go @@ -1194,7 +1194,7 @@ var sliceKeyTests = []struct { input: "-foo_bar", err: `invalid slice reference: "-foo_bar"`, }, { - input: "foo+_bar", + input: "foo+_bar", expected: setup.SliceKey{Package: "foo+", Slice: "bar"}, }, { input: "foo_slice123", From 0f724aeb5367e367f786b424e8b9cbdcc559dbec Mon Sep 17 00:00:00 2001 From: Rafid Bin Mostofa Date: Wed, 14 Feb 2024 16:55:33 +0600 Subject: [PATCH 06/10] refactor: add pkg and slice name expression string Add pkg and slice name expression string variables to remove duplication and increase test coverage. --- internal/setup/setup.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/setup/setup.go b/internal/setup/setup.go index b3faa492..23079796 100644 --- a/internal/setup/setup.go +++ b/internal/setup/setup.go @@ -242,14 +242,17 @@ func order(pkgs map[string]*Package, keys []SliceKey) ([]SliceKey, error) { return order, nil } +var pkgNameExpStr = `([a-z0-9](?:-?[.a-z0-9+]){1,})` +var sliceNameExpStr = `([a-z](?:-?[a-z0-9]){2,})` + // fnameExp matches the slice definition file basename. -var fnameExp = regexp.MustCompile(`^([a-z0-9](?:-?[.a-z0-9+]){1,})\.yaml$`) +var fnameExp = regexp.MustCompile(`^` + pkgNameExpStr + `\.yaml$`) // snameExp matches only the slice name, without the leading package name. -var snameExp = regexp.MustCompile(`^([a-z](?:-?[a-z0-9]){2,})$`) +var snameExp = regexp.MustCompile(`^` + sliceNameExpStr + `$`) // 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,})$`) +var knameExp = regexp.MustCompile(`^` + pkgNameExpStr + `_` + sliceNameExpStr + `$`) func ParseSliceKey(sliceKey string) (SliceKey, error) { match := knameExp.FindStringSubmatch(sliceKey) From be332e0d2dbd897836e1e3c6f1d1d08109701538 Mon Sep 17 00:00:00 2001 From: Rafid Bin Mostofa Date: Thu, 15 Feb 2024 12:02:11 +0600 Subject: [PATCH 07/10] test: add more test data for ParseSliceKey --- internal/setup/setup_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/internal/setup/setup_test.go b/internal/setup/setup_test.go index 966a6d8c..18ddbce1 100644 --- a/internal/setup/setup_test.go +++ b/internal/setup/setup_test.go @@ -1199,6 +1199,30 @@ var sliceKeyTests = []struct { }, { input: "foo_slice123", expected: setup.SliceKey{Package: "foo", Slice: "slice123"}, +}, { + input: "foo_bar_baz", + err: `invalid slice reference: "foo_bar_baz"`, +}, { + input: "g++_bins", + expected: setup.SliceKey{Package: "g++", Slice: "bins"}, +}, { + input: "a+_bar", + expected: setup.SliceKey{Package: "a+", Slice: "bar"}, +}, { + input: "a-_bar", + err: `invalid slice reference: "a-_bar"`, +}, { + input: "a._bar", + expected: setup.SliceKey{Package: "a.", Slice: "bar"}, +}, { + input: "+++_bar", + err: `invalid slice reference: .*`, +}, { + input: "..._bar", + err: `invalid slice reference: .*`, +}, { + input: "white space_no-whitespace", + err: `invalid slice reference: "white space_no-whitespace"`, }} func (s *S) TestParseSliceKey(c *C) { From 1f8cedfed84ce595093e6f95d962bbac37918432 Mon Sep 17 00:00:00 2001 From: Rafid Bin Mostofa Date: Thu, 15 Feb 2024 15:43:06 +0600 Subject: [PATCH 08/10] refactor: group the ParseSliceKey tests --- internal/setup/setup_test.go | 52 ++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/internal/setup/setup_test.go b/internal/setup/setup_test.go index 18ddbce1..eeda3d50 100644 --- a/internal/setup/setup_test.go +++ b/internal/setup/setup_test.go @@ -1160,27 +1160,42 @@ var sliceKeyTests = []struct { }, { 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_bar", - expected: setup.SliceKey{Package: "1234", Slice: "bar"}, }, { input: "1234_789", err: `invalid slice reference: "1234_789"`, -}, { - input: "foo1.1-2-3_bar", - expected: setup.SliceKey{Package: "foo1.1-2-3", Slice: "bar"}, }, { input: "chicken_bar.b.q", err: `invalid slice reference: "chicken_bar.b.q"`, -}, { - input: "foo-pkg_dashed-slice-name", - expected: setup.SliceKey{Package: "foo-pkg", Slice: "dashed-slice-name"}, }, { input: "foo-_-bar", err: `invalid slice reference: "foo-_-bar"`, @@ -1193,33 +1208,18 @@ var sliceKeyTests = []struct { }, { input: "-foo_bar", err: `invalid slice reference: "-foo_bar"`, -}, { - input: "foo+_bar", - expected: setup.SliceKey{Package: "foo+", Slice: "bar"}, -}, { - input: "foo_slice123", - expected: setup.SliceKey{Package: "foo", Slice: "slice123"}, }, { input: "foo_bar_baz", err: `invalid slice reference: "foo_bar_baz"`, -}, { - input: "g++_bins", - expected: setup.SliceKey{Package: "g++", Slice: "bins"}, -}, { - input: "a+_bar", - expected: setup.SliceKey{Package: "a+", Slice: "bar"}, }, { input: "a-_bar", err: `invalid slice reference: "a-_bar"`, -}, { - input: "a._bar", - expected: setup.SliceKey{Package: "a.", Slice: "bar"}, }, { input: "+++_bar", - err: `invalid slice reference: .*`, + err: `invalid slice reference: "\+\+\+_bar"`, }, { input: "..._bar", - err: `invalid slice reference: .*`, + err: `invalid slice reference: "\.\.\._bar"`, }, { input: "white space_no-whitespace", err: `invalid slice reference: "white space_no-whitespace"`, From eadd7047d6d1dc7a78263718bbea971405471822 Mon Sep 17 00:00:00 2001 From: Rafid Bin Mostofa Date: Thu, 15 Feb 2024 15:58:50 +0600 Subject: [PATCH 09/10] test: use proper values in test data --- internal/setup/setup_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/setup/setup_test.go b/internal/setup/setup_test.go index eeda3d50..02fd4887 100644 --- a/internal/setup/setup_test.go +++ b/internal/setup/setup_test.go @@ -1194,8 +1194,8 @@ var sliceKeyTests = []struct { input: "1234_789", err: `invalid slice reference: "1234_789"`, }, { - input: "chicken_bar.b.q", - err: `invalid slice reference: "chicken_bar.b.q"`, + input: "foo_bar.x.y", + err: `invalid slice reference: "foo_bar.x.y"`, }, { input: "foo-_-bar", err: `invalid slice reference: "foo-_-bar"`, From 662f3312b13e7c457a7ef690e42f732486e13ae0 Mon Sep 17 00:00:00 2001 From: Rafid Bin Mostofa Date: Fri, 8 Mar 2024 06:58:21 +0600 Subject: [PATCH 10/10] Revert "refactor: add pkg and slice name expression string" This reverts commit 0f724aeb5367e367f786b424e8b9cbdcc559dbec. --- internal/setup/setup.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/internal/setup/setup.go b/internal/setup/setup.go index 23079796..b3faa492 100644 --- a/internal/setup/setup.go +++ b/internal/setup/setup.go @@ -242,17 +242,14 @@ func order(pkgs map[string]*Package, keys []SliceKey) ([]SliceKey, error) { return order, nil } -var pkgNameExpStr = `([a-z0-9](?:-?[.a-z0-9+]){1,})` -var sliceNameExpStr = `([a-z](?:-?[a-z0-9]){2,})` - // fnameExp matches the slice definition file basename. -var fnameExp = regexp.MustCompile(`^` + pkgNameExpStr + `\.yaml$`) +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(`^` + sliceNameExpStr + `$`) +var snameExp = regexp.MustCompile(`^([a-z](?:-?[a-z0-9]){2,})$`) // knameExp matches the slice full name in pkg_slice format. -var knameExp = regexp.MustCompile(`^` + pkgNameExpStr + `_` + sliceNameExpStr + `$`) +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)