[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Make revised tutorial tests run under python3



commit c79dc3d401de243bc7ae0e3146c8a6290a7f554a
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue May 26 09:39:05 2015 -0700

    Make revised tutorial tests run under python3
    
    Python3 drops execfile(), so adding a helper function that does the same...
    
      https://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3-0
---
 test/unit/tutorial.py          |   14 ++++++++++----
 test/unit/tutorial_examples.py |   20 ++++++++++++++------
 2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py
index 39e4f03..95634dc 100644
--- a/test/unit/tutorial.py
+++ b/test/unit/tutorial.py
@@ -36,6 +36,12 @@ MIRROR_MIRROR_OUTPUT = """\
 """
 
 
+def exec_file(path):
+  with open(path) as f:
+    code = compile(f.read(), path, 'exec')
+    exec(code)
+
+
 class TestTutorial(unittest.TestCase):
   @patch('sys.stdout', new_callable = StringIO)
   @patch('stem.control.Controller.from_port', spec = Controller)
@@ -46,7 +52,7 @@ class TestTutorial(unittest.TestCase):
       'traffic/written': '29649',
     }[arg]
 
-    execfile('docs/_static/example/hello_world.py')
+    exec_file('docs/_static/example/hello_world.py')
     self.assertEqual('My Tor relay has read 33406 bytes and written 29649.\n', stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO)
@@ -120,7 +126,7 @@ class TestTutorial(unittest.TestCase):
   def test_mirror_mirror_on_the_wall_1(self, downloader_mock, stdout_mock):
     downloader_mock().get_consensus().run.return_value = [mocking.get_router_status_entry_v2()]
 
-    execfile('docs/_static/example/current_descriptors.py')
+    exec_file('docs/_static/example/current_descriptors.py')
     self.assertEqual('found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n', stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO)
@@ -129,7 +135,7 @@ class TestTutorial(unittest.TestCase):
     controller = from_port_mock().__enter__()
     controller.get_network_statuses.return_value = [mocking.get_router_status_entry_v2()]
 
-    execfile('docs/_static/example/descriptor_from_tor_control_socket.py')
+    exec_file('docs/_static/example/descriptor_from_tor_control_socket.py')
     self.assertEqual('found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n', stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO)
@@ -158,7 +164,7 @@ class TestTutorial(unittest.TestCase):
     reader = reader_mock().__enter__()
     reader.__iter__.return_value = iter([mocking.get_relay_server_descriptor()])
 
-    execfile('docs/_static/example/past_descriptors.py')
+    exec_file('docs/_static/example/past_descriptors.py')
     self.assertEqual('found relay caerSidi (None)\n', stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO)
diff --git a/test/unit/tutorial_examples.py b/test/unit/tutorial_examples.py
index ae73239..246b483 100644
--- a/test/unit/tutorial_examples.py
+++ b/test/unit/tutorial_examples.py
@@ -32,6 +32,8 @@ try:
 except ImportError:
   from mock import Mock, patch
 
+OPEN_FUNCTION = open  # make a reference so mocking open() won't mess with us
+
 CIRC_CONTENT = '650 CIRC %d %s \
 %s \
 PURPOSE=%s'
@@ -99,6 +101,12 @@ A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB: caerSidi
 """
 
 
+def exec_file(path):
+  with OPEN_FUNCTION(path, 'rb') as f:
+    code = compile(f.read(), path, 'exec')
+    exec(code)
+
+
 def _get_event(content):
   controller_event = mocking.get_message(content)
   stem.response.convert('EVENT', controller_event)
@@ -167,7 +175,7 @@ class TestTutorialExamples(unittest.TestCase):
       path_7[0]: _get_router_status('176.67.169.171')
     }[fingerprint]
 
-    execfile('docs/_static/example/list_circuits.py')
+    exec_file('docs/_static/example/list_circuits.py')
     self.assert_equal_unordered(LIST_CIRCUITS_OUTPUT, stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO)
@@ -231,7 +239,7 @@ class TestTutorialExamples(unittest.TestCase):
       get_relay_server_descriptor({'opt': 'contact Sambuddha Basu', 'platform': 'node-Tor 0.1.0 on Linux x86_64'}),
     ]
 
-    execfile('docs/_static/example/outdated_relays.py')
+    exec_file('docs/_static/example/outdated_relays.py')
 
     self.assert_equal_unordered(OUTDATED_RELAYS_OUTPUT, stdout_mock.getvalue())
 
@@ -272,7 +280,7 @@ class TestTutorialExamples(unittest.TestCase):
       [get_network_status_document_v3(routers = (entry[5], entry[6], entry[7], entry[8], entry[9]))],
     ]
 
-    execfile('docs/_static/example/compare_flags.py')
+    exec_file('docs/_static/example/compare_flags.py')
 
     self.assert_equal_unordered(COMPARE_FLAGS_OUTPUT, stdout_mock.getvalue())
 
@@ -311,7 +319,7 @@ class TestTutorialExamples(unittest.TestCase):
 
     query_mock.side_effect = [query1, query2, query3, query4]
 
-    execfile('docs/_static/example/votes_by_bandwidth_authorities.py')
+    exec_file('docs/_static/example/votes_by_bandwidth_authorities.py')
     self.assert_equal_unordered(VOTES_BY_BANDWIDTH_AUTHORITIES_OUTPUT, stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO)
@@ -335,7 +343,7 @@ class TestTutorialExamples(unittest.TestCase):
     query_mock().run.return_value = [network_status]
     parse_file_mock.return_value = itertools.cycle([network_status])
 
-    execfile('docs/_static/example/persisting_a_consensus.py')
-    execfile('docs/_static/example/persisting_a_consensus_with_parse_file.py')
+    exec_file('docs/_static/example/persisting_a_consensus.py')
+    exec_file('docs/_static/example/persisting_a_consensus_with_parse_file.py')
 
     self.assertEqual(PERSISTING_A_CONSENSUS_OUTPUT, stdout_mock.getvalue())



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits