/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/tests/test_workspace.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-07-28 02:47:10 UTC
  • mfrom: (7519.1.1 merge-3.1)
  • Revision ID: breezy.the.bot@gmail.com-20200728024710-a2ylds219f1lsl62
Merge lp:brz/3.1.

Merged from https://code.launchpad.net/~jelmer/brz/merge-3.1/+merge/388173

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2020 Jelmer Vernooij <jelmer@jelmer.uk>
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
import os
 
18
 
 
19
from . import (
 
20
    TestCaseWithTransport,
 
21
    multiply_scenarios,
 
22
    )
 
23
from .scenarios import load_tests_apply_scenarios
 
24
 
 
25
from ..workspace import (
 
26
    WorkspaceDirty,
 
27
    Workspace,
 
28
    check_clean_tree,
 
29
    )
 
30
 
 
31
 
 
32
load_tests = load_tests_apply_scenarios
 
33
 
 
34
 
 
35
class CheckCleanTreeTests(TestCaseWithTransport):
 
36
 
 
37
    def make_test_tree(self, format=None):
 
38
        tree = self.make_branch_and_tree('.', format=format)
 
39
        self.build_tree_contents([
 
40
            ('debian/', ),
 
41
            ('debian/control', """\
 
42
Source: blah
 
43
Vcs-Git: https://example.com/blah
 
44
Testsuite: autopkgtest
 
45
 
 
46
Binary: blah
 
47
Arch: all
 
48
 
 
49
"""),
 
50
            ('debian/changelog', 'Some contents')])
 
51
        tree.add(['debian', 'debian/changelog', 'debian/control'])
 
52
        tree.commit('Initial thingy.')
 
53
        return tree
 
54
 
 
55
    def test_pending_changes(self):
 
56
        tree = self.make_test_tree()
 
57
        self.build_tree_contents([('debian/changelog', 'blah')])
 
58
        with tree.lock_write():
 
59
            self.assertRaises(
 
60
                WorkspaceDirty, check_clean_tree, tree)
 
61
 
 
62
    def test_pending_changes_bzr_empty_dir(self):
 
63
        # See https://bugs.debian.org/914038
 
64
        tree = self.make_test_tree(format='bzr')
 
65
        self.build_tree_contents([('debian/upstream/', )])
 
66
        with tree.lock_write():
 
67
            self.assertRaises(
 
68
                WorkspaceDirty, check_clean_tree, tree)
 
69
 
 
70
    def test_pending_changes_git_empty_dir(self):
 
71
        # See https://bugs.debian.org/914038
 
72
        tree = self.make_test_tree(format='git')
 
73
        self.build_tree_contents([('debian/upstream/', )])
 
74
        with tree.lock_write():
 
75
            check_clean_tree(tree)
 
76
 
 
77
    def test_pending_changes_git_dir_with_ignored(self):
 
78
        # See https://bugs.debian.org/914038
 
79
        tree = self.make_test_tree(format='git')
 
80
        self.build_tree_contents([
 
81
            ('debian/upstream/', ),
 
82
            ('debian/upstream/blah', ''),
 
83
            ('.gitignore', 'blah\n'),
 
84
            ])
 
85
        tree.add('.gitignore')
 
86
        tree.commit('add gitignore')
 
87
        with tree.lock_write():
 
88
            check_clean_tree(tree)
 
89
 
 
90
    def test_extra(self):
 
91
        tree = self.make_test_tree()
 
92
        self.build_tree_contents([('debian/foo', 'blah')])
 
93
        with tree.lock_write():
 
94
            self.assertRaises(
 
95
                WorkspaceDirty, check_clean_tree,
 
96
                tree)
 
97
 
 
98
 
 
99
def vary_by_inotify():
 
100
    return [
 
101
        ('with_inotify', dict(_use_inotify=True)),
 
102
        ('without_inotify', dict(_use_inotify=False)),
 
103
    ]
 
104
 
 
105
 
 
106
def vary_by_format():
 
107
    return [
 
108
        ('bzr', dict(_format='bzr')),
 
109
        ('git', dict(_format='git')),
 
110
    ]
 
111
 
 
112
 
 
113
class WorkspaceTests(TestCaseWithTransport):
 
114
 
 
115
    scenarios = multiply_scenarios(
 
116
        vary_by_inotify(),
 
117
        vary_by_format(),
 
118
    )
 
119
 
 
120
    def test_root_add(self):
 
121
        tree = self.make_branch_and_tree('.', format=self._format)
 
122
        with Workspace(tree, use_inotify=self._use_inotify) as ws:
 
123
            self.build_tree_contents([('afile', 'somecontents')])
 
124
            changes = [c for c in ws.iter_changes() if c.path[1] != '']
 
125
            self.assertEqual(1, len(changes), changes)
 
126
            self.assertEqual((None, 'afile'), changes[0].path)
 
127
            ws.commit(message='Commit message')
 
128
            self.assertEqual(list(ws.iter_changes()), [])
 
129
            self.build_tree_contents([('afile', 'newcontents')])
 
130
            [change] = list(ws.iter_changes())
 
131
            self.assertEqual(('afile', 'afile'), change.path)
 
132
 
 
133
    def test_root_remove(self):
 
134
        tree = self.make_branch_and_tree('.', format=self._format)
 
135
        self.build_tree_contents([('afile', 'somecontents')])
 
136
        tree.add(['afile'])
 
137
        tree.commit('Afile')
 
138
        with Workspace(tree, use_inotify=self._use_inotify) as ws:
 
139
            os.remove('afile')
 
140
            changes = list(ws.iter_changes())
 
141
            self.assertEqual(1, len(changes), changes)
 
142
            self.assertEqual(('afile', None), changes[0].path)
 
143
            ws.commit(message='Commit message')
 
144
            self.assertEqual(list(ws.iter_changes()), [])
 
145
 
 
146
    def test_subpath_add(self):
 
147
        tree = self.make_branch_and_tree('.', format=self._format)
 
148
        self.build_tree(['subpath/'])
 
149
        tree.add('subpath')
 
150
        tree.commit('add subpath')
 
151
        with Workspace(
 
152
                tree, subpath='subpath', use_inotify=self._use_inotify) as ws:
 
153
            self.build_tree_contents([('outside', 'somecontents')])
 
154
            self.build_tree_contents([('subpath/afile', 'somecontents')])
 
155
            changes = [c for c in ws.iter_changes() if c.path[1] != 'subpath']
 
156
            self.assertEqual(1, len(changes), changes)
 
157
            self.assertEqual((None, 'subpath/afile'), changes[0].path)
 
158
            ws.commit(message='Commit message')
 
159
            self.assertEqual(list(ws.iter_changes()), [])
 
160
 
 
161
    def test_dirty(self):
 
162
        tree = self.make_branch_and_tree('.', format=self._format)
 
163
        self.build_tree(['subpath'])
 
164
        self.assertRaises(
 
165
            WorkspaceDirty, Workspace(tree, use_inotify=self._use_inotify).__enter__)
 
166
 
 
167
    def test_reset(self):
 
168
        tree = self.make_branch_and_tree('.', format=self._format)
 
169
        with Workspace(tree, use_inotify=self._use_inotify) as ws:
 
170
            self.build_tree(['blah'])
 
171
            ws.reset()
 
172
            self.assertPathDoesNotExist('blah')
 
173
 
 
174
    def test_tree_path(self):
 
175
        tree = self.make_branch_and_tree('.', format=self._format)
 
176
        tree.mkdir('subdir')
 
177
        tree.commit('Add subdir')
 
178
        with Workspace(tree, use_inotify=self._use_inotify) as ws:
 
179
            self.assertEqual('foo', ws.tree_path('foo'))
 
180
            self.assertEqual('', ws.tree_path())
 
181
        with Workspace(tree, subpath='subdir', use_inotify=self._use_inotify) as ws:
 
182
            self.assertEqual('subdir/foo', ws.tree_path('foo'))
 
183
            self.assertEqual('subdir/', ws.tree_path())
 
184
 
 
185
    def test_abspath(self):
 
186
        tree = self.make_branch_and_tree('.', format=self._format)
 
187
        tree.mkdir('subdir')
 
188
        tree.commit('Add subdir')
 
189
        with Workspace(tree, use_inotify=self._use_inotify) as ws:
 
190
            self.assertEqual(tree.abspath('foo'), ws.abspath('foo'))
 
191
            self.assertEqual(tree.abspath(''), ws.abspath())
 
192
        with Workspace(tree, subpath='subdir', use_inotify=self._use_inotify) as ws:
 
193
            self.assertEqual(tree.abspath('subdir/foo'), ws.abspath('foo'))
 
194
            self.assertEqual(tree.abspath('subdir') + '/', ws.abspath(''))
 
195
            self.assertEqual(tree.abspath('subdir') + '/', ws.abspath())
 
196
 
 
197
    def test_open_containing(self):
 
198
        tree = self.make_branch_and_tree('.', format=self._format)
 
199
        tree.mkdir('subdir')
 
200
        tree.commit('Add subdir')
 
201
        ws = Workspace.from_path('subdir')
 
202
        self.assertEqual(ws.tree.abspath('.'), tree.abspath('.'))
 
203
        self.assertEqual(ws.subpath, 'subdir')
 
204
        self.assertEqual(None, ws.use_inotify)