summaryrefslogtreecommitdiff
path: root/tests/test_fetch_branches.py
blob: f8f5aaceafd963bb6d19e299f425402a4e1f0c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import test_util

import unittest

from mercurial import error
from mercurial import hg
from mercurial import node

from hgsubversion import compathacks

revsymbol = test_util.revsymbol

class TestFetchBranches(test_util.TestBase):
    stupid_mode_tests = True

    def _load_fixture_and_fetch_with_anchor(self, fixture_name, anchor):
        repo_path = self.load_svndump(fixture_name)
        source = '%s#%s' % (test_util.fileurl(repo_path), anchor)
        test_util.hgclone(self.ui(), source, self.wc_path)
        return hg.repository(self.ui(), self.wc_path)

    def branches(self, repo):
        hctxs = [repo[hn] for hn in repo.heads()]
        openbranches = set(ctx.branch() for ctx in hctxs if
                           ctx.extra().get('close', None) != '1')
        closedbranches = set(ctx.branch() for ctx in hctxs if
                             ctx.extra().get('close', None) == '1')
        return sorted(openbranches), sorted(closedbranches)

    def openbranches(self, repo):
        return self.branches(repo)[0]

    def test_rename_branch_parent(self):
        repo = self._load_fixture_and_fetch('rename_branch_parent_dir.svndump')
        heads = [repo[n] for n in repo.heads()]
        heads = dict([(ctx.branch(), ctx) for ctx in heads])
        # Let these tests disabled yet as the fix is not obvious
        self.assertEqual(['dev_branch'], self.openbranches(repo))

    def test_unrelatedbranch(self):
        repo = self._load_fixture_and_fetch('unrelatedbranch.svndump')
        heads = [repo[n] for n in repo.heads()]
        heads = dict([(ctx.branch(), ctx) for ctx in heads])
        # Let these tests disabled yet as the fix is not obvious
        self.assertEqual(heads['branch1'].manifest().keys(), ['b'])
        self.assertEqual(sorted(heads['branch2'].manifest().keys()),
                         ['a', 'b'])

    def test_unorderedbranch(self):
        repo = self._load_fixture_and_fetch('unorderedbranch.svndump')
        r = revsymbol(repo, 'branch')
        self.assertEqual(0, r.parents()[0].rev())
        self.assertEqual(['a', 'c', 'z'], sorted(r.manifest()))

    def test_renamed_branch_to_trunk(self):
        repo = self._load_fixture_and_fetch('branch_rename_to_trunk.svndump')
        self.assertEqual(revsymbol(repo, 'default').parents()[0].branch(), 'dev_branch')
        self.assert_('iota' in revsymbol(repo, 'default'))
        self.assertEqual(revsymbol(repo, 'old_trunk').parents()[0].branch(), 'default')
        self.assert_('iota' not in revsymbol(repo, 'old_trunk'))
        expected = ['default', 'old_trunk']
        self.assertEqual(self.openbranches(repo), expected)

    def test_replace_trunk_with_branch(self):
        repo = self._load_fixture_and_fetch('replace_trunk_with_branch.svndump')
        self.assertEqual(revsymbol(repo, 'default').parents()[0].branch(), 'test')
        self.assertEqual(revsymbol(repo, 'tip').branch(), 'default')
        self.assertEqual(revsymbol(repo, 'tip').extra().get('close'), '1')
        self.assertEqual(self.openbranches(repo), ['default'])

    def test_copybeforeclose(self):
        repo = self._load_fixture_and_fetch('copybeforeclose.svndump')
        self.assertEqual(revsymbol(repo, 'tip').branch(), 'test')
        self.assertEqual(revsymbol(repo, 'test').extra().get('close'), '1')
        self.assertEqual(revsymbol(repo, 'test')['b'].data(), 'a\n')

    def test_copyafterclose(self):
        repo = self._load_fixture_and_fetch('copyafterclose.svndump')
        self.assertEqual(revsymbol(repo, 'tip').branch(), 'test')
        self.assert_('file' in revsymbol(repo, 'test'))
        self.assertEqual(revsymbol(repo, 'test')['file'].data(), 'trunk2\n')
        self.assert_('dir/file' in revsymbol(repo, 'test'))
        self.assertEqual(revsymbol(repo, 'test')['dir/file'].data(), 'trunk2\n')


    def test_branch_create_with_dir_delete_works(self):
        repo = self._load_fixture_and_fetch('branch_create_with_dir_delete.svndump')
        self.assertEqual(sorted(revsymbol(repo, 'tip').manifest().keys()),
                         ['alpha', 'beta', 'gamma', 'iota', ])

    def test_branch_tip_update_to_default(self):
        repo = self._load_fixture_and_fetch('unorderedbranch.svndump',
                                            noupdate=False)
        self.assertEqual(repo[None].branch(), 'default')
        self.assertTrue('tip' not in repo[None].tags())

    def test_branch_pull_anchor(self):
        self.assertRaises(error.RepoLookupError,
                          self._load_fixture_and_fetch_with_anchor,
                          'unorderedbranch.svndump', 'NaN')
        repo = self._load_fixture_and_fetch_with_anchor(
            'unorderedbranch.svndump', '4')
        self.assertTrue('c' not in set(repo.branchmap()))

    def test_branches_weird_moves(self):
        repo = self._load_fixture_and_fetch('renamedproject.svndump',
                                            subdir='project')
        heads = [repo[n] for n in repo.heads()]
        heads = dict((ctx.branch(), ctx) for ctx in heads)
        mdefault = sorted(heads['default'].manifest().keys())
        mbranch = sorted(heads['branch'].manifest().keys())
        self.assertEqual(mdefault, ['a', 'b', 'd/a'])
        self.assertEqual(mbranch, ['a'])

    def test_branch_delete_parent_dir(self):
        repo = self._load_fixture_and_fetch('branch_delete_parent_dir.svndump')
        openb, closedb = self.branches(repo)
        self.assertEqual(openb, [])
        self.assertEqual(closedb, ['dev_branch'])
        self.assertEqual(list(revsymbol(repo, 'dev_branch')), ['foo'])

    def test_replace_branch_with_branch(self):
        repo = self._load_fixture_and_fetch('replace_branch_with_branch.svndump')
        self.assertEqual(7, test_util.repolen(repo))
        # tip is former topological branch1 being closed
        ctx = revsymbol(repo, 'tip')
        self.assertEqual('1', ctx.extra().get('close', '0'))
        self.assertEqual('branch1', ctx.branch())
        # r5 is where the replacement takes place
        ctx = repo[5]
        self.assertEqual(set(['a', 'c', 'dir/e', 'dir2/e', 'f', 'g']), set(ctx))
        self.assertEqual('0', ctx.extra().get('close', '0'))
        self.assertEqual('branch1', ctx.branch())
        self.assertEqual('c\n', ctx['c'].data())
        self.assertEqual('d\n', ctx['a'].data())
        self.assertEqual('e\n', ctx['dir/e'].data())
        self.assertEqual('e\n', ctx['dir2/e'].data())
        self.assertEqual('f\n', ctx['f'].data())
        self.assertEqual('g\n', ctx['g'].data())
        for f in ctx:
            self.assertTrue(not ctx[f].renamed())

    def test_misspelled_branches_tags(self):
        config = {
            'hgsubversion.branchdir': 'branchez',
            'hgsubversion.tagpaths': 'tagz',
            }
        '''Tests using the tags dir for branches and the branches dir for tags'''
        repo = self._load_fixture_and_fetch('misspelled_branches_tags.svndump',
                                            layout='standard',
                                            config=config)

        heads = set([repo[n].branch() for n in repo.heads()])
        expected_heads = set(['default', 'branch'])

        self.assertEqual(heads, expected_heads)

        tags = set(repo.tags())
        expected_tags = set(['tip', 'tag_from_trunk', 'tag_from_branch'])
        self.assertEqual(tags, expected_tags)

    def test_subdir_branches_tags(self):
        '''Tests using the tags dir for branches and the branches dir for tags'''
        config = {
            'hgsubversion.branchdir': 'bran/ches',
            'hgsubversion.tagpaths': 'ta/gs',
            }
        repo = self._load_fixture_and_fetch('subdir_branches_tags.svndump',
                                            layout='standard',
                                            config=config)

        heads = set([repo[n].branch() for n in repo.heads()])
        expected_heads = set(['default', 'branch'])

        self.assertEqual(heads, expected_heads)

        tags = set(repo.tags())
        expected_tags = set(['tip', 'tag_from_trunk', 'tag_from_branch'])
        self.assertEqual(tags, expected_tags)

    def test_subproject_fetch(self):
        config = {
            'hgsubversion.infix': 'project',
            }
        repo = self._load_fixture_and_fetch('subprojects.svndump',
                                            layout='standard',
                                            config=config)

        heads = set([repo[n].branch() for n in repo.heads()])
        expected_heads = set(['default', 'branch'])
        self.assertEqual(heads, expected_heads)

        tags = set(repo.tags())
        expected_tags = set(['tip', 'tag_from_trunk', 'tag_from_branch'])
        self.assertEqual(tags, expected_tags)

        for head in repo.heads():
            ctx = repo[head]
            self.assertFalse('project/file' in ctx, 'failed to strip infix')
            self.assertTrue('file' in ctx, 'failed to track a simple file')
            self.assertFalse('other/phile' in ctx, 'pulled in other project')
            self.assertFalse('phile' in ctx, 'merged other project in repo')


def suite():
    all_tests = [unittest.TestLoader().loadTestsFromTestCase(TestFetchBranches),
          ]
    return unittest.TestSuite(all_tests)