/brz/remove-bazaar

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