Fix more 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_revprops(self, url, revnum, name, value):
81         rev = svn.core.svn_opt_revision_t()
82         rev.kind = svn.core.svn_opt_revision_number
83         rev.value = revnum
84         svn.client.revprop_set(name, value, url, rev, True, 
85             self.client_ctx)
86         
87     def client_commit(self, dir, message=None, recursive=True):
88         """Commit current changes in specified working copy.
89         
90         :param relpath: List of paths to commit.
91         """
92         olddir = os.path.abspath('.')
93         os.chdir(dir)
94         info = svn.client.commit3(["."], recursive, False, self.client_ctx)
95         os.chdir(olddir)
96         return (info.revision, info.date, info.author)
97
98     def client_add(self, relpath, recursive=True):
99         """Add specified files to working copy.
100         
101         :param relpath: Path to the files to add.
102         """
103         svn.client.add3(relpath, recursive, False, False, self.client_ctx)
104
105     def client_delete(self, relpaths):
106         """Remove specified files from working copy.
107
108         :param relpath: Path to the files to remove.
109         """
110         svn.client.delete2([relpaths], True, self.client_ctx)
111
112     def client_copy(self, oldpath, newpath):
113         """Copy file in working copy.
114
115         :param oldpath: Relative path to original file.
116         :param newpath: Relative path to new file.
117         """
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)
121
122     def build_tree(self, files):
123         """Create a directory tree.
124         
125         :param files: Dictionary with filenames as keys, contents as 
126             values. None as value indicates a directory.
127         """
128         for f in files:
129             if files[f] is None:
130                 os.mkdir(f)
131             else:
132                 try:
133                     os.makedirs(os.path.dirname(f))
134                 except OSError:
135                     pass
136                 open(f, 'w').write(files[f])
137
138     def make_client_and_bzrdir(self, repospath, clientpath):
139         repos_url = self.make_client(repospath, clientpath)
140
141         return BzrDir.open("svn+%s" % repos_url)
142
143     def make_client(self, repospath, clientpath):
144         """Create a repository and a checkout. Return the checkout.
145
146         :param relpath: Optional relpath to check out if not the full 
147             repository.
148         :return: Repository URL.
149         """
150         repos_url = self.make_repository(repospath)
151         self.make_checkout(repos_url, clientpath)
152         return repos_url
153
154     def make_ra(self, relpath):
155         """Create a repository and a ra connection to it. 
156         
157         :param relpath: Path to create repository at.
158         :return: The ra connection.
159         """
160
161         repos_url = self.make_repository(relpath)
162
163         return svn.ra.open2(repos_url, svn.ra.callbacks2_t(), None, None)
164
165     def dumpfile(self, repos):
166         """Create a dumpfile for the specified repository.
167
168         :return: File name of the dumpfile.
169         """
170         raise NotImplementedError(self.dumpfile)
171
172     def open_fs(self, relpath):
173         """Open a fs.
174
175         :return: FS.
176         """
177         repos = svn.repos.open(relpath)
178
179         return svn.repos.fs(repos)
180
181
182 def test_suite():
183     from unittest import TestSuite, TestLoader
184     
185     from bzrlib.tests import TestUtil
186
187     loader = TestUtil.TestLoader()
188
189     suite = TestSuite()
190
191     testmod_names = [
192             'test_repos', 
193             'test_branch', 
194             'test_fileids', 
195             'test_logwalker',
196             'test_scheme', 
197             'test_transport',
198             'test_workingtree']
199     suite.addTest(loader.loadTestsFromModuleNames(["%s.%s" % (__name__, i) for i in testmod_names]))
200
201     return suite