/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/git/tests/test_workingtree.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-06 01:18:08 UTC
  • mfrom: (7143 work)
  • mto: This revision was merged to the branch mainline in revision 7151.
  • Revision ID: jelmer@jelmer.uk-20181106011808-y870f4vq0ork3ahu
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
2
# Copyright (C) 2011 Canonical Ltd.
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
"""Tests for Git working trees."""
 
19
 
 
20
from __future__ import absolute_import
 
21
 
 
22
import os
 
23
import stat
 
24
 
 
25
from dulwich.objects import (
 
26
    Blob,
 
27
    Tree,
 
28
    ZERO_SHA,
 
29
    )
 
30
 
 
31
from ... import (
 
32
    conflicts as _mod_conflicts,
 
33
    workingtree as _mod_workingtree,
 
34
    )
 
35
from ...delta import TreeDelta
 
36
from ..mapping import (
 
37
    default_mapping,
 
38
    GitFileIdMap,
 
39
    )
 
40
from ..tree import (
 
41
    changes_between_git_tree_and_working_copy,
 
42
    tree_delta_from_git_changes,
 
43
    )
 
44
from ..workingtree import (
 
45
    FLAG_STAGEMASK,
 
46
    )
 
47
from ...tests import (
 
48
    TestCase,
 
49
    TestCaseWithTransport,
 
50
    )
 
51
 
 
52
 
 
53
class GitWorkingTreeTests(TestCaseWithTransport):
 
54
 
 
55
    def setUp(self):
 
56
        super(GitWorkingTreeTests, self).setUp()
 
57
        self.tree = self.make_branch_and_tree('.', format="git")
 
58
 
 
59
    def test_conflict_list(self):
 
60
        self.assertIsInstance(
 
61
                self.tree.conflicts(),
 
62
                _mod_conflicts.ConflictList)
 
63
 
 
64
    def test_add_conflict(self):
 
65
        self.build_tree(['conflicted'])
 
66
        self.tree.add(['conflicted'])
 
67
        with self.tree.lock_tree_write():
 
68
            self.tree.index[b'conflicted'] = self.tree.index[b'conflicted'][:9] + (FLAG_STAGEMASK, )
 
69
            self.tree._index_dirty = True
 
70
        conflicts = self.tree.conflicts()
 
71
        self.assertEqual(1, len(conflicts))
 
72
 
 
73
    def test_revert_empty(self):
 
74
        self.build_tree(['a'])
 
75
        self.tree.add(['a'])
 
76
        self.assertTrue(self.tree.is_versioned('a'))
 
77
        self.tree.revert(['a'])
 
78
        self.assertFalse(self.tree.is_versioned('a'))
 
79
 
 
80
    def test_is_ignored_directory(self):
 
81
        self.assertFalse(self.tree.is_ignored('a'))
 
82
        self.build_tree(['a/'])
 
83
        self.assertFalse(self.tree.is_ignored('a'))
 
84
        self.build_tree_contents([('.gitignore', 'a\n')])
 
85
        self.tree._ignoremanager = None
 
86
        self.assertTrue(self.tree.is_ignored('a'))
 
87
        self.build_tree_contents([('.gitignore', 'a/\n')])
 
88
        self.tree._ignoremanager = None
 
89
        self.assertTrue(self.tree.is_ignored('a'))
 
90
 
 
91
 
 
92
class GitWorkingTreeFileTests(TestCaseWithTransport):
 
93
 
 
94
    def setUp(self):
 
95
        super(GitWorkingTreeFileTests, self).setUp()
 
96
        self.tree = self.make_branch_and_tree('actual', format="git")
 
97
        self.build_tree_contents([('linked/',), ('linked/.git', 'gitdir: ../actual/.git')])
 
98
        self.wt = _mod_workingtree.WorkingTree.open('linked')
 
99
 
 
100
    def test_add(self):
 
101
        self.build_tree(['linked/somefile'])
 
102
        self.wt.add(["somefile"])
 
103
        self.wt.commit("Add somefile")
 
104
 
 
105
 
 
106
class TreeDeltaFromGitChangesTests(TestCase):
 
107
 
 
108
    def test_empty(self):
 
109
        delta = TreeDelta()
 
110
        changes = []
 
111
        self.assertEqual(
 
112
            delta,
 
113
            tree_delta_from_git_changes(changes, default_mapping,
 
114
                (GitFileIdMap({}, default_mapping),
 
115
                 GitFileIdMap({}, default_mapping))))
 
116
 
 
117
    def test_missing(self):
 
118
        delta = TreeDelta()
 
119
        delta.removed.append(('a', b'a-id', 'file'))
 
120
        changes = [((b'a', b'a'), (stat.S_IFREG | 0o755, 0), (b'a' * 40, b'a' * 40))]
 
121
        self.assertEqual(
 
122
            delta,
 
123
            tree_delta_from_git_changes(changes, default_mapping,
 
124
                (GitFileIdMap({u'a': b'a-id'}, default_mapping),
 
125
                 GitFileIdMap({u'a': b'a-id'}, default_mapping))))
 
126
 
 
127
 
 
128
class ChangesBetweenGitTreeAndWorkingCopyTests(TestCaseWithTransport):
 
129
 
 
130
    def setUp(self):
 
131
        super(ChangesBetweenGitTreeAndWorkingCopyTests, self).setUp()
 
132
        self.wt = self.make_branch_and_tree('.', format='git')
 
