changeset 91:7d10165cf3d9

tests: Mock the mercurial.ui.ui class like we really should to capture output. Has the nice side effect that we can use nose 0.11's multiprocess plugin.
author Augie Fackler <durin42@gmail.com>
date Fri, 14 Nov 2008 16:52:30 -0600
parents 4c419603d41b
children 7486c6f6cccc
files tests/test_fetch_command.py tests/test_fetch_renames.py tests/test_tags.py tests/test_util.py
diffstat 4 files changed, 48 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/tests/test_fetch_command.py
+++ b/tests/test_fetch_command.py
@@ -10,17 +10,7 @@ import fetch_command
 import test_util
 
 
-class TestBasicRepoLayout(unittest.TestCase):
-    def setUp(self):
-        self.oldwd = os.getcwd()
-        self.tmpdir = tempfile.mkdtemp('svnwrap_test')
-        self.repo_path = '%s/testrepo' % self.tmpdir
-        self.wc_path = '%s/testrepo_wc' % self.tmpdir
-
-    def tearDown(self):
-        test_util.rmtree(self.tmpdir)
-        os.chdir(self.oldwd)
-
+class TestBasicRepoLayout(test_util.TestBase):
     def _load_fixture_and_fetch(self, fixture_name):
         return test_util.load_fixture_and_fetch(fixture_name, self.repo_path,
                                                 self.wc_path)
@@ -120,17 +110,7 @@ class TestBasicRepoLayout(unittest.TestC
         #'1316ef606dda89354ee8c4df725e6264177b5129')
 
 
-class TestStupidPull(unittest.TestCase):
-    def setUp(self):
-        self.oldwd = os.getcwd()
-        self.tmpdir = tempfile.mkdtemp('svnwrap_test')
-        self.repo_path = '%s/testrepo' % self.tmpdir
-        self.wc_path = '%s/testrepo_wc' % self.tmpdir
-
-    def tearDown(self):
-        test_util.rmtree(self.tmpdir)
-        os.chdir(self.oldwd)
-
+class TestStupidPull(test_util.TestBase):
     def test_stupid(self):
         repo = test_util.load_fixture_and_fetch('two_heads.svndump',
                                                 self.repo_path,
--- a/tests/test_fetch_renames.py
+++ b/tests/test_fetch_renames.py
@@ -12,17 +12,7 @@ import fetch_command
 import test_util
 
 
-class TestFetchRenames(unittest.TestCase):
-    def setUp(self):
-        self.oldwd = os.getcwd()
-        self.tmpdir = tempfile.mkdtemp('svnwrap_test')
-        self.repo_path = '%s/testrepo' % self.tmpdir
-        self.wc_path = '%s/testrepo_wc' % self.tmpdir
-
-    def tearDown(self):
-        test_util.rmtree(self.tmpdir)
-        os.chdir(self.oldwd)
-
+class TestFetchRenames(test_util.TestBase):
     def _load_fixture_and_fetch(self, fixture_name, stupid):
         return test_util.load_fixture_and_fetch(fixture_name, self.repo_path,
                                                 self.wc_path, stupid=stupid)
@@ -43,7 +33,7 @@ class TestFetchRenames(unittest.TestCase
         # Map revnum to mappings of dest name to (source name, dest content)
         copies = {
             4: {
-                'a1': ('a', 'a\n'), 
+                'a1': ('a', 'a\n'),
                 'a2': ('a', 'a\n'),
                 'b1': ('b', 'b\nc\n'),
                 'da1/daf': ('da/daf', 'c\n'),
--- a/tests/test_tags.py
+++ b/tests/test_tags.py
@@ -10,19 +10,9 @@ import test_util
 
 import svncommand
 
-class TestTags(unittest.TestCase):
-    def setUp(self):
-        self.oldwd = os.getcwd()
-        self.tmpdir = tempfile.mkdtemp('svnwrap_test')
-        self.repo_path = '%s/testrepo' % self.tmpdir
-        self.wc_path = '%s/testrepo_wc' % self.tmpdir
-
-    def tearDown(self):
-        test_util.rmtree(self.tmpdir)
-        os.chdir(self.oldwd)
-        
+class TestTags(test_util.TestBase):
     def _load_fixture_and_fetch(self, fixture_name, stupid=False):
-        return test_util.load_fixture_and_fetch(fixture_name, self.repo_path, 
+        return test_util.load_fixture_and_fetch(fixture_name, self.repo_path,
                                                 self.wc_path, stupid=stupid)
 
     def _test_tag_revision_info(self, repo):
@@ -31,33 +21,33 @@ class TestTags(unittest.TestCase):
         self.assertEqual(node.hex(repo['tip'].node()),
                          'bf3767835b3b32ecc775a298c2fa27134dd91c11')
         self.assertEqual(repo['tip'], repo[1])
-    
+
     def test_tags(self, stupid=False):
-        repo = self._load_fixture_and_fetch('basic_tag_tests.svndump', 
+        repo = self._load_fixture_and_fetch('basic_tag_tests.svndump',
                                             stupid=stupid)
         self._test_tag_revision_info(repo)
         svncommand.generate_hg_tags(ui.ui(), self.wc_path)
         repo = hg.repository(ui.ui(), self.wc_path)
         self.assertEqual(repo['tip'].node(), repo['tag/tag_r3'].node())
         self.assertEqual(repo['tip'].node(), repo['tag/copied_tag'].node())
-    
+
     def test_tags_stupid(self):
         self.test_tags(stupid=True)
 
     def test_remove_tag(self, stupid=False):
-        repo = self._load_fixture_and_fetch('remove_tag_test.svndump', 
+        repo = self._load_fixture_and_fetch('remove_tag_test.svndump',
                                             stupid=stupid)
         self._test_tag_revision_info(repo)
         svncommand.generate_hg_tags(ui.ui(), self.wc_path)
         repo = hg.repository(ui.ui(), self.wc_path)
         self.assertEqual(repo['tip'].node(), repo['tag/tag_r3'].node())
         self.assert_('tag/copied_tag' not in repo.tags())
-    
+
     def test_remove_tag_stupid(self):
         self.test_remove_tag(stupid=True)
 
     def test_rename_tag(self, stupid=False):
-        repo = self._load_fixture_and_fetch('rename_tag_test.svndump', 
+        repo = self._load_fixture_and_fetch('rename_tag_test.svndump',
                                             stupid=stupid)
         self._test_tag_revision_info(repo)
         svncommand.generate_hg_tags(ui.ui(), self.wc_path)
@@ -65,25 +55,25 @@ class TestTags(unittest.TestCase):
         self.assertEqual(repo['tip'].node(), repo['tag/tag_r3'].node())
         self.assertEqual(repo['tip'].node(), repo['tag/other_tag_r3'].node())
         self.assert_('tag/copied_tag' not in repo.tags())
-    
+
     def test_rename_tag_stupid(self):
         self.test_rename_tag(stupid=True)
 
     def test_branch_from_tag(self, stupid=False):
-        repo = self._load_fixture_and_fetch('branch_from_tag.svndump', 
+        repo = self._load_fixture_and_fetch('branch_from_tag.svndump',
                                             stupid=stupid)
         svncommand.generate_hg_tags(ui.ui(), self.wc_path)
         repo = hg.repository(ui.ui(), self.wc_path)
         self.assertEqual(repo['tip'].node(), repo['branch_from_tag'].node())
         self.assertEqual(repo[1].node(), repo['tag/tag_r3'].node())
-        self.assertEqual(repo['branch_from_tag'].parents()[0].node(), 
+        self.assertEqual(repo['branch_from_tag'].parents()[0].node(),
                          repo['tag/copied_tag'].node())
-    
+
     def test_branch_from_tag_stupid(self):
         self.test_branch_from_tag(stupid=True)
-    
+
     def test_tag_by_renaming_branch(self, stupid=False):
-        repo = self._load_fixture_and_fetch('tag_by_rename_branch.svndump', 
+        repo = self._load_fixture_and_fetch('tag_by_rename_branch.svndump',
                                             stupid=stupid)
         svncommand.generate_hg_tags(ui.ui(), self.wc_path)
         repo = hg.repository(ui.ui(), self.wc_path)
@@ -91,9 +81,9 @@ class TestTags(unittest.TestCase):
                          '1b941f92acc343939274bd8bbf25984fa9706bb9')
         self.assertEqual(node.hex(repo['tag/dummy'].node()),
                          '68f5f7d82b00a2efe3aca28b615ebab98235d55f')
-    
+
     def test_tag_by_renaming_branch_stupid(self):
-        self.test_tag_by_renaming_branch(stupid=True)    
+        self.test_tag_by_renaming_branch(stupid=True)
 
 def suite():
     return unittest.TestLoader().loadTestsFromTestCase(TestTags)
--- a/tests/test_util.py
+++ b/tests/test_util.py
@@ -2,6 +2,7 @@ import errno
 import os
 import subprocess
 import shutil
+import StringIO
 import stat
 import tempfile
 import unittest
@@ -18,7 +19,7 @@ import push_cmd
 FIXTURES = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                         'fixtures')
 
-def fileurl(path):    
+def fileurl(path):
     path = os.path.abspath(path)
     drive, path = os.path.splitdrive(path)
     path = urllib.pathname2url(path)
@@ -63,17 +64,38 @@ def rmtree(path):
                 os.chmod(f, s.st_mode | stat.S_IWRITE)
     shutil.rmtree(path)
 
+
+class MockUI(object):
+    real_ui = ui.ui
+    _isatty = False
+    def __init__(self, parentui=None):
+        self.stream = StringIO.StringIO()
+        self.inner_ui = self.real_ui(parentui=parentui)
+
+    def status(self, *args):
+        self.stream.write(*args)
+
+    def warn(self, *args):
+        self.stream.write(*args)
+
+    def __getattr__(self, attr):
+        return getattr(self.inner_ui, attr)
+
+
 class TestBase(unittest.TestCase):
     def setUp(self):
         self.oldwd = os.getcwd()
         self.tmpdir = tempfile.mkdtemp('svnwrap_test')
         self.repo_path = '%s/testrepo' % self.tmpdir
         self.wc_path = '%s/testrepo_wc' % self.tmpdir
+        self._real_ui = ui.ui
+        ui.ui = MockUI
 
     def tearDown(self):
         rmtree(self.tmpdir)
         os.chdir(self.oldwd)
-        
+        ui.ui = self._real_ui
+
     # define this as a property so that it reloads anytime we need it
     @property
     def repo(self):
@@ -88,8 +110,8 @@ class TestBase(unittest.TestCase):
         path = self.repo_path + '/' + path
         path = fileurl(path)
         args = ['svn', 'ls', '-r', rev, '-R', path]
-        p = subprocess.Popen(args, 
-                             stdout=subprocess.PIPE, 
+        p = subprocess.Popen(args,
+                             stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE)
         stdout, stderr = p.communicate()
         if p.returncode:
@@ -104,7 +126,7 @@ class TestBase(unittest.TestCase):
         'changes' is a sequence of tuples (source, dest, data). It can look
         like:
         - (source, source, data) to set source content to data
-        - (source, dest, None) to set dest content to source one, and mark it as 
+        - (source, dest, None) to set dest content to source one, and mark it as
         copied from source.
         - (source, dest, data) to set dest content to data, and mark it as copied
         from source.
@@ -135,7 +157,7 @@ class TestBase(unittest.TestCase):
                                       islink=False,
                                       isexec=False,
                                       copied=copied)
-        
+
         ctx = context.memctx(repo,
                              (parentctx.node(), node.nullid),
                              'automated test',