Permalink
Browse files

refactoring: Provide our own implementation of a resource.

This used to use a StringIO instance in case Rope was unavailable,
but that did not provide all the interface we needed. Implement our
own version so we can easily provide all APIs as needed.

Fixes #54
  • Loading branch information...
1 parent 766f137 commit 7d259bbabb0cba5b137f47a67875b3bc96912780 @jorgenschaefer committed Mar 29, 2013
Showing with 24 additions and 3 deletions.
  1. +12 −3 elpy/refactor.py
  2. +12 −0 elpy/tests/test_refactor.py
View
@@ -67,7 +67,6 @@
ROPE_AVAILABLE = False
from elpy.utils import autoimport
-from elpy.compat import StringIO
def options(description, **kwargs):
@@ -102,8 +101,7 @@ def __init__(self, project_root, filename):
self.resource = path_to_resource(self.project, filename)
else:
self.project = None
- with open(filename) as f:
- self.resource = StringIO(f.read())
+ self.resource = FakeResource(filename)
def get_refactor_options(self, start, end=None):
"""Return a list of options for refactoring at the given position.
@@ -347,3 +345,14 @@ def translate_changes(initial_change):
'type': 'file',
'file': change.resource.real_path})
return result
+
+
+class FakeResource(object):
+ """A fake resource in case Rope is absence."""
+
+ def __init__(self, filename):
+ self.real_path = filename
+
+ def read(self):
+ with open(filename) as f:
+ return f.read()
@@ -391,3 +391,15 @@ def test_should_call_autoimport_get_changes(self, get_changes):
ref = refactor.Refactor(self.project_root, filename)
ref.refactor_add_missing_imports()
get_changes.assert_called_with(filename)
+
+ @mock.patch.object(elpy.utils.autoimport, 'get_changes')
+ def test_should_work_without_rope(self, get_changes):
+ filename, offset = self.create_file("foo.py")
+ try:
+ old_rope_available = refactor.ROPE_AVAILABLE
+ refactor.ROPE_AVAILABLE = False
+ ref = refactor.Refactor(self.project_root, filename)
+ finally:
+ refactor.ROPE_AVAILABLE = old_rope_available
+ ref.refactor_add_missing_imports()
+ get_changes.assert_called_with(filename)

0 comments on commit 7d259bb

Please sign in to comment.