Debian Codegraph @

lib/tests/test_extdiff.py

raw symbols SCIP
1 if __name__ == '__main__':
2 import breezy
3 breezy.initialize()
4 import breezy.plugin
5 breezy.plugin.set_plugins_path()
6 breezy.plugin.load_plugins()
7
8 import os, tempfile
9 from breezy.plugins.qbrz.lib.tests import QTestCase
10 from breezy.plugins.qbrz.lib.tests.mock import MockFunction
11 from breezy.plugins.qbrz.lib import diff
12 from breezy.workingtree import WorkingTree
13 from contextlib import contextmanager
14
15
16 class TestCommandString(QTestCase):
17 def setUp(self):
18 QTestCase.setUp(self)
19 self.tree = self.make_branch_and_tree('tree')
20 self.build_tree_contents([('tree/a', b"content")])
21 self.tree.add(["a"])
22 self.tree.commit(message='1')
23 self.differ = diff._ExtDiffer("test", self.tree.basis_tree(), self.tree)
24 self.addCleanup(self.differ.finish)
25
26 def test_no_arguments(self):
27 self.differ.set_command_string("test")
28 self.assertEqual(self.differ.command_template, ["test", "{old_path}", "{new_path}"])
29
30 def test_has_arguments(self):
31 self.differ.set_command_string("test --old {old_path} --new {new_path}")
32 self.assertEqual(self.differ.command_template, ["test", "--old", "{old_path}", "--new", "{new_path}"])
33
34 class TestPrefix(QTestCase):
35 def setUp(self):
36 QTestCase.setUp(self)
37 self.tree = self.make_branch_and_tree('tree')
38 self.build_tree_contents([('tree/a', b"content")])
39 self.tree.add(["a"])
40 self.tree.commit(message='1')
41 self.build_tree_contents([('tree/a', b"new content")])
42 self.tree.commit(message='2')
43 self.differ = diff._ExtDiffer("test", self.tree.basis_tree(), self.tree)
44 self.addCleanup(self.differ.finish)
45
46 def test_same_prefix_for_same_workingtree(self):
47 prefix = self.differ.get_prefix(self.tree)
48 tree2 = WorkingTree.open(self.tree.abspath(""))
49 self.assertEqual(self.differ.get_prefix(tree2), prefix)
50
51 def test_another_prefix_for_another_workingtree(self):
52 tree2 = self.make_branch_and_tree('tree2')
53 prefix = self.differ.get_prefix(self.tree)
54 self.assertNotEqual(self.differ.get_prefix(tree2), prefix)
55
56 def test_same_prefix_for_same_revisiontree(self):
57 self.differ = diff._ExtDiffer("test", self.tree.basis_tree(), self.tree)
58 tree1 = self.tree.basis_tree()
59 tree2 = self.tree.basis_tree()
60 self.assertNotEqual(tree1, tree2)
61 prefix = self.differ.get_prefix(tree1)
62 self.assertEqual(self.differ.get_prefix(tree2), prefix)
63 self.assertNotEqual(self.differ.get_prefix(self.tree), prefix)
64
65 def test_another_prefix_for_another_revtree(self):
66 b = self.tree.branch
67 trees = [b.repository.revision_tree(b.get_rev_id(no)) for no in (1,2)]
68 prefix = self.differ.get_prefix(trees[0])
69 self.assertNotEqual(self.differ.get_prefix(trees[1]), prefix)
70
71 def test_same_prefix_for_same_object(self):
72 obj = object()
73 prefix = self.differ.get_prefix(obj)
74 self.assertEqual(self.differ.get_prefix(obj), prefix)
75
76 def test_another_prefix_for_another_object(self):
77 obj = object()
78 prefix = self.differ.get_prefix(obj)
79 self.assertNotEqual(self.differ.get_prefix(object()), prefix)
80
81 class TestExtDiffBase(QTestCase):
82 def setUp(self):
83 QTestCase.setUp(self)
84 self.popen_mock = MockFunction()
85 popen = diff.subprocess.Popen
86 diff.subprocess.Popen = self.popen_mock
87 def restore():
88 diff.subprocess.Popen = popen
89 self.addCleanup(restore)
90 self.tree = self.make_branch_and_tree('tree')
91 self.ctx = self.create_context()
92 self.addCleanup(self.ctx.finish)
93
94 def create_context(self, parent=None):
95 ctx = diff.ExtDiffContext(parent)
96 self.addCleanup(ctx.finish)
97 return ctx
98
99 def assertFileContent(self, path, content):
100 self.assertTrue(os.path.isfile(path))
101 f = open(path)
102 self.assertEqual("\n".join(f.readlines()), content)
103 f.close()
104
105 class TestCleanup(TestExtDiffBase):
106 def setUp(self):
107 TestExtDiffBase.setUp(self)
108 self.build_tree_contents([('tree/a', b"a")])
109 self.tree.add(['a'])
110 self.tree.commit(message='1')
111 self.build_tree_contents([('tree/a', b"aa")])
112
113 def test_remove_root(self):
114 self.ctx.setup("diff.txt", self.tree.basis_tree(), self.tree)
115 rootdir = self.ctx.rootdir
116 self.assertTrue(len(rootdir) > 0)
117 self.assertTrue(os.path.isdir(rootdir))
118 self.ctx.finish()
119 self.assertTrue(self.ctx.rootdir is None)
120 self.assertFalse(os.path.exists(rootdir))
121
122 def test_dont_remove_root_of_other_context(self):
123 self.ctx.setup("diff.txt", self.tree.basis_tree(), self.tree)
124 ctx2 = self.create_context()
125 ctx2.setup("diff.txt", self.tree.basis_tree(), self.tree)
126 rootdir = self.ctx.rootdir
127 self.assertNotEqual(rootdir, ctx2.rootdir)
128 self.ctx.finish()
129 self.assertTrue(self.ctx.rootdir is None)
130 self.assertTrue(os.path.exists(ctx2.rootdir))
131
132 @contextmanager
133 def invalidate_rmtree(self):
134 rmtree = diff.osutils.rmtree
135
136 def rmtree_mock(path):
137 raise IOError("osutils.rmtree is now invalidated.")
138 try:
139 diff.osutils.rmtree = MockFunction(func=rmtree_mock)
140 yield
141 finally:
142 diff.osutils.rmtree = rmtree
143
144 def test_mark_deletable_if_delete_failed(self):
145 # If failed to delete tempdir, mark it deletable to delete later.
146 self.ctx.setup("diff.txt", self.tree.basis_tree(), self.tree)
147 rootdir = self.ctx.rootdir
148 self.assertTrue(len(rootdir) > 0)
149 self.assertTrue(os.path.isdir(rootdir))
150 with self.invalidate_rmtree():
151 self.ctx.finish()
152 self.assertTrue(self.ctx.rootdir is None)
153 self.assertTrue(os.path.isfile(os.path.join(rootdir, ".delete")))
154
155 def test_cleanup_deletable_roots(self):
156 ctx2 = self.create_context()
157 ctx3 = self.create_context()
158 self.ctx.setup("diff.txt", self.tree.basis_tree(), self.tree)
159 ctx2.setup("diff.txt", self.tree.basis_tree(), self.tree)
160 ctx3.setup("diff.txt", self.tree.basis_tree(), self.tree)
161 root = self.ctx.rootdir
162 root2 = ctx2.rootdir
163 root3 = ctx3.rootdir
164 with self.invalidate_rmtree():
165 self.ctx.finish()
166 self.assertTrue(os.path.isfile(os.path.join(root, ".delete")))
167 os.chdir(tempfile.gettempdir())
168 ctx2.finish()
169 self.assertFalse(os.path.exists(root))
170 self.assertFalse(os.path.exists(root2))
171 self.assertTrue(os.path.exists(root3))
172
173 def test_finish_lazy(self):
174 self.ctx.setup("diff.txt", self.tree.basis_tree(), self.tree)
175 rootdir = self.ctx.rootdir
176 self.assertTrue(len(rootdir) > 0)
177 self.assertTrue(os.path.isdir(rootdir))
178 self.ctx.finish_lazy()
179 self.assertTrue(self.ctx.rootdir is None)
180 self.assertTrue(os.path.isfile(os.path.join(rootdir, ".delete")))
181
182
183 def test_cleanup_deletable_roots_by_finish_lazy(self):
184 ctx2 = self.create_context()
185 ctx3 = self.create_context()
186 self.ctx.setup("diff.txt", self.tree.basis_tree(), self.tree)
187 ctx2.setup("diff.txt", self.tree.basis_tree(), self.tree)
188 ctx3.setup("diff.txt", self.tree.basis_tree(), self.tree)
189 root = self.ctx.rootdir
190 root2 = ctx2.rootdir
191 root3 = ctx3.rootdir
192 self.ctx.finish_lazy()
193 ctx2.finish()
194 self.assertFalse(os.path.exists(root))
195 self.assertFalse(os.path.exists(root2))
196 self.assertTrue(os.path.exists(root3))
197
198 class TestWorkingTreeDiff(TestExtDiffBase):
199 def setUp(self):
200 TestExtDiffBase.setUp(self)
201
202 self.build_tree(['tree/dir1/', 'tree/dir2/'])
203 self.build_tree_contents([
204 ('tree/a', b"a"),
205 ('tree/dir1/b', b"b"),
206 ('tree/dir1/c', b"c"),
207 ('tree/dir1/d', b"d"),
208 ('tree/dir2/e', b"e"),
209 ('tree/dir2/f', b"f"),
210 ])
211 self.tree.add(["a", "dir1", "dir2", "dir1/b", "dir1/c", "dir1/d", "dir2/e", "dir2/f"])
212 self.tree.commit(message='1')
213 self.build_tree_contents([
214 ('tree/a', b"A"),
215 ('tree/dir1/b', b"B"),
216 ('tree/dir1/c', b"C"),
217 ('tree/dir2/e', b"E"),
218 ])
219 self.ctx.setup("diff.exe", self.tree.basis_tree(), self.tree)
220
221 def assertPopen(self, paths, old_contents):
222 for args, path, old_content in zip(self.popen_mock.args, paths, old_contents):
223 # tool, old_path, new_path = args[0][0]
224 tool, old_path, new_path = args[0][0]
225 self.assertEqual(tool, "diff.exe")
226 self.assertFileContent(old_path, old_content)
227 self.assertEqual(new_path, self.tree.abspath(path))
228
229 def test_diff_ids(self):
230 paths = ['a', 'dir1/b']
231 self.ctx.diff_ids([self.tree.path2id(p) for p in paths])
232 self.assertPopen(paths, ["a", "b"])
233
234 def test_diff_paths(self):
235 paths = ['a', 'dir1/b']
236 self.ctx.diff_paths(paths)
237 self.assertPopen(paths, ["a", "b"])
238
239 def test_diff_paths_for_dir(self):
240 paths = ['dir1/b', 'dir1/c']
241 self.ctx.diff_paths(['dir1'])
242 self.assertPopen(paths, ["b", "c"])
243
244 def test_diff_tree(self):
245 paths = ['a', 'dir1/b', 'dir1/c', 'dir2/e']
246 old_contents = ['a', 'b', 'c', 'e']
247 self.ctx.diff_tree()
248 self.assertPopen(paths, old_contents)
249
250 def test_diff_tree_for_dir(self):
251 paths = ['dir1/b', 'dir1/c']
252 self.ctx.diff_tree(specific_files=['dir1'])
253 self.assertPopen(paths, ["b", "c"])
254
255 def test_diff_for_renamed_files(self):
256 self.tree.rename_one('a', 'a2')
257 paths = ['a2']
258 self.ctx.diff_paths(['a2'])
259 self.assertPopen(paths, ["a"])
260
261 def test_skip_added_file(self):
262 self.build_tree_contents([('tree/a2', b"a"),])
263 self.tree.add(['a2'])
264 self.ctx.diff_paths(['a2'])
265 self.assertPopen([], [])
266
267 def test_skip_removed_file_1(self):
268 self.tree.remove(['a'], keep_files=True)
269 self.ctx.diff_paths(['a'])
270 self.assertPopen([], [])
271
272 def test_skip_removed_file_2(self):
273 self.tree.remove(['a'], keep_files=False)
274 self.ctx.diff_paths(['a'])
275 self.assertPopen([], [])
276
277 if __name__=='__main__':
278 import unittest
279 unittest.main()