Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: package names can be of length two #120

Merged
merged 10 commits into from
Mar 8, 2024
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$`)
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for missing this earlier, but why do we have a dot (.) in that second group? That doesn't look right.

Same question below.


// 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",
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
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
}
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved

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"},
}}
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved

func (s *S) TestParseSliceKey(c *C) {
for _, test := range sliceKeyTests {
key, err := setup.ParseSliceKey(test.input)
if err != nil || test.err != "" {
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
c.Assert(err, ErrorMatches, test.err)
continue
}
c.Assert(err, IsNil)
c.Assert(key, DeepEquals, test.expected)
}
}