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

snap: detect layouts vs layout in snap.yaml #5869

Merged
merged 7 commits into from Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions snap/info_snap_yaml.go
Expand Up @@ -53,6 +53,17 @@ type snapYaml struct {
Apps map[string]appYaml `yaml:"apps,omitempty"`
Hooks map[string]hookYaml `yaml:"hooks,omitempty"`
Layout map[string]layoutYaml `yaml:"layout,omitempty"`

// TypoLayouts is used to detect the use of the incorrect plural form of "layout"
TypoLayouts typoDetector `yaml:"layouts,omitempty"`
}

type typoDetector struct {
Hint string
}

func (td *typoDetector) UnmarshalYAML(func(interface{}) error) error {
return fmt.Errorf("typo detected: %s", td.Hint)
}

type appYaml struct {
Expand Down Expand Up @@ -116,6 +127,8 @@ type socketsYaml struct {
// InfoFromSnapYaml creates a new info based on the given snap.yaml data
func InfoFromSnapYaml(yamlData []byte) (*Info, error) {
var y snapYaml
// Customize hints for the typo detector.
y.TypoLayouts.Hint = `use singular "layout" instead of plural "layouts"`
Copy link
Contributor

Choose a reason for hiding this comment

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

heh. ingenious.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See, I told you :D

err := yaml.Unmarshal(yamlData, &y)
if err != nil {
return nil, fmt.Errorf("cannot parse snap.yaml: %s", err)
Expand Down
13 changes: 13 additions & 0 deletions snap/info_snap_yaml_test.go
Expand Up @@ -1693,6 +1693,19 @@ layout:
})
}

func (s *YamlSuite) TestLayoutsWithTypo(c *C) {
y := []byte(`
name: foo
version: 1.0
layouts:
/usr/share/foo:
bind: $SNAP/usr/share/foo
`)
info, err := snap.InfoFromSnapYaml(y)
c.Assert(err, ErrorMatches, `cannot parse snap.yaml: typo detected: use singular "layout" instead of plural "layouts"`)
c.Assert(info, IsNil)
}

func (s *YamlSuite) TestSnapYamlAppTimer(c *C) {
y := []byte(`name: wat
version: 42
Expand Down