[tor-commits] [stem/master] Doc example unit tests used relative paths
atagar at torproject.org
atagar at torproject.org
Fri May 29 16:12:36 UTC 2015
commit 63c08cc70c69776e55197d0a9b6bb86d6b644c6b
Author: Damian Johnson <atagar at torproject.org>
Date: Fri May 29 09:11:15 2015 -0700
Doc example unit tests used relative paths
Recently we changed our unit tests for tutorial examples to test the docs
rather than a copy of the code (yay, no more duplication!). However, we used
relative paths so our unit tests failed if executed outside of our base stem
directory. Caught by toralf.
---
run_tests.py | 11 +++++++----
test/unit/__init__.py | 12 ++++++++++++
test/unit/tutorial.py | 15 +++++----------
test/unit/tutorial_examples.py | 19 +++++++------------
4 files changed, 31 insertions(+), 26 deletions(-)
diff --git a/run_tests.py b/run_tests.py
index c7ac574..d988900 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -357,10 +357,13 @@ def _run_test(args, test_class, output_filters, logging_buffer):
try:
suite = unittest.TestLoader().loadTestsFromName(test_class)
- except AttributeError:
- # should only come up if user provided '--test' for something that doesn't exist
- println(' no such test', ERROR)
- return None
+ except AttributeError as exc:
+ if args.specific_test:
+ # should only come up if user provided '--test' for something that doesn't exist
+ println(' no such test', ERROR)
+ return None
+ else:
+ raise exc
except Exception as exc:
println(' failed', ERROR)
traceback.print_exc(exc)
diff --git a/test/unit/__init__.py b/test/unit/__init__.py
index 9a83be9..d76ecfe 100644
--- a/test/unit/__init__.py
+++ b/test/unit/__init__.py
@@ -11,3 +11,15 @@ __all__ = [
'util',
'version',
]
+
+
+import os
+import test.util
+
+
+def exec_documentation_example(filename):
+ path = os.path.join(test.util.STEM_BASE, 'docs', '_static', 'example', filename)
+
+ with open(path) as f:
+ code = compile(f.read(), path, 'exec')
+ exec(code)
diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py
index 95634dc..3c5bfd6 100644
--- a/test/unit/tutorial.py
+++ b/test/unit/tutorial.py
@@ -9,6 +9,7 @@ from stem.control import Controller
from stem.descriptor.reader import DescriptorReader
from stem.descriptor.server_descriptor import RelayDescriptor
from test import mocking
+from test.unit import exec_documentation_example
try:
from StringIO import StringIO
@@ -36,12 +37,6 @@ 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)
@@ -52,7 +47,7 @@ class TestTutorial(unittest.TestCase):
'traffic/written': '29649',
}[arg]
- exec_file('docs/_static/example/hello_world.py')
+ exec_documentation_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)
@@ -126,7 +121,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()]
- exec_file('docs/_static/example/current_descriptors.py')
+ exec_documentation_example('current_descriptors.py')
self.assertEqual('found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n', stdout_mock.getvalue())
@patch('sys.stdout', new_callable = StringIO)
@@ -135,7 +130,7 @@ class TestTutorial(unittest.TestCase):
controller = from_port_mock().__enter__()
controller.get_network_statuses.return_value = [mocking.get_router_status_entry_v2()]
- exec_file('docs/_static/example/descriptor_from_tor_control_socket.py')
+ exec_documentation_example('descriptor_from_tor_control_socket.py')
self.assertEqual('found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n', stdout_mock.getvalue())
@patch('sys.stdout', new_callable = StringIO)
@@ -164,7 +159,7 @@ class TestTutorial(unittest.TestCase):
reader = reader_mock().__enter__()
reader.__iter__.return_value = iter([mocking.get_relay_server_descriptor()])
- exec_file('docs/_static/example/past_descriptors.py')
+ exec_documentation_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 246b483..c102c1a 100644
--- a/test/unit/tutorial_examples.py
+++ b/test/unit/tutorial_examples.py
@@ -19,6 +19,7 @@ from stem.control import Controller
from stem.descriptor.remote import DIRECTORY_AUTHORITIES
from test import mocking
+from test.unit import exec_documentation_example
from test.mocking import (
get_relay_server_descriptor,
get_router_status_entry_v3,
@@ -101,12 +102,6 @@ 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)
@@ -175,7 +170,7 @@ class TestTutorialExamples(unittest.TestCase):
path_7[0]: _get_router_status('176.67.169.171')
}[fingerprint]
- exec_file('docs/_static/example/list_circuits.py')
+ exec_documentation_example('list_circuits.py')
self.assert_equal_unordered(LIST_CIRCUITS_OUTPUT, stdout_mock.getvalue())
@patch('sys.stdout', new_callable = StringIO)
@@ -239,7 +234,7 @@ class TestTutorialExamples(unittest.TestCase):
get_relay_server_descriptor({'opt': 'contact Sambuddha Basu', 'platform': 'node-Tor 0.1.0 on Linux x86_64'}),
]
- exec_file('docs/_static/example/outdated_relays.py')
+ exec_documentation_example('outdated_relays.py')
self.assert_equal_unordered(OUTDATED_RELAYS_OUTPUT, stdout_mock.getvalue())
@@ -280,7 +275,7 @@ class TestTutorialExamples(unittest.TestCase):
[get_network_status_document_v3(routers = (entry[5], entry[6], entry[7], entry[8], entry[9]))],
]
- exec_file('docs/_static/example/compare_flags.py')
+ exec_documentation_example('compare_flags.py')
self.assert_equal_unordered(COMPARE_FLAGS_OUTPUT, stdout_mock.getvalue())
@@ -319,7 +314,7 @@ class TestTutorialExamples(unittest.TestCase):
query_mock.side_effect = [query1, query2, query3, query4]
- exec_file('docs/_static/example/votes_by_bandwidth_authorities.py')
+ exec_documentation_example('votes_by_bandwidth_authorities.py')
self.assert_equal_unordered(VOTES_BY_BANDWIDTH_AUTHORITIES_OUTPUT, stdout_mock.getvalue())
@patch('sys.stdout', new_callable = StringIO)
@@ -343,7 +338,7 @@ class TestTutorialExamples(unittest.TestCase):
query_mock().run.return_value = [network_status]
parse_file_mock.return_value = itertools.cycle([network_status])
- exec_file('docs/_static/example/persisting_a_consensus.py')
- exec_file('docs/_static/example/persisting_a_consensus_with_parse_file.py')
+ exec_documentation_example('persisting_a_consensus.py')
+ exec_documentation_example('persisting_a_consensus_with_parse_file.py')
self.assertEqual(PERSISTING_A_CONSENSUS_OUTPUT, stdout_mock.getvalue())
More information about the tor-commits
mailing list