Skip to content

Commit 796548e

Browse files
Merge pull request #938 from jfunez/validator_fix_lista_de_erros
Fixes no Stylechecker validator
2 parents 0bb80ae + ed975db commit 796548e

File tree

4 files changed

+38
-52
lines changed

4 files changed

+38
-52
lines changed

scielomanager/articletrack/tests/tests_pages.py

+3-20
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,8 @@ def get_xml_uri(self, attempt_id, target_name):
296296
self.assertIsNone(xml_data['annotations'])
297297
self.assertEqual(xml_data['uri'], expected_response['uri'])
298298
expect_errors = {
299-
'error_lines': '1',
300-
'results': [
301-
{
302-
'column': 6,
303-
'line': 1,
304-
'message': u'Premature end of data in tag xml line 1, line 1, column 6',
305-
'level': 'ERROR'
306-
}
307-
]
299+
'error_lines': '',
300+
'results': []
308301
}
309302
self.assertEqual(xml_data['validation_errors'], expect_errors)
310303
self.assertEqual(xml_data['file_name'], expected_response['filename'])
@@ -427,17 +420,7 @@ def get_xml_uri(self, attempt_id, target_name):
427420
self.assertIsNone(xml_data['annotations'])
428421
self.assertEqual(xml_data['uri'], expected_response['uri'])
429422
self.assertEqual(xml_data['file_name'], expected_response['filename'])
430-
expect_errors = {
431-
'error_lines': '1',
432-
'results': [
433-
{
434-
'column': 6,
435-
'line': 1,
436-
'message': u'Premature end of data in tag xml line 1, line 1, column 6',
437-
'level': 'ERROR'
438-
}
439-
]
440-
}
423+
expect_errors = {'error_lines': '', 'results': []}
441424
self.assertEqual(xml_data['validation_errors'], expect_errors)
442425

443426
def test_annotations_of_syntax_error(self):

scielomanager/validator/templates/validator/includes/xml_annotated.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,25 @@ <h4><i class="icon-minus-sign"></i> {% trans "The XML could not be analyzed" %}<
3434
{% else %}
3535
{# xml analyzed and with annotations #}
3636
<div class="alert alert-block alert-error">
37-
<h4 class="alert-heading"> {% trans "The XML have some errors" %}:</h4>
37+
<h4 class="alert-heading">{{ xml_data.validation_errors.results|length }} {% trans "error(s) found while checking this XML" %}:</h4>
3838
</div>
3939
{% if xml_data.validation_errors %}
4040
<table id="validation_errors_table" class="table table-striped table-condensed">
4141
<thead>
4242
<tr>
43-
<th>{% trans "Level" %}:</th>
44-
<th>{% trans "Line" %}:</th>
45-
<th>{% trans "Column" %}:</th>
43+
<th class="span1">{% trans "Level" %}:</th>
4644
<th>{% trans "Message" %}:</th>
45+
<th class="span2">{% trans "Line" %}:</th>
46+
<th class="span2">{% trans "Column" %}:</th>
4747
</tr>
4848
</thead>
4949
<tbody>
5050
{% for error in xml_data.validation_errors.results %}
5151
<tr>
5252
<td><span class="label label-{% trans_status error.level to_label='True' %}">{{ error.level|upper }}</span></td>
53+
<td><a class="error_message" href="#" data-search-term="{{ error.message }}">{{ error.message }}</a></td>
5354
<td>{{ error.line|default:"--" }}</td>
5455
<td>{{ error.column|default:"--" }}</td>
55-
<td><a class="error_message" href="#" data-search-term="{{ error.message }}">{{ error.message }}</a></td>
5656
</tr>
5757
{% endfor %}
5858
</tbody>

scielomanager/validator/tests/tests_pages.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ def test_submite_invalid_url_then_form_is_not_valid(self):
150150
self.assertFalse(hasattr(response.context, 'results'))
151151

152152
def test_submit_valid_url_then_form_is_valid(self):
153+
"""
154+
Submitting a valid url, will not raise a validation error of the form.
155+
"""
153156
# with
154157
self._addWaffleFlag()
155158
self._mocker_replace_stylechecker()
@@ -166,17 +169,7 @@ def test_submit_valid_url_then_form_is_valid(self):
166169
self.assertEqual(response.status_code, 200)
167170
self.assertTrue(response.context['form'].is_valid())
168171
expected_results = {
169-
'validation_errors': {
170-
'error_lines': '1',
171-
'results': [
172-
{
173-
'column': 6,
174-
'level': 'ERROR',
175-
'line': 1,
176-
'message': u'Premature end of data in tag xml line 1, line 1, column 6'
177-
}
178-
]
179-
},
172+
'validation_errors': {'error_lines': '', 'results': []},
180173
'annotations': None,
181174
'can_be_analyzed': (True, None)
182175
}

scielomanager/validator/utils.py

+26-16
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88

99
class ErrorCollection(object):
10-
_errors = []
1110

12-
def add_object_error(self, error_obj=None, line='--', column='--', message='', level="ERROR"):
11+
def __init__(self):
12+
self._errors = []
13+
14+
def add_object_error(self, error_obj=None, line='--', column='--', message='', level="ERROR", allow_repeted=True):
1315
if error_obj:
1416
line = getattr(error_obj, 'line', line)
1517
column = getattr(error_obj, 'column', column)
@@ -22,24 +24,23 @@ def add_object_error(self, error_obj=None, line='--', column='--', message='', l
2224
'message': message,
2325
'level': level,
2426
}
25-
if error_data not in self._errors:
27+
if (error_data not in self._errors) or allow_repeted:
28+
# if error was not included yet, let's append it.
29+
# if error already included but allow_repeted == True, it's ok to append it again.
2630
self._errors.append(error_data)
2731

28-
def add_exception_error(self, exception_instance):
32+
def add_exception_error(self, exception_instance, allow_repeted=True):
2933
message = exception_instance.message
3034
if hasattr(exception_instance, 'position'):
3135
line, column = exception_instance.position
3236
else:
3337
line, column = None, None
34-
self.add_object_error(error_obj=None, line=line, column=column, message=message)
38+
self.add_object_error(error_obj=None, line=line, column=column, message=message, allow_repeted=allow_repeted)
3539

3640
def add_list_of_errors(self, iterable, allow_repeted=True):
37-
for error in iterable:
38-
if error not in self._errors:
39-
self.add_object_error(error)
40-
elif allow_repeted:
41-
# error already exist in _errors list, but is added again
42-
self.add_object_error(error)
41+
if iterable:
42+
for error in iterable:
43+
self.add_object_error(error, allow_repeted=allow_repeted)
4344

4445
def get_list(self):
4546
return self._errors
@@ -60,7 +61,6 @@ class StyleCheckerAnalyzer(object):
6061
_can_be_analyzed = (False, "Can't be analyzed")
6162
_can_be_analyzed_as_exception = False
6263
_annotations = None
63-
_validation_errors = {'results': [], 'error_lines': [], }
6464
_error_collection = None
6565

6666
def __init__(self, target_input):
@@ -77,12 +77,20 @@ def __init__(self, target_input):
7777
self._can_be_analyzed = (False, "IOError while starting Stylechecker.XML(), please verify if the input is correct")
7878
except Exception as e:
7979
self._can_be_analyzed = (False, "Error while starting Stylechecker.XML()")
80+
self._validation_errors = {'results': [], 'error_lines': [], }
8081
self._error_collection = ErrorCollection()
8182

8283

8384
def get_validation_errors(self):
85+
"""
86+
returns a dict like { 'results' : ... , 'error_lines': ''}
87+
'results' is a dict with a structure necessary to display errors table (error level, line, cols, message)
88+
'error_lines' is a coma separated string that list the un-repeated numbers of lines, where the XML has a error annotation
89+
(this list is used to highlight the line to the user with a plugin).
90+
"""
8491
self._validation_errors['results'] = self._error_collection.get_list()
85-
self._validation_errors['error_lines'] = ", ".join(self._error_collection.get_lines())
92+
lines_distinct = [str(l) for l in set(self._error_collection.get_lines())]
93+
self._validation_errors['error_lines'] = ", ".join(lines_distinct)
8694
return self._validation_errors
8795

8896

@@ -95,22 +103,24 @@ def analyze(self):
95103
if self._can_be_analyzed_as_exception:
96104
# in case of exceptions: self._target_data is the exception
97105
self._annotations = self._target_data.message
98-
self._error_collection.add_exception_error(self._target_data)
106+
self._error_collection.add_exception_error(self._target_data, allow_repeted=False)
99107
results['can_be_analyzed'] = (True, None)
100108
elif self._can_be_analyzed[0]:
101109
try:
102-
# TODO: improve it!
103110
vs_status, vs_errors = self._target_data.validate_style()
104111
v_status, v_errors = self._target_data.validate()
105112
except Exception as e:
106113
self._annotations = e.message
107-
self._error_collection.add_exception_error(e)
114+
self._error_collection.add_exception_error(e, allow_repeted=False)
108115
results['can_be_analyzed'] = (True, None)
109116
else:
110117
if not vs_status or not v_status: # have errors
111118
self._target_data.annotate_errors()
112119
self._annotations = str(self._target_data)
120+
if not vs_status:
113121
self._error_collection.add_list_of_errors(vs_errors)
122+
if not v_status:
123+
self._error_collection.add_list_of_errors(v_errors)
114124
results['can_be_analyzed'] = (True, None)
115125
else:
116126
results['can_be_analyzed'] = self._can_be_analyzed

0 commit comments

Comments
 (0)