621
662
self.assertEqual((set(['e']), set(['f', 'g'])),
622
663
graph.find_difference('e', 'f'))
624
666
def test_stacked_parents_provider(self):
625
667
parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
626
668
parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
627
stacked = _mod_graph._StackedParentsProvider([parents1, parents2])
669
stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
670
self.assertEqual({'rev1':['rev4'], 'rev2':['rev3']},
671
stacked.get_parent_map(['rev1', 'rev2']))
672
self.assertEqual({'rev2':['rev3'], 'rev1':['rev4']},
673
stacked.get_parent_map(['rev2', 'rev1']))
674
self.assertEqual({'rev2':['rev3']},
675
stacked.get_parent_map(['rev2', 'rev2']))
676
self.assertEqual({'rev1':['rev4']},
677
stacked.get_parent_map(['rev1', 'rev1']))
679
def test_stacked_parents_provider_overlapping(self):
680
# rev2 is availible in both providers.
684
parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev1']})
685
parents2 = _mod_graph.DictParentsProvider({'rev2': ['rev1']})
686
stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
687
self.assertEqual({'rev2': ['rev1']},
688
stacked.get_parent_map(['rev2']))
690
def test__stacked_parents_provider_deprecated(self):
691
parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
692
parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
693
stacked = self.applyDeprecated(deprecated_in((1, 16, 0)),
694
_mod_graph._StackedParentsProvider, [parents1, parents2])
628
695
self.assertEqual({'rev1':['rev4'], 'rev2':['rev3']},
629
696
stacked.get_parent_map(['rev1', 'rev2']))
630
697
self.assertEqual({'rev2':['rev3'], 'rev1':['rev4']},
1228
1322
['h', 'i', 'j', 'y'], 'j', ['z'])
1325
class TestGraphFindDistanceToNull(TestGraphBase):
1326
"""Test an api that should be able to compute a revno"""
1328
def assertFindDistance(self, revno, graph, target_id, known_ids):
1329
"""Assert the output of Graph.find_distance_to_null()"""
1330
actual = graph.find_distance_to_null(target_id, known_ids)
1331
self.assertEqual(revno, actual)
1333
def test_nothing_known(self):
1334
graph = self.make_graph(ancestry_1)
1335
self.assertFindDistance(0, graph, NULL_REVISION, [])
1336
self.assertFindDistance(1, graph, 'rev1', [])
1337
self.assertFindDistance(2, graph, 'rev2a', [])
1338
self.assertFindDistance(2, graph, 'rev2b', [])
1339
self.assertFindDistance(3, graph, 'rev3', [])
1340
self.assertFindDistance(4, graph, 'rev4', [])
1342
def test_rev_is_ghost(self):
1343
graph = self.make_graph(ancestry_1)
1344
e = self.assertRaises(errors.GhostRevisionsHaveNoRevno,
1345
graph.find_distance_to_null, 'rev_missing', [])
1346
self.assertEqual('rev_missing', e.revision_id)
1347
self.assertEqual('rev_missing', e.ghost_revision_id)
1349
def test_ancestor_is_ghost(self):
1350
graph = self.make_graph({'rev':['parent']})
1351
e = self.assertRaises(errors.GhostRevisionsHaveNoRevno,
1352
graph.find_distance_to_null, 'rev', [])
1353
self.assertEqual('rev', e.revision_id)
1354
self.assertEqual('parent', e.ghost_revision_id)
1356
def test_known_in_ancestry(self):
1357
graph = self.make_graph(ancestry_1)
1358
self.assertFindDistance(2, graph, 'rev2a', [('rev1', 1)])
1359
self.assertFindDistance(3, graph, 'rev3', [('rev2a', 2)])
1361
def test_known_in_ancestry_limits(self):
1362
graph = self.make_breaking_graph(ancestry_1, ['rev1'])
1363
self.assertFindDistance(4, graph, 'rev4', [('rev3', 3)])
1365
def test_target_is_ancestor(self):
1366
graph = self.make_graph(ancestry_1)
1367
self.assertFindDistance(2, graph, 'rev2a', [('rev3', 3)])
1369
def test_target_is_ancestor_limits(self):
1370
"""We shouldn't search all history if we run into ourselves"""
1371
graph = self.make_breaking_graph(ancestry_1, ['rev1'])
1372
self.assertFindDistance(3, graph, 'rev3', [('rev4', 4)])
1374
def test_target_parallel_to_known_limits(self):
1375
# Even though the known revision isn't part of the other ancestry, they
1376
# eventually converge
1377
graph = self.make_breaking_graph(with_tail, ['a'])
1378
self.assertFindDistance(6, graph, 'f', [('g', 6)])
1379
self.assertFindDistance(7, graph, 'h', [('g', 6)])
1380
self.assertFindDistance(8, graph, 'i', [('g', 6)])
1381
self.assertFindDistance(6, graph, 'g', [('i', 8)])
1384
class TestFindMergeOrder(TestGraphBase):
1386
def assertMergeOrder(self, expected, graph, tip, base_revisions):
1387
self.assertEqual(expected, graph.find_merge_order(tip, base_revisions))
1389
def test_parents(self):
1390
graph = self.make_graph(ancestry_1)
1391
self.assertMergeOrder(['rev3', 'rev2b'], graph, 'rev4',
1393
self.assertMergeOrder(['rev3', 'rev2b'], graph, 'rev4',
1396
def test_ancestors(self):
1397
graph = self.make_graph(ancestry_1)
1398
self.assertMergeOrder(['rev1', 'rev2b'], graph, 'rev4',
1400
self.assertMergeOrder(['rev1', 'rev2b'], graph, 'rev4',
1403
def test_shortcut_one_ancestor(self):
1404
# When we have enough info, we can stop searching
1405
graph = self.make_breaking_graph(ancestry_1, ['rev3', 'rev2b', 'rev4'])
1406
# Single ancestors shortcut right away
1407
self.assertMergeOrder(['rev3'], graph, 'rev4', ['rev3'])
1409
def test_shortcut_after_one_ancestor(self):
1410
graph = self.make_breaking_graph(ancestry_1, ['rev2a', 'rev2b'])
1411
self.assertMergeOrder(['rev3', 'rev1'], graph, 'rev4', ['rev1', 'rev3'])
1231
1414
class TestCachingParentsProvider(tests.TestCase):
1415
"""These tests run with:
1417
self.inst_pp, a recording parents provider with a graph of a->b, and b is a
1419
self.caching_pp, a CachingParentsProvider layered on inst_pp.
1233
1422
def setUp(self):
1234
1423
super(TestCachingParentsProvider, self).setUp()
1270
1458
# Use sorted because we don't care about the order, just that each is
1271
1459
# only present 1 time.
1272
1460
self.assertEqual(['a', 'b'], sorted(self.inst_pp.calls))
1462
def test_note_missing_key(self):
1463
"""After noting that a key is missing it is cached."""
1464
self.caching_pp.note_missing_key('b')
1465
self.assertEqual({}, self.caching_pp.get_parent_map(['b']))
1466
self.assertEqual([], self.inst_pp.calls)
1467
self.assertEqual(set(['b']), self.caching_pp.missing_keys)
1470
class TestCachingParentsProviderExtras(tests.TestCaseWithTransport):
1471
"""Test the behaviour when parents are provided that were not requested."""
1474
super(TestCachingParentsProviderExtras, self).setUp()
1475
class ExtraParentsProvider(object):
1477
def get_parent_map(self, keys):
1478
return {'rev1': [], 'rev2': ['rev1',]}
1480
self.inst_pp = InstrumentedParentsProvider(ExtraParentsProvider())
1481
self.caching_pp = _mod_graph.CachingParentsProvider(
1482
get_parent_map=self.inst_pp.get_parent_map)
1484
def test_uncached(self):
1485
self.caching_pp.disable_cache()
1486
self.assertEqual({'rev1': []},
1487
self.caching_pp.get_parent_map(['rev1']))
1488
self.assertEqual(['rev1'], self.inst_pp.calls)
1489
self.assertIs(None, self.caching_pp._cache)
1491
def test_cache_initially_empty(self):
1492
self.assertEqual({}, self.caching_pp._cache)
1494
def test_cached(self):
1495
self.assertEqual({'rev1': []},
1496
self.caching_pp.get_parent_map(['rev1']))
1497
self.assertEqual(['rev1'], self.inst_pp.calls)
1498
self.assertEqual({'rev1': [], 'rev2': ['rev1']},
1499
self.caching_pp._cache)
1500
self.assertEqual({'rev1': []},
1501
self.caching_pp.get_parent_map(['rev1']))
1502
self.assertEqual(['rev1'], self.inst_pp.calls)
1504
def test_disable_cache_clears_cache(self):
1505
# Put something in the cache
1506
self.caching_pp.get_parent_map(['rev1'])
1507
self.assertEqual(2, len(self.caching_pp._cache))
1508
self.caching_pp.disable_cache()
1509
self.assertIs(None, self.caching_pp._cache)
1511
def test_enable_cache_raises(self):
1512
e = self.assertRaises(AssertionError, self.caching_pp.enable_cache)
1513
self.assertEqual('Cache enabled when already enabled.', str(e))
1515
def test_cache_misses(self):
1516
self.caching_pp.get_parent_map(['rev3'])
1517
self.caching_pp.get_parent_map(['rev3'])
1518
self.assertEqual(['rev3'], self.inst_pp.calls)
1520
def test_no_cache_misses(self):
1521
self.caching_pp.disable_cache()
1522
self.caching_pp.enable_cache(cache_misses=False)
1523
self.caching_pp.get_parent_map(['rev3'])
1524
self.caching_pp.get_parent_map(['rev3'])
1525
self.assertEqual(['rev3', 'rev3'], self.inst_pp.calls)
1527
def test_cache_extras(self):
1528
self.assertEqual({}, self.caching_pp.get_parent_map(['rev3']))
1529
self.assertEqual({'rev2': ['rev1']},
1530
self.caching_pp.get_parent_map(['rev2']))
1531
self.assertEqual(['rev3'], self.inst_pp.calls)
1534
class TestCollapseLinearRegions(tests.TestCase):
1536
def assertCollapsed(self, collapsed, original):
1537
self.assertEqual(collapsed,
1538
_mod_graph.collapse_linear_regions(original))
1540
def test_collapse_nothing(self):
1541
d = {1:[2, 3], 2:[], 3:[]}
1542
self.assertCollapsed(d, d)
1543
d = {1:[2], 2:[3, 4], 3:[5], 4:[5], 5:[]}
1544
self.assertCollapsed(d, d)
1546
def test_collapse_chain(self):
1547
# Any time we have a linear chain, we should be able to collapse
1548
d = {1:[2], 2:[3], 3:[4], 4:[5], 5:[]}
1549
self.assertCollapsed({1:[5], 5:[]}, d)
1550
d = {5:[4], 4:[3], 3:[2], 2:[1], 1:[]}
1551
self.assertCollapsed({5:[1], 1:[]}, d)
1552
d = {5:[3], 3:[4], 4:[1], 1:[2], 2:[]}
1553
self.assertCollapsed({5:[2], 2:[]}, d)
1555
def test_collapse_with_multiple_children(self):
1566
# 4 and 5 cannot be removed because 6 has 2 children
1567
# 2 and 3 cannot be removed because 1 has 2 parents
1568
d = {1:[2, 3], 2:[4], 4:[6], 3:[5], 5:[6], 6:[7], 7:[]}
1569
self.assertCollapsed(d, d)
1572
class TestPendingAncestryResultGetKeys(TestCaseWithMemoryTransport):
1573
"""Tests for bzrlib.graph.PendingAncestryResult."""
1575
def test_get_keys(self):
1576
builder = self.make_branch_builder('b')
1577
builder.start_series()
1578
builder.build_snapshot('rev-1', None, [
1579
('add', ('', 'root-id', 'directory', ''))])
1580
builder.build_snapshot('rev-2', ['rev-1'], [])
1581
builder.finish_series()
1582
repo = builder.get_branch().repository
1584
self.addCleanup(repo.unlock)
1585
result = _mod_graph.PendingAncestryResult(['rev-2'], repo)
1586
self.assertEqual(set(['rev-1', 'rev-2']), set(result.get_keys()))
1588
def test_get_keys_excludes_ghosts(self):
1589
builder = self.make_branch_builder('b')
1590
builder.start_series()
1591
builder.build_snapshot('rev-1', None, [
1592
('add', ('', 'root-id', 'directory', ''))])
1593
builder.build_snapshot('rev-2', ['rev-1', 'ghost'], [])
1594
builder.finish_series()
1595
repo = builder.get_branch().repository
1597
self.addCleanup(repo.unlock)
1598
result = _mod_graph.PendingAncestryResult(['rev-2'], repo)
1599
self.assertEqual(sorted(['rev-1', 'rev-2']), sorted(result.get_keys()))
1601
def test_get_keys_excludes_null(self):
1602
# Make a 'graph' with an iter_ancestry that returns NULL_REVISION
1603
# somewhere other than the last element, which can happen in real
1605
class StubGraph(object):
1606
def iter_ancestry(self, keys):
1607
return [(NULL_REVISION, ()), ('foo', (NULL_REVISION,))]
1608
result = _mod_graph.PendingAncestryResult(['rev-3'], None)
1609
result_keys = result._get_keys(StubGraph())
1610
# Only the non-null keys from the ancestry appear.
1611
self.assertEqual(set(['foo']), set(result_keys))
1614
class TestPendingAncestryResultRefine(TestGraphBase):
1616
def test_refine(self):
1617
# Used when pulling from a stacked repository, so test some revisions
1618
# being satisfied from the stacking branch.
1619
g = self.make_graph(
1620
{"tip":["mid"], "mid":["base"], "tag":["base"],
1621
"base":[NULL_REVISION], NULL_REVISION:[]})
1622
result = _mod_graph.PendingAncestryResult(['tip', 'tag'], None)
1623
result = result.refine(set(['tip']), set(['mid']))
1624
self.assertEqual(set(['mid', 'tag']), result.heads)
1625
result = result.refine(set(['mid', 'tag', 'base']),
1626
set([NULL_REVISION]))
1627
self.assertEqual(set([NULL_REVISION]), result.heads)
1628
self.assertTrue(result.is_empty())
1631
class TestSearchResultRefine(TestGraphBase):
1633
def test_refine(self):
1634
# Used when pulling from a stacked repository, so test some revisions
1635
# being satisfied from the stacking branch.
1636
g = self.make_graph(
1637
{"tip":["mid"], "mid":["base"], "tag":["base"],
1638
"base":[NULL_REVISION], NULL_REVISION:[]})
1639
result = _mod_graph.SearchResult(set(['tip', 'tag']),
1640
set([NULL_REVISION]), 4, set(['tip', 'mid', 'tag', 'base']))
1641
result = result.refine(set(['tip']), set(['mid']))
1642
recipe = result.get_recipe()
1643
# We should be starting from tag (original head) and mid (seen ref)
1644
self.assertEqual(set(['mid', 'tag']), recipe[1])
1645
# We should be stopping at NULL (original stop) and tip (seen head)
1646
self.assertEqual(set([NULL_REVISION, 'tip']), recipe[2])
1647
self.assertEqual(3, recipe[3])
1648
result = result.refine(set(['mid', 'tag', 'base']),
1649
set([NULL_REVISION]))
1650
recipe = result.get_recipe()
1651
# We should be starting from nothing (NULL was known as a cut point)
1652
self.assertEqual(set([]), recipe[1])
1653
# We should be stopping at NULL (original stop) and tip (seen head) and
1654
# tag (seen head) and mid(seen mid-point head). We could come back and
1655
# define this as not including mid, for minimal results, but it is
1656
# still 'correct' to include mid, and simpler/easier.
1657
self.assertEqual(set([NULL_REVISION, 'tip', 'tag', 'mid']), recipe[2])
1658
self.assertEqual(0, recipe[3])
1659
self.assertTrue(result.is_empty())