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_prop(self, path, name, value):
81 svn.client.propset2(name, value, path, False, True, self.client_ctx)
83 def client_set_revprops(self, url, revnum, name, value):
84 rev = svn.core.svn_opt_revision_t()
85 rev.kind = svn.core.svn_opt_revision_number
86 rev.value.number = revnum
87 svn.client.revprop_set(name, value, url, rev, True, self.client_ctx)
89 def client_get_revprop(self, url, revnum, name):
90 rev = svn.core.svn_opt_revision_t()
91 rev.kind = svn.core.svn_opt_revision_number
92 rev.value.number = revnum
93 return svn.client.revprop_get(name, url, rev, self.client_ctx)[0]
95 def client_commit(self, dir, message=None, recursive=True):
96 """Commit current changes in specified working copy.
98 :param relpath: List of paths to commit.
100 olddir = os.path.abspath('.')
102 info = svn.client.commit3(["."], recursive, False, self.client_ctx)
104 return (info.revision, info.date, info.author)
106 def client_add(self, relpath, recursive=True):
107 """Add specified files to working copy.
109 :param relpath: Path to the files to add.
111 svn.client.add3(relpath, recursive, False, False, self.client_ctx)
113 def client_delete(self, relpaths):
114 """Remove specified files from working copy.
116 :param relpath: Path to the files to remove.
118 svn.client.delete2([relpaths], True, self.client_ctx)
120 def client_copy(self, oldpath, newpath):
121 """Copy file in working copy.
123 :param oldpath: Relative path to original file.
124 :param newpath: Relative path to new file.
126 rev = svn.core.svn_opt_revision_t()
127 rev.kind = svn.core.svn_opt_revision_head
128 svn.client.copy3(oldpath, rev, newpath, self.client_ctx)
130 def build_tree(self, files):
131 """Create a directory tree.
133 :param files: Dictionary with filenames as keys, contents as
134 values. None as value indicates a directory.
144 os.makedirs(os.path.dirname(f))
147 open(f, 'w').write(files[f])
149 def make_client_and_bzrdir(self, repospath, clientpath):
150 repos_url = self.make_client(repospath, clientpath)
152 return BzrDir.open("svn+%s" % repos_url)
154 def make_client(self, repospath, clientpath):
155 """Create a repository and a checkout. Return the checkout.
157 :param relpath: Optional relpath to check out if not the full
159 :return: Repository URL.
161 repos_url = self.make_repository(repospath)
162 self.make_checkout(repos_url, clientpath)
165 def make_ra(self, relpath):
166 """Create a repository and a ra connection to it.
168 :param relpath: Path to create repository at.
169 :return: The ra connection.
172 repos_url = self.make_repository(relpath)
174 return svn.ra.open2(repos_url, svn.ra.callbacks2_t(), None, None)
176 def dumpfile(self, repos):
177 """Create a dumpfile for the specified repository.
179 :return: File name of the dumpfile.
181 raise NotImplementedError(self.dumpfile)
183 def open_fs(self, relpath):
188 repos = svn.repos.open(relpath)
190 return svn.repos.fs(repos)
194 from unittest import TestSuite, TestLoader
196 from bzrlib.tests import TestUtil
198 loader = TestUtil.TestLoader()
211 suite.addTest(loader.loadTestsFromModuleNames(["%s.%s" % (__name__, i) for i in testmod_names]))