2008-11-10 03:08:35 +03:00
|
|
|
import errno
|
2009-05-22 17:12:31 +04:00
|
|
|
import imp
|
2008-10-08 03:42:43 +04:00
|
|
|
import os
|
2008-11-10 03:08:35 +03:00
|
|
|
import subprocess
|
|
|
|
import shutil
|
2008-11-15 01:52:30 +03:00
|
|
|
import StringIO
|
2008-11-10 03:08:35 +03:00
|
|
|
import stat
|
2008-11-15 01:18:24 +03:00
|
|
|
import tempfile
|
|
|
|
import unittest
|
2008-11-10 03:08:35 +03:00
|
|
|
import urllib
|
2008-10-08 03:42:43 +04:00
|
|
|
|
2009-05-22 17:12:31 +04:00
|
|
|
import __init__
|
|
|
|
|
2008-11-15 01:18:24 +03:00
|
|
|
from mercurial import context
|
2009-05-15 21:18:43 +04:00
|
|
|
from mercurial import commands
|
2008-10-09 05:09:28 +04:00
|
|
|
from mercurial import hg
|
2008-11-15 01:18:24 +03:00
|
|
|
from mercurial import node
|
|
|
|
from mercurial import ui
|
2008-10-09 05:09:28 +04:00
|
|
|
|
2009-05-14 06:39:39 +04:00
|
|
|
from hgsubversion import util
|
2009-05-12 22:12:32 +04:00
|
|
|
|
2009-07-29 18:26:29 +04:00
|
|
|
import sys
|
|
|
|
# Documentation for Subprocess.Popen() says:
|
|
|
|
# "Note that on Windows, you cannot set close_fds to true and
|
|
|
|
# also redirect the standard handles by setting stdin, stdout or
|
|
|
|
# stderr."
|
|
|
|
canCloseFds='win32' not in sys.platform
|
|
|
|
|
2009-01-22 05:27:51 +03:00
|
|
|
# Fixtures that need to be pulled at a subdirectory of the repo path
|
|
|
|
subdir = {'truncatedhistory.svndump': '/project2',
|
|
|
|
'fetch_missing_files_subdir.svndump': '/foo',
|
2009-04-22 00:25:08 +04:00
|
|
|
'empty_dir_in_trunk_not_repo_root.svndump': '/project',
|
2009-05-23 08:37:33 +04:00
|
|
|
'project_root_not_repo_root.svndump': '/dummyproj',
|
2009-01-22 05:27:51 +03:00
|
|
|
}
|
|
|
|
|
2008-10-08 03:42:43 +04:00
|
|
|
FIXTURES = os.path.join(os.path.abspath(os.path.dirname(__file__)),
|
|
|
|
'fixtures')
|
|
|
|
|
2008-11-15 01:52:30 +03:00
|
|
|
def fileurl(path):
|
2008-11-10 03:08:35 +03:00
|
|
|
path = os.path.abspath(path)
|
|
|
|
drive, path = os.path.splitdrive(path)
|
|
|
|
path = urllib.pathname2url(path)
|
|
|
|
if drive:
|
|
|
|
drive = '/' + drive
|
2009-05-20 20:38:01 +04:00
|
|
|
url = 'file://%s%s' % (drive, path)
|
2008-11-10 03:08:35 +03:00
|
|
|
return url
|
|
|
|
|
2008-10-08 03:42:43 +04:00
|
|
|
def load_svndump_fixture(path, fixture_name):
|
|
|
|
'''Loads an svnadmin dump into a fresh repo at path, which should not
|
|
|
|
already exist.
|
|
|
|
'''
|
2009-05-15 21:18:43 +04:00
|
|
|
if os.path.exists(path): rmtree(path)
|
|
|
|
subprocess.call(['svnadmin', 'create', path,],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
2008-10-08 03:42:43 +04:00
|
|
|
inp = open(os.path.join(FIXTURES, fixture_name))
|
2009-05-15 21:18:43 +04:00
|
|
|
proc = subprocess.Popen(['svnadmin', 'load', path,], stdin=inp,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
2008-11-10 03:08:35 +03:00
|
|
|
proc.communicate()
|
2008-10-09 05:09:28 +04:00
|
|
|
|
2009-04-25 05:36:38 +04:00
|
|
|
def load_fixture_and_fetch(fixture_name, repo_path, wc_path, stupid=False, subdir='', noupdate=True):
|
2008-10-09 05:09:28 +04:00
|
|
|
load_svndump_fixture(repo_path, fixture_name)
|
2008-11-26 01:21:57 +03:00
|
|
|
if subdir:
|
|
|
|
repo_path += '/' + subdir
|
2009-05-14 06:39:39 +04:00
|
|
|
|
2009-05-15 21:18:43 +04:00
|
|
|
_ui = ui.ui()
|
|
|
|
_ui.setconfig('hgsubversion', 'stupid', str(stupid))
|
|
|
|
commands.clone(_ui, fileurl(repo_path), wc_path, noupdate=noupdate)
|
|
|
|
_ui = ui.ui()
|
|
|
|
_ui.setconfig('hgsubversion', 'stupid', str(stupid))
|
|
|
|
return hg.repository(_ui, wc_path)
|
2008-11-10 03:08:35 +03:00
|
|
|
|
|
|
|
def rmtree(path):
|
|
|
|
# Read-only files cannot be removed under Windows
|
|
|
|
for root, dirs, files in os.walk(path):
|
|
|
|
for f in files:
|
|
|
|
f = os.path.join(root, f)
|
|
|
|
try:
|
|
|
|
s = os.stat(f)
|
|
|
|
except OSError, e:
|
|
|
|
if e.errno == errno.ENOENT:
|
|
|
|
continue
|
|
|
|
raise
|
|
|
|
if (s.st_mode & stat.S_IWRITE) == 0:
|
|
|
|
os.chmod(f, s.st_mode | stat.S_IWRITE)
|
|
|
|
shutil.rmtree(path)
|
2008-11-15 01:18:24 +03:00
|
|
|
|
|
|
|
class TestBase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.oldwd = os.getcwd()
|
2008-12-21 04:13:46 +03:00
|
|
|
self.tmpdir = tempfile.mkdtemp(
|
|
|
|
'svnwrap_test', dir=os.environ.get('HGSUBVERSION_TEST_TEMP', None))
|
2009-03-31 19:50:21 +04:00
|
|
|
self.hgrc = os.path.join(self.tmpdir, '.hgrc')
|
|
|
|
os.environ['HGRCPATH'] = self.hgrc
|
|
|
|
rc = open(self.hgrc, 'w')
|
2009-05-12 22:14:15 +04:00
|
|
|
for l in '[extensions]', 'hgsubversion=':
|
|
|
|
print >> rc, l
|
2008-12-21 04:13:46 +03:00
|
|
|
|
2008-11-15 01:18:24 +03:00
|
|
|
self.repo_path = '%s/testrepo' % self.tmpdir
|
|
|
|
self.wc_path = '%s/testrepo_wc' % self.tmpdir
|
2009-05-15 21:18:43 +04:00
|
|
|
|
|
|
|
# Previously, we had a MockUI class that wrapped ui, and giving access
|
|
|
|
# to the stream. The ui.pushbuffer() and ui.popbuffer() can be used
|
|
|
|
# instead. Using the regular UI class, with all stderr redirected to
|
|
|
|
# stdout ensures that the test setup is much more similar to usage
|
|
|
|
# setups.
|
2009-05-12 22:14:15 +04:00
|
|
|
self.patch = (ui.ui.write_err, ui.ui.write)
|
|
|
|
setattr(ui.ui, self.patch[0].func_name, self.patch[1])
|
2008-11-15 01:18:24 +03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
rmtree(self.tmpdir)
|
|
|
|
os.chdir(self.oldwd)
|
2009-05-12 22:14:15 +04:00
|
|
|
setattr(ui.ui, self.patch[0].func_name, self.patch[0])
|
2008-11-15 01:52:30 +03:00
|
|
|
|
2008-12-12 04:49:36 +03:00
|
|
|
def _load_fixture_and_fetch(self, fixture_name, subdir='', stupid=False):
|
|
|
|
return load_fixture_and_fetch(fixture_name, self.repo_path,
|
|
|
|
self.wc_path, subdir=subdir,
|
|
|
|
stupid=stupid)
|
|
|
|
|
2008-11-15 01:18:24 +03:00
|
|
|
# define this as a property so that it reloads anytime we need it
|
|
|
|
@property
|
|
|
|
def repo(self):
|
|
|
|
return hg.repository(ui.ui(), self.wc_path)
|
|
|
|
|
2009-05-28 00:51:04 +04:00
|
|
|
def pushrevisions(self, stupid=False, expected_extra_back=0):
|
2008-12-31 05:33:57 +03:00
|
|
|
before = len(self.repo)
|
2009-05-15 21:18:43 +04:00
|
|
|
self.repo.ui.setconfig('hgsubversion', 'stupid', str(stupid))
|
|
|
|
commands.push(self.repo.ui, self.repo)
|
2008-12-31 05:33:57 +03:00
|
|
|
after = len(self.repo)
|
2009-05-28 00:51:04 +04:00
|
|
|
self.assertEqual(expected_extra_back, after - before)
|
2008-11-15 01:18:24 +03:00
|
|
|
|
|
|
|
def svnls(self, path, rev='HEAD'):
|
|
|
|
path = self.repo_path + '/' + path
|
2009-05-12 22:12:32 +04:00
|
|
|
path = util.normalize_url(fileurl(path))
|
2008-11-15 01:18:24 +03:00
|
|
|
args = ['svn', 'ls', '-r', rev, '-R', path]
|
2008-11-15 01:52:30 +03:00
|
|
|
p = subprocess.Popen(args,
|
|
|
|
stdout=subprocess.PIPE,
|
2009-05-15 21:18:43 +04:00
|
|
|
stderr=subprocess.STDOUT)
|
2008-11-15 01:18:24 +03:00
|
|
|
stdout, stderr = p.communicate()
|
|
|
|
if p.returncode:
|
|
|
|
raise Exception('svn ls failed on %s: %r' % (path, stderr))
|
|
|
|
entries = [e.strip('/') for e in stdout.splitlines()]
|
|
|
|
entries.sort()
|
|
|
|
return entries
|
2008-11-15 01:18:24 +03:00
|
|
|
|
2009-05-28 00:51:04 +04:00
|
|
|
def commitchanges(self, changes, parent='tip', message='automated test'):
|
2008-11-15 01:18:24 +03:00
|
|
|
"""Commit changes to mercurial directory
|
|
|
|
|
|
|
|
'changes' is a sequence of tuples (source, dest, data). It can look
|
|
|
|
like:
|
|
|
|
- (source, source, data) to set source content to data
|
2008-11-15 01:52:30 +03:00
|
|
|
- (source, dest, None) to set dest content to source one, and mark it as
|
2008-11-15 01:18:24 +03:00
|
|
|
copied from source.
|
|
|
|
- (source, dest, data) to set dest content to data, and mark it as copied
|
|
|
|
from source.
|
|
|
|
- (source, None, None) to remove source.
|
|
|
|
"""
|
|
|
|
repo = self.repo
|
2009-04-25 05:31:17 +04:00
|
|
|
parentctx = repo[parent]
|
2008-11-15 01:18:24 +03:00
|
|
|
|
|
|
|
changed, removed = [], []
|
|
|
|
for source, dest, newdata in changes:
|
|
|
|
if dest is None:
|
|
|
|
removed.append(source)
|
|
|
|
else:
|
|
|
|
changed.append(dest)
|
|
|
|
|
|
|
|
def filectxfn(repo, memctx, path):
|
|
|
|
if path in removed:
|
|
|
|
raise IOError()
|
|
|
|
entry = [e for e in changes if path == e[1]][0]
|
|
|
|
source, dest, newdata = entry
|
|
|
|
if newdata is None:
|
|
|
|
newdata = parentctx[source].data()
|
|
|
|
copied = None
|
|
|
|
if source != dest:
|
|
|
|
copied = source
|
|
|
|
return context.memfilectx(path=dest,
|
|
|
|
data=newdata,
|
|
|
|
islink=False,
|
|
|
|
isexec=False,
|
|
|
|
copied=copied)
|
2008-11-15 01:52:30 +03:00
|
|
|
|
2008-11-15 01:18:24 +03:00
|
|
|
ctx = context.memctx(repo,
|
|
|
|
(parentctx.node(), node.nullid),
|
2009-05-28 00:51:04 +04:00
|
|
|
message,
|
2008-11-15 01:18:24 +03:00
|
|
|
changed + removed,
|
|
|
|
filectxfn,
|
|
|
|
'an_author',
|
|
|
|
'2008-10-07 20:59:48 -0500')
|
|
|
|
nodeid = repo.commitctx(ctx)
|
|
|
|
repo = self.repo
|
2009-04-25 05:31:17 +04:00
|
|
|
hg.clean(repo, nodeid)
|
2008-11-15 01:18:24 +03:00
|
|
|
return nodeid
|
2008-11-21 07:41:16 +03:00
|
|
|
|
|
|
|
def assertchanges(self, changes, ctx):
|
|
|
|
"""Assert that all 'changes' (as in defined in commitchanged())
|
|
|
|
went into ctx.
|
|
|
|
"""
|
|
|
|
for source, dest, data in changes:
|
|
|
|
if dest is None:
|
|
|
|
self.assertTrue(source not in ctx)
|
|
|
|
continue
|
|
|
|
self.assertTrue(dest in ctx)
|
|
|
|
if data is None:
|
|
|
|
data = ctx.parents()[0][source].data()
|
|
|
|
self.assertEqual(ctx[dest].data(), data)
|
|
|
|
if dest != source:
|
|
|
|
copy = ctx[dest].renamed()
|
|
|
|
self.assertEqual(copy[0], source)
|