Bunch of random fixes, tests.
[jelmer/subvertpy.git] / tests / __init__.py
1 # Copyright (C) 2006 Jelmer Vernooij <jelmer@samba.org>
2
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.
7
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.
12
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
16
17 import svn.repos
18 import os
19 import bzrlib
20 from bzrlib import osutils
21 from bzrlib.bzrdir import BzrDir
22 from bzrlib.tests import TestCaseInTempDir
23
24 import svn.ra, svn.repos, svn.wc
25
26 class TestCaseWithSubversionRepository(TestCaseInTempDir):
27     """A test case that provides the ability to build Subversion 
28     repositories."""
29
30     def setUp(self):
31         super(TestCaseWithSubversionRepository, self).setUp()
32         self.client_ctx = svn.client.create_context()
33
34     def make_repository(self, relpath):
35         """Create a repository.
36
37         :return: Handle to the repository.
38         """
39         abspath = os.path.join(self.test_dir, relpath)
40         repos_url = "file://%s" % abspath
41
42         repos = svn.repos.create(abspath, '', '', None, None)
43
44         revprop_hook = os.path.join(abspath, "hooks", "pre-revprop-change")
45
46         open(revprop_hook, 'w').write("#!/bin/sh")
47
48         os.chmod(revprop_hook, os.stat(revprop_hook).st_mode | 0111)
49
50         return repos_url
51
52     def make_remote_bzrdir(self, relpath):
53         """Create a repository."""
54
55         repos_url = self.make_repository(relpath)
56
57         return BzrDir.open("svn+%s" % repos_url)
58
59     def open_local_bzrdir(self, repos_url, relpath):
60         """Open a local BzrDir."""
61
62         self.make_checkout(repos_url, relpath)
63
64         return BzrDir.open(relpath)
65
66     def make_local_bzrdir(self, repos_path, relpath):
67         """Create a repository and checkout."""
68
69         repos_url = self.make_repository(repos_path)
70
71         return self.open_local_bzrdir(repos_url, relpath)
72
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
76
77         svn.client.checkout2(repos_url, relpath, 
78                 rev, rev, True, False, self.client_ctx)
79
80     def client_set_prop(self, path, name, value):
81         svn.client.propset2(name, value, path, False, True, self.client_ctx)
82
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)
88
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]
94         
95     def client_commit(self, dir, message=None, recursive=True):
96         """Commit current changes in specified working copy.
97         
98         :param relpath: List of paths to commit.
99         """
100         olddir = os.path.abspath('.')
101         os.chdir(dir)
102         info = svn.client.commit3(["."], recursive, False, self.client_ctx)
103         os.chdir(olddir)
104         return (info.revision, info.date, info.author)
105
106     def client_add(self, relpath, recursive=True):
107         """Add specified files to working copy.
108         
109         :param relpath: Path to the files to add.
110         """
111         svn.client.add3(relpath, recursive, False, False, self.client_ctx)
112
113     def client_delete(self, relpaths):
114         """Remove specified files from working copy.
115
116         :param relpath: Path to the files to remove.
117         """
118         svn.client.delete2([relpaths], True, self.client_ctx)
119
120     def client_copy(self, oldpath, newpath):
121         """Copy file in working copy.
122
123         :param oldpath: Relative path to original file.
124         :param newpath: Relative path to new file.
125         """
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)
129
130     def build_tree(self, files):
131         """Create a directory tree.
132         
133         :param files: Dictionary with filenames as keys, contents as 
134             values. None as value indicates a directory.
135         """
136         for f in files:
137             if files[f] is None:
138                 try:
139                     os.makedirs(f)
140                 except OSError:
141                     pass
142             else:
143                 try:
144                     os.makedirs(os.path.dirname(f))
145                 except OSError:
146                     pass
147                 open(f, 'w').write(files[f])
148
149     def make_client_and_bzrdir(self, repospath, clientpath):
150         repos_url = self.make_client(repospath, clientpath)
151
152         return BzrDir.open("svn+%s" % repos_url)
153
154     def make_client(self, repospath, clientpath):
155         """Create a repository and a checkout. Return the checkout.
156
157         :param relpath: Optional relpath to check out if not the full 
158             repository.
159         :return: Repository URL.
160         """
161         repos_url = self.make_repository(repospath)
162         self.make_checkout(repos_url, clientpath)
163         return repos_url
164
165     def make_ra(self, relpath):
166         """Create a repository and a ra connection to it. 
167         
168         :param relpath: Path to create repository at.
169         :return: The ra connection.
170         """
171
172         repos_url = self.make_repository(relpath)
173
174         return svn.ra.open2(repos_url, svn.ra.callbacks2_t(), None, None)
175
176     def dumpfile(self, repos):
177         """Create a dumpfile for the specified repository.
178
179         :return: File name of the dumpfile.
180         """
181         raise NotImplementedError(self.dumpfile)
182
183     def open_fs(self, relpath):
184         """Open a fs.
185
186         :return: FS.
187         """
188         repos = svn.repos.open(relpath)
189
190         return svn.repos.fs(repos)
191
192
193 def test_suite():
194     from unittest import TestSuite, TestLoader
195     
196     from bzrlib.tests import TestUtil
197
198     loader = TestUtil.TestLoader()
199
200     suite = TestSuite()
201
202     testmod_names = [
203             'test_branch', 
204             'test_commit',
205             'test_fileids', 
206             'test_logwalker',
207             'test_repos', 
208             'test_scheme', 
209             'test_transport',
210             'test_workingtree']
211     suite.addTest(loader.loadTestsFromModuleNames(["%s.%s" % (__name__, i) for i in testmod_names]))
212
213     return suite