[tor-commits] [bridgedb/master] Move method wrapper utility @fileCheckDecorator() to bridgedb.test.util.

isis at torproject.org isis at torproject.org
Sun Jan 12 06:06:33 UTC 2014


commit 689ff72c72b4d98fbe86c83b84da2a0e354af4f0
Author: Isis Lovecruft <isis at torproject.org>
Date:   Tue Nov 19 07:55:44 2013 +0000

    Move method wrapper utility @fileCheckDecorator() to bridgedb.test.util.
    
     * ADD bridgedb.test.util, with method decorator @fileCheckDecorator, and give
       that decorator a docstring with a doctest example.
---
 lib/bridgedb/test/test_bridgedb.py |   15 +--------
 lib/bridgedb/test/util.py          |   62 ++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 14 deletions(-)

diff --git a/lib/bridgedb/test/test_bridgedb.py b/lib/bridgedb/test/test_bridgedb.py
index a7093a6..3d21080 100644
--- a/lib/bridgedb/test/test_bridgedb.py
+++ b/lib/bridgedb/test/test_bridgedb.py
@@ -18,7 +18,6 @@ import shutil
 import signal
 import time
 
-from functools import wraps
 from os.path import join as pjoin
 from subprocess import Popen, PIPE
 
@@ -26,20 +25,8 @@ from twisted.python import log
 from twisted.python.procutils import which
 from twisted.trial import unittest
 
+from bridgedb.test.util import fileCheckDecorator
 
-def fileCheckDecorator(func):
-    @wraps(func)
-    def wrapper(self, src, dst, description):
-        print("Copying %s:\n  %r\n\t\t↓ ↓ ↓\n  %r\n"
-              % (str(description), src, dst))
-        self.assertTrue(
-            os.path.isfile(src),
-            "Couldn't find original %s file: %r" % (str(description), src))
-        func(self, src, dst, description)
-        self.assertTrue(
-            os.path.isfile(dst),
-            "Couldn't find new %s file: %r" % (str(description), dst))
-    return wrapper
 
 class BridgeDBCliTest(unittest.TestCase):
     """Test the `bridgedb` command."""
diff --git a/lib/bridgedb/test/util.py b/lib/bridgedb/test/util.py
new file mode 100644
index 0000000..4520e29
--- /dev/null
+++ b/lib/bridgedb/test/util.py
@@ -0,0 +1,62 @@
+# -*- coding: utf-8 -*-
+#
+# This file is part of BridgeDB, a Tor bridge distribution system.
+#
+# :authors: Isis Lovecruft 0xA3ADB67A2CDB8B35 <isis at torproject.org>
+#           please also see AUTHORS file
+# :copyright: (c) 2013, Isis Lovecruft
+#             (c) 2007-2013, The Tor Project, Inc.
+#             (c) 2007-2013, all entities within the AUTHORS file
+# :license: 3-Clause BSD, see LICENSE for licensing information
+
+"""Unittests utilitys the `bridgedb.test` package."""
+
+from __future__ import print_function
+from __future__ import unicode_literals
+
+import doctest
+import os
+
+from functools import wraps
+
+
+def fileCheckDecorator(func):
+    """Method decorator for a t.t.unittest.TestCase test_* method.
+
+    >>> import shutil
+    >>> from twisted.trial import unittest
+    >>> pyunit = __import__('unittest')
+    >>> class TestTests(unittest.TestCase):
+    ...     @fileCheckDecorator
+    ...     def doCopyFile(src, dst, description=None):
+    ...         shutil.copy(src, dst)
+    ...     def test_doCopyFile(self):
+    ...         srcfile = self.mktemp()
+    ...         dstfile = self.mktemp()
+    ...         with open(srcfile, 'wb') as fh:
+    ...             fh.write('testing TestCase method decorator utility')
+    ...             fh.flush()
+    ...         self.doCopyFile(srcfile, dstfile, 'asparagus')
+    ...
+    >>> testtest = TestTests()
+    >>> testtest.runTest()
+
+    :type func: callable
+    :param func: The ``test_*`` method, from a
+        :class:`twisted.trial.unittest.TestCase` instance, to wrap.
+    """
+    @wraps(func)
+    def wrapper(self, src, dst, description):
+        print("Copying %s:\n  %r\n\t\t↓ ↓ ↓\n  %r\n"
+              % (str(description), src, dst))
+        self.assertTrue(os.path.isfile(src),
+                        "Couldn't find original %s file: %r"
+                        % (str(description), src))
+        func(self, src, dst, description)
+        self.assertTrue(os.path.isfile(dst),
+                        "Couldn't find new %s file: %r"
+                        % (str(description), dst))
+    return wrapper
+
+if __name__ == "__main__":
+    doctest.run_docstring_examples(fileCheckDecorator, None)





More information about the tor-commits mailing list