Merge lp:~chipaca/snappy/deprecated-architectures into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by John Lenton on 2015-05-02
Status: Merged
Approved by: Sergio Schvezov on 2015-05-05
Approved revision: 442
Merged at revision: 439
Proposed branch: lp:~chipaca/snappy/deprecated-architectures
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Prerequisite: lp:~chipaca/snappy/copyfile
Diff against target: 135 lines (+61/-16)
4 files modified
snappy/build.go (+6/-2)
snappy/build_test.go (+6/-0)
snappy/snapp.go (+25/-14)
snappy/snapp_test.go (+24/-0)
To merge this branch: bzr merge lp:~chipaca/snappy/deprecated-architectures
Reviewer Review Type Date Requested Status
Sergio Schvezov Approve on 2015-05-05
Michael Vogt 2015-05-02 Needs Information on 2015-05-03
Review via email: mp+258112@code.launchpad.net

This proposal supersedes a proposal from 2015-05-02.

Commit Message

Improved parsing of deprecated "architecture" entry of package.yaml.

Description of the Change

Currently, the deprecated "architecture" entry in package.yaml is loaded as an interface{}, and only some of the values therein are considered.

I've had a bit of fun loading arbitrarily large data structures into that before I got bored and fixed it.

Two silly examples of things that should've been failing with an error but weren't are included as tests. One leaves the packageYaml struct with a nil 'Architectures' field (which goes on to cause a panic during build); the other panics in the parser itself. You get to guess which one did which (or you can revert back to the first commit, which is just the tests).

The panic during build is because debArchitecture doesn't check bounds. Not sure what it can do if Architecture is empty on the package, though; have gone with returning "unknown", but maybe we _want_ it to panic there.

Way-too-much-fun-on-the-train-home-ly yours,

To post a comment you must log in.
Michael Vogt (mvo) wrote :

Thanks for fixing this! Code looks great, one question inline.

review: Approve
Michael Vogt (mvo) wrote :

Hm, setting to needs information just to be sure that the inline question is not forgotten :)

review: Needs Information
John Lenton (chipaca) :
Sergio Schvezov (sergiusens) wrote :

looks good and clean, just one comment

Sergio Schvezov (sergiusens) wrote :

nevermind my flaky eyes!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'snappy/build.go'
2--- snappy/build.go 2015-05-02 22:53:19 +0000
3+++ snappy/build.go 2015-05-02 22:53:19 +0000
4@@ -83,10 +83,14 @@
5
6 // small helper that return the architecture or "multi" if its multiple arches
7 func debArchitecture(m *packageYaml) string {
8- if len(m.Architectures) > 1 {
9+ switch len(m.Architectures) {
10+ case 0:
11+ return "unknown"
12+ case 1:
13+ return m.Architectures[0]
14+ default:
15 return "multi"
16 }
17- return m.Architectures[0]
18 }
19
20 func parseReadme(readme string) (title, description string, err error) {
21
22=== modified file 'snappy/build_test.go'
23--- snappy/build_test.go 2015-04-16 16:30:26 +0000
24+++ snappy/build_test.go 2015-05-02 22:53:19 +0000
25@@ -403,3 +403,9 @@
26 _, err := Build(sourceDir, "")
27 c.Assert(err, ErrorMatches, ".*binary and service both called foo.*")
28 }
29+
30+func (s *SnapTestSuite) TestDebArchitecture(c *C) {
31+ c.Check(debArchitecture(&packageYaml{Architectures: []string{"foo"}}), Equals, "foo")
32+ c.Check(debArchitecture(&packageYaml{Architectures: []string{"foo", "bar"}}), Equals, "multi")
33+ c.Check(debArchitecture(&packageYaml{Architectures: nil}), Equals, "unknown")
34+}
35
36=== modified file 'snappy/snapp.go'
37--- snappy/snapp.go 2015-05-01 12:32:42 +0000
38+++ snappy/snapp.go 2015-05-02 22:53:19 +0000
39@@ -172,6 +172,26 @@
40
41 var commasplitter = regexp.MustCompile(`\s*,\s*`).Split
42
43+// deprecarch handles the vagaries of the now-deprecated
44+// "architecture" field of the package.yaml
45+type deprecarch []string
46+
47+func (v *deprecarch) UnmarshalYAML(unmarshal func(interface{}) error) error {
48+ var l []string
49+
50+ if err := unmarshal(&l); err != nil {
51+ var s string
52+ if err := unmarshal(&s); err != nil {
53+ return err
54+
55+ }
56+ l = append([]string(nil), s)
57+ }
58+ *v = deprecarch(l)
59+
60+ return nil
61+}
62+
63 // TODO split into payloads per package type composing the common
64 // elements for all snaps.
65 type packageYaml struct {
66@@ -183,8 +203,8 @@
67
68 // the spec allows a string or a list here *ick* so we need
69 // to convert that into something sensible via reflect
70- DeprecatedArchitecture interface{} `yaml:"architecture"`
71- Architectures []string `yaml:"architectures"`
72+ DeprecatedArchitecture deprecarch `yaml:"architecture"`
73+ Architectures []string `yaml:"architectures"`
74
75 DeprecatedFramework string `yaml:"framework,omitempty"`
76 Frameworks []string `yaml:"frameworks,omitempty"`
77@@ -247,20 +267,11 @@
78 return nil, err
79 }
80
81- // parse the architecture: field that is either a string or a list
82- // or empty (yes, you read that correctly)
83 if m.Architectures == nil {
84- v := reflect.ValueOf(m.DeprecatedArchitecture)
85- switch v.Kind() {
86- case reflect.Invalid:
87+ if m.DeprecatedArchitecture == nil {
88 m.Architectures = []string{"all"}
89- case reflect.String:
90- m.Architectures = []string{v.String()}
91- case reflect.Slice:
92- v2 := m.DeprecatedArchitecture.([]interface{})
93- for _, arch := range v2 {
94- m.Architectures = append(m.Architectures, arch.(string))
95- }
96+ } else {
97+ m.Architectures = m.DeprecatedArchitecture
98 }
99 }
100
101
102=== modified file 'snappy/snapp_test.go'
103--- snappy/snapp_test.go 2015-04-22 09:06:36 +0000
104+++ snappy/snapp_test.go 2015-05-02 22:53:19 +0000
105@@ -801,6 +801,30 @@
106 c.Assert(m.Architectures, DeepEquals, []string{"all"})
107 }
108
109+func (s *SnapTestSuite) TestPackageYamlBadArchitectureParsing(c *C) {
110+ data := []byte(`name: fatbinary
111+version: 1.0
112+vendor: Michael Vogt <mvo@ubuntu.com>
113+architecture:
114+ armhf:
115+ no
116+`)
117+ _, err := parsePackageYamlData(data)
118+ c.Assert(err, NotNil)
119+}
120+
121+func (s *SnapTestSuite) TestPackageYamlWorseArchitectureParsing(c *C) {
122+ data := []byte(`name: fatbinary
123+version: 1.0
124+vendor: Michael Vogt <mvo@ubuntu.com>
125+architecture:
126+ - armhf:
127+ sometimes
128+`)
129+ _, err := parsePackageYamlData(data)
130+ c.Assert(err, NotNil)
131+}
132+
133 func (s *SnapTestSuite) TestPackageYamlLicenseParsing(c *C) {
134 y := filepath.Join(s.tempdir, "package.yaml")
135 ioutil.WriteFile(y, []byte(`explicit-license-agreement: Y`), 0644)

Subscribers

People subscribed via source and target branches