Skip to content

Commit

Permalink
Merge pull request #902 from ChillarAnand/yapf
Browse files Browse the repository at this point in the history
Catch yapf exceptions
  • Loading branch information
jorgenschaefer committed Jun 13, 2016
2 parents 45ad707 + 23fa66f commit c0a9233
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
8 changes: 6 additions & 2 deletions elpy/tests/test_yapf.py
@@ -1,20 +1,24 @@
# coding: utf-8
"""Tests for the elpy.yapf module"""

import sys
import unittest

from elpy import yapfutil
from elpy.rpc import Fault
from elpy.tests.support import BackendTestCase


@unittest.skipIf(yapfutil.YAPF_NOT_SUPPORTED,
'yapf not supported for current python version')
class YAPFTestCase(BackendTestCase):
def setUp(self):
if not yapfutil.YAPF_NOT_SUPPORTED:
if yapfutil.YAPF_NOT_SUPPORTED:
raise unittest.SkipTest

def test_fix_code_should_throw_error_for_invalid_code(self):
src = 'x = '
self.assertRaises(Fault, yapfutil.fix_code, src)

def test_fix_code(self):
testdata = [
('x= 123\n', 'x = 123\n'),
Expand Down
17 changes: 11 additions & 6 deletions elpy/yapfutil.py
Expand Up @@ -4,10 +4,11 @@

import os
import sys

from elpy.rpc import Fault

YAPF_NOT_SUPPORTED = sys.version_info < (2, 7) or (
sys.version_info >= (3, 0) and sys.version_info <= (3, 3))
sys.version_info >= (3, 0) and sys.version_info < (3, 4))

try:
if YAPF_NOT_SUPPORTED:
Expand All @@ -26,8 +27,12 @@ def fix_code(code):
if not yapf_api:
raise Fault('yapf not installed', code=400)
style_config = file_resources.GetDefaultStyleForDir(os.getcwd())
reformatted_source, _ = yapf_api.FormatCode(code,
filename='<stdin>',
style_config=style_config,
verify=False)
return reformatted_source
try:
reformatted_source, _ = yapf_api.FormatCode(code,
filename='<stdin>',
style_config=style_config,
verify=False)
return reformatted_source
except Exception as e:
raise Fault("Error during formatting: {}".format(e),
code=400)
12 changes: 12 additions & 0 deletions test/elpy-yapf-fix-code-test.el
Expand Up @@ -39,3 +39,15 @@
"z = 3"
"_|_x = 3"
))))))

(ert-deftest elpy-yapf-fix-code-should-throw-error-for-invalid-code ()
(let* ((pyversion (getenv "TRAVIS_PYTHON_VERSION"))
(yapf-not-supported (or (string< pyversion "2.7")
(and (not (string< pyversion "3.0"))
(string< pyversion "3.4")))))
(unless yapf-not-supported
(elpy-testcase ()
(set-buffer-string-with-point
"x =_|_"
)
(should-error (elpy-yapf-fix-code))))))

0 comments on commit c0a9233

Please sign in to comment.