1
# Copyright (C) 2020 Jelmer Vernooij <jelmer@jelmer.uk>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
TestCaseWithTransport,
24
from .scenarios import load_tests_apply_scenarios
26
from ..workspace import (
33
load_tests = load_tests_apply_scenarios
36
class CheckCleanTreeTests(TestCaseWithTransport):
38
def make_test_tree(self, format=None):
39
tree = self.make_branch_and_tree('.', format=format)
40
self.build_tree_contents([
42
('debian/control', """\
44
Vcs-Git: https://example.com/blah
45
Testsuite: autopkgtest
51
('debian/changelog', 'Some contents')])
52
tree.add(['debian', 'debian/changelog', 'debian/control'])
53
tree.commit('Initial thingy.')
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():
61
WorkspaceDirty, check_clean_tree, tree)
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():
69
WorkspaceDirty, check_clean_tree, tree)
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)
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'),
86
tree.add('.gitignore')
87
tree.commit('add gitignore')
88
with tree.lock_write():
89
check_clean_tree(tree)
92
tree = self.make_test_tree()
93
self.build_tree_contents([('debian/foo', 'blah')])
94
with tree.lock_write():
96
WorkspaceDirty, check_clean_tree,
100
def vary_by_inotify():
102
('with_inotify', dict(_use_inotify=True)),
103
('without_inotify', dict(_use_inotify=False)),
107
def vary_by_format():
109
('bzr', dict(_format='bzr')),
110
('git', dict(_format='git')),
114
class WorkspaceTests(TestCaseWithTransport):
116
scenarios = multiply_scenarios(
122
super(WorkspaceTests, self).setUp()
123
if self._use_inotify:
124
self.requireFeature(features.pyinotify)
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)
139
def test_root_remove(self):
140
tree = self.make_branch_and_tree('.', format=self._format)
141
self.build_tree_contents([('afile', 'somecontents')])
144
with Workspace(tree, use_inotify=self._use_inotify) as ws:
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()), [])
152
def test_subpath_add(self):
153
tree = self.make_branch_and_tree('.', format=self._format)
154
self.build_tree(['subpath/'])
156
tree.commit('add subpath')
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()), [])
167
def test_dirty(self):
168
tree = self.make_branch_and_tree('.', format=self._format)
169
self.build_tree(['subpath'])
171
WorkspaceDirty, Workspace(tree, use_inotify=self._use_inotify).__enter__)
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'])
178
self.assertPathDoesNotExist('blah')
180
def test_tree_path(self):
181
tree = self.make_branch_and_tree('.', format=self._format)
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())
191
def test_abspath(self):
192
tree = self.make_branch_and_tree('.', format=self._format)
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())
203
def test_open_containing(self):
204
tree = self.make_branch_and_tree('.', format=self._format)
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)