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
1diff --git a/reviewtools/schemas/schema_validator.py b/reviewtools/schemas/schema_validator.py
2new file mode 100644
3index 0000000..20946d4
4--- /dev/null
5+++ b/reviewtools/schemas/schema_validator.py
6@@ -0,0 +1,14 @@
7+import os
8+import jsonschema
9+
10+
11+def validate(yaml, schema):
12+ resolver = jsonschema.RefResolver(
13+ "file://%s/" % os.path.abspath(os.path.dirname(__file__)), None
14+ )
15+
16+ try:
17+ jsonschema.validate(yaml, schema, resolver=resolver)
18+ return None
19+ except jsonschema.ValidationError as e:
20+ return e
21diff --git a/reviewtools/sr_lint.py b/reviewtools/sr_lint.py
22index 68a7664..1d4caae 100644
23--- a/reviewtools/sr_lint.py
24+++ b/reviewtools/sr_lint.py
25@@ -15,6 +15,7 @@
26 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28 from __future__ import print_function
29+from reviewtools.schemas.schema_validator import validate
30 from reviewtools.sr_common import SnapReview
31 from reviewtools.common import (
32 find_external_symlinks,
33@@ -31,7 +32,6 @@ import os
34 import re
35 import shlex
36 import unicodedata
37-import jsonschema
38 import json
39
40
41@@ -77,17 +77,14 @@ class SnapReviewLint(SnapReview):
42
43 def check_schema(self):
44 """Check snap.yaml against the schema"""
45- t = "info"
46 n = self._get_check_name("snap_schema")
47- s = "OK"
48 manual_review = False
49
50 with open("reviewtools/schemas/snap.json") as fd:
51 schema = json.loads(fd.read())
52
53- try:
54- jsonschema.validate(self.snap_yaml, schema)
55- except jsonschema.ValidationError as e:
56+ e = validate(self.snap_yaml, schema)
57+ if e is not None:
58 e.absolute_path.appendleft("$")
59 t = "error"
60 s = "Error found in snap.yaml while validating %s: %s" % (
61diff --git a/reviewtools/tests/schemas/test_schema_against_store.py b/reviewtools/tests/schemas/test_schema_against_store.py
62index 16259e2..8020f1b 100644
63--- a/reviewtools/tests/schemas/test_schema_against_store.py
64+++ b/reviewtools/tests/schemas/test_schema_against_store.py
65@@ -3,8 +3,8 @@ import json
66 import yaml
67 import os
68 import sys
69-import jsonschema
70 import unittest
71+from reviewtools.schemas.schema_validator import validate
72
73 SNAP_SCHEMA = "reviewtools/schemas/snap.json"
74 SNAP_YAML_CACHE = os.path.expanduser("~/.cache/review-tools")
75@@ -60,9 +60,8 @@ def test_store():
76 continue
77
78 # Validate the snap.yaml against the schema
79- try:
80- jsonschema.validate(snap_yaml, snap_schema)
81- except jsonschema.ValidationError as e:
82+ e = validate(snap_yaml, snap_schema)
83+ if e is not None:
84 print(
85 "Error to validate %s for snap %s: %s" % (e.json_path, snap, e.message)
86 )
87diff --git a/reviewtools/tests/schemas/test_schema_base.py b/reviewtools/tests/schemas/test_schema_base.py
88index b41f5dd..9a39e16 100644
89--- a/reviewtools/tests/schemas/test_schema_base.py
90+++ b/reviewtools/tests/schemas/test_schema_base.py
91@@ -1,7 +1,7 @@
92 import json
93-import jsonschema
94 import copy
95 import unittest
96+from reviewtools.schemas.schema_validator import validate
97
98
99 class TestSchemaBase(unittest.TestCase):
100@@ -14,17 +14,9 @@ class TestSchemaBase(unittest.TestCase):
101 self.schema = json.loads(fd.read())
102
103 # Validate base yaml against the schema
104- error = self._validate(self.yaml, self.schema)
105+ error = validate(self.yaml, self.schema)
106 self.assertEqual(None, error)
107
108- def _validate(self, yaml, schema):
109- # Validate yaml against the schema
110- try:
111- jsonschema.validate(yaml, schema)
112- return None
113- except jsonschema.ValidationError as e:
114- return e.message
115-
116 def _test_value(self, key, value, expected_error):
117 # Prepare yaml, make a copy so we use the pristine yaml on each subtest
118 yaml = copy.deepcopy(self.yaml)
119@@ -34,11 +26,11 @@ class TestSchemaBase(unittest.TestCase):
120 else:
121 yaml[key] = value
122
123- # Validate the schema
124- error = self._validate(yaml, self.schema)
125+ # Validate yaml against the schema
126+ e = validate(yaml, self.schema)
127
128 # Compare the errors against the expected ones
129 if expected_error is None:
130- self.assertIsNone(error)
131+ self.assertIsNone(e.message)
132 else:
133- self.assertIn(expected_error, error)
134+ self.assertIn(expected_error, e.message)

Subscribers

People subscribed via source and target branches