Rev 2484: Add DirState._validate and call from the tests in http://sourcefrog.net/bzr/dirstate-robert

Martin Pool mbp at sourcefrog.net
Thu Mar 1 12:05:31 GMT 2007


At http://sourcefrog.net/bzr/dirstate-robert

------------------------------------------------------------
revno: 2484
revision-id: mbp at sourcefrog.net-20070301120450-umm5g6q0xqqt7296
parent: robertc at robertcollins.net-20070301115646-t40d51cvqsrok6b5
committer: Martin Pool <mbp at sourcefrog.net>
branch nick: dirstate-robert
timestamp: Thu 2007-03-01 23:04:50 +1100
message:
  Add DirState._validate and call from the tests
modified:
  bzrlib/dirstate.py             dirstate.py-20060728012006-d6mvoihjb3je9peu-1
  bzrlib/tests/test_dirstate.py  test_dirstate.py-20060728012006-d6mvoihjb3je9peu-2
=== modified file 'bzrlib/dirstate.py'
--- a/bzrlib/dirstate.py	2007-03-01 11:50:27 +0000
+++ b/bzrlib/dirstate.py	2007-03-01 12:04:50 +0000
@@ -1955,6 +1955,28 @@
 
         self._dirblock_state = DirState.IN_MEMORY_MODIFIED
 
+    def _validate(self):
+        """Check that invariants on the dirblock are correct.
+
+        This can be useful in debugging; it shouldn't be necessary in 
+        normal code.
+        """
+        from pprint import pformat
+        if len(self._dirblocks) > 0:
+            assert self._dirblocks[0][0] == '', \
+                    "dirblocks don't start with root block:\n" + \
+                    pformat(dirblocks)
+        if len(self._dirblocks) > 1:
+            assert self._dirblocks[1][0] == '', \
+                    "dirblocks missing root directory:\n" + \
+                    pformat(dirblocks)
+        assert self._dirblocks[1:] == sorted(self._dirblocks[1:]), \
+                "dirblocks are not in sorted order:\n" + \
+                pformat(self._dirblocks)
+        for dirblock in self._dirblocks:
+            assert dirblock[1] == sorted(dirblock[1]), \
+                "dirblock for %r is not sorted:\n%s" % \
+                (dirblock[0], pformat(dirblock))
 
     def _wipe_state(self):
         """Forget all state information about the dirstate."""

=== modified file 'bzrlib/tests/test_dirstate.py'
--- a/bzrlib/tests/test_dirstate.py	2007-03-01 08:33:25 +0000
+++ b/bzrlib/tests/test_dirstate.py	2007-03-01 12:04:50 +0000
@@ -75,6 +75,7 @@
         state = self.create_empty_dirstate()
         try:
             state._set_data([], dirblocks)
+            state._validate()
         except:
             state.unlock()
             raise
@@ -146,6 +147,7 @@
         dirblocks.append(('a', [e_entry, f_entry]))
         dirblocks.append(('b', [g_entry, h_entry]))
         state = dirstate.DirState.initialize('dirstate')
+        state._validate()
         try:
             state._set_data([], dirblocks)
         except:
@@ -197,6 +199,7 @@
              [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
              ])])
         state = dirstate.DirState.from_tree(tree, 'dirstate')
+        state._validate()
         self.check_state_with_reopen(expected_result, state)
 
     def test_1_parents_empty_to_dirstate(self):
@@ -211,6 +214,7 @@
              ])])
         state = dirstate.DirState.from_tree(tree, 'dirstate')
         self.check_state_with_reopen(expected_result, state)
+        state._validate()
 
     def test_2_parents_empty_to_dirstate(self):
         # create a parent by doing a commit
@@ -227,6 +231,7 @@
              ])])
         state = dirstate.DirState.from_tree(tree, 'dirstate')
         self.check_state_with_reopen(expected_result, state)
+        state._validate()
 
     def test_empty_unknowns_are_ignored_to_dirstate(self):
         """We should be able to create a dirstate for an empty tree."""
@@ -418,6 +423,7 @@
         finally:
             state.unlock()
         state = dirstate.DirState.on_file('dirstate')
+        state._validate()
         state.lock_read()
         try:
             self.assertEqual(expected_rows, list(state._iter_entries()))
@@ -432,6 +438,7 @@
         mt.commit('foo', rev_id='parent-revid')
         rt = mt.branch.repository.revision_tree('parent-revid')
         state = dirstate.DirState.initialize('dirstate')
+        state._validate()
         try:
             state.set_parent_trees([('parent-revid', rt)], ghosts=[])
             state.set_path_id('', 'foobarbaz')
@@ -486,7 +493,9 @@
                 ['ghost-rev'])
             # check we can reopen and use the dirstate after setting parent
             # trees.
+            state._validate()
             state.save()
+            state._validate()
         finally:
             state.unlock()
         state = dirstate.DirState.on_file('dirstate')
@@ -645,6 +654,7 @@
             state.unlock()
         state = dirstate.DirState.on_file('dirstate')
         state.lock_read()
+        state._validate()
         try:
             self.assertEqual(expected_entries, list(state._iter_entries()))
         finally:




More information about the bazaar-commits mailing list