Merge ~jslarraz/review-tools:unify-validate-call into review-tools:master

Proposed by Jorge Sancho Larraz
Status: Rejected
Rejected by: Jorge Sancho Larraz
Proposed branch: ~jslarraz/review-tools:unify-validate-call
Merge into: review-tools:master
Diff against target: 134 lines (+26/-24)
4 files modified
reviewtools/schemas/schema_validator.py (+14/-0)
reviewtools/sr_lint.py (+3/-6)
reviewtools/tests/schemas/test_schema_against_store.py (+3/-4)
reviewtools/tests/schemas/test_schema_base.py (+6/-14)
Reviewer Review Type Date Requested Status
MyApps reviewers Pending
Review via email: mp+466303@code.launchpad.net

Commit message

sr_lint,test_schema_base: unify how jsonschema.validate is called in sr_lint and tests

To post a comment you must log in.

Unmerged commits

bc8a130... by Jorge Sancho Larraz

rt/{sr_lint,tests/schema}: unify how jsonschema is validated

Succeeded
[SUCCEEDED] test:0 (build)
[SUCCEEDED] coverage:0 (build)
12 of 2 results

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/reviewtools/schemas/schema_validator.py b/reviewtools/schemas/schema_validator.py
0new file mode 1006440new file mode 100644
index 0000000..20946d4
--- /dev/null
+++ b/reviewtools/schemas/schema_validator.py
@@ -0,0 +1,14 @@
1import os
2import jsonschema
3
4
5def validate(yaml, schema):
6 resolver = jsonschema.RefResolver(
7 "file://%s/" % os.path.abspath(os.path.dirname(__file__)), None
8 )
9
10 try:
11 jsonschema.validate(yaml, schema, resolver=resolver)
12 return None
13 except jsonschema.ValidationError as e:
14 return e
diff --git a/reviewtools/sr_lint.py b/reviewtools/sr_lint.py
index 68a7664..1d4caae 100644
--- a/reviewtools/sr_lint.py
+++ b/reviewtools/sr_lint.py
@@ -15,6 +15,7 @@
15# along with this program. If not, see <http://www.gnu.org/licenses/>.15# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17from __future__ import print_function17from __future__ import print_function
18from reviewtools.schemas.schema_validator import validate
18from reviewtools.sr_common import SnapReview19from reviewtools.sr_common import SnapReview
19from reviewtools.common import (20from reviewtools.common import (
20 find_external_symlinks,21 find_external_symlinks,
@@ -31,7 +32,6 @@ import os
31import re32import re
32import shlex33import shlex
33import unicodedata34import unicodedata
34import jsonschema
35import json35import json
3636
3737
@@ -77,17 +77,14 @@ class SnapReviewLint(SnapReview):
7777
78 def check_schema(self):78 def check_schema(self):
79 """Check snap.yaml against the schema"""79 """Check snap.yaml against the schema"""
80 t = "info"
81 n = self._get_check_name("snap_schema")80 n = self._get_check_name("snap_schema")
82 s = "OK"
83 manual_review = False81 manual_review = False
8482
85 with open("reviewtools/schemas/snap.json") as fd:83 with open("reviewtools/schemas/snap.json") as fd:
86 schema = json.loads(fd.read())84 schema = json.loads(fd.read())
8785
88 try:86 e = validate(self.snap_yaml, schema)
89 jsonschema.validate(self.snap_yaml, schema)87 if e is not None:
90 except jsonschema.ValidationError as e:
91 e.absolute_path.appendleft("$")88 e.absolute_path.appendleft("$")
92 t = "error"89 t = "error"
93 s = "Error found in snap.yaml while validating %s: %s" % (90 s = "Error found in snap.yaml while validating %s: %s" % (
diff --git a/reviewtools/tests/schemas/test_schema_against_store.py b/reviewtools/tests/schemas/test_schema_against_store.py
index 16259e2..8020f1b 100644
--- a/reviewtools/tests/schemas/test_schema_against_store.py
+++ b/reviewtools/tests/schemas/test_schema_against_store.py
@@ -3,8 +3,8 @@ import json
3import yaml3import yaml
4import os4import os
5import sys5import sys
6import jsonschema
7import unittest6import unittest
7from reviewtools.schemas.schema_validator import validate
88
9SNAP_SCHEMA = "reviewtools/schemas/snap.json"9SNAP_SCHEMA = "reviewtools/schemas/snap.json"
10SNAP_YAML_CACHE = os.path.expanduser("~/.cache/review-tools")10SNAP_YAML_CACHE = os.path.expanduser("~/.cache/review-tools")
@@ -60,9 +60,8 @@ def test_store():
60 continue60 continue
6161
62 # Validate the snap.yaml against the schema62 # Validate the snap.yaml against the schema
63 try:63 e = validate(snap_yaml, snap_schema)
64 jsonschema.validate(snap_yaml, snap_schema)64 if e is not None:
65 except jsonschema.ValidationError as e:
66 print(65 print(
67 "Error to validate %s for snap %s: %s" % (e.json_path, snap, e.message)66 "Error to validate %s for snap %s: %s" % (e.json_path, snap, e.message)
68 )67 )
diff --git a/reviewtools/tests/schemas/test_schema_base.py b/reviewtools/tests/schemas/test_schema_base.py
index b41f5dd..9a39e16 100644
--- a/reviewtools/tests/schemas/test_schema_base.py
+++ b/reviewtools/tests/schemas/test_schema_base.py
@@ -1,7 +1,7 @@
1import json1import json
2import jsonschema
3import copy2import copy
4import unittest3import unittest
4from reviewtools.schemas.schema_validator import validate
55
66
7class TestSchemaBase(unittest.TestCase):7class TestSchemaBase(unittest.TestCase):
@@ -14,17 +14,9 @@ class TestSchemaBase(unittest.TestCase):
14 self.schema = json.loads(fd.read())14 self.schema = json.loads(fd.read())
1515
16 # Validate base yaml against the schema16 # Validate base yaml against the schema
17 error = self._validate(self.yaml, self.schema)17 error = validate(self.yaml, self.schema)
18 self.assertEqual(None, error)18 self.assertEqual(None, error)
1919
20 def _validate(self, yaml, schema):
21 # Validate yaml against the schema
22 try:
23 jsonschema.validate(yaml, schema)
24 return None
25 except jsonschema.ValidationError as e:
26 return e.message
27
28 def _test_value(self, key, value, expected_error):20 def _test_value(self, key, value, expected_error):
29 # Prepare yaml, make a copy so we use the pristine yaml on each subtest21 # Prepare yaml, make a copy so we use the pristine yaml on each subtest
30 yaml = copy.deepcopy(self.yaml)22 yaml = copy.deepcopy(self.yaml)
@@ -34,11 +26,11 @@ class TestSchemaBase(unittest.TestCase):
34 else:26 else:
35 yaml[key] = value27 yaml[key] = value
3628
37 # Validate the schema29 # Validate yaml against the schema
38 error = self._validate(yaml, self.schema)30 e = validate(yaml, self.schema)
3931
40 # Compare the errors against the expected ones32 # Compare the errors against the expected ones
41 if expected_error is None:33 if expected_error is None:
42 self.assertIsNone(error)34 self.assertIsNone(e.message)
43 else:35 else:
44 self.assertIn(expected_error, error)36 self.assertIn(expected_error, e.message)

Subscribers

People subscribed via source and target branches