Skip to content

Commit

Permalink
More descriptive error for version issue in Spidermonkey; bug 639584
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbasta committed Mar 9, 2011
1 parent 7e93b45 commit 259286c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
43 changes: 43 additions & 0 deletions tests/test_js_spidermonkey.py
@@ -0,0 +1,43 @@
import json
import validator.testcases.javascript.spidermonkey as spidermonkey


def test_reflectparse_presence():
"Tests that when Spidermonkey is too old, a proper error is produced"

sp = spidermonkey.subprocess
spidermonkey.subprocess = MockSubprocess()

try:
spidermonkey._get_tree("foo bar", "[path]")
except RuntimeError as err:
print str(err)
assert str(err) == \
"Spidermonkey version too old; " \
"1.8pre+ required; error='ReferenceError: [errmsg]'; " \
"spidermonkey='[path]'"
except:
raise

spidermonkey.subprocess = sp


class MockSubprocess(object):
"A class to mock subprocess"

def __init__(self):
self.PIPE = True

def Popen(self, command, shell, stderr, stdout):
return MockSubprocessObject()


class MockSubprocessObject(object):
"A class to mock a subprocess object and implement the communicate method"

def communicate(self):
data = json.dumps({"error": True,
"error_message": "ReferenceError: [errmsg]"})
return data, ""


22 changes: 12 additions & 10 deletions validator/testcases/javascript/spidermonkey.py
Expand Up @@ -56,7 +56,7 @@ def get_tree(code, err=None, filename=None, shell=None):
"being properly read by the Spidermonkey JS engine.",
str(exc)],
filename=filename)


class JSReflectException(Exception):
"An exception to indicate that tokenization has failed"
Expand Down Expand Up @@ -125,10 +125,10 @@ def strip_weird_chars(chardata, err=None, name=""):

def _get_tree(code, shell=SPIDERMONKEY_INSTALLATION):
"Returns an AST tree of the JS passed in `code`."

if not code:
return None

temp = tempfile.NamedTemporaryFile(mode="w+", delete=False)
temp.write(code)
temp.flush()
Expand All @@ -146,14 +146,14 @@ def _get_tree(code, shell=SPIDERMONKEY_INSTALLATION):
try:
cmd = [shell, "-e", data]
try:
shell = subprocess.Popen(cmd,
shell=False,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
shell_obj = subprocess.Popen(cmd,
shell=False,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
except OSError:
raise OSError("Spidermonkey shell could not be run.")

data, stderr = shell.communicate()
data, stderr = shell_obj.communicate()
if stderr: raise RuntimeError('Error calling %r: %s' % (cmd, stderr))

# Closing the temp file will delete it.
Expand All @@ -170,13 +170,15 @@ def _get_tree(code, shell=SPIDERMONKEY_INSTALLATION):
data = unicode(data)
except UnicodeDecodeError:
data = unicode(filter_ascii(data))

parsed = json.loads(data, strict=False)

if "error" in parsed and parsed["error"]:
if parsed["error_message"][:14] == "ReferenceError":
raise RuntimeError("Spidermonkey version too old; "
"1.8pre+ required")
"1.8pre+ required; error='%s'; "
"spidermonkey='%s'" % (parsed["error_message"],
shell))
else:
raise JSReflectException(parsed["error_message"]).line_num(
parsed["line_number"])
Expand Down

0 comments on commit 259286c

Please sign in to comment.