1 # Copyright (C) 2006 Jelmer Vernooij <jelmer@samba.org>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 from bzrlib import osutils
21 from bzrlib.bzrdir import BzrDir
22 from bzrlib.tests import TestCaseInTempDir
24 import svn.ra, svn.repos, svn.wc
26 class TestCaseWithSubversionRepository(TestCaseInTempDir):
27 """A test case that provides the ability to build Subversion
31 super(TestCaseWithSubversionRepository, self).setUp()
32 self.client_ctx = svn.client.create_context()
34 def make_repository(self, relpath):
35 """Create a repository.
37 :return: Handle to the repository.
39 abspath = os.path.join(self.test_dir, relpath)
40 repos_url = "file://%s" % abspath
42 repos = svn.repos.create(abspath, '', '', None, None)
44 revprop_hook = os.path.join(abspath, "hooks", "pre-revprop-change")
46 open(revprop_hook, 'w').write("#!/bin/sh")
48 os.chmod(revprop_hook, os.stat(revprop_hook).st_mode | 0111)
52 def make_remote_bzrdir(self, relpath):
53 """Create a repository."""
55 repos_url = self.make_repository(relpath)
57 return BzrDir.open("svn+%s" % repos_url)
59 def open_local_bzrdir(self, repos_url, relpath):
60 """Open a local BzrDir."""
62 self.make_checkout(repos_url, relpath)
64 return BzrDir.open(relpath)
66 def make_local_bzrdir(self, repos_path, relpath):
67 """Create a repository and checkout."""
69 repos_url = self.make_repository(repos_path)
71 return self.open_local_bzrdir(repos_url, relpath)
73 def make_checkout(self, repos_url, relpath):
74 rev = svn.core.svn_opt_revision_t()
75 rev.kind = svn.core.svn_opt_revision_head
77 svn.client.checkout2(repos_url, relpath,
78 rev, rev, True, False, self.client_ctx)
80 def client_set_revprops(self, url, revnum, name, value):
81 rev = svn.core.svn_opt_revision_t()
82 rev.kind = svn.core.svn_opt_revision_number
84 svn.client.revprop_set(name, value, url, rev, True,
87 def client_commit(self, dir, message=None, recursive=True):
88 """Commit current changes in specified working copy.
90 :param relpath: List of paths to commit.
92 olddir = os.path.abspath('.')
94 info = svn.client.commit3(["."], recursive, False, self.client_ctx)
96 return (info.revision, info.date, info.author)
98 def client_add(self, relpath, recursive=True):
99 """Add specified files to working copy.
101 :param relpath: Path to the files to add.
103 svn.client.add3(relpath, recursive, False, False, self.client_ctx)
105 def client_delete(self, relpaths):
106 """Remove specified files from working copy.
108 :param relpath: Path to the files to remove.
110 svn.client.delete2([relpaths], True, self.client_ctx)
112 def client_copy(self, oldpath, newpath):
113 """Copy file in working copy.
115 :param oldpath: Relative path to original file.
116 :param newpath: Relative path to new file.
118 rev = svn.core.svn_opt_revision_t()
119 rev.kind = svn.core.svn_opt_revision_head
120 svn.client.copy3(oldpath, rev, newpath, self.client_ctx)
122 def build_tree(self, files):
123 """Create a directory tree.
125 :param files: Dictionary with filenames as keys, contents as
126 values. None as value indicates a directory.
133 os.makedirs(os.path.dirname(f))
136 open(f, 'w').write(files[f])
138 def make_client_and_bzrdir(self, repospath, clientpath):
139 repos_url = self.make_client(repospath, clientpath)
141 return BzrDir.open("svn+%s" % repos_url)
143 def make_client(self, repospath, clientpath):
144 """Create a repository and a checkout. Return the checkout.
146 :param relpath: Optional relpath to check out if not the full
148 :return: Repository URL.
150 repos_url = self.make_repository(repospath)
151 self.make_checkout(repos_url, clientpath)
154 def make_ra(self, relpath):
155 """Create a repository and a ra connection to it.
157 :param relpath: Path to create repository at.
158 :return: The ra connection.
161 repos_url = self.make_repository(relpath)
163 return svn.ra.open2(repos_url, svn.ra.callbacks2_t(), None, None)
165 def dumpfile(self, repos):
166 """Create a dumpfile for the specified repository.
168 :return: File name of the dumpfile.
170 raise NotImplementedError(self.dumpfile)
172 def open_fs(self, relpath):
177 repos = svn.repos.open(relpath)
179 return svn.repos.fs(repos)
183 from unittest import TestSuite, TestLoader
185 from bzrlib.tests import TestUtil
187 loader = TestUtil.TestLoader()
199 suite.addTest(loader.loadTestsFromModuleNames(["%s.%s" % (__name__, i) for i in testmod_names]))