1296
1299
def test_from_string(self):
1297
1300
diff_obj = DiffFromTool.from_string('diff', None, None, None)
1298
1301
self.addCleanup(diff_obj.finish)
1299
self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
1302
self.assertEqual(['diff', '@old_path', '@new_path'],
1300
1303
diff_obj.command_template)
1302
1305
def test_from_string_u5(self):
1303
1306
diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
1304
1307
self.addCleanup(diff_obj.finish)
1305
self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
1308
self.assertEqual(['diff', '-u 5', '@old_path', '@new_path'],
1306
1309
diff_obj.command_template)
1307
1310
self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1308
1311
diff_obj._get_command('old-path', 'new-path'))
1334
1337
tree.commit('old tree')
1335
1338
tree.lock_read()
1336
1339
self.addCleanup(tree.unlock)
1340
basis_tree = tree.basis_tree()
1341
basis_tree.lock_read()
1342
self.addCleanup(basis_tree.unlock)
1337
1343
diff_obj = DiffFromTool(['python', '-c',
1338
'print "%(old_path)s %(new_path)s"'],
1344
'print "@old_path @new_path"'],
1345
basis_tree, tree, output)
1340
1346
diff_obj._prepare_files('file-id', 'file', 'file')
1341
self.assertReadableByAttrib(diff_obj._root, 'old\\file', r'old\\file')
1342
self.assertReadableByAttrib(diff_obj._root, 'new\\file', r'new\\file')
1347
# The old content should be readonly
1348
self.assertReadableByAttrib(diff_obj._root, 'old\\file',
1350
# The new content should use the tree object, not a 'new' file anymore
1351
self.assertEndsWith(tree.basedir, 'work/tree')
1352
self.assertReadableByAttrib(tree.basedir, 'file', r'work\\tree\\file$')
1344
1354
def assertReadableByAttrib(self, cwd, relpath, regex):
1345
1355
proc = subprocess.Popen(['attrib', relpath],
1346
1356
stdout=subprocess.PIPE,
1349
result = proc.stdout.read()
1350
self.assertContainsRe(result, regex)
1358
(result, err) = proc.communicate()
1359
self.assertContainsRe(result.replace('\r\n', '\n'), regex)
1352
1361
def test_prepare_files(self):
1353
1362
output = StringIO()
1376
1385
self.assertContainsRe(old_path, 'old/oldname$')
1377
1386
self.assertEqual(0, os.stat(old_path).st_mtime)
1378
self.assertContainsRe(new_path, 'new/newname$')
1387
self.assertContainsRe(new_path, 'tree/newname$')
1379
1388
self.assertFileEqual('oldcontent', old_path)
1380
1389
self.assertFileEqual('newcontent', new_path)
1381
1390
if osutils.host_os_dereferences_symlinks():
1382
1391
self.assertTrue(os.path.samefile('tree/newname', new_path))
1383
1392
# make sure we can create files with the same parent directories
1384
1393
diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1396
class TestGetTreesAndBranchesToDiff(TestCaseWithTransport):
1398
def test_basic(self):
1399
tree = self.make_branch_and_tree('tree')
1400
(old_tree, new_tree,
1401
old_branch, new_branch,
1402
specific_files, extra_trees) = \
1403
get_trees_and_branches_to_diff(['tree'], None, None, None)
1405
self.assertIsInstance(old_tree, RevisionTree)
1406
#print dir (old_tree)
1407
self.assertEqual(_mod_revision.NULL_REVISION, old_tree.get_revision_id())
1408
self.assertEqual(tree.basedir, new_tree.basedir)
1409
self.assertEqual(tree.branch.base, old_branch.base)
1410
self.assertEqual(tree.branch.base, new_branch.base)
1411
self.assertIs(None, specific_files)
1412
self.assertIs(None, extra_trees)
1414
def test_with_rev_specs(self):
1415
tree = self.make_branch_and_tree('tree')
1416
self.build_tree_contents([('tree/file', 'oldcontent')])
1417
tree.add('file', 'file-id')
1418
tree.commit('old tree', timestamp=0, rev_id="old-id")
1419
self.build_tree_contents([('tree/file', 'newcontent')])
1420
tree.commit('new tree', timestamp=0, rev_id="new-id")
1422
revisions = [RevisionSpec.from_string('1'),
1423
RevisionSpec.from_string('2')]
1424
(old_tree, new_tree,
1425
old_branch, new_branch,
1426
specific_files, extra_trees) = \
1427
get_trees_and_branches_to_diff(['tree'], revisions, None, None)
1429
self.assertIsInstance(old_tree, RevisionTree)
1430
self.assertEqual("old-id", old_tree.get_revision_id())
1431
self.assertIsInstance(new_tree, RevisionTree)
1432
self.assertEqual("new-id", new_tree.get_revision_id())
1433
self.assertEqual(tree.branch.base, old_branch.base)
1434
self.assertEqual(tree.branch.base, new_branch.base)
1435
self.assertIs(None, specific_files)
1436
self.assertEqual(tree.basedir, extra_trees[0].basedir)