133
 
 
134
    def expectDelta(self, expected_changes,
 
135
                    expected_extras=None, want_unversioned=False):
 
136
        store = self.wt.branch.repository._git.object_store
 
137
        try:
 
138
            tree_id = store[self.wt.branch.repository._git.head()].tree
 
139
        except KeyError:
 
140
            tree_id = None
 
141
        with self.wt.lock_read():
 
142
            changes, extras = changes_between_git_tree_and_working_copy(
 
143
                store, tree_id, self.wt, want_unversioned=want_unversioned)
 
144
            self.assertEqual(expected_changes, list(changes))
 
145
        if expected_extras is None:
 
146
            expected_extras = set()
 
147
        self.assertEqual(set(expected_extras), set(extras))
 
148
 
 
149
    def test_empty(self):
 
150
        self.expectDelta(
 
151
            [((None, b''), (None, stat.S_IFDIR), (None, Tree().id))])
 
152
 
 
153
    def test_added_file(self):
 
154
        self.build_tree(['a'])
 
155
        self.wt.add(['a'])
 
156
        a = Blob.from_string(b'contents of a\n')
 
157
        t = Tree()
 
158
        t.add(b"a", stat.S_IFREG | 0o644, a.id)
 
159
        self.expectDelta(
 
160
            [((None, b''), (None, stat.S_IFDIR), (None, t.id)),
 
161
             ((None, b'a'), (None, stat.S_IFREG | 0o644), (None, a.id))])
 
162
 
 
163
    def test_added_unknown_file(self):
 
164
        self.build_tree(['a'])
 
165
        t = Tree()
 
166
        self.expectDelta(
 
167
            [((None, b''), (None, stat.S_IFDIR), (None, t.id))])
 
168
        a = Blob.from_string(b'contents of a\n')
 
169
        t = Tree()
 
170
        t.add(b"a", stat.S_IFREG | 0o644, a.id)
 
171
        self.expectDelta(
 
172
            [((None, b''), (None, stat.S_IFDIR), (None, t.id)),
 
173
             ((None, b'a'), (None, stat.S_IFREG | 0o644), (None, a.id))],
 
174
            [b'a'],
 
175
            want_unversioned=True)
 
176
 
 
177
    def test_missing_added_file(self):
 
178
        self.build_tree(['a'])
 
179
        self.wt.add(['a'])
 
180
        os.unlink('a')
 
181
        a = Blob.from_string(b'contents of a\n')
 
182
        t = Tree()
 
183
        t.add(b"a", 0, ZERO_SHA)
 
184
        self.expectDelta(
 
185
            [((None, b''), (None, stat.S_IFDIR), (None, t.id)),
 
186
             ((None, b'a'), (None, 0), (None, ZERO_SHA))],
 
187
            [])
 
188
 
 
189
    def test_missing_versioned_file(self):
 
190
        self.build_tree(['a'])
 
191
        self.wt.add(['a'])
 
192
        self.wt.commit('')
 
193
        os.unlink('a')
 
194
        a = Blob.from_string(b'contents of a\n')
 
195
        oldt = Tree()
 
196
        oldt.add(b"a", stat.S_IFREG | 0o644, a.id)
 
197
        newt = Tree()
 
198
        newt.add(b"a", 0, ZERO_SHA)
 
199
        self.expectDelta(
 
200
                [((b'', b''), (stat.S_IFDIR, stat.S_IFDIR), (oldt.id, newt.id)),
 
201
                 ((b'a', b'a'), (stat.S_IFREG|0o644, 0), (a.id, ZERO_SHA))])
 
202
 
 
203
    def test_versioned_replace_by_dir(self):
 
204
        self.build_tree(['a'])
 
205
        self.wt.add(['a'])
 
206
        self.wt.commit('')
 
207
        os.unlink('a')
 
208
        os.mkdir('a')
 
209
        olda = Blob.from_string(b'contents of a\n')
 
210
        oldt = Tree()
 
211
        oldt.add(b"a", stat.S_IFREG | 0o644, olda.id)
 
212
        newt = Tree()
 
213
        newa = Tree()
 
214
        newt.add(b"a", stat.S_IFDIR, newa.id)
 
215
        self.expectDelta([
 
216
            ((b'', b''),
 
217
            (stat.S_IFDIR, stat.S_IFDIR),
 
218
            (oldt.id, newt.id)),
 
219
            ((b'a', b'a'), (stat.S_IFREG | 0o644, stat.S_IFDIR), (olda.id, newa.id))
 
220
            ], want_unversioned=False)
 
221
        self.expectDelta([
 
222
            ((b'', b''),
 
223
            (stat.S_IFDIR, stat.S_IFDIR),
 
224
            (oldt.id, newt.id)),
 
225
            ((b'a', b'a'), (stat.S_IFREG | 0o644, stat.S_IFDIR), (olda.id, newa.id))
 
226
            ], want_unversioned=True)
 
227
 
 
228
    def test_extra(self):
 
229
        self.build_tree(['a'])
 
230
        newa = Blob.from_string(b'contents of a\n')
 
231
        newt = Tree()
 
232
        newt.add(b"a", stat.S_IFREG | 0o644, newa.id)
 
233
        self.expectDelta([
 
234
            ((None, b''),
 
235
            (None, stat.S_IFDIR),
 
236
            (None, newt.id)),
 
237
            ((None, b'a'), (None, stat.S_IFREG | 0o644), (None, newa.id))
 
238
            ], [b'a'], want_unversioned=True)