Skip to content

Commit

Permalink
Clear Invalid Filename Error Message (#280)
Browse files Browse the repository at this point in the history
* Provide clear error message. State exact error, if filename is invalid. Fixes #22

Signed-off-by: Lincoln Simba <lincolncmba@gmail.com>
  • Loading branch information
lincmba authored and ukanga committed May 19, 2019
1 parent 808e1b9 commit 3fe7546
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pyxform/survey_element.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import re

import json

from pyxform import constants
from pyxform.utils import is_valid_xml_tag, node, unicode
from pyxform.xls2json import print_pyobj_to_json
from pyxform.question_type_dictionary import QUESTION_TYPE_DICT
from pyxform.errors import PyXFormError
from pyxform.question_type_dictionary import QUESTION_TYPE_DICT
from pyxform.utils import is_valid_xml_tag, node, unicode, \
INVALID_XFORM_TAG_REGEXP
from pyxform.xls2json import print_pyobj_to_json

try:
from functools import lru_cache
Expand Down Expand Up @@ -136,9 +139,12 @@ def add_children(self, children):

def validate(self):
if not is_valid_xml_tag(self.name):
msg = "The name '%s' is an invalid xml tag. Names must begin with" \
" a letter, colon, or underscore, subsequent characters can" \
" include numbers, dashes, and periods." % self.name
invalid_char = re.search(INVALID_XFORM_TAG_REGEXP, self.name)
msg = "The name '{}' is an invalid XML tag, it contains an " \
"invalid character '{}'. Names must begin with a letter, " \
"colon, or underscore, subsequent characters can include " \
"numbers, dashes, and periods".format(
self.name, invalid_char.group(0))
raise PyXFormError(msg)

# TODO: Make sure renaming this doesn't cause any problems
Expand Down
19 changes: 19 additions & 0 deletions pyxform/tests_v1/test_sheet_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,25 @@ def test_missing_list_name(self):
'list name',
])

def test_clear_filename_error_message(self):
"""Test clear filename error message"""
error_message = "The name 'bad@filename' is an invalid XML tag, it " \
"contains an invalid character '@'. Names must begin" \
" with a letter, colon, or underscore, subsequent " \
"characters can include numbers, dashes, and periods"
self.assertPyxformXform(
name='bad@filename',
ss_structure=self._simple_choice_ss([
{'list_name': 'l1',
'name': 'c1',
'label': 'choice 1'},
{'list_name': 'l1',
'name': 'c2',
'label': 'choice 2'}]),
errored=True,
error__contains=[error_message]
)


class AliasesTests(PyxformTestCase):
"""
Expand Down
3 changes: 3 additions & 0 deletions pyxform/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"start": TAG_START_CHAR,
"char": TAG_CHAR
}

INVALID_XFORM_TAG_REGEXP = r"[^a-zA-Z:_][^a-zA-Z:_0-9\-.]*"

NSMAP = {
u"xmlns": u"http://www.w3.org/2002/xforms",
u"xmlns:h": u"http://www.w3.org/1999/xhtml",
Expand Down

0 comments on commit 3fe7546

Please sign in to comment.