[tor-commits] [stem/master] Context manager for making a torrc

atagar at torproject.org atagar at torproject.org
Thu Jun 8 17:17:55 UTC 2017


commit 65226e53a66156c16c87e4e4ec19a265bda74755
Author: Damian Johnson <atagar at torproject.org>
Date:   Wed Jun 7 10:01:02 2017 -0700

    Context manager for making a torrc
    
    Few spots would benefit with a context manager that makes a torrc too.
---
 test/integ/process.py | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/test/integ/process.py b/test/integ/process.py
index a047540..9c23077 100644
--- a/test/integ/process.py
+++ b/test/integ/process.py
@@ -58,6 +58,17 @@ def tmp_directory():
     shutil.rmtree(tmp_dir)
 
 
+ at contextmanager
+def torrc():
+  with tmp_directory() as data_directory:
+    torrc_path = os.path.join(data_directory, 'torrc')
+
+    with open(torrc_path, 'w') as torrc_file:
+      torrc_file.write(BASIC_RELAY_TORRC % data_directory)
+
+    yield torrc_path
+
+
 def run_tor(tor_cmd, *args, **kwargs):
   # python doesn't allow us to have individual keyword arguments when there's
   # an arbitrary number of positional arguments, so explicitly checking
@@ -69,13 +80,8 @@ def run_tor(tor_cmd, *args, **kwargs):
   if kwargs:
     raise ValueError('Got unexpected keyword arguments: %s' % kwargs)
 
-  with tmp_directory() as data_directory:
+  with torrc() as torrc_path:
     if with_torrc:
-      torrc_path = os.path.join(data_directory, 'torrc')
-
-      with open(torrc_path, 'w') as torrc_file:
-        torrc_file.write(BASIC_RELAY_TORRC % data_directory)
-
       args = ['-f', torrc_path] + list(args)
 
     args = [tor_cmd] + list(args)
@@ -307,12 +313,7 @@ class TestProcess(unittest.TestCase):
     Pass configuration options on the commandline.
     """
 
-    with tmp_directory() as data_directory:
-      torrc_path = os.path.join(data_directory, 'torrc')
-
-      with open(torrc_path, 'w') as torrc_file:
-        torrc_file.write(BASIC_RELAY_TORRC % data_directory)
-
+    with torrc() as torrc_path:
       config_args = [
         '+SocksPort', '9090',  # append an extra SocksPort
         '/ExtORPort',  # drops our ExtORPort
@@ -388,7 +389,15 @@ class TestProcess(unittest.TestCase):
 
         def short_launch():
           try:
-            stem.process.launch_tor_with_config({'SocksPort': 'invalid', 'DataDirectory': data_directory}, tor_cmd, 100, None, timeout_arg)
+            stem.process.launch_tor_with_config(
+              tor_cmd = tor_cmd,
+              config = {
+                'SocksPort': 'invalid',
+                'DataDirectory': data_directory,
+              },
+              completion_percent = 100,
+              timeout = timeout_arg,
+            )
           except Exception as exc:
             raised_exc[0] = exc
 





More information about the tor-commits mailing list