Added libbe.TESTING (defaults to False).
authorW. Trevor King <wking@drexel.edu>
Fri, 4 Dec 2009 02:19:54 +0000 (21:19 -0500)
committerW. Trevor King <wking@drexel.edu>
Fri, 4 Dec 2009 02:19:54 +0000 (21:19 -0500)
This flag allows us to skip unittest and testsuite declaration if we
woln't need them.  It speeds up simple be calls a suprising amount.
With Testing=True (the old behavior):
  wking@thor:be.wtk$ time ./be > /dev/null

  real    0m0.393s
  user    0m0.340s
  sys     0m0.048s
With TESTING=False (the new behavior):
  be.wtk$ time ./be > /dev/null

  real    0m0.216s
  user    0m0.152s
  sys     0m0.064s

This adjustment was inspired by Jakub Wilk's Debian bug:
  http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=559295

26 files changed:
becommands/import_xml.py
libbe/__init__.py
libbe/arch.py
libbe/beuuid.py
libbe/bug.py
libbe/bugdir.py
libbe/bzr.py
libbe/cmdutil.py
libbe/comment.py
libbe/config.py
libbe/darcs.py
libbe/diff.py
libbe/editor.py
libbe/encoding.py
libbe/git.py
libbe/hg.py
libbe/mapfile.py
libbe/plugin.py
libbe/properties.py
libbe/settings_object.py
libbe/subproc.py
libbe/tree.py
libbe/upgrade.py
libbe/utility.py
libbe/vcs.py
test.py

index 892a09e47a7c6310f59ee602da5048ebfafe6067..d1ea0267890ed6e62cea642128c2fe0fb39aba24 100644 (file)
@@ -14,6 +14,7 @@
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 """Import comments and bugs from XML"""
+import libbe
 from libbe import cmdutil, bugdir, bug, comment, utility
 from becommands.comment import complete
 import copy
@@ -23,8 +24,9 @@ try: # import core module, Python >= 2.5
     from xml.etree import ElementTree
 except ImportError: # look for non-core module
     from elementtree import ElementTree
-import doctest
-import unittest
+if libbe.TESTING == True:
+    import doctest
+    import unittest
 __desc__ = __doc__
 
 def execute(args, manipulate_encodings=True, restrict_file_access=False):
@@ -302,127 +304,129 @@ def help():
     return get_parser().help_str() + longhelp
 
 
-class LonghelpTestCase (unittest.TestCase):
-    """
-    Test import scenarios given in longhelp.
-    """
-    def setUp(self):
-        self.bugdir = bugdir.SimpleBugDir()
-        self.original_working_dir = os.getcwd()
-        os.chdir(self.bugdir.root)
-        bugA = self.bugdir.bug_from_uuid('a')
-        self.bugdir.remove_bug(bugA)
-        self.bugdir.set_sync_with_disk(False)
-        bugB = self.bugdir.bug_from_uuid('b')
-        bugB.creator = 'John'
-        bugB.status = 'open'
-        bugB.extra_strings += ["don't forget your towel"]
-        bugB.extra_strings += ['helps with space travel']
-        comm1 = bugB.comment_root.new_reply(body='Hello\n')
-        comm1.uuid = 'c1'
-        comm1.author = 'Jane'
-        comm2 = bugB.comment_root.new_reply(body='World\n')
-        comm2.uuid = 'c2'
-        comm2.author = 'Jess'
-        bugB.save()
-        self.bugdir.set_sync_with_disk(True)
-        self.xml = """
-        <be-xml>
-          <bug>
-            <uuid>b</uuid>
-            <status>fixed</status>
-            <summary>a test bug</summary>
-            <extra-string>don't forget your towel</extra-string>
-            <extra-string>watch out for flying dolphins</extra-string>
-            <comment>
-              <uuid>c1</uuid>
-              <body>So long</body>
-            </comment>
-            <comment>
-              <uuid>c3</uuid>
-              <author>Jed</author>
-              <body>And thanks</body>
-            </comment>
-          </bug>
-        </be-xml>
+if libbe.TESTING == True:
+    class LonghelpTestCase (unittest.TestCase):
+        """
+        Test import scenarios given in longhelp.
         """
-    def tearDown(self):
-        os.chdir(self.original_working_dir)
-        self.bugdir.cleanup()
-    def _execute(self, *args):
-        import StringIO
-        orig_stdin = sys.stdin
-        sys.stdin = StringIO.StringIO(self.xml)
-        execute(list(args)+["-"], manipulate_encodings=False,
-                restrict_file_access=True)
-        sys.stdin = orig_stdin
-        self.bugdir._clear_bugs()
-    def testCleanBugdir(self):
-        uuids = list(self.bugdir.uuids())
-        self.failUnless(uuids == ['b'], uuids)
-    def testNotAddOnly(self):
-        self._execute()
-        uuids = list(self.bugdir.uuids())
-        self.failUnless(uuids == ['b'], uuids)
-        bugB = self.bugdir.bug_from_uuid('b')
-        self.failUnless(bugB.uuid == 'b', bugB.uuid)
-        self.failUnless(bugB.creator == 'John', bugB.creator)
-        self.failUnless(bugB.status == 'fixed', bugB.status)
-        estrs = ["don't forget your towel",
-                 'helps with space travel',
-                 'watch out for flying dolphins']
-        self.failUnless(bugB.extra_strings == estrs, bugB.extra_strings)
-        comments = list(bugB.comments())
-        self.failUnless(len(comments) == 3,
-                        ['%s (%s, %s)' % (c.uuid, c.alt_id, c.body) for c in comments])
-        c1 = bugB.comment_from_uuid('c1')
-        comments.remove(c1)
-        self.failUnless(c1.uuid == 'c1', c1.uuid)
-        self.failUnless(c1.alt_id == None, c1.alt_id)
-        self.failUnless(c1.author == 'Jane', c1.author)
-        self.failUnless(c1.body == 'So long\n', c1.body)
-        c2 = bugB.comment_from_uuid('c2')
-        comments.remove(c2)
-        self.failUnless(c2.uuid == 'c2', c2.uuid)
-        self.failUnless(c2.alt_id == None, c2.alt_id)
-        self.failUnless(c2.author == 'Jess', c2.author)
-        self.failUnless(c2.body == 'World\n', c2.body)
-        c4 = comments[0]
-        self.failUnless(len(c4.uuid) == 36, c4.uuid)
-        self.failUnless(c4.alt_id == 'c3', c4.alt_id)
-        self.failUnless(c4.author == 'Jed', c4.author)
-        self.failUnless(c4.body == 'And thanks\n', c4.body)
-    def testAddOnly(self): 
-        self._execute('--add-only')
-        uuids = list(self.bugdir.uuids())
-        self.failUnless(uuids == ['b'], uuids)
-        bugB = self.bugdir.bug_from_uuid('b')
-        self.failUnless(bugB.uuid == 'b', bugB.uuid)
-        self.failUnless(bugB.creator == 'John', bugB.creator)
-        self.failUnless(bugB.status == 'open', bugB.status)
-        estrs = ["don't forget your towel",
-                 'helps with space travel']
-        self.failUnless(bugB.extra_strings == estrs, bugB.extra_strings)
-        comments = list(bugB.comments())
-        self.failUnless(len(comments) == 3,
-                        ['%s (%s)' % (c.uuid, c.alt_id) for c in comments])
-        c1 = bugB.comment_from_uuid('c1')
-        comments.remove(c1)
-        self.failUnless(c1.uuid == 'c1', c1.uuid)
-        self.failUnless(c1.alt_id == None, c1.alt_id)
-        self.failUnless(c1.author == 'Jane', c1.author)
-        self.failUnless(c1.body == 'Hello\n', c1.body)
-        c2 = bugB.comment_from_uuid('c2')
-        comments.remove(c2)
-        self.failUnless(c2.uuid == 'c2', c2.uuid)
-        self.failUnless(c2.alt_id == None, c2.alt_id)
-        self.failUnless(c2.author == 'Jess', c2.author)
-        self.failUnless(c2.body == 'World\n', c2.body)
-        c4 = comments[0]
-        self.failUnless(len(c4.uuid) == 36, c4.uuid)
-        self.failUnless(c4.alt_id == 'c3', c4.alt_id)
-        self.failUnless(c4.author == 'Jed', c4.author)
-        self.failUnless(c4.body == 'And thanks\n', c4.body)
+        def setUp(self):
+            self.bugdir = bugdir.SimpleBugDir()
+            self.original_working_dir = os.getcwd()
+            os.chdir(self.bugdir.root)
+            bugA = self.bugdir.bug_from_uuid('a')
+            self.bugdir.remove_bug(bugA)
+            self.bugdir.set_sync_with_disk(False)
+            bugB = self.bugdir.bug_from_uuid('b')
+            bugB.creator = 'John'
+            bugB.status = 'open'
+            bugB.extra_strings += ["don't forget your towel"]
+            bugB.extra_strings += ['helps with space travel']
+            comm1 = bugB.comment_root.new_reply(body='Hello\n')
+            comm1.uuid = 'c1'
+            comm1.author = 'Jane'
+            comm2 = bugB.comment_root.new_reply(body='World\n')
+            comm2.uuid = 'c2'
+            comm2.author = 'Jess'
+            bugB.save()
+            self.bugdir.set_sync_with_disk(True)
+            self.xml = """
+            <be-xml>
+              <bug>
+                <uuid>b</uuid>
+                <status>fixed</status>
+                <summary>a test bug</summary>
+                <extra-string>don't forget your towel</extra-string>
+                <extra-string>watch out for flying dolphins</extra-string>
+                <comment>
+                  <uuid>c1</uuid>
+                  <body>So long</body>
+                </comment>
+                <comment>
+                  <uuid>c3</uuid>
+                  <author>Jed</author>
+                  <body>And thanks</body>
+                </comment>
+              </bug>
+            </be-xml>
+            """
+        def tearDown(self):
+            os.chdir(self.original_working_dir)
+            self.bugdir.cleanup()
+        def _execute(self, *args):
+            import StringIO
+            orig_stdin = sys.stdin
+            sys.stdin = StringIO.StringIO(self.xml)
+            execute(list(args)+["-"], manipulate_encodings=False,
+                    restrict_file_access=True)
+            sys.stdin = orig_stdin
+            self.bugdir._clear_bugs()
+        def testCleanBugdir(self):
+            uuids = list(self.bugdir.uuids())
+            self.failUnless(uuids == ['b'], uuids)
+        def testNotAddOnly(self):
+            self._execute()
+            uuids = list(self.bugdir.uuids())
+            self.failUnless(uuids == ['b'], uuids)
+            bugB = self.bugdir.bug_from_uuid('b')
+            self.failUnless(bugB.uuid == 'b', bugB.uuid)
+            self.failUnless(bugB.creator == 'John', bugB.creator)
+            self.failUnless(bugB.status == 'fixed', bugB.status)
+            estrs = ["don't forget your towel",
+                     'helps with space travel',
+                     'watch out for flying dolphins']
+            self.failUnless(bugB.extra_strings == estrs, bugB.extra_strings)
+            comments = list(bugB.comments())
+            self.failUnless(len(comments) == 3,
+                            ['%s (%s, %s)' % (c.uuid, c.alt_id, c.body)
+                             for c in comments])
+            c1 = bugB.comment_from_uuid('c1')
+            comments.remove(c1)
+            self.failUnless(c1.uuid == 'c1', c1.uuid)
+            self.failUnless(c1.alt_id == None, c1.alt_id)
+            self.failUnless(c1.author == 'Jane', c1.author)
+            self.failUnless(c1.body == 'So long\n', c1.body)
+            c2 = bugB.comment_from_uuid('c2')
+            comments.remove(c2)
+            self.failUnless(c2.uuid == 'c2', c2.uuid)
+            self.failUnless(c2.alt_id == None, c2.alt_id)
+            self.failUnless(c2.author == 'Jess', c2.author)
+            self.failUnless(c2.body == 'World\n', c2.body)
+            c4 = comments[0]
+            self.failUnless(len(c4.uuid) == 36, c4.uuid)
+            self.failUnless(c4.alt_id == 'c3', c4.alt_id)
+            self.failUnless(c4.author == 'Jed', c4.author)
+            self.failUnless(c4.body == 'And thanks\n', c4.body)
+        def testAddOnly(self):
+            self._execute('--add-only')
+            uuids = list(self.bugdir.uuids())
+            self.failUnless(uuids == ['b'], uuids)
+            bugB = self.bugdir.bug_from_uuid('b')
+            self.failUnless(bugB.uuid == 'b', bugB.uuid)
+            self.failUnless(bugB.creator == 'John', bugB.creator)
+            self.failUnless(bugB.status == 'open', bugB.status)
+            estrs = ["don't forget your towel",
+                     'helps with space travel']
+            self.failUnless(bugB.extra_strings == estrs, bugB.extra_strings)
+            comments = list(bugB.comments())
+            self.failUnless(len(comments) == 3,
+                            ['%s (%s)' % (c.uuid, c.alt_id) for c in comments])
+            c1 = bugB.comment_from_uuid('c1')
+            comments.remove(c1)
+            self.failUnless(c1.uuid == 'c1', c1.uuid)
+            self.failUnless(c1.alt_id == None, c1.alt_id)
+            self.failUnless(c1.author == 'Jane', c1.author)
+            self.failUnless(c1.body == 'Hello\n', c1.body)
+            c2 = bugB.comment_from_uuid('c2')
+            comments.remove(c2)
+            self.failUnless(c2.uuid == 'c2', c2.uuid)
+            self.failUnless(c2.alt_id == None, c2.alt_id)
+            self.failUnless(c2.author == 'Jess', c2.author)
+            self.failUnless(c2.body == 'World\n', c2.body)
+            c4 = comments[0]
+            self.failUnless(len(c4.uuid) == 36, c4.uuid)
+            self.failUnless(c4.alt_id == 'c3', c4.alt_id)
+            self.failUnless(c4.author == 'Jed', c4.author)
+            self.failUnless(c4.body == 'And thanks\n', c4.body)
 
-unitsuite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
-suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
+    unitsuite =unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
+    suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
index 794013cd2fa1ab3a0ef4217988fe7d86a29bf429..a646d76297b0eb804a38b303179129c02c5256a3 100644 (file)
@@ -14,3 +14,8 @@
 # You should have received a copy of the GNU General Public License along
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# To reduce module load time, test suite generation is turned of by
+# default.  If you _do_ want to generate the test suites, set
+# TESTING=True before loading any libbe or becommands submodules.
+TESTING = False
index 48129b5bb88bddb70ee8c16dcf9b75b163ad6ca5..45a3284aab4f7e8d5209870380b32bf84132c395 100644 (file)
@@ -28,13 +28,14 @@ import re
 import shutil
 import sys
 import time
-import unittest
-import doctest
 
+import libbe
 from beuuid import uuid_gen
 import config
 import vcs
-
+if libbe.TESTING == True:
+    import unittest
+    import doctest
 
 
 DEFAULT_CLIENT = "tla"
@@ -307,7 +308,8 @@ class CantAddFile(Exception):
 
 
 \f
-vcs.make_vcs_testcase_subclasses(Arch, sys.modules[__name__])
+if libbe.TESTING == True:
+    vcs.make_vcs_testcase_subclasses(Arch, sys.modules[__name__])
 
-unitsuite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
-suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
+    unitsuite =unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
+    suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
index 260f3dc3f1a851347cef280fcc037aad0f18beab..a3a3b6c532824dad24fad2c3f881a6fecec8f410 100644 (file)
@@ -20,7 +20,9 @@ Backwards compatibility support for Python 2.4.  Once people give up
 on 2.4 ;), the uuid call should be merged into bugdir.py
 """
 
-import unittest
+import libbe
+if libbe.TESTING == True:
+    import unittest
 
 
 try:
@@ -56,9 +58,10 @@ except ImportError:
             raise Exception, strerror
         return output.rstrip('\n')
 
-class UUIDtestCase(unittest.TestCase):
-    def testUUID_gen(self):
-        id = uuid_gen()
-        self.failUnless(len(id) == 36, "invalid UUID '%s'" % id)
+if libbe.TESTING == True:
+    class UUIDtestCase(unittest.TestCase):
+        def testUUID_gen(self):
+            id = uuid_gen()
+            self.failUnless(len(id) == 36, "invalid UUID '%s'" % id)
 
-suite = unittest.TestLoader().loadTestsFromTestCase(UUIDtestCase)
+    suite = unittest.TestLoader().loadTestsFromTestCase(UUIDtestCase)
index 897d84171dac9ff1407d3a14ee0a91251a9756ef..1a190c3c4df2c4443ad8bebf8b390fde60590bdb 100644 (file)
@@ -31,8 +31,8 @@ try: # import core module, Python >= 2.5
 except ImportError: # look for non-core module
     from elementtree import ElementTree
 import xml.sax.saxutils
-import doctest
 
+import libbe
 from beuuid import uuid_gen
 from properties import Property, doc_property, local_property, \
     defaulting_property, checked_property, cached_property, \
@@ -41,6 +41,8 @@ import settings_object
 import mapfile
 import comment
 import utility
+if libbe.TESTING == True:
+    import doctest
 
 
 class DiskAccessRequired (Exception):
@@ -877,4 +879,5 @@ def cmp_last_modified(bug_1, bug_2):
     return -cmp(val_1, val_2)
 
 
-suite = doctest.DocTestSuite()
+if libbe.TESTING == True:
+    suite = doctest.DocTestSuite()
index 301ceb6c183fb4c98d2ef3da61f5a9ef1f115487..d78e761728ddb34d83f684a6a89785026bdb0086 100644 (file)
@@ -29,9 +29,8 @@ import os
 import os.path
 import sys
 import time
-import unittest
-import doctest
 
+import libbe
 import bug
 import encoding
 from properties import Property, doc_property, local_property, \
@@ -43,6 +42,9 @@ import vcs
 import settings_object
 import upgrade
 import utility
+if libbe.TESTING == True:
+    import unittest
+    import doctest
 
 
 class NoBugDir(Exception):
@@ -689,145 +691,156 @@ class SimpleBugDir (BugDir):
             self._dir_ref.cleanup()
         BugDir.cleanup(self)
 
-class BugDirTestCase(unittest.TestCase):
-    def setUp(self):
-        self.dir = utility.Dir()
-        self.bugdir = BugDir(self.dir.path, sink_to_existing_root=False,
-                             allow_vcs_init=True)
-        self.vcs = self.bugdir.vcs
-    def tearDown(self):
-        self.bugdir.cleanup()
-        self.dir.cleanup()
-    def fullPath(self, path):
-        return os.path.join(self.dir.path, path)
-    def assertPathExists(self, path):
-        fullpath = self.fullPath(path)
-        self.failUnless(os.path.exists(fullpath)==True,
-                        "path %s does not exist" % fullpath)
-        self.assertRaises(AlreadyInitialized, BugDir,
-                          self.dir.path, assertNewBugDir=True)
-    def versionTest(self):
-        if self.vcs.versioned == False:
-            return
-        original = self.bugdir.vcs.commit("Began versioning")
-        bugA = self.bugdir.bug_from_uuid("a")
-        bugA.status = "fixed"
-        self.bugdir.save()
-        new = self.vcs.commit("Fixed bug a")
-        dupdir = self.bugdir.duplicate_bugdir(original)
-        self.failUnless(dupdir.root != self.bugdir.root,
-                        "%s, %s" % (dupdir.root, self.bugdir.root))
-        bugAorig = dupdir.bug_from_uuid("a")
-        self.failUnless(bugA != bugAorig,
-                        "\n%s\n%s" % (bugA.string(), bugAorig.string()))
-        bugAorig.status = "fixed"
-        self.failUnless(bug.cmp_status(bugA, bugAorig)==0,
-                        "%s, %s" % (bugA.status, bugAorig.status))
-        self.failUnless(bug.cmp_severity(bugA, bugAorig)==0,
-                        "%s, %s" % (bugA.severity, bugAorig.severity))
-        self.failUnless(bug.cmp_assigned(bugA, bugAorig)==0,
-                        "%s, %s" % (bugA.assigned, bugAorig.assigned))
-        self.failUnless(bug.cmp_time(bugA, bugAorig)==0,
-                        "%s, %s" % (bugA.time, bugAorig.time))
-        self.failUnless(bug.cmp_creator(bugA, bugAorig)==0,
-                        "%s, %s" % (bugA.creator, bugAorig.creator))
-        self.failUnless(bugA == bugAorig,
-                        "\n%s\n%s" % (bugA.string(), bugAorig.string()))
-        self.bugdir.remove_duplicate_bugdir()
-        self.failUnless(os.path.exists(dupdir.root)==False, str(dupdir.root))
-    def testRun(self):
-        self.bugdir.new_bug(uuid="a", summary="Ant")
-        self.bugdir.new_bug(uuid="b", summary="Cockroach")
-        self.bugdir.new_bug(uuid="c", summary="Praying mantis")
-        length = len(self.bugdir)
-        self.failUnless(length == 3, "%d != 3 bugs" % length)
-        uuids = list(self.bugdir.uuids())
-        self.failUnless(len(uuids) == 3, "%d != 3 uuids" % len(uuids))
-        self.failUnless(uuids == ["a","b","c"], str(uuids))
-        bugA = self.bugdir.bug_from_uuid("a")
-        bugAprime = self.bugdir.bug_from_shortname("a")
-        self.failUnless(bugA == bugAprime, "%s != %s" % (bugA, bugAprime))
-        self.bugdir.save()
-        self.versionTest()
-    def testComments(self, sync_with_disk=False):
-        if sync_with_disk == True:
-            self.bugdir.set_sync_with_disk(True)
-        self.bugdir.new_bug(uuid="a", summary="Ant")
-        bug = self.bugdir.bug_from_uuid("a")
-        comm = bug.comment_root
-        rep = comm.new_reply("Ants are small.")
-        rep.new_reply("And they have six legs.")
-        if sync_with_disk == False:
+if libbe.TESTING == True:
+    class BugDirTestCase(unittest.TestCase):
+        def setUp(self):
+            self.dir = utility.Dir()
+            self.bugdir = BugDir(self.dir.path, sink_to_existing_root=False,
+                                 allow_vcs_init=True)
+            self.vcs = self.bugdir.vcs
+        def tearDown(self):
+            self.bugdir.cleanup()
+            self.dir.cleanup()
+        def fullPath(self, path):
+            return os.path.join(self.dir.path, path)
+        def assertPathExists(self, path):
+            fullpath = self.fullPath(path)
+            self.failUnless(os.path.exists(fullpath)==True,
+                            "path %s does not exist" % fullpath)
+            self.assertRaises(AlreadyInitialized, BugDir,
+                              self.dir.path, assertNewBugDir=True)
+        def versionTest(self):
+            if self.vcs.versioned == False:
+                return
+            original = self.bugdir.vcs.commit("Began versioning")
+            bugA = self.bugdir.bug_from_uuid("a")
+            bugA.status = "fixed"
             self.bugdir.save()
-            self.bugdir.set_sync_with_disk(True)
-        self.bugdir._clear_bugs()
-        bug = self.bugdir.bug_from_uuid("a")
-        bug.load_comments()
-        if sync_with_disk == False:
-            self.bugdir.set_sync_with_disk(False)
-        self.failUnless(len(bug.comment_root)==1, len(bug.comment_root))
-        for index,comment in enumerate(bug.comments()):
-            if index == 0:
-                repLoaded = comment
-                self.failUnless(repLoaded.uuid == rep.uuid, repLoaded.uuid)
-                self.failUnless(comment.sync_with_disk == sync_with_disk,
-                                comment.sync_with_disk)
-                self.failUnless(comment.content_type == "text/plain",
-                                comment.content_type)
-                self.failUnless(repLoaded.settings["Content-type"]=="text/plain",
-                                repLoaded.settings)
-                self.failUnless(repLoaded.body == "Ants are small.",
-                                repLoaded.body)
-            elif index == 1:
-                self.failUnless(comment.in_reply_to == repLoaded.uuid,
-                                repLoaded.uuid)
-                self.failUnless(comment.body == "And they have six legs.",
-                                comment.body)
-            else:
-                self.failIf(True, "Invalid comment: %d\n%s" % (index, comment))
-    def testSyncedComments(self):
-        self.testComments(sync_with_disk=True)
-
-class SimpleBugDirTestCase (unittest.TestCase):
-    def setUp(self):
-        # create a pre-existing bugdir in a temporary directory
-        self.dir = utility.Dir()
-        self.original_working_dir = os.getcwd()
-        os.chdir(self.dir.path)
-        self.bugdir = BugDir(self.dir.path, sink_to_existing_root=False,
-                             allow_vcs_init=True)
-        self.bugdir.new_bug("preexisting", summary="Hopefully not imported")
-        self.bugdir.save()
-    def tearDown(self):
-        os.chdir(self.original_working_dir)
-        self.bugdir.cleanup()
-        self.dir.cleanup()
-    def testOnDiskCleanLoad(self):
-        """SimpleBugDir(sync_with_disk==True) should not import preexisting bugs."""
-        bugdir = SimpleBugDir(sync_with_disk=True)
-        self.failUnless(bugdir.sync_with_disk==True, bugdir.sync_with_disk)
-        uuids = sorted([bug.uuid for bug in bugdir])
-        self.failUnless(uuids == ['a', 'b'], uuids)
-        bugdir._clear_bugs()
-        uuids = sorted([bug.uuid for bug in bugdir])
-        self.failUnless(uuids == [], uuids)
-        bugdir.load_all_bugs()
-        uuids = sorted([bug.uuid for bug in bugdir])
-        self.failUnless(uuids == ['a', 'b'], uuids)
-        bugdir.cleanup()
-    def testInMemoryCleanLoad(self):
-        """SimpleBugDir(sync_with_disk==False) should not import preexisting bugs."""
-        bugdir = SimpleBugDir(sync_with_disk=False)
-        self.failUnless(bugdir.sync_with_disk==False, bugdir.sync_with_disk)
-        uuids = sorted([bug.uuid for bug in bugdir])
-        self.failUnless(uuids == ['a', 'b'], uuids)
-        self.failUnlessRaises(DiskAccessRequired, bugdir.load_all_bugs)
-        uuids = sorted([bug.uuid for bug in bugdir])
-        self.failUnless(uuids == ['a', 'b'], uuids)
-        bugdir._clear_bugs()
-        uuids = sorted([bug.uuid for bug in bugdir])
-        self.failUnless(uuids == [], uuids)
-        bugdir.cleanup()
-
-unitsuite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
-suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
+            new = self.vcs.commit("Fixed bug a")
+            dupdir = self.bugdir.duplicate_bugdir(original)
+            self.failUnless(dupdir.root != self.bugdir.root,
+                            "%s, %s" % (dupdir.root, self.bugdir.root))
+            bugAorig = dupdir.bug_from_uuid("a")
+            self.failUnless(bugA != bugAorig,
+                            "\n%s\n%s" % (bugA.string(), bugAorig.string()))
+            bugAorig.status = "fixed"
+            self.failUnless(bug.cmp_status(bugA, bugAorig)==0,
+                            "%s, %s" % (bugA.status, bugAorig.status))
+            self.failUnless(bug.cmp_severity(bugA, bugAorig)==0,
+                            "%s, %s" % (bugA.severity, bugAorig.severity))
+            self.failUnless(bug.cmp_assigned(bugA, bugAorig)==0,
+                            "%s, %s" % (bugA.assigned, bugAorig.assigned))
+            self.failUnless(bug.cmp_time(bugA, bugAorig)==0,
+                            "%s, %s" % (bugA.time, bugAorig.time))
+            self.failUnless(bug.cmp_creator(bugA, bugAorig)==0,
+                            "%s, %s" % (bugA.creator, bugAorig.creator))
+            self.failUnless(bugA == bugAorig,
+                            "\n%s\n%s" % (bugA.string(), bugAorig.string()))
+            self.bugdir.remove_duplicate_bugdir()
+            self.failUnless(os.path.exists(dupdir.root)==False,
+                            str(dupdir.root))
+        def testRun(self):
+            self.bugdir.new_bug(uuid="a", summary="Ant")
+            self.bugdir.new_bug(uuid="b", summary="Cockroach")
+            self.bugdir.new_bug(uuid="c", summary="Praying mantis")
+            length = len(self.bugdir)
+            self.failUnless(length == 3, "%d != 3 bugs" % length)
+            uuids = list(self.bugdir.uuids())
+            self.failUnless(len(uuids) == 3, "%d != 3 uuids" % len(uuids))
+            self.failUnless(uuids == ["a","b","c"], str(uuids))
+            bugA = self.bugdir.bug_from_uuid("a")
+            bugAprime = self.bugdir.bug_from_shortname("a")
+            self.failUnless(bugA == bugAprime, "%s != %s" % (bugA, bugAprime))
+            self.bugdir.save()
+            self.versionTest()
+        def testComments(self, sync_with_disk=False):
+            if sync_with_disk == True:
+                self.bugdir.set_sync_with_disk(True)
+            self.bugdir.new_bug(uuid="a", summary="Ant")
+            bug = self.bugdir.bug_from_uuid("a")
+            comm = bug.comment_root
+            rep = comm.new_reply("Ants are small.")
+            rep.new_reply("And they have six legs.")
+            if sync_with_disk == False:
+                self.bugdir.save()
+                self.bugdir.set_sync_with_disk(True)
+            self.bugdir._clear_bugs()
+            bug = self.bugdir.bug_from_uuid("a")
+            bug.load_comments()
+            if sync_with_disk == False:
+                self.bugdir.set_sync_with_disk(False)
+            self.failUnless(len(bug.comment_root)==1, len(bug.comment_root))
+            for index,comment in enumerate(bug.comments()):
+                if index == 0:
+                    repLoaded = comment
+                    self.failUnless(repLoaded.uuid == rep.uuid, repLoaded.uuid)
+                    self.failUnless(comment.sync_with_disk == sync_with_disk,
+                                    comment.sync_with_disk)
+                    self.failUnless(comment.content_type == "text/plain",
+                                    comment.content_type)
+                    self.failUnless(repLoaded.settings["Content-type"] == \
+                                        "text/plain",
+                                    repLoaded.settings)
+                    self.failUnless(repLoaded.body == "Ants are small.",
+                                    repLoaded.body)
+                elif index == 1:
+                    self.failUnless(comment.in_reply_to == repLoaded.uuid,
+                                    repLoaded.uuid)
+                    self.failUnless(comment.body == "And they have six legs.",
+                                    comment.body)
+                else:
+                    self.failIf(True,
+                                "Invalid comment: %d\n%s" % (index, comment))
+        def testSyncedComments(self):
+            self.testComments(sync_with_disk=True)
+    
+    class SimpleBugDirTestCase (unittest.TestCase):
+        def setUp(self):
+            # create a pre-existing bugdir in a temporary directory
+            self.dir = utility.Dir()
+            self.original_working_dir = os.getcwd()
+            os.chdir(self.dir.path)
+            self.bugdir = BugDir(self.dir.path, sink_to_existing_root=False,
+                                 allow_vcs_init=True)
+            self.bugdir.new_bug("preexisting",summary="Hopefully not imported")
+            self.bugdir.save()
+        def tearDown(self):
+            os.chdir(self.original_working_dir)
+            self.bugdir.cleanup()
+            self.dir.cleanup()
+        def testOnDiskCleanLoad(self):
+            """
+            SimpleBugDir(sync_with_disk==True) should not import
+            preexisting bugs.
+            """
+            bugdir = SimpleBugDir(sync_with_disk=True)
+            self.failUnless(bugdir.sync_with_disk==True, bugdir.sync_with_disk)
+            uuids = sorted([bug.uuid for bug in bugdir])
+            self.failUnless(uuids == ['a', 'b'], uuids)
+            bugdir._clear_bugs()
+            uuids = sorted([bug.uuid for bug in bugdir])
+            self.failUnless(uuids == [], uuids)
+            bugdir.load_all_bugs()
+            uuids = sorted([bug.uuid for bug in bugdir])
+            self.failUnless(uuids == ['a', 'b'], uuids)
+            bugdir.cleanup()
+        def testInMemoryCleanLoad(self):
+            """
+            SimpleBugDir(sync_with_disk==False) should not import
+            preexisting bugs.
+            """
+            bugdir = SimpleBugDir(sync_with_disk=False)
+            self.failUnless(bugdir.sync_with_disk==False,
+                            bugdir.sync_with_disk)
+            uuids = sorted([bug.uuid for bug in bugdir])
+            self.failUnless(uuids == ['a', 'b'], uuids)
+            self.failUnlessRaises(DiskAccessRequired, bugdir.load_all_bugs)
+            uuids = sorted([bug.uuid for bug in bugdir])
+            self.failUnless(uuids == ['a', 'b'], uuids)
+            bugdir._clear_bugs()
+            uuids = sorted([bug.uuid for bug in bugdir])
+            self.failUnless(uuids == [], uuids)
+            bugdir.cleanup()
+    
+    unitsuite =unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
+    suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
index 281493dff847bf12b9b0d811478a043bd465989d..62a9b1102e84cdbb14d59407edea2091255b7fba 100644 (file)
@@ -26,9 +26,11 @@ import os
 import re
 import sys
 import unittest
-import doctest
 
+import libbe
 import vcs
+if libbe.TESTING == True:
+    import doctest
 
 
 def new():
@@ -108,7 +110,8 @@ class Bzr(vcs.VCS):
         return str(current_revision+index+1)
 
 \f    
-vcs.make_vcs_testcase_subclasses(Bzr, sys.modules[__name__])
+if libbe.TESTING == True:
+    vcs.make_vcs_testcase_subclasses(Bzr, sys.modules[__name__])
 
-unitsuite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
-suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
+    unitsuite =unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
+    suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
index 78645abb3afe4ab1590c2220a1f6a55d5f230b02..dcd4ca946a3d97df95e44fd5f379159f93b5cbc0 100644 (file)
@@ -27,12 +27,15 @@ import os
 from textwrap import TextWrapper
 from StringIO import StringIO
 import sys
-import doctest
 
+import libbe
 import bugdir
 import comment
 import plugin
 import encoding
+if libbe.TESTING == True:
+    import doctest
+
 
 class UserError(Exception):
     def __init__(self, msg):
@@ -293,4 +296,5 @@ def bug_comment_from_id(bdir, id):
             raise UserError(e.message)
     return (bug, comm)
 
-suite = doctest.DocTestSuite()
+if libbe.TESTING == True:
+    suite = doctest.DocTestSuite()
index c5f1cc9c36b6d7d6f9c434dfc4ee33aa1bea8fe2..32536d4f94bb9a6ef35253b26a357b6ad6fd5ea5 100644 (file)
@@ -32,8 +32,8 @@ try: # import core module, Python >= 2.5
 except ImportError: # look for non-core module
     from elementtree import ElementTree
 import xml.sax.saxutils
-import doctest
 
+import libbe
 from beuuid import uuid_gen
 from properties import Property, doc_property, local_property, \
     defaulting_property, checked_property, cached_property, \
@@ -42,6 +42,8 @@ import settings_object
 import mapfile
 from tree import Tree
 import utility
+if libbe.TESTING == True:
+    import doctest
 
 
 class InvalidShortname(KeyError):
@@ -796,4 +798,5 @@ class CommentCompoundComparator (object):
         
 cmp_full = CommentCompoundComparator()
 
-suite = doctest.DocTestSuite()
+if libbe.TESTING == True:
+    suite = doctest.DocTestSuite()
index 4f3273133e8e6f729179786948834483832cae00..ccd236b6dfd719daddab449e3b96211f43af984f 100644 (file)
@@ -25,7 +25,10 @@ import codecs
 import locale
 import os.path
 import sys
-import doctest
+
+import libbe
+if libbe.TESTING == True:
+    import doctest
 
 
 default_encoding = sys.getfilesystemencoding() or locale.getpreferredencoding()
@@ -87,4 +90,5 @@ def get_val(name, section="DEFAULT", default=None, encoding=None):
     else:
         return default
 
-suite = doctest.DocTestSuite()
+if libbe.TESTING == True:
+    suite = doctest.DocTestSuite()
index 059452ce6bceb2f58477b86147613dc5128db572..d94eaef08f478a1d72347c56eb23013505289292 100644 (file)
@@ -28,10 +28,12 @@ try: # import core module, Python >= 2.5
 except ImportError: # look for non-core module
     from elementtree import ElementTree
 from xml.sax.saxutils import unescape
-import doctest
-import unittest
 
+import libbe
 import vcs
+if libbe.TESTING == True:
+    import doctest
+    import unittest
 
 
 def new():
@@ -183,7 +185,8 @@ class Darcs(vcs.VCS):
         except IndexError:
             return None
 \f    
-vcs.make_vcs_testcase_subclasses(Darcs, sys.modules[__name__])
+if libbe.TESTING == True:
+    vcs.make_vcs_testcase_subclasses(Darcs, sys.modules[__name__])
 
-unitsuite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
-suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
+    unitsuite =unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
+    suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
index 6e830c67ed51610c64cabd80122bb36a98b6fba7..4c225977ab84ec97186a5b6f435eae45d1f20a28 100644 (file)
 """Compare two bug trees."""
 
 import difflib
-import doctest
 
+import libbe
 from libbe import bugdir, bug, settings_object, tree
 from libbe.utility import time_to_str
+if libbe.TESTING == True:
+    import doctest
 
 
 class DiffTree (tree.Tree):
@@ -417,4 +419,5 @@ class Diff (object):
         return difflib.unified_diff(old_body, new_body)
 
 
-suite = doctest.DocTestSuite()
+if libbe.TESTING == True:
+    suite = doctest.DocTestSuite()
index ed497a0e06a9a238d1c78e76a7b87aa082b79c3c..859cedc373bdb35d76c1929caec6f30c9620bd3d 100644 (file)
@@ -26,7 +26,10 @@ import locale
 import os
 import sys
 import tempfile
-import doctest
+
+import libbe
+if libbe.TESTING == True:
+    import doctest
 
 
 default_encoding = sys.getfilesystemencoding() or locale.getpreferredencoding()
@@ -106,4 +109,5 @@ def trimmed_string(instring):
         out.append(line)
     return ''.join(out)
 
-suite = doctest.DocTestSuite()
+if libbe.TESTING == True:
+    suite = doctest.DocTestSuite()
index b0a04cce53ec3b8af51ca5d36f982736be57a6a5..d09117f3c1e49d2c05df505696eabc5b123dad5a 100644 (file)
@@ -23,7 +23,10 @@ Support input/output/filesystem encodings (e.g. UTF-8).
 import codecs
 import locale
 import sys
-import doctest
+
+import libbe
+if libbe.TESTING == True:
+    import doctest
 
 
 ENCODING = None # override get_encoding() output by setting this
@@ -59,4 +62,5 @@ def set_IO_stream_encodings(encoding):
     sys.stdout = codecs.getwriter(encoding)(sys.__stdout__)
     sys.stderr = codecs.getwriter(encoding)(sys.__stderr__)
 
-suite = doctest.DocTestSuite()
+if libbe.TESTING == True:
+    suite = doctest.DocTestSuite()
index 55556defd7704d56143a7c0ff8a25b327a5ab340..7f6e53ae50bdc09475fc1a1ed193fe2096cfcfe4 100644 (file)
@@ -25,9 +25,11 @@ import os
 import re
 import sys
 import unittest
-import doctest
 
+import libbe
 import vcs
+if libbe.TESTING == True:
+    import doctest
 
 
 def new():
@@ -142,7 +144,8 @@ class Git(vcs.VCS):
             return None
 
 \f    
-vcs.make_vcs_testcase_subclasses(Git, sys.modules[__name__])
+if libbe.TESTING == True:
+    vcs.make_vcs_testcase_subclasses(Git, sys.modules[__name__])
 
-unitsuite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
-suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
+    unitsuite =unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
+    suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
index d7eb7967942515de7f88cb6c0c10fcc15c7f84e3..ed277179201bb534451a4fef2dab7dba00680d92 100644 (file)
@@ -24,11 +24,14 @@ Mercurial (hg) backend.
 import os
 import re
 import sys
-import unittest
-import doctest
 
+import libbe
 import vcs
 
+if libbe.TESTING == True:
+    import unittest
+    import doctest
+
 
 def new():
     return Hg()
@@ -98,7 +101,8 @@ class Hg(vcs.VCS):
         return None
 
 \f    
-vcs.make_vcs_testcase_subclasses(Hg, sys.modules[__name__])
+if libbe.TESTING == True:
+    vcs.make_vcs_testcase_subclasses(Hg, sys.modules[__name__])
 
-unitsuite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
-suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
+    unitsuite =unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
+    suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
index 062606b1d917a3c9373c3caee1a6d5d533c1ecd7..8e1e2790d1bf9609b8eaacc1b8209eddd7c1bece 100644 (file)
@@ -25,7 +25,10 @@ independent/conflicting changes.
 import errno
 import os.path
 import yaml
-import doctest
+
+import libbe
+if libbe.TESTING == True:
+    import doctest
 
 
 class IllegalKey(Exception):
@@ -119,4 +122,5 @@ def map_load(vcs, path, allow_no_vcs=False):
                                      binary=True)
     return parse(contents)
 
-suite = doctest.DocTestSuite()
+if libbe.TESTING == True:
+    suite = doctest.DocTestSuite()
index e6b06fb02121d546e363628a1e4c78043768cbcd..03f68fc9320bcf681010e23e1d07692028b4c05b 100644 (file)
@@ -25,7 +25,10 @@ submodules (i.e. "plugins").
 import os
 import os.path
 import sys
-import doctest
+
+import libbe
+if libbe.TESTING == True:
+    import doctest
 
 def my_import(mod_name):
     module = __import__(mod_name)
@@ -68,4 +71,5 @@ plugin_path = os.path.realpath(os.path.dirname(os.path.dirname(__file__)))
 if plugin_path not in sys.path:
     sys.path.append(plugin_path)
 
-suite = doctest.DocTestSuite()
+if libbe.TESTING == True:
+    suite = doctest.DocTestSuite()
index 54375b4b8d7c9d4e858690ac6068d63d5f3bd4cf..f756ff03e39606dc49a201be386c09a78e1450f9 100644 (file)
@@ -30,7 +30,10 @@ for more information on decorators.
 
 import copy
 import types
-import unittest
+
+import libbe
+if libbe.TESTING == True:
+    import unittest
 
 
 class ValueCheckError (ValueError):
@@ -383,257 +386,257 @@ def change_hook_property(hook, mutable=False, default=None):
         return funcs
     return decorator
 
-
-class DecoratorTests(unittest.TestCase):
-    def testLocalDoc(self):
-        class Test(object):
-            @Property
-            @doc_property("A fancy property")
-            def x():
-                return {}
-        self.failUnless(Test.x.__doc__ == "A fancy property",
-                        Test.x.__doc__)
-    def testLocalProperty(self):
-        class Test(object):
-            @Property
-            @local_property(name="LOCAL")
-            def x():
-                return {}
-        t = Test()
-        self.failUnless(t.x == None, str(t.x))
-        t.x = 'z' # the first set initializes ._LOCAL_value
-        self.failUnless(t.x == 'z', str(t.x))
-        self.failUnless("_LOCAL_value" in dir(t), dir(t))
-        self.failUnless(t._LOCAL_value == 'z', t._LOCAL_value)
-    def testSettingsProperty(self):
-        class Test(object):
-            @Property
-            @settings_property(name="attr")
-            def x():
-                return {}
-            def __init__(self):
-                self.settings = {}
-        t = Test()
-        self.failUnless(t.x == None, str(t.x))
-        t.x = 'z' # the first set initializes ._LOCAL_value
-        self.failUnless(t.x == 'z', str(t.x))
-        self.failUnless("attr" in t.settings, t.settings)
-        self.failUnless(t.settings["attr"] == 'z', t.settings["attr"])
-    def testDefaultingLocalProperty(self):
-        class Test(object):
-            @Property
-            @defaulting_property(default='y', null='x')
-            @local_property(name="DEFAULT", null=5)
-            def x(): return {}
-        t = Test()
-        self.failUnless(t.x == 5, str(t.x))
-        t.x = 'x'
-        self.failUnless(t.x == 'y', str(t.x))
-        t.x = 'y'
-        self.failUnless(t.x == 'y', str(t.x))
-        t.x = 'z'
-        self.failUnless(t.x == 'z', str(t.x))
-        t.x = 5
-        self.failUnless(t.x == 5, str(t.x))
-    def testCheckedLocalProperty(self):
-        class Test(object):
-            @Property
-            @checked_property(allowed=['x', 'y', 'z'])
-            @local_property(name="CHECKED")
-            def x(): return {}
-            def __init__(self):
-                self._CHECKED_value = 'x'
-        t = Test()
-        self.failUnless(t.x == 'x', str(t.x))
-        try:
+if libbe.TESTING == True:
+    class DecoratorTests(unittest.TestCase):
+        def testLocalDoc(self):
+            class Test(object):
+                @Property
+                @doc_property("A fancy property")
+                def x():
+                    return {}
+            self.failUnless(Test.x.__doc__ == "A fancy property",
+                            Test.x.__doc__)
+        def testLocalProperty(self):
+            class Test(object):
+                @Property
+                @local_property(name="LOCAL")
+                def x():
+                    return {}
+            t = Test()
+            self.failUnless(t.x == None, str(t.x))
+            t.x = 'z' # the first set initializes ._LOCAL_value
+            self.failUnless(t.x == 'z', str(t.x))
+            self.failUnless("_LOCAL_value" in dir(t), dir(t))
+            self.failUnless(t._LOCAL_value == 'z', t._LOCAL_value)
+        def testSettingsProperty(self):
+            class Test(object):
+                @Property
+                @settings_property(name="attr")
+                def x():
+                    return {}
+                def __init__(self):
+                    self.settings = {}
+            t = Test()
+            self.failUnless(t.x == None, str(t.x))
+            t.x = 'z' # the first set initializes ._LOCAL_value
+            self.failUnless(t.x == 'z', str(t.x))
+            self.failUnless("attr" in t.settings, t.settings)
+            self.failUnless(t.settings["attr"] == 'z', t.settings["attr"])
+        def testDefaultingLocalProperty(self):
+            class Test(object):
+                @Property
+                @defaulting_property(default='y', null='x')
+                @local_property(name="DEFAULT", null=5)
+                def x(): return {}
+            t = Test()
+            self.failUnless(t.x == 5, str(t.x))
+            t.x = 'x'
+            self.failUnless(t.x == 'y', str(t.x))
+            t.x = 'y'
+            self.failUnless(t.x == 'y', str(t.x))
+            t.x = 'z'
+            self.failUnless(t.x == 'z', str(t.x))
+            t.x = 5
+            self.failUnless(t.x == 5, str(t.x))
+        def testCheckedLocalProperty(self):
+            class Test(object):
+                @Property
+                @checked_property(allowed=['x', 'y', 'z'])
+                @local_property(name="CHECKED")
+                def x(): return {}
+                def __init__(self):
+                    self._CHECKED_value = 'x'
+            t = Test()
+            self.failUnless(t.x == 'x', str(t.x))
+            try:
+                t.x = None
+                e = None
+            except ValueCheckError, e:
+                pass
+            self.failUnless(type(e) == ValueCheckError, type(e))
+        def testTwoCheckedLocalProperties(self):
+            class Test(object):
+                @Property
+                @checked_property(allowed=['x', 'y', 'z'])
+                @local_property(name="X")
+                def x(): return {}
+
+                @Property
+                @checked_property(allowed=['a', 'b', 'c'])
+                @local_property(name="A")
+                def a(): return {}
+                def __init__(self):
+                    self._A_value = 'a'
+                    self._X_value = 'x'
+            t = Test()
+            try:
+                t.x = 'a'
+                e = None
+            except ValueCheckError, e:
+                pass
+            self.failUnless(type(e) == ValueCheckError, type(e))
+            t.x = 'x'
+            t.x = 'y'
+            t.x = 'z'
+            try:
+                t.a = 'x'
+                e = None
+            except ValueCheckError, e:
+                pass
+            self.failUnless(type(e) == ValueCheckError, type(e))
+            t.a = 'a'
+            t.a = 'b'
+            t.a = 'c'
+        def testFnCheckedLocalProperty(self):
+            class Test(object):
+                @Property
+                @fn_checked_property(lambda v : v in ['x', 'y', 'z'])
+                @local_property(name="CHECKED")
+                def x(): return {}
+                def __init__(self):
+                    self._CHECKED_value = 'x'
+            t = Test()
+            self.failUnless(t.x == 'x', str(t.x))
+            try:
+                t.x = None
+                e = None
+            except ValueCheckError, e:
+                pass
+            self.failUnless(type(e) == ValueCheckError, type(e))
+        def testCachedLocalProperty(self):
+            class Gen(object):
+                def __init__(self):
+                    self.i = 0
+                def __call__(self, owner):
+                    self.i += 1
+                    return self.i
+            class Test(object):
+                @Property
+                @cached_property(generator=Gen(), initVal=None)
+                @local_property(name="CACHED")
+                def x(): return {}
+            t = Test()
+            self.failIf("_CACHED_cache" in dir(t),
+                        getattr(t, "_CACHED_cache", None))
+            self.failUnless(t.x == 1, t.x)
+            self.failUnless(t.x == 1, t.x)
+            self.failUnless(t.x == 1, t.x)
+            t.x = 8
+            self.failUnless(t.x == 8, t.x)
+            self.failUnless(t.x == 8, t.x)
+            t._CACHED_cache = False        # Caching is off, but the stored value
+            val = t.x                      # is 8, not the initVal (None), so we
+            self.failUnless(val == 8, val) # get 8.
+            t._CACHED_value = None         # Now we've set the stored value to None
+            val = t.x                      # so future calls to fget (like this)
+            self.failUnless(val == 2, val) # will call the generator every time...
+            val = t.x
+            self.failUnless(val == 3, val)
+            val = t.x
+            self.failUnless(val == 4, val)
+            t._CACHED_cache = True              # We turn caching back on, and get
+            self.failUnless(t.x == 1, str(t.x)) # the original cached value.
+            del t._CACHED_cached_value          # Removing that value forces a
+            self.failUnless(t.x == 5, str(t.x)) # single cache-regenerating call
+            self.failUnless(t.x == 5, str(t.x)) # to the genenerator, after which
+            self.failUnless(t.x == 5, str(t.x)) # we get the new cached value.
+        def testPrimedLocalProperty(self):
+            class Test(object):
+                def prime(self):
+                    self.settings["PRIMED"] = "initialized"
+                @Property
+                @primed_property(primer=prime, initVal=None)
+                @settings_property(name="PRIMED")
+                def x(): return {}
+                def __init__(self):
+                    self.settings={}
+            t = Test()
+            self.failIf("_PRIMED_prime" in dir(t),
+                        getattr(t, "_PRIMED_prime", None))
+            self.failUnless(t.x == "initialized", t.x)
+            t.x = 1
+            self.failUnless(t.x == 1, t.x)
             t.x = None
-            e = None
-        except ValueCheckError, e:
-            pass
-        self.failUnless(type(e) == ValueCheckError, type(e))
-    def testTwoCheckedLocalProperties(self):
-        class Test(object):
-            @Property
-            @checked_property(allowed=['x', 'y', 'z'])
-            @local_property(name="X")
-            def x(): return {}
-
-            @Property
-            @checked_property(allowed=['a', 'b', 'c'])
-            @local_property(name="A")
-            def a(): return {}
-            def __init__(self):
-                self._A_value = 'a'
-                self._X_value = 'x'
-        t = Test()
-        try:
-            t.x = 'a'
-            e = None
-        except ValueCheckError, e:
-            pass
-        self.failUnless(type(e) == ValueCheckError, type(e))
-        t.x = 'x'
-        t.x = 'y'
-        t.x = 'z'
-        try:
-            t.a = 'x'
-            e = None
-        except ValueCheckError, e:
-            pass
-        self.failUnless(type(e) == ValueCheckError, type(e))
-        t.a = 'a'
-        t.a = 'b'
-        t.a = 'c'
-    def testFnCheckedLocalProperty(self):
-        class Test(object):
-            @Property
-            @fn_checked_property(lambda v : v in ['x', 'y', 'z'])
-            @local_property(name="CHECKED")
-            def x(): return {}
-            def __init__(self):
-                self._CHECKED_value = 'x'
-        t = Test()
-        self.failUnless(t.x == 'x', str(t.x))
-        try:
-            t.x = None
-            e = None
-        except ValueCheckError, e:
-            pass
-        self.failUnless(type(e) == ValueCheckError, type(e))
-    def testCachedLocalProperty(self):
-        class Gen(object):
-            def __init__(self):
-                self.i = 0
-            def __call__(self, owner):
-                self.i += 1
-                return self.i
-        class Test(object):
-            @Property
-            @cached_property(generator=Gen(), initVal=None)
-            @local_property(name="CACHED")
-            def x(): return {}
-        t = Test()
-        self.failIf("_CACHED_cache" in dir(t), getattr(t, "_CACHED_cache", None))
-        self.failUnless(t.x == 1, t.x)
-        self.failUnless(t.x == 1, t.x)
-        self.failUnless(t.x == 1, t.x)
-        t.x = 8
-        self.failUnless(t.x == 8, t.x)
-        self.failUnless(t.x == 8, t.x)
-        t._CACHED_cache = False        # Caching is off, but the stored value
-        val = t.x                      # is 8, not the initVal (None), so we
-        self.failUnless(val == 8, val) # get 8.
-        t._CACHED_value = None         # Now we've set the stored value to None
-        val = t.x                      # so future calls to fget (like this)
-        self.failUnless(val == 2, val) # will call the generator every time...
-        val = t.x
-        self.failUnless(val == 3, val)
-        val = t.x
-        self.failUnless(val == 4, val)
-        t._CACHED_cache = True              # We turn caching back on, and get
-        self.failUnless(t.x == 1, str(t.x)) # the original cached value.
-        del t._CACHED_cached_value          # Removing that value forces a
-        self.failUnless(t.x == 5, str(t.x)) # single cache-regenerating call
-        self.failUnless(t.x == 5, str(t.x)) # to the genenerator, after which
-        self.failUnless(t.x == 5, str(t.x)) # we get the new cached value.
-    def testPrimedLocalProperty(self):
-        class Test(object):
-            def prime(self):
-                self.settings["PRIMED"] = "initialized"
-            @Property
-            @primed_property(primer=prime, initVal=None)
-            @settings_property(name="PRIMED")
-            def x(): return {}
-            def __init__(self):
-                self.settings={}
-        t = Test()
-        self.failIf("_PRIMED_prime" in dir(t), getattr(t, "_PRIMED_prime", None))
-        self.failUnless(t.x == "initialized", t.x)
-        t.x = 1
-        self.failUnless(t.x == 1, t.x)
-        t.x = None
-        self.failUnless(t.x == "initialized", t.x)
-        t._PRIMED_prime = True
-        t.x = 3
-        self.failUnless(t.x == "initialized", t.x)
-        t._PRIMED_prime = False
-        t.x = 3
-        self.failUnless(t.x == 3, t.x)
-    def testChangeHookLocalProperty(self):
-        class Test(object):
-            def _hook(self, old, new):
-                self.old = old
-                self.new = new
-
-            @Property
-            @change_hook_property(_hook)
-            @local_property(name="HOOKED")
-            def x(): return {}
-        t = Test()
-        t.x = 1
-        self.failUnless(t.old == None, t.old)
-        self.failUnless(t.new == 1, t.new)
-        t.x = 1
-        self.failUnless(t.old == None, t.old)
-        self.failUnless(t.new == 1, t.new)
-        t.x = 2
-        self.failUnless(t.old == 1, t.old)
-        self.failUnless(t.new == 2, t.new)
-    def testChangeHookMutableProperty(self):
-        class Test(object):
-            def _hook(self, old, new):
-                self.old = old
-                self.new = new
-                self.hook_calls += 1
-
-            @Property
-            @change_hook_property(_hook, mutable=True)
-            @local_property(name="HOOKED")
-            def x(): return {}
-        t = Test()
-        t.hook_calls = 0
-        t.x = []
-        self.failUnless(t.old == None, t.old)
-        self.failUnless(t.new == [], t.new)
-        self.failUnless(t.hook_calls == 1, t.hook_calls)
-        a = t.x
-        a.append(5)
-        t.x = a
-        self.failUnless(t.old == [], t.old)
-        self.failUnless(t.new == [5], t.new)
-        self.failUnless(t.hook_calls == 2, t.hook_calls)
-        t.x = []
-        self.failUnless(t.old == [5], t.old)
-        self.failUnless(t.new == [], t.new)
-        self.failUnless(t.hook_calls == 3, t.hook_calls)
-        # now append without reassigning.  this doesn't trigger the
-        # change, since we don't ever set t.x, only get it and mess
-        # with it.  It does, however, update our t.new, since t.new =
-        # t.x and is not a static copy.
-        t.x.append(5)
-        self.failUnless(t.old == [5], t.old)
-        self.failUnless(t.new == [5], t.new)
-        self.failUnless(t.hook_calls == 3, t.hook_calls)
-        # however, the next t.x get _will_ notice the change...
-        a = t.x
-        self.failUnless(t.old == [], t.old)
-        self.failUnless(t.new == [5], t.new)
-        self.failUnless(t.hook_calls == 4, t.hook_calls)
-        t.x.append(6) # this append(6) is not noticed yet
-        self.failUnless(t.old == [], t.old)
-        self.failUnless(t.new == [5,6], t.new)
-        self.failUnless(t.hook_calls == 4, t.hook_calls)
-        # this append(7) is not noticed, but the t.x get causes the
-        # append(6) to be noticed
-        t.x.append(7)
-        self.failUnless(t.old == [5], t.old)
-        self.failUnless(t.new == [5,6,7], t.new)
-        self.failUnless(t.hook_calls == 5, t.hook_calls)
-        a = t.x # now the append(7) is noticed
-        self.failUnless(t.old == [5,6], t.old)
-        self.failUnless(t.new == [5,6,7], t.new)
-        self.failUnless(t.hook_calls == 6, t.hook_calls)
-
-
-suite = unittest.TestLoader().loadTestsFromTestCase(DecoratorTests)
-
+            self.failUnless(t.x == "initialized", t.x)
+            t._PRIMED_prime = True
+            t.x = 3
+            self.failUnless(t.x == "initialized", t.x)
+            t._PRIMED_prime = False
+            t.x = 3
+            self.failUnless(t.x == 3, t.x)
+        def testChangeHookLocalProperty(self):
+            class Test(object):
+                def _hook(self, old, new):
+                    self.old = old
+                    self.new = new
+
+                @Property
+                @change_hook_property(_hook)
+                @local_property(name="HOOKED")
+                def x(): return {}
+            t = Test()
+            t.x = 1
+            self.failUnless(t.old == None, t.old)
+            self.failUnless(t.new == 1, t.new)
+            t.x = 1
+            self.failUnless(t.old == None, t.old)
+            self.failUnless(t.new == 1, t.new)
+            t.x = 2
+            self.failUnless(t.old == 1, t.old)
+            self.failUnless(t.new == 2, t.new)
+        def testChangeHookMutableProperty(self):
+            class Test(object):
+                def _hook(self, old, new):
+                    self.old = old
+                    self.new = new
+                    self.hook_calls += 1
+
+                @Property
+                @change_hook_property(_hook, mutable=True)
+                @local_property(name="HOOKED")
+                def x(): return {}
+            t = Test()
+            t.hook_calls = 0
+            t.x = []
+            self.failUnless(t.old == None, t.old)
+            self.failUnless(t.new == [], t.new)
+            self.failUnless(t.hook_calls == 1, t.hook_calls)
+            a = t.x
+            a.append(5)
+            t.x = a
+            self.failUnless(t.old == [], t.old)
+            self.failUnless(t.new == [5], t.new)
+            self.failUnless(t.hook_calls == 2, t.hook_calls)
+            t.x = []
+            self.failUnless(t.old == [5], t.old)
+            self.failUnless(t.new == [], t.new)
+            self.failUnless(t.hook_calls == 3, t.hook_calls)
+            # now append without reassigning.  this doesn't trigger the
+            # change, since we don't ever set t.x, only get it and mess
+            # with it.  It does, however, update our t.new, since t.new =
+            # t.x and is not a static copy.
+            t.x.append(5)
+            self.failUnless(t.old == [5], t.old)
+            self.failUnless(t.new == [5], t.new)
+            self.failUnless(t.hook_calls == 3, t.hook_calls)
+            # however, the next t.x get _will_ notice the change...
+            a = t.x
+            self.failUnless(t.old == [], t.old)
+            self.failUnless(t.new == [5], t.new)
+            self.failUnless(t.hook_calls == 4, t.hook_calls)
+            t.x.append(6) # this append(6) is not noticed yet
+            self.failUnless(t.old == [], t.old)
+            self.failUnless(t.new == [5,6], t.new)
+            self.failUnless(t.hook_calls == 4, t.hook_calls)
+            # this append(7) is not noticed, but the t.x get causes the
+            # append(6) to be noticed
+            t.x.append(7)
+            self.failUnless(t.old == [5], t.old)
+            self.failUnless(t.new == [5,6,7], t.new)
+            self.failUnless(t.hook_calls == 5, t.hook_calls)
+            a = t.x # now the append(7) is noticed
+            self.failUnless(t.old == [5,6], t.old)
+            self.failUnless(t.new == [5,6,7], t.new)
+            self.failUnless(t.hook_calls == 6, t.hook_calls)
+
+    suite = unittest.TestLoader().loadTestsFromTestCase(DecoratorTests)
index 7efda5bb9e5ca6b1eb76bcc6e786c4af78454ff4..6a00ba99e7230aaee12a4eacdbbfde9d300c3637 100644 (file)
@@ -23,13 +23,14 @@ property storage useful for BE objects with saved properties
 unittests at the end of the module.
 """
 
-import doctest
-import unittest
-
+import libbe
 from properties import Property, doc_property, local_property, \
     defaulting_property, checked_property, fn_checked_property, \
     cached_property, primed_property, change_hook_property, \
     settings_property
+if libbe.TESTING == True:
+    import doctest
+    import unittest
 
 
 class _Token (object):
@@ -228,186 +229,205 @@ class SavedSettingsObject(object):
                 self.clear_cached_setting(setting)
 
 
-class SavedSettingsObjectTests(unittest.TestCase):
-    def testSimpleProperty(self):
-        """Testing a minimal versioned property"""
-        class Test(SavedSettingsObject):
-            settings_properties = []
-            required_saved_properties = []
-            @versioned_property(name="Content-type",
-                                doc="A test property",
-                                settings_properties=settings_properties,
-                                required_saved_properties=required_saved_properties)
-            def content_type(): return {}
-            def __init__(self):
-                SavedSettingsObject.__init__(self)
-        t = Test()
-        # access missing setting
-        self.failUnless(t._settings_loaded == False, t._settings_loaded)
-        self.failUnless(len(t.settings) == 0, len(t.settings))
-        self.failUnless(t.content_type == None, t.content_type)
-        # accessing t.content_type triggers the priming, which runs
-        # t._setup_saved_settings, which fills out t.settings with
-        # EMPTY data.  t._settings_loaded is still false though, since
-        # the default priming does not do any of the `official' loading
-        # that occurs in t.load_settings.
-        self.failUnless(len(t.settings) == 1, len(t.settings))
-        self.failUnless(t.settings["Content-type"] == EMPTY,
-                        t.settings["Content-type"])
-        self.failUnless(t._settings_loaded == False, t._settings_loaded)
-        # load settings creates an EMPTY value in the settings array
-        t.load_settings()
-        self.failUnless(t._settings_loaded == True, t._settings_loaded)
-        self.failUnless(t.settings["Content-type"] == EMPTY,
-                        t.settings["Content-type"])
-        self.failUnless(t.content_type == None, t.content_type)
-        self.failUnless(len(t.settings) == 1, len(t.settings))
-        self.failUnless(t.settings["Content-type"] == EMPTY,
-                        t.settings["Content-type"])
-        # now we set a value
-        t.content_type = 5
-        self.failUnless(t.settings["Content-type"] == 5,
-                        t.settings["Content-type"])
-        self.failUnless(t.content_type == 5, t.content_type)
-        self.failUnless(t.settings["Content-type"] == 5,
-                        t.settings["Content-type"])
-        # now we set another value
-        t.content_type = "text/plain"
-        self.failUnless(t.content_type == "text/plain", t.content_type)
-        self.failUnless(t.settings["Content-type"] == "text/plain",
-                        t.settings["Content-type"])
-        self.failUnless(t._get_saved_settings()=={"Content-type":"text/plain"},
-                        t._get_saved_settings())
-        # now we clear to the post-primed value
-        t.content_type = EMPTY
-        self.failUnless(t._settings_loaded == True, t._settings_loaded)
-        self.failUnless(t.settings["Content-type"] == EMPTY,
-                        t.settings["Content-type"])
-        self.failUnless(t.content_type == None, t.content_type)
-        self.failUnless(len(t.settings) == 1, len(t.settings))
-        self.failUnless(t.settings["Content-type"] == EMPTY,
-                        t.settings["Content-type"])
-    def testDefaultingProperty(self):
-        """Testing a defaulting versioned property"""
-        class Test(SavedSettingsObject):
-            settings_properties = []
-            required_saved_properties = []
-            @versioned_property(name="Content-type",
-                                doc="A test property",
-                                default="text/plain",
-                                settings_properties=settings_properties,
-                                required_saved_properties=required_saved_properties)
-            def content_type(): return {}
-            def __init__(self):
-                SavedSettingsObject.__init__(self)
-        t = Test()
-        self.failUnless(t._settings_loaded == False, t._settings_loaded)
-        self.failUnless(t.content_type == "text/plain", t.content_type)
-        self.failUnless(t._settings_loaded == False, t._settings_loaded)
-        t.load_settings()
-        self.failUnless(t._settings_loaded == True, t._settings_loaded)
-        self.failUnless(t.content_type == "text/plain", t.content_type)
-        self.failUnless(t.settings["Content-type"] == EMPTY,
-                        t.settings["Content-type"])
-        self.failUnless(t._get_saved_settings() == {}, t._get_saved_settings())
-        t.content_type = "text/html"
-        self.failUnless(t.content_type == "text/html",
-                        t.content_type)
-        self.failUnless(t.settings["Content-type"] == "text/html",
-                        t.settings["Content-type"])
-        self.failUnless(t._get_saved_settings()=={"Content-type":"text/html"},
-                        t._get_saved_settings())
-    def testRequiredDefaultingProperty(self):
-        """Testing a required defaulting versioned property"""
-        class Test(SavedSettingsObject):
-            settings_properties = []
-            required_saved_properties = []
-            @versioned_property(name="Content-type",
-                                doc="A test property",
-                                default="text/plain",
-                                settings_properties=settings_properties,
-                                required_saved_properties=required_saved_properties,
-                                require_save=True)
-            def content_type(): return {}
-            def __init__(self):
-                SavedSettingsObject.__init__(self)
-        t = Test()
-        self.failUnless(t._get_saved_settings()=={"Content-type":"text/plain"},
-                        t._get_saved_settings())
-        t.content_type = "text/html"
-        self.failUnless(t._get_saved_settings()=={"Content-type":"text/html"},
-                        t._get_saved_settings())
-    def testClassVersionedPropertyDefinition(self):
-        """Testing a class-specific _versioned property decorator"""
-        class Test(SavedSettingsObject):
-            settings_properties = []
-            required_saved_properties = []
-            def _versioned_property(settings_properties=settings_properties,
-                                    required_saved_properties=required_saved_properties,
-                                    **kwargs):
-                if "settings_properties" not in kwargs:
-                    kwargs["settings_properties"] = settings_properties
-                if "required_saved_properties" not in kwargs:
-                    kwargs["required_saved_properties"]=required_saved_properties
-                return versioned_property(**kwargs)
-            @_versioned_property(name="Content-type",
-                                doc="A test property",
-                                default="text/plain",
-                                require_save=True)
-            def content_type(): return {}
-            def __init__(self):
-                SavedSettingsObject.__init__(self)
-        t = Test()
-        self.failUnless(t._get_saved_settings()=={"Content-type":"text/plain"},
-                        t._get_saved_settings())
-        t.content_type = "text/html"
-        self.failUnless(t._get_saved_settings()=={"Content-type":"text/html"},
-                        t._get_saved_settings())
-    def testMutableChangeHookedProperty(self):
-        """Testing a mutable change-hooked property"""
-        SAVES = []
-        def prop_log_save_settings(self, old, new, saves=SAVES):
-            saves.append("'%s' -> '%s'" % (str(old), str(new)))
-            prop_save_settings(self, old, new)
-        class Test(SavedSettingsObject):
-            settings_properties = []
-            required_saved_properties = []
-            @versioned_property(name="List-type",
-                                doc="A test property",
-                                mutable=True,
-                                change_hook=prop_log_save_settings,
-                                settings_properties=settings_properties,
-                                required_saved_properties=required_saved_properties)
-            def list_type(): return {}
-            def __init__(self):
-                SavedSettingsObject.__init__(self)
-        t = Test()
-        self.failUnless(t._settings_loaded == False, t._settings_loaded)
-        t.load_settings()
-        self.failUnless(SAVES == [], SAVES)
-        self.failUnless(t._settings_loaded == True, t._settings_loaded)
-        self.failUnless(t.list_type == None, t.list_type)
-        self.failUnless(SAVES == [], SAVES)
-        self.failUnless(t.settings["List-type"]==EMPTY,t.settings["List-type"])
-        t.list_type = []
-        self.failUnless(t.settings["List-type"] == [], t.settings["List-type"])
-        self.failUnless(SAVES == [
-                "'<class 'libbe.settings_object.EMPTY'>' -> '[]'"
-                ], SAVES)
-        t.list_type.append(5)
-        self.failUnless(SAVES == [
-                "'<class 'libbe.settings_object.EMPTY'>' -> '[]'",
-                ], SAVES)
-        self.failUnless(t.settings["List-type"] == [5],t.settings["List-type"])
-        self.failUnless(SAVES == [ # the append(5) has not yet been saved
-                "'<class 'libbe.settings_object.EMPTY'>' -> '[]'",
-                ], SAVES)
-        self.failUnless(t.list_type == [5], t.list_type) # <-get triggers saved
-
-        self.failUnless(SAVES == [ # now the append(5) has been saved.
-                "'<class 'libbe.settings_object.EMPTY'>' -> '[]'",
-                "'[]' -> '[5]'"
-                ], SAVES)
-
-unitsuite=unittest.TestLoader().loadTestsFromTestCase(SavedSettingsObjectTests)
-suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
+if libbe.TESTING == True:
+    class SavedSettingsObjectTests(unittest.TestCase):
+        def testSimpleProperty(self):
+            """Testing a minimal versioned property"""
+            class Test(SavedSettingsObject):
+                settings_properties = []
+                required_saved_properties = []
+                @versioned_property(name="Content-type",
+                                    doc="A test property",
+                                    settings_properties=settings_properties,
+                                    required_saved_properties= \
+                                        required_saved_properties)
+                def content_type(): return {}
+                def __init__(self):
+                    SavedSettingsObject.__init__(self)
+            t = Test()
+            # access missing setting
+            self.failUnless(t._settings_loaded == False, t._settings_loaded)
+            self.failUnless(len(t.settings) == 0, len(t.settings))
+            self.failUnless(t.content_type == None, t.content_type)
+            # accessing t.content_type triggers the priming, which runs
+            # t._setup_saved_settings, which fills out t.settings with
+            # EMPTY data.  t._settings_loaded is still false though, since
+            # the default priming does not do any of the `official' loading
+            # that occurs in t.load_settings.
+            self.failUnless(len(t.settings) == 1, len(t.settings))
+            self.failUnless(t.settings["Content-type"] == EMPTY,
+                            t.settings["Content-type"])
+            self.failUnless(t._settings_loaded == False, t._settings_loaded)
+            # load settings creates an EMPTY value in the settings array
+            t.load_settings()
+            self.failUnless(t._settings_loaded == True, t._settings_loaded)
+            self.failUnless(t.settings["Content-type"] == EMPTY,
+                            t.settings["Content-type"])
+            self.failUnless(t.content_type == None, t.content_type)
+            self.failUnless(len(t.settings) == 1, len(t.settings))
+            self.failUnless(t.settings["Content-type"] == EMPTY,
+                            t.settings["Content-type"])
+            # now we set a value
+            t.content_type = 5
+            self.failUnless(t.settings["Content-type"] == 5,
+                            t.settings["Content-type"])
+            self.failUnless(t.content_type == 5, t.content_type)
+            self.failUnless(t.settings["Content-type"] == 5,
+                            t.settings["Content-type"])
+            # now we set another value
+            t.content_type = "text/plain"
+            self.failUnless(t.content_type == "text/plain", t.content_type)
+            self.failUnless(t.settings["Content-type"] == "text/plain",
+                            t.settings["Content-type"])
+            self.failUnless(t._get_saved_settings() == \
+                                {"Content-type":"text/plain"},
+                            t._get_saved_settings())
+            # now we clear to the post-primed value
+            t.content_type = EMPTY
+            self.failUnless(t._settings_loaded == True, t._settings_loaded)
+            self.failUnless(t.settings["Content-type"] == EMPTY,
+                            t.settings["Content-type"])
+            self.failUnless(t.content_type == None, t.content_type)
+            self.failUnless(len(t.settings) == 1, len(t.settings))
+            self.failUnless(t.settings["Content-type"] == EMPTY,
+                            t.settings["Content-type"])
+        def testDefaultingProperty(self):
+            """Testing a defaulting versioned property"""
+            class Test(SavedSettingsObject):
+                settings_properties = []
+                required_saved_properties = []
+                @versioned_property(name="Content-type",
+                                    doc="A test property",
+                                    default="text/plain",
+                                    settings_properties=settings_properties,
+                                    required_saved_properties= \
+                                        required_saved_properties)
+                def content_type(): return {}
+                def __init__(self):
+                    SavedSettingsObject.__init__(self)
+            t = Test()
+            self.failUnless(t._settings_loaded == False, t._settings_loaded)
+            self.failUnless(t.content_type == "text/plain", t.content_type)
+            self.failUnless(t._settings_loaded == False, t._settings_loaded)
+            t.load_settings()
+            self.failUnless(t._settings_loaded == True, t._settings_loaded)
+            self.failUnless(t.content_type == "text/plain", t.content_type)
+            self.failUnless(t.settings["Content-type"] == EMPTY,
+                            t.settings["Content-type"])
+            self.failUnless(t._get_saved_settings() == {},
+                            t._get_saved_settings())
+            t.content_type = "text/html"
+            self.failUnless(t.content_type == "text/html",
+                            t.content_type)
+            self.failUnless(t.settings["Content-type"] == "text/html",
+                            t.settings["Content-type"])
+            self.failUnless(t._get_saved_settings() == \
+                                {"Content-type":"text/html"},
+                            t._get_saved_settings())
+        def testRequiredDefaultingProperty(self):
+            """Testing a required defaulting versioned property"""
+            class Test(SavedSettingsObject):
+                settings_properties = []
+                required_saved_properties = []
+                @versioned_property(name="Content-type",
+                                    doc="A test property",
+                                    default="text/plain",
+                                    settings_properties=settings_properties,
+                                    required_saved_properties= \
+                                        required_saved_properties,
+                                    require_save=True)
+                def content_type(): return {}
+                def __init__(self):
+                    SavedSettingsObject.__init__(self)
+            t = Test()
+            self.failUnless(t._get_saved_settings() == \
+                                {"Content-type":"text/plain"},
+                            t._get_saved_settings())
+            t.content_type = "text/html"
+            self.failUnless(t._get_saved_settings() == \
+                                {"Content-type":"text/html"},
+                            t._get_saved_settings())
+        def testClassVersionedPropertyDefinition(self):
+            """Testing a class-specific _versioned property decorator"""
+            class Test(SavedSettingsObject):
+                settings_properties = []
+                required_saved_properties = []
+                def _versioned_property(settings_properties= \
+                                            settings_properties,
+                                        required_saved_properties= \
+                                            required_saved_properties,
+                                        **kwargs):
+                    if "settings_properties" not in kwargs:
+                        kwargs["settings_properties"] = settings_properties
+                    if "required_saved_properties" not in kwargs:
+                        kwargs["required_saved_properties"] = \
+                            required_saved_properties
+                    return versioned_property(**kwargs)
+                @_versioned_property(name="Content-type",
+                                    doc="A test property",
+                                    default="text/plain",
+                                    require_save=True)
+                def content_type(): return {}
+                def __init__(self):
+                    SavedSettingsObject.__init__(self)
+            t = Test()
+            self.failUnless(t._get_saved_settings() == \
+                                {"Content-type":"text/plain"},
+                            t._get_saved_settings())
+            t.content_type = "text/html"
+            self.failUnless(t._get_saved_settings() == \
+                                {"Content-type":"text/html"},
+                            t._get_saved_settings())
+        def testMutableChangeHookedProperty(self):
+            """Testing a mutable change-hooked property"""
+            SAVES = []
+            def prop_log_save_settings(self, old, new, saves=SAVES):
+                saves.append("'%s' -> '%s'" % (str(old), str(new)))
+                prop_save_settings(self, old, new)
+            class Test(SavedSettingsObject):
+                settings_properties = []
+                required_saved_properties = []
+                @versioned_property(name="List-type",
+                                    doc="A test property",
+                                    mutable=True,
+                                    change_hook=prop_log_save_settings,
+                                    settings_properties=settings_properties,
+                                    required_saved_properties= \
+                                        required_saved_properties)
+                def list_type(): return {}
+                def __init__(self):
+                    SavedSettingsObject.__init__(self)
+            t = Test()
+            self.failUnless(t._settings_loaded == False, t._settings_loaded)
+            t.load_settings()
+            self.failUnless(SAVES == [], SAVES)
+            self.failUnless(t._settings_loaded == True, t._settings_loaded)
+            self.failUnless(t.list_type == None, t.list_type)
+            self.failUnless(SAVES == [], SAVES)
+            self.failUnless(t.settings["List-type"]==EMPTY,
+                            t.settings["List-type"])
+            t.list_type = []
+            self.failUnless(t.settings["List-type"] == [],
+                            t.settings["List-type"])
+            self.failUnless(SAVES == [
+                    "'<class 'libbe.settings_object.EMPTY'>' -> '[]'"
+                    ], SAVES)
+            t.list_type.append(5)
+            self.failUnless(SAVES == [
+                    "'<class 'libbe.settings_object.EMPTY'>' -> '[]'",
+                    ], SAVES)
+            self.failUnless(t.settings["List-type"] == [5],
+                            t.settings["List-type"])
+            self.failUnless(SAVES == [ # the append(5) has not yet been saved
+                    "'<class 'libbe.settings_object.EMPTY'>' -> '[]'",
+                    ], SAVES)
+            self.failUnless(t.list_type == [5], t.list_type)#get triggers saved
+
+            self.failUnless(SAVES == [ # now the append(5) has been saved.
+                    "'<class 'libbe.settings_object.EMPTY'>' -> '[]'",
+                    "'[]' -> '[5]'"
+                    ], SAVES)
+
+    unitsuite = unittest.TestLoader().loadTestsFromTestCase( \
+        SavedSettingsObjectTests)
+    suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
index fe8820605dd37bb468df060b3d323f57c8e0b080..8806e26497b8999bac640961cf0d1c986dfbab0f 100644 (file)
@@ -20,9 +20,11 @@ Functions for running external commands in subprocesses.
 
 from subprocess import Popen, PIPE
 import sys
-import doctest
 
+import libbe
 from encoding import get_encoding
+if libbe.TESTING == True:
+    import doctest
 
 _MSWINDOWS = sys.platform == 'win32'
 _POSIX = not _MSWINDOWS
@@ -217,4 +219,5 @@ class Pipe (object):
             stderrs = read_strings
             return (stdout, stderrs)
 
-suite = doctest.DocTestSuite()
+if libbe.TESTING == True:
+    suite = doctest.DocTestSuite()
index 1bfd803365cde2aebaf5b5eecf2953900c9496a3..8077da76d267a8433727f90260458931eb8fd9ab 100644 (file)
@@ -20,7 +20,9 @@
 Define a traversable tree structure.
 """
 
-import doctest
+import libbe
+if libbe.TESTING == True:
+    import doctest
 
 class Tree(list):
     """
@@ -181,4 +183,5 @@ class Tree(list):
                 return True
         return False
 
-suite = doctest.DocTestSuite()
+if libbe.TESTING == True:
+    suite = doctest.DocTestSuite()
index 4123c720ecc81dc7cedd09d85c7130a7eaa025ff..785249dda1e25ebfb93b472ce14ac960a9387e2f 100644 (file)
@@ -20,11 +20,13 @@ Handle conversion between the various on-disk images.
 
 import os, os.path
 import sys
-import doctest
 
+import libbe
 import encoding
 import mapfile
 import vcs
+if libbe.TESTING == True:
+    import doctest
 
 # a list of all past versions
 BUGDIR_DISK_VERSIONS = ["Bugs Everywhere Tree 1 0",
@@ -184,4 +186,5 @@ def upgrade(path, current_version,
                 break
             i += 1
 
-suite = doctest.DocTestSuite()
+if libbe.TESTING == True:
+    suite = doctest.DocTestSuite()
index 7510b1626e9d48d50bb6cf71f900c9a1876e67a7..f954422ba60c3ffb40885f54f33c632857eea93d 100644 (file)
@@ -27,7 +27,10 @@ import shutil
 import tempfile
 import time
 import types
-import doctest
+
+import libbe
+if libbe.TESTING == True:
+    import doctest
 
 class InvalidXML(ValueError):
     """
@@ -144,4 +147,5 @@ def iterable_full_of_strings(value, alternative=None):
             return False
     return True
 
-suite = doctest.DocTestSuite()
+if libbe.TESTING == True:
+    suite = doctest.DocTestSuite()
index 57a024539b750b65f07008e295732fc100a6e784..1298a2c48968a4be96f3f6425c1b360f5bf6532a 100644 (file)
@@ -33,13 +33,17 @@ from socket import gethostname
 import shutil
 import sys
 import tempfile
-import unittest
-import doctest
 
+import libbe
 from utility import Dir, search_parent_directories
 from subproc import CommandError, invoke
 from plugin import get_plugin
 
+if libbe.TESTING == True:
+    import unittest
+    import doctest
+
+
 # List VCS modules in order of preference.
 # Don't list this module, it is implicitly last.
 VCS_ORDER = ['arch', 'bzr', 'darcs', 'git', 'hg']
@@ -627,306 +631,307 @@ class VCS(object):
         return (summary, body)
         
 \f
-def setup_vcs_test_fixtures(testcase):
-    """Set up test fixtures for VCS test case."""
-    testcase.vcs = testcase.Class()
-    testcase.dir = Dir()
-    testcase.dirname = testcase.dir.path
-
-    vcs_not_supporting_uninitialized_user_id = []
-    vcs_not_supporting_set_user_id = ["None", "hg"]
-    testcase.vcs_supports_uninitialized_user_id = (
-        testcase.vcs.name not in vcs_not_supporting_uninitialized_user_id)
-    testcase.vcs_supports_set_user_id = (
-        testcase.vcs.name not in vcs_not_supporting_set_user_id)
+if libbe.TESTING == True:
+    def setup_vcs_test_fixtures(testcase):
+        """Set up test fixtures for VCS test case."""
+        testcase.vcs = testcase.Class()
+        testcase.dir = Dir()
+        testcase.dirname = testcase.dir.path
 
-    if not testcase.vcs.installed():
-        testcase.fail(
-            "%(name)s VCS not found" % vars(testcase.Class))
+        vcs_not_supporting_uninitialized_user_id = []
+        vcs_not_supporting_set_user_id = ["None", "hg"]
+        testcase.vcs_supports_uninitialized_user_id = (
+            testcase.vcs.name not in vcs_not_supporting_uninitialized_user_id)
+        testcase.vcs_supports_set_user_id = (
+            testcase.vcs.name not in vcs_not_supporting_set_user_id)
 
-    if testcase.Class.name != "None":
-        testcase.failIf(
-            testcase.vcs.detect(testcase.dirname),
-            "Detected %(name)s VCS before initialising"
-                % vars(testcase.Class))
+        if not testcase.vcs.installed():
+            testcase.fail(
+                "%(name)s VCS not found" % vars(testcase.Class))
 
-    testcase.vcs.init(testcase.dirname)
+        if testcase.Class.name != "None":
+            testcase.failIf(
+                testcase.vcs.detect(testcase.dirname),
+                "Detected %(name)s VCS before initialising"
+                    % vars(testcase.Class))
 
+        testcase.vcs.init(testcase.dirname)
 
-class VCSTestCase(unittest.TestCase):
-    """Test cases for base VCS class."""
+    class VCSTestCase(unittest.TestCase):
+        """Test cases for base VCS class."""
 
-    Class = VCS
+        Class = VCS
 
-    def __init__(self, *args, **kwargs):
-        super(VCSTestCase, self).__init__(*args, **kwargs)
-        self.dirname = None
+        def __init__(self, *args, **kwargs):
+            super(VCSTestCase, self).__init__(*args, **kwargs)
+            self.dirname = None
 
-    def setUp(self):
-        super(VCSTestCase, self).setUp()
-        setup_vcs_test_fixtures(self)
+        def setUp(self):
+            super(VCSTestCase, self).setUp()
+            setup_vcs_test_fixtures(self)
 
-    def tearDown(self):
-        self.vcs.cleanup()
-        self.dir.cleanup()
-        super(VCSTestCase, self).tearDown()
+        def tearDown(self):
+            self.vcs.cleanup()
+            self.dir.cleanup()
+            super(VCSTestCase, self).tearDown()
 
-    def full_path(self, rel_path):
-        return os.path.join(self.dirname, rel_path)
+        def full_path(self, rel_path):
+            return os.path.join(self.dirname, rel_path)
 
 
-class VCS_init_TestCase(VCSTestCase):
-    """Test cases for VCS.init method."""
+    class VCS_init_TestCase(VCSTestCase):
+        """Test cases for VCS.init method."""
 
-    def test_detect_should_succeed_after_init(self):
-        """Should detect VCS in directory after initialization."""
-        self.failUnless(
-            self.vcs.detect(self.dirname),
-            "Did not detect %(name)s VCS after initialising"
-                % vars(self.Class))
-
-    def test_vcs_rootdir_in_specified_root_path(self):
-        """VCS root directory should be in specified root path."""
-        rp = os.path.realpath(self.vcs.rootdir)
-        dp = os.path.realpath(self.dirname)
-        vcs_name = self.Class.name
-        self.failUnless(
-            dp == rp or rp == None,
-            "%(vcs_name)s VCS root in wrong dir (%(dp)s %(rp)s)" % vars())
+        def test_detect_should_succeed_after_init(self):
+            """Should detect VCS in directory after initialization."""
+            self.failUnless(
+                self.vcs.detect(self.dirname),
+                "Did not detect %(name)s VCS after initialising"
+                    % vars(self.Class))
+
+        def test_vcs_rootdir_in_specified_root_path(self):
+            """VCS root directory should be in specified root path."""
+            rp = os.path.realpath(self.vcs.rootdir)
+            dp = os.path.realpath(self.dirname)
+            vcs_name = self.Class.name
+            self.failUnless(
+                dp == rp or rp == None,
+                "%(vcs_name)s VCS root in wrong dir (%(dp)s %(rp)s)" % vars())
 
 
-class VCS_get_user_id_TestCase(VCSTestCase):
-    """Test cases for VCS.get_user_id method."""
+    class VCS_get_user_id_TestCase(VCSTestCase):
+        """Test cases for VCS.get_user_id method."""
 
-    def test_gets_existing_user_id(self):
-        """Should get the existing user ID."""
-        if not self.vcs_supports_uninitialized_user_id:
-            return
+        def test_gets_existing_user_id(self):
+            """Should get the existing user ID."""
+            if not self.vcs_supports_uninitialized_user_id:
+                return
 
-        user_id = self.vcs.get_user_id()
-        self.failUnless(
-            user_id is not None,
-            "unable to get a user id")
+            user_id = self.vcs.get_user_id()
+            self.failUnless(
+                user_id is not None,
+                "unable to get a user id")
 
 
-class VCS_set_user_id_TestCase(VCSTestCase):
-    """Test cases for VCS.set_user_id method."""
+    class VCS_set_user_id_TestCase(VCSTestCase):
+        """Test cases for VCS.set_user_id method."""
 
-    def setUp(self):
-        super(VCS_set_user_id_TestCase, self).setUp()
+        def setUp(self):
+            super(VCS_set_user_id_TestCase, self).setUp()
 
-        if self.vcs_supports_uninitialized_user_id:
-            self.prev_user_id = self.vcs.get_user_id()
-        else:
-            self.prev_user_id = "Uninitialized identity <bogus@example.org>"
-
-        if self.vcs_supports_set_user_id:
-            self.test_new_user_id = "John Doe <jdoe@example.com>"
-            self.vcs.set_user_id(self.test_new_user_id)
-
-    def tearDown(self):
-        if self.vcs_supports_set_user_id:
-            self.vcs.set_user_id(self.prev_user_id)
-        super(VCS_set_user_id_TestCase, self).tearDown()
-
-    def test_raises_error_in_unsupported_vcs(self):
-        """Should raise an error in a VCS that doesn't support it."""
-        if self.vcs_supports_set_user_id:
-            return
-        self.assertRaises(
-            SettingIDnotSupported,
-            self.vcs.set_user_id, "foo")
-
-    def test_updates_user_id_in_supporting_vcs(self):
-        """Should update the user ID in an VCS that supports it."""
-        if not self.vcs_supports_set_user_id:
-            return
-        user_id = self.vcs.get_user_id()
-        self.failUnlessEqual(
-            self.test_new_user_id, user_id,
-            "user id not set correctly (expected %s, got %s)"
-                % (self.test_new_user_id, user_id))
-
-
-def setup_vcs_revision_test_fixtures(testcase):
-    """Set up revision test fixtures for VCS test case."""
-    testcase.test_dirs = ['a', 'a/b', 'c']
-    for path in testcase.test_dirs:
-        testcase.vcs.mkdir(testcase.full_path(path))
-
-    testcase.test_files = ['a/text', 'a/b/text']
-
-    testcase.test_contents = {
-        'rev_1': "Lorem ipsum",
-        'uncommitted': "dolor sit amet",
-        }
-
-
-class VCS_mkdir_TestCase(VCSTestCase):
-    """Test cases for VCS.mkdir method."""
-
-    def setUp(self):
-        super(VCS_mkdir_TestCase, self).setUp()
-        setup_vcs_revision_test_fixtures(self)
-
-    def tearDown(self):
-        for path in reversed(sorted(self.test_dirs)):
-            self.vcs.recursive_remove(self.full_path(path))
-        super(VCS_mkdir_TestCase, self).tearDown()
-
-    def test_mkdir_creates_directory(self):
-        """Should create specified directory in filesystem."""
-        for path in self.test_dirs:
-            full_path = self.full_path(path)
-            self.failUnless(
-                os.path.exists(full_path),
-                "path %(full_path)s does not exist" % vars())
-
-
-class VCS_commit_TestCase(VCSTestCase):
-    """Test cases for VCS.commit method."""
-
-    def setUp(self):
-        super(VCS_commit_TestCase, self).setUp()
-        setup_vcs_revision_test_fixtures(self)
-
-    def tearDown(self):
-        for path in reversed(sorted(self.test_dirs)):
-            self.vcs.recursive_remove(self.full_path(path))
-        super(VCS_commit_TestCase, self).tearDown()
-
-    def test_file_contents_as_specified(self):
-        """Should set file contents as specified."""
-        test_contents = self.test_contents['rev_1']
-        for path in self.test_files:
-            full_path = self.full_path(path)
-            self.vcs.set_file_contents(full_path, test_contents)
-            current_contents = self.vcs.get_file_contents(full_path)
-            self.failUnlessEqual(test_contents, current_contents)
-
-    def test_file_contents_as_committed(self):
-        """Should have file contents as specified after commit."""
-        test_contents = self.test_contents['rev_1']
-        for path in self.test_files:
-            full_path = self.full_path(path)
-            self.vcs.set_file_contents(full_path, test_contents)
-            revision = self.vcs.commit("Initial file contents.")
-            current_contents = self.vcs.get_file_contents(full_path)
-            self.failUnlessEqual(test_contents, current_contents)
-
-    def test_file_contents_as_set_when_uncommitted(self):
-        """Should set file contents as specified after commit."""
-        if not self.vcs.versioned:
-            return
-        for path in self.test_files:
-            full_path = self.full_path(path)
-            self.vcs.set_file_contents(
-                full_path, self.test_contents['rev_1'])
-            revision = self.vcs.commit("Initial file contents.")
-            self.vcs.set_file_contents(
-                full_path, self.test_contents['uncommitted'])
-            current_contents = self.vcs.get_file_contents(full_path)
-            self.failUnlessEqual(
-                self.test_contents['uncommitted'], current_contents)
-
-    def test_revision_file_contents_as_committed(self):
-        """Should get file contents as committed to specified revision."""
-        if not self.vcs.versioned:
-            return
-        for path in self.test_files:
-            full_path = self.full_path(path)
-            self.vcs.set_file_contents(
-                full_path, self.test_contents['rev_1'])
-            revision = self.vcs.commit("Initial file contents.")
-            self.vcs.set_file_contents(
-                full_path, self.test_contents['uncommitted'])
-            committed_contents = self.vcs.get_file_contents(
-                full_path, revision)
-            self.failUnlessEqual(
-                self.test_contents['rev_1'], committed_contents)
-
-    def test_revision_id_as_committed(self):
-        """Check for compatibility between .commit() and .revision_id()"""
-        if not self.vcs.versioned:
-            self.failUnlessEqual(self.vcs.revision_id(5), None)
-            return
-        committed_revisions = []
-        for path in self.test_files:
-            full_path = self.full_path(path)
-            self.vcs.set_file_contents(
-                full_path, self.test_contents['rev_1'])
-            revision = self.vcs.commit("Initial %s contents." % path)
-            committed_revisions.append(revision)
-            self.vcs.set_file_contents(
-                full_path, self.test_contents['uncommitted'])
-            revision = self.vcs.commit("Altered %s contents." % path)
-            committed_revisions.append(revision)
-        for i,revision in enumerate(committed_revisions):
-            self.failUnlessEqual(self.vcs.revision_id(i), revision)
-            i += -len(committed_revisions) # check negative indices
-            self.failUnlessEqual(self.vcs.revision_id(i), revision)
-        i = len(committed_revisions)
-        self.failUnlessEqual(self.vcs.revision_id(i), None)
-        self.failUnlessEqual(self.vcs.revision_id(-i-1), None)
-
-    def test_revision_id_as_committed(self):
-        """Check revision id before first commit"""
-        if not self.vcs.versioned:
-            self.failUnlessEqual(self.vcs.revision_id(5), None)
-            return
-        committed_revisions = []
-        for path in self.test_files:
-            self.failUnlessEqual(self.vcs.revision_id(0), None)
-
-
-class VCS_duplicate_repo_TestCase(VCSTestCase):
-    """Test cases for VCS.duplicate_repo method."""
-
-    def setUp(self):
-        super(VCS_duplicate_repo_TestCase, self).setUp()
-        setup_vcs_revision_test_fixtures(self)
-
-    def tearDown(self):
-        self.vcs.remove_duplicate_repo()
-        for path in reversed(sorted(self.test_dirs)):
-            self.vcs.recursive_remove(self.full_path(path))
-        super(VCS_duplicate_repo_TestCase, self).tearDown()
-
-    def test_revision_file_contents_as_committed(self):
-        """Should match file contents as committed to specified revision."""
-        if not self.vcs.versioned:
-            return
-        for path in self.test_files:
-            full_path = self.full_path(path)
-            self.vcs.set_file_contents(
-                full_path, self.test_contents['rev_1'])
-            revision = self.vcs.commit("Commit current status")
-            self.vcs.set_file_contents(
-                full_path, self.test_contents['uncommitted'])
-            dup_repo_path = self.vcs.duplicate_repo(revision)
-            dup_file_path = os.path.join(dup_repo_path, path)
-            dup_file_contents = file(dup_file_path, 'rb').read()
+            if self.vcs_supports_uninitialized_user_id:
+                self.prev_user_id = self.vcs.get_user_id()
+            else:
+                self.prev_user_id = "Uninitialized identity <bogus@example.org>"
+
+            if self.vcs_supports_set_user_id:
+                self.test_new_user_id = "John Doe <jdoe@example.com>"
+                self.vcs.set_user_id(self.test_new_user_id)
+
+        def tearDown(self):
+            if self.vcs_supports_set_user_id:
+                self.vcs.set_user_id(self.prev_user_id)
+            super(VCS_set_user_id_TestCase, self).tearDown()
+
+        def test_raises_error_in_unsupported_vcs(self):
+            """Should raise an error in a VCS that doesn't support it."""
+            if self.vcs_supports_set_user_id:
+                return
+            self.assertRaises(
+                SettingIDnotSupported,
+                self.vcs.set_user_id, "foo")
+
+        def test_updates_user_id_in_supporting_vcs(self):
+            """Should update the user ID in an VCS that supports it."""
+            if not self.vcs_supports_set_user_id:
+                return
+            user_id = self.vcs.get_user_id()
             self.failUnlessEqual(
-                self.test_contents['rev_1'], dup_file_contents)
+                self.test_new_user_id, user_id,
+                "user id not set correctly (expected %s, got %s)"
+                    % (self.test_new_user_id, user_id))
+
+
+    def setup_vcs_revision_test_fixtures(testcase):
+        """Set up revision test fixtures for VCS test case."""
+        testcase.test_dirs = ['a', 'a/b', 'c']
+        for path in testcase.test_dirs:
+            testcase.vcs.mkdir(testcase.full_path(path))
+
+        testcase.test_files = ['a/text', 'a/b/text']
+
+        testcase.test_contents = {
+            'rev_1': "Lorem ipsum",
+            'uncommitted': "dolor sit amet",
+            }
+
+
+    class VCS_mkdir_TestCase(VCSTestCase):
+        """Test cases for VCS.mkdir method."""
+
+        def setUp(self):
+            super(VCS_mkdir_TestCase, self).setUp()
+            setup_vcs_revision_test_fixtures(self)
+
+        def tearDown(self):
+            for path in reversed(sorted(self.test_dirs)):
+                self.vcs.recursive_remove(self.full_path(path))
+            super(VCS_mkdir_TestCase, self).tearDown()
+
+        def test_mkdir_creates_directory(self):
+            """Should create specified directory in filesystem."""
+            for path in self.test_dirs:
+                full_path = self.full_path(path)
+                self.failUnless(
+                    os.path.exists(full_path),
+                    "path %(full_path)s does not exist" % vars())
+
+
+    class VCS_commit_TestCase(VCSTestCase):
+        """Test cases for VCS.commit method."""
+
+        def setUp(self):
+            super(VCS_commit_TestCase, self).setUp()
+            setup_vcs_revision_test_fixtures(self)
+
+        def tearDown(self):
+            for path in reversed(sorted(self.test_dirs)):
+                self.vcs.recursive_remove(self.full_path(path))
+            super(VCS_commit_TestCase, self).tearDown()
+
+        def test_file_contents_as_specified(self):
+            """Should set file contents as specified."""
+            test_contents = self.test_contents['rev_1']
+            for path in self.test_files:
+                full_path = self.full_path(path)
+                self.vcs.set_file_contents(full_path, test_contents)
+                current_contents = self.vcs.get_file_contents(full_path)
+                self.failUnlessEqual(test_contents, current_contents)
+
+        def test_file_contents_as_committed(self):
+            """Should have file contents as specified after commit."""
+            test_contents = self.test_contents['rev_1']
+            for path in self.test_files:
+                full_path = self.full_path(path)
+                self.vcs.set_file_contents(full_path, test_contents)
+                revision = self.vcs.commit("Initial file contents.")
+                current_contents = self.vcs.get_file_contents(full_path)
+                self.failUnlessEqual(test_contents, current_contents)
+
+        def test_file_contents_as_set_when_uncommitted(self):
+            """Should set file contents as specified after commit."""
+            if not self.vcs.versioned:
+                return
+            for path in self.test_files:
+                full_path = self.full_path(path)
+                self.vcs.set_file_contents(
+                    full_path, self.test_contents['rev_1'])
+                revision = self.vcs.commit("Initial file contents.")
+                self.vcs.set_file_contents(
+                    full_path, self.test_contents['uncommitted'])
+                current_contents = self.vcs.get_file_contents(full_path)
+                self.failUnlessEqual(
+                    self.test_contents['uncommitted'], current_contents)
+
+        def test_revision_file_contents_as_committed(self):
+            """Should get file contents as committed to specified revision."""
+            if not self.vcs.versioned:
+                return
+            for path in self.test_files:
+                full_path = self.full_path(path)
+                self.vcs.set_file_contents(
+                    full_path, self.test_contents['rev_1'])
+                revision = self.vcs.commit("Initial file contents.")
+                self.vcs.set_file_contents(
+                    full_path, self.test_contents['uncommitted'])
+                committed_contents = self.vcs.get_file_contents(
+                    full_path, revision)
+                self.failUnlessEqual(
+                    self.test_contents['rev_1'], committed_contents)
+
+        def test_revision_id_as_committed(self):
+            """Check for compatibility between .commit() and .revision_id()"""
+            if not self.vcs.versioned:
+                self.failUnlessEqual(self.vcs.revision_id(5), None)
+                return
+            committed_revisions = []
+            for path in self.test_files:
+                full_path = self.full_path(path)
+                self.vcs.set_file_contents(
+                    full_path, self.test_contents['rev_1'])
+                revision = self.vcs.commit("Initial %s contents." % path)
+                committed_revisions.append(revision)
+                self.vcs.set_file_contents(
+                    full_path, self.test_contents['uncommitted'])
+                revision = self.vcs.commit("Altered %s contents." % path)
+                committed_revisions.append(revision)
+            for i,revision in enumerate(committed_revisions):
+                self.failUnlessEqual(self.vcs.revision_id(i), revision)
+                i += -len(committed_revisions) # check negative indices
+                self.failUnlessEqual(self.vcs.revision_id(i), revision)
+            i = len(committed_revisions)
+            self.failUnlessEqual(self.vcs.revision_id(i), None)
+            self.failUnlessEqual(self.vcs.revision_id(-i-1), None)
+
+        def test_revision_id_as_committed(self):
+            """Check revision id before first commit"""
+            if not self.vcs.versioned:
+                self.failUnlessEqual(self.vcs.revision_id(5), None)
+                return
+            committed_revisions = []
+            for path in self.test_files:
+                self.failUnlessEqual(self.vcs.revision_id(0), None)
+
+
+    class VCS_duplicate_repo_TestCase(VCSTestCase):
+        """Test cases for VCS.duplicate_repo method."""
+
+        def setUp(self):
+            super(VCS_duplicate_repo_TestCase, self).setUp()
+            setup_vcs_revision_test_fixtures(self)
+
+        def tearDown(self):
             self.vcs.remove_duplicate_repo()
-
-
-def make_vcs_testcase_subclasses(vcs_class, namespace):
-    """Make VCSTestCase subclasses for vcs_class in the namespace."""
-    vcs_testcase_classes = [
-        c for c in (
-            ob for ob in globals().values() if isinstance(ob, type))
-        if issubclass(c, VCSTestCase)]
-
-    for base_class in vcs_testcase_classes:
-        testcase_class_name = vcs_class.__name__ + base_class.__name__
-        testcase_class_bases = (base_class,)
-        testcase_class_dict = dict(base_class.__dict__)
-        testcase_class_dict['Class'] = vcs_class
-        testcase_class = type(
-            testcase_class_name, testcase_class_bases, testcase_class_dict)
-        setattr(namespace, testcase_class_name, testcase_class)
-
-
-unitsuite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
-suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
+            for path in reversed(sorted(self.test_dirs)):
+                self.vcs.recursive_remove(self.full_path(path))
+            super(VCS_duplicate_repo_TestCase, self).tearDown()
+
+        def test_revision_file_contents_as_committed(self):
+            """Should match file contents as committed to specified revision.
+            """
+            if not self.vcs.versioned:
+                return
+            for path in self.test_files:
+                full_path = self.full_path(path)
+                self.vcs.set_file_contents(
+                    full_path, self.test_contents['rev_1'])
+                revision = self.vcs.commit("Commit current status")
+                self.vcs.set_file_contents(
+                    full_path, self.test_contents['uncommitted'])
+                dup_repo_path = self.vcs.duplicate_repo(revision)
+                dup_file_path = os.path.join(dup_repo_path, path)
+                dup_file_contents = file(dup_file_path, 'rb').read()
+                self.failUnlessEqual(
+                    self.test_contents['rev_1'], dup_file_contents)
+                self.vcs.remove_duplicate_repo()
+
+
+    def make_vcs_testcase_subclasses(vcs_class, namespace):
+        """Make VCSTestCase subclasses for vcs_class in the namespace."""
+        vcs_testcase_classes = [
+            c for c in (
+                ob for ob in globals().values() if isinstance(ob, type))
+            if issubclass(c, VCSTestCase)]
+
+        for base_class in vcs_testcase_classes:
+            testcase_class_name = vcs_class.__name__ + base_class.__name__
+            testcase_class_bases = (base_class,)
+            testcase_class_dict = dict(base_class.__dict__)
+            testcase_class_dict['Class'] = vcs_class
+            testcase_class = type(
+                testcase_class_name, testcase_class_bases, testcase_class_dict)
+            setattr(namespace, testcase_class_name, testcase_class)
+
+
+    unitsuite =unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
+    suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()])
diff --git a/test.py b/test.py
index 81674cf49ff473a241e4ed7f0781f8ebede8dacc..4f20808b326ad1c078e5dd28c1d49fd8524e9033 100644 (file)
--- a/test.py
+++ b/test.py
@@ -8,6 +8,8 @@ When called with module name arguments, only run the doctests from
 those modules.
 """
 
+import libbe
+libbe.TESTING = True
 from libbe import plugin, vcs
 import unittest
 import doctest