Improve snapcraft.yaml validation errors. #296

Merged
merged 1 commit into from Feb 4, 2016

Conversation

Projects
None yet
3 participants
Member

kyrofa commented Feb 4, 2016

This PR fixes LP: #1524663 by improving YAML validation errors across the board. Specifically:

  • Improve error message if icon isn't a .png or .svg, or doesn't exist.

Was: "'icon.png' is not an icon-path"
Now: "Specified icon 'icon.png' does not exist" or "'icon' must be either a .png or a .svg"

  • Improve error message if apparmor or seccomp profiles don't exist.

    Was: "The 'migration' property does not match the required schema: {'security-policy': {'apparmor': 'path/profile', 'seccomp': 'path/profile'}, 'type': 'migration-skill'} is not valid under any of the given schemas"
    Now: "Specified file 'path/profile' does not exist"

  • Improve generic jsonschema errors to include the properties which had the error.

    Was: "None is not of type 'object'"
    Now: "The 'parts' property does not match the required schema: None is not of type 'object'"

snapcraft/tests/test_yaml.py
@@ -283,7 +283,89 @@ def test_invalid_yaml_invalid_name_as_number(self, mock_loadPlugin):
snapcraft.yaml.Config()
self.assertEqual(raised.exception.message,
- '1 is not of type \'string\'')
+ 'The \'name\' property does not match the required '
+ 'schema: 1 is not of type \'string\'')
@elopio

elopio Feb 4, 2016

Member

If you use double quotes then you won't have to escape the single quotes. Same for the rest of the tests.

@kyrofa

kyrofa Feb 4, 2016

Member

Yeah, but jsonschema's error messages use single-quotes, so I had to bite the bullet and maintain consistency.

@elopio

elopio Feb 4, 2016

Member

right. I mean the exception to the rule of using single quotes to avoid me going nuts is when the string contains single quotes. In that case, use double quotes. Hah, confusing. So, the style we are trying to follow in the code is:
"The 'name' property does not match the required "
"schema: 1 is not of type 'string'"

Of course not all the old code is following it, as your deleted lines show.

@kyrofa

kyrofa Feb 4, 2016

Member

Haha! Good to know @elopio, I'll update this real quick.

@kyrofa

kyrofa Feb 4, 2016

Member

Fixed.

Collaborator

sergiusens commented Feb 4, 2016

Looks good, jsonschema could be better and make this easier instead of needing to go into hacks^w clever tricks for the formatter 😉

Let's wait for @elopio and the tests!

Member

kyrofa commented Feb 4, 2016

@sergiusens heh, no kidding! I couldn't agree more.

snapcraft/yaml.py
@jsonschema.FormatChecker.cls_checks('icon-path')
def _validate_icon(instance):
normalized_icon = instance.lower()
- ext = normalized_icon.endswith('.png') or normalized_icon.endswith('.svg')
- return os.path.exists(instance) and ext
+ if not (normalized_icon.endswith('.png') or
@elopio

elopio Feb 4, 2016

Member

You could use os.path.splitext once, and then just check for equality.
I'm not too convinced that it would be much clearer, so feel free to ignore.

@sergiusens

sergiusens Feb 4, 2016

Collaborator

if you do that, I'd prefer to use one of the fancy python things https://docs.python.org/3.5/library/functions.html#any

@kyrofa

kyrofa Feb 4, 2016

Member

Alright I revamped this a bit-- I think it's cleaner. Though @sergiusens I didn't see a good use for any here.

Member

elopio commented Feb 4, 2016

👍
just an ignorable comment about using splitext.

Improve snapcraft.yaml validation errors.
Specifically:

- Improve error message if icon isn't a .png or .svg, or doesn't
  exist.
- Improve error message if apparmor or seccomp profiles don't exist.
- Improve generic jsonschema errors to include the properties which
  had the error.

LP: #1524663

Signed-off-by: Kyle Fazzari <kyle@canonical.com>
Collaborator

sergiusens commented Feb 4, 2016

👍

kyrofa added a commit that referenced this pull request Feb 4, 2016

@kyrofa kyrofa merged commit dab07dd into snapcore:master Feb 4, 2016

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls First build on master at 93.212%
Details

@kyrofa kyrofa deleted the kyrofa:bugfix/1524663/better_yaml_errors branch Feb 4, 2016

smoser pushed a commit to smoser/snapcraft that referenced this pull request Sep 14, 2016

Merge pull request #296 from brendandixon/telemetry
Improved telemetry and enabled re-spawning of child process

kalikiana pushed a commit to kalikiana/snapcraft that referenced this pull request Apr 6, 2017

Merge pull request #296 from kyrofa/bugfix/1524663/better_yaml_errors
Improve snapcraft.yaml validation errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment