comparison tests/test_util.py @ 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 01e747937d35
children 9b5e528f67f8
comparison
equal deleted inserted replaced
90:4c419603d41b 91:7d10165cf3d9
1 import errno 1 import errno
2 import os 2 import os
3 import subprocess 3 import subprocess
4 import shutil 4 import shutil
5 import StringIO
5 import stat 6 import stat
6 import tempfile 7 import tempfile
7 import unittest 8 import unittest
8 import urllib 9 import urllib
9 10
16 import push_cmd 17 import push_cmd
17 18
18 FIXTURES = os.path.join(os.path.abspath(os.path.dirname(__file__)), 19 FIXTURES = os.path.join(os.path.abspath(os.path.dirname(__file__)),
19 'fixtures') 20 'fixtures')
20 21
21 def fileurl(path): 22 def fileurl(path):
22 path = os.path.abspath(path) 23 path = os.path.abspath(path)
23 drive, path = os.path.splitdrive(path) 24 drive, path = os.path.splitdrive(path)
24 path = urllib.pathname2url(path) 25 path = urllib.pathname2url(path)
25 if drive: 26 if drive:
26 drive = '/' + drive 27 drive = '/' + drive
61 raise 62 raise
62 if (s.st_mode & stat.S_IWRITE) == 0: 63 if (s.st_mode & stat.S_IWRITE) == 0:
63 os.chmod(f, s.st_mode | stat.S_IWRITE) 64 os.chmod(f, s.st_mode | stat.S_IWRITE)
64 shutil.rmtree(path) 65 shutil.rmtree(path)
65 66
67
68 class MockUI(object):
69 real_ui = ui.ui
70 _isatty = False
71 def __init__(self, parentui=None):
72 self.stream = StringIO.StringIO()
73 self.inner_ui = self.real_ui(parentui=parentui)
74
75 def status(self, *args):
76 self.stream.write(*args)
77
78 def warn(self, *args):
79 self.stream.write(*args)
80
81 def __getattr__(self, attr):
82 return getattr(self.inner_ui, attr)
83
84
66 class TestBase(unittest.TestCase): 85 class TestBase(unittest.TestCase):
67 def setUp(self): 86 def setUp(self):
68 self.oldwd = os.getcwd() 87 self.oldwd = os.getcwd()
69 self.tmpdir = tempfile.mkdtemp('svnwrap_test') 88 self.tmpdir = tempfile.mkdtemp('svnwrap_test')
70 self.repo_path = '%s/testrepo' % self.tmpdir 89 self.repo_path = '%s/testrepo' % self.tmpdir
71 self.wc_path = '%s/testrepo_wc' % self.tmpdir 90 self.wc_path = '%s/testrepo_wc' % self.tmpdir
91 self._real_ui = ui.ui
92 ui.ui = MockUI
72 93
73 def tearDown(self): 94 def tearDown(self):
74 rmtree(self.tmpdir) 95 rmtree(self.tmpdir)
75 os.chdir(self.oldwd) 96 os.chdir(self.oldwd)
76 97 ui.ui = self._real_ui
98
77 # define this as a property so that it reloads anytime we need it 99 # define this as a property so that it reloads anytime we need it
78 @property 100 @property
79 def repo(self): 101 def repo(self):
80 return hg.repository(ui.ui(), self.wc_path) 102 return hg.repository(ui.ui(), self.wc_path)
81 103
86 108
87 def svnls(self, path, rev='HEAD'): 109 def svnls(self, path, rev='HEAD'):
88 path = self.repo_path + '/' + path 110 path = self.repo_path + '/' + path
89 path = fileurl(path) 111 path = fileurl(path)
90 args = ['svn', 'ls', '-r', rev, '-R', path] 112 args = ['svn', 'ls', '-r', rev, '-R', path]
91 p = subprocess.Popen(args, 113 p = subprocess.Popen(args,
92 stdout=subprocess.PIPE, 114 stdout=subprocess.PIPE,
93 stderr=subprocess.PIPE) 115 stderr=subprocess.PIPE)
94 stdout, stderr = p.communicate() 116 stdout, stderr = p.communicate()
95 if p.returncode: 117 if p.returncode:
96 raise Exception('svn ls failed on %s: %r' % (path, stderr)) 118 raise Exception('svn ls failed on %s: %r' % (path, stderr))
97 entries = [e.strip('/') for e in stdout.splitlines()] 119 entries = [e.strip('/') for e in stdout.splitlines()]
102 """Commit changes to mercurial directory 124 """Commit changes to mercurial directory
103 125
104 'changes' is a sequence of tuples (source, dest, data). It can look 126 'changes' is a sequence of tuples (source, dest, data). It can look
105 like: 127 like:
106 - (source, source, data) to set source content to data 128 - (source, source, data) to set source content to data
107 - (source, dest, None) to set dest content to source one, and mark it as 129 - (source, dest, None) to set dest content to source one, and mark it as
108 copied from source. 130 copied from source.
109 - (source, dest, data) to set dest content to data, and mark it as copied 131 - (source, dest, data) to set dest content to data, and mark it as copied
110 from source. 132 from source.
111 - (source, None, None) to remove source. 133 - (source, None, None) to remove source.
112 """ 134 """
133 return context.memfilectx(path=dest, 155 return context.memfilectx(path=dest,
134 data=newdata, 156 data=newdata,
135 islink=False, 157 islink=False,
136 isexec=False, 158 isexec=False,
137 copied=copied) 159 copied=copied)
138 160
139 ctx = context.memctx(repo, 161 ctx = context.memctx(repo,
140 (parentctx.node(), node.nullid), 162 (parentctx.node(), node.nullid),
141 'automated test', 163 'automated test',
142 changed + removed, 164 changed + removed,
143 filectxfn, 165 filectxfn,