1 # Copyright (C) 2006-2007 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
17 from bzrlib.branch import Branch
18 from bzrlib.bzrdir import BzrDir, BzrDirTestProviderAdapter, BzrDirFormat
19 from bzrlib.errors import NoSuchFile
20 from bzrlib.inventory import ROOT_ID
21 from bzrlib.repository import Repository
22 from bzrlib.trace import mutter
25 from unittest import TestCase
27 import svn.core, svn.client
29 from branch import FakeControlFiles, SvnBranchFormat
30 from convert import load_dumpfile
32 from repository import MAPPING_VERSION, generate_svn_revision_id
33 from tests import TestCaseWithSubversionRepository
35 class WorkingSubversionBranch(TestCaseWithSubversionRepository):
36 def test_last_rev_rev_hist(self):
37 repos_url = self.make_client("a", "dc")
38 branch = Branch.open(repos_url)
39 branch.revision_history()
40 self.assertEqual(None, branch.last_revision())
42 def test_set_parent(self):
43 repos_url = self.make_client('a', 'dc')
44 branch = Branch.open(repos_url)
45 branch.set_parent("foobar")
47 def test_num_revnums(self):
48 repos_url = self.make_client('a', 'dc')
49 bzrdir = BzrDir.open("svn+"+repos_url)
50 branch = bzrdir.open_branch()
51 self.assertEqual(None, branch.last_revision())
53 self.build_tree({'dc/foo': "data"})
54 self.client_add("dc/foo")
55 self.client_commit("dc", "My Message")
57 bzrdir = BzrDir.open("svn+"+repos_url)
58 branch = bzrdir.open_branch()
59 repos = bzrdir.find_repository()
61 self.assertEqual(repos.generate_revision_id(1, ""),
62 branch.last_revision())
64 self.build_tree({'dc/foo': "data2"})
65 self.client_commit("dc", "My Message")
67 branch = Branch.open("svn+"+repos_url)
68 repos = Repository.open("svn+"+repos_url)
70 self.assertEqual(repos.generate_revision_id(2, ""),
71 branch.last_revision())
73 def test_set_revision_history(self):
74 repos_url = self.make_client('a', 'dc')
75 branch = Branch.open("svn+"+repos_url)
76 self.assertRaises(NotImplementedError, branch.set_revision_history, [])
78 def test_get_root_id_empty(self):
79 repos_url = self.make_client('a', 'dc')
80 branch = Branch.open("svn+"+repos_url)
81 self.assertEqual(ROOT_ID, branch.get_root_id())
83 def test_get_root_id_trunk(self):
84 repos_url = self.make_client('a', 'dc')
85 self.build_tree({'dc/trunk': None})
86 self.client_add("dc/trunk")
87 self.client_commit("dc", "msg")
88 branch = Branch.open("svn+"+repos_url+"/trunk")
89 self.assertEqual(ROOT_ID, branch.get_root_id())
91 def test_break_lock(self):
92 repos_url = self.make_client('a', 'dc')
93 branch = Branch.open("svn+"+repos_url)
94 branch.control_files.break_lock()
97 repos_url = self.make_client('a', 'dc')
98 branch = Branch.open("svn+"+repos_url)
99 self.assertEqual("SvnBranch('svn+%s')" % repos_url, branch.__repr__())
101 def test_get_physical_lock_status(self):
102 repos_url = self.make_client('a', 'dc')
103 branch = Branch.open("svn+"+repos_url)
104 self.assertFalse(branch.get_physical_lock_status())
106 def test_set_push_location(self):
107 repos_url = self.make_client('a', 'dc')
108 branch = Branch.open("svn+"+repos_url)
109 self.assertRaises(NotImplementedError, branch.set_push_location, [])
111 def test_get_parent(self):
112 repos_url = self.make_client('a', 'dc')
113 branch = Branch.open("svn+"+repos_url)
114 self.assertEqual("svn+"+repos_url, branch.get_parent())
116 def test_append_revision(self):
117 repos_url = self.make_client('a', 'dc')
118 branch = Branch.open("svn+"+repos_url)
119 branch.append_revision([])
121 def test_get_push_location(self):
122 repos_url = self.make_client('a', 'dc')
123 branch = Branch.open("svn+"+repos_url)
124 self.assertIs(None, branch.get_push_location())
126 def test_revision_history(self):
127 repos_url = self.make_client('a', 'dc')
129 branch = Branch.open("svn+"+repos_url)
130 self.assertEqual([], branch.revision_history())
132 self.build_tree({'dc/foo': "data"})
133 self.client_add("dc/foo")
134 self.client_commit("dc", "My Message")
136 branch = Branch.open("svn+"+repos_url)
137 repos = Repository.open("svn+"+repos_url)
139 self.assertEqual([repos.generate_revision_id(1, "")],
140 branch.revision_history())
142 self.build_tree({'dc/foo': "data34"})
143 self.client_commit("dc", "My Message")
145 branch = Branch.open("svn+"+repos_url)
146 repos = Repository.open("svn+"+repos_url)
149 repos.generate_revision_id(1, ""),
150 repos.generate_revision_id(2, "")],
151 branch.revision_history())
153 def test_get_nick_none(self):
154 repos_url = self.make_client('a', 'dc')
156 self.build_tree({'dc/foo': "data"})
157 self.client_add("dc/foo")
158 self.client_commit("dc", "My Message")
160 branch = Branch.open("svn+"+repos_url)
162 self.assertIs(None, branch.nick)
164 def test_get_nick_path(self):
165 repos_url = self.make_client('a', 'dc')
167 self.build_tree({'dc/trunk': "data"})
168 self.client_add("dc/trunk")
169 self.client_commit("dc", "My Message")
171 branch = Branch.open("svn+"+repos_url+"/trunk")
173 self.assertEqual("trunk", branch.nick)
175 def test_get_revprops(self):
176 repos_url = self.make_client('a', 'dc')
178 self.build_tree({'dc/foo': "data"})
179 self.client_add("dc/foo")
180 self.client_set_prop("dc", "bzr:revprop:branch-nick", "mybranch")
181 self.client_commit("dc", "My Message")
183 branch = Branch.open("svn+"+repos_url)
185 rev = branch.repository.get_revision(branch.last_revision())
187 self.assertEqual("mybranch", rev.properties["branch-nick"])
189 def test_fetch_replace(self):
190 filename = os.path.join(self.test_dir, "dumpfile")
191 open(filename, 'w').write("""SVN-fs-dump-format-version: 2
193 UUID: 6f95bc5c-e18d-4021-aca8-49ed51dbcb75
196 Prop-content-length: 56
202 2006-07-30T12:41:25.270824Z
206 Prop-content-length: 94
220 2006-07-30T12:41:26.117512Z
226 Prop-content-length: 10
232 Node-path: trunk/hosts
235 Prop-content-length: 10
236 Text-content-length: 4
237 Text-content-md5: 771ec3328c29d17af5aacf7f895dd885
244 Prop-content-length: 94
258 2006-07-30T12:41:27.130044Z
261 Node-path: trunk/hosts
264 Text-content-length: 4
265 Text-content-md5: 6c2479dbb342b8df96d84db7ab92c412
271 Prop-content-length: 94
285 2006-07-30T12:41:28.114350Z
288 Node-path: trunk/hosts
291 Text-content-length: 4
292 Text-content-md5: 368cb8d3db6186e2e83d9434f165c525
298 Prop-content-length: 94
312 2006-07-30T12:41:29.129563Z
318 Prop-content-length: 10
325 Prop-content-length: 94
339 2006-07-30T12:41:31.130508Z
342 Node-path: branches/foobranch
346 Node-copyfrom-path: trunk
350 Prop-content-length: 94
364 2006-07-30T12:41:33.129149Z
367 Node-path: branches/foobranch/hosts
371 Node-path: branches/foobranch/hosts
375 Node-copyfrom-path: trunk/hosts
381 Prop-content-length: 94
395 2006-07-30T12:41:34.136423Z
398 Node-path: branches/foobranch/hosts
401 Text-content-length: 8
402 Text-content-md5: 0e328d3517a333a4879ebf3d88fd82bb
409 load_dumpfile("dumpfile", "old")
411 url = "old/branches/foobranch"
412 mutter('open %r' % url)
413 olddir = BzrDir.open(url)
415 newdir = olddir.sprout("new")
417 newbranch = newdir.open_branch()
419 uuid = "6f95bc5c-e18d-4021-aca8-49ed51dbcb75"
420 tree = newbranch.repository.revision_tree(
421 generate_svn_revision_id(uuid, 7, "branches/foobranch"))
423 weave = tree.get_weave(tree.inventory.path2id("hosts"))
425 generate_svn_revision_id(uuid, 6, "branches/foobranch"),
426 generate_svn_revision_id(uuid, 7, "branches/foobranch")],
430 def test_fetch_odd(self):
431 repos_url = self.make_client('d', 'dc')
433 self.build_tree({'dc/trunk': None,
434 'dc/trunk/hosts': 'hej1'})
435 self.client_add("dc/trunk")
436 self.client_commit("dc", "created trunk and added hosts") #1
438 self.build_tree({'dc/trunk/hosts': 'hej2'})
439 self.client_commit("dc", "rev 2") #2
441 self.build_tree({'dc/trunk/hosts': 'hej3'})
442 self.client_commit("dc", "rev 3") #3
444 self.build_tree({'dc/branches': None})
445 self.client_add("dc/branches")
446 self.client_commit("dc", "added branches") #4
448 self.client_copy("dc/trunk", "dc/branches/foobranch")
449 self.client_commit("dc", "added branch foobranch") #5
451 self.build_tree({'dc/branches/foobranch/hosts': 'foohosts'})
452 self.client_commit("dc", "foohosts") #6
456 url = "svn+"+repos_url+"/branches/foobranch"
457 mutter('open %r' % url)
458 olddir = BzrDir.open(url)
460 newdir = olddir.sprout("new")
462 newbranch = newdir.open_branch()
464 uuid = olddir.find_repository().uuid
465 tree = newbranch.repository.revision_tree(
466 generate_svn_revision_id(uuid, 6, "branches/foobranch"))
468 weave = tree.get_weave(tree.inventory.path2id("hosts"))
470 generate_svn_revision_id(uuid, 1, "trunk"),
471 generate_svn_revision_id(uuid, 2, "trunk"),
472 generate_svn_revision_id(uuid, 3, "trunk"),
473 generate_svn_revision_id(uuid, 6, "branches/foobranch")],
476 def test_check(self):
477 repos_url = self.make_client('d', 'dc')
478 branch = Branch.open('d')
479 result = branch.check()
480 self.assertEqual(branch, result.branch)
482 def test_generate_revision_id(self):
483 repos_url = self.make_client('d', 'dc')
484 branch = Branch.open('d')
485 self.assertEqual("svn-v%d-undefined:%s::1" % (MAPPING_VERSION, branch.repository.uuid), branch.generate_revision_id(1))
487 def test_create_checkout(self):
488 repos_url = self.make_client('d', 'dc')
490 self.build_tree({'dc/trunk': None, 'dc/trunk/hosts': 'hej1'})
491 self.client_add("dc/trunk")
492 self.client_commit("dc", "created trunk and added hosts") #1
494 url = "svn+"+repos_url+"/trunk"
495 oldbranch = Branch.open(url)
497 newtree = oldbranch.create_checkout("e")
498 self.assertTrue(newtree.branch.repository.has_revision(
499 oldbranch.generate_revision_id(1)))
501 self.assertTrue(os.path.exists("e/.bzr"))
502 self.assertFalse(os.path.exists("e/.svn"))
504 def test_create_checkout_lightweight(self):
505 repos_url = self.make_client('d', 'dc')
507 self.build_tree({'dc/trunk': None,
508 'dc/trunk/hosts': 'hej1'})
509 self.client_add("dc/trunk")
510 self.client_commit("dc", "created trunk and added hosts") #1
512 url = "svn+"+repos_url+"/trunk"
513 oldbranch = Branch.open(url)
515 newtree = oldbranch.create_checkout("e", lightweight=True)
516 self.assertEqual(oldbranch.generate_revision_id(1), newtree.base_revid)
517 self.assertTrue(os.path.exists("e/.svn"))
518 self.assertFalse(os.path.exists("e/.bzr"))
520 def test_create_checkout_lightweight_stop_rev(self):
521 repos_url = self.make_client('d', 'dc')
523 self.build_tree({'dc/trunk': None,
524 'dc/trunk/hosts': 'hej1'})
525 self.client_add("dc/trunk")
526 self.client_commit("dc", "created trunk and added hosts") #1
528 self.build_tree({'dc/trunk/hosts': 'bloe'})
529 self.client_commit("dc", "added another revision")
531 url = "svn+"+repos_url+"/trunk"
532 oldbranch = Branch.open(url)
534 newtree = oldbranch.create_checkout("e", revision_id=
535 oldbranch.generate_revision_id(1), lightweight=True)
536 self.assertEqual(oldbranch.generate_revision_id(1),
538 self.assertTrue(os.path.exists("e/.svn"))
539 self.assertFalse(os.path.exists("e/.bzr"))
541 def test_fetch_branch(self):
542 repos_url = self.make_client('d', 'sc')
544 self.build_tree({'sc/foo/bla': "data"})
545 self.client_add("sc/foo")
546 self.client_commit("sc", "foo")
548 olddir = BzrDir.open("sc")
552 newdir = olddir.sprout('dc')
555 olddir.open_branch().last_revision(),
556 newdir.open_branch().last_revision())
558 def test_ghost_workingtree(self):
559 # Looks like bazaar has trouble creating a working tree of a
560 # revision that has ghost parents
561 repos_url = self.make_client('d', 'sc')
563 self.build_tree({'sc/foo/bla': "data"})
564 self.client_add("sc/foo")
565 self.client_set_prop("sc", "bzr:merge", "some-ghost\n")
566 self.client_commit("sc", "foo")
568 olddir = BzrDir.open("sc")
572 newdir = olddir.sprout('dc')
573 newdir.find_repository().get_revision(
574 newdir.open_branch().last_revision())
575 newdir.find_repository().get_revision_inventory(
576 newdir.open_branch().last_revision())
578 class TestFakeControlFiles(TestCase):
579 def test_get_utf8(self):
580 f = FakeControlFiles()
581 self.assertRaises(NoSuchFile, f.get_utf8, "foo")
585 f = FakeControlFiles()
586 self.assertRaises(NoSuchFile, f.get, "foobla")
588 class BranchFormatTests(TestCase):
590 self.format = SvnBranchFormat()
592 def test_initialize(self):
593 self.assertRaises(NotImplementedError, self.format.initialize, None)
595 def test_get_format_string(self):
596 self.assertEqual("Subversion Smart Server",
597 self.format.get_format_string())
599 def test_get_format_description(self):
600 self.assertEqual("Subversion Smart Server",
601 self.format.get_format_description())