|
6 | 6 | import sys
|
7 | 7 | import tempfile
|
8 | 8 | import io
|
| 9 | +from functools import partial |
9 | 10 | from contextlib import contextmanager
|
10 | 11 | from six.moves import StringIO
|
11 | 12 |
|
@@ -284,6 +285,68 @@ def test_variable_is_cleared(self):
|
284 | 285 | self.assertNotIn('b', self.repl.interp.locals)
|
285 | 286 |
|
286 | 287 |
|
| 288 | +class TestCurtsiesReevaluateWithImport(TestCase): |
| 289 | + def setUp(self): |
| 290 | + self.repl = create_repl() |
| 291 | + self.open = partial(io.open, mode='wt', encoding='utf-8') |
| 292 | + self.dont_write_bytecode = sys.dont_write_bytecode |
| 293 | + sys.dont_write_bytecode = True |
| 294 | + |
| 295 | + def tearDown(self): |
| 296 | + sys.dont_write_bytecode = self.dont_write_bytecode |
| 297 | + |
| 298 | + def push(self, line): |
| 299 | + self.repl._current_line = line |
| 300 | + self.repl.on_enter() |
| 301 | + |
| 302 | + def head(self, path): |
| 303 | + self.push('import sys') |
| 304 | + self.push('sys.path.append("%s")' % (path)) |
| 305 | + |
| 306 | + @staticmethod |
| 307 | + @contextmanager |
| 308 | + def tempfile(): |
| 309 | + with tempfile.NamedTemporaryFile(suffix='.py') as temp: |
| 310 | + path, name = os.path.split(temp.name) |
| 311 | + yield temp.name, path, name.replace('.py', '') |
| 312 | + |
| 313 | + def test_module_content_changed(self): |
| 314 | + with self.tempfile() as (fullpath, path, modname): |
| 315 | + with self.open(fullpath) as f: |
| 316 | + f.write('a = 0\n') |
| 317 | + self.head(path) |
| 318 | + self.push('import %s' % (modname)) |
| 319 | + self.push('a = %s.a' % (modname)) |
| 320 | + self.assertIn('a', self.repl.interp.locals) |
| 321 | + self.assertEqual(self.repl.interp.locals['a'], 0) |
| 322 | + with self.open(fullpath) as f: |
| 323 | + f.write('a = 1\n') |
| 324 | + self.repl.clear_modules_and_reevaluate() |
| 325 | + self.assertIn('a', self.repl.interp.locals) |
| 326 | + self.assertEqual(self.repl.interp.locals['a'], 1) |
| 327 | + |
| 328 | + def test_import_module_with_rewind(self): |
| 329 | + with self.tempfile() as (fullpath, path, modname): |
| 330 | + with self.open(fullpath) as f: |
| 331 | + f.write('a = 0\n') |
| 332 | + self.head(path) |
| 333 | + self.push('import %s' % (modname)) |
| 334 | + self.assertIn(modname, self.repl.interp.locals) |
| 335 | + self.repl.undo() |
| 336 | + self.assertNotIn(modname, self.repl.interp.locals) |
| 337 | + self.repl.clear_modules_and_reevaluate() |
| 338 | + self.assertNotIn(modname, self.repl.interp.locals) |
| 339 | + self.push('import %s' % (modname)) |
| 340 | + self.push('a = %s.a' % (modname)) |
| 341 | + self.assertIn('a', self.repl.interp.locals) |
| 342 | + self.assertEqual(self.repl.interp.locals['a'], 0) |
| 343 | + with self.open(fullpath) as f: |
| 344 | + f.write('a = 1\n') |
| 345 | + self.repl.clear_modules_and_reevaluate() |
| 346 | + self.assertIn('a', self.repl.interp.locals) |
| 347 | + self.assertEqual(self.repl.interp.locals['a'], 1) |
| 348 | + |
| 349 | + |
287 | 350 | class TestCurtsiesPagerText(TestCase):
|
288 | 351 |
|
289 | 352 | def setUp(self):
|
|
0 commit comments