Skip to content

Commit

Permalink
feat: package names can be of length two
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.

Fixes #119.
  • Loading branch information
rebornplusplus committed Feb 8, 2024
1 parent 11a324c commit 0701528
Show file tree
Hide file tree
Showing 2 changed files with 118 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
110 changes: 110 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,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)
}
}

0 comments on commit 0701528

Please sign in to comment.