Rev 2699: Add records_written attribute to ContainerWriter's. (Robert Collins). in http://people.ubuntu.com/~robertc/baz2.0/pack

Robert Collins robertc at robertcollins.net
Wed Aug 15 02:13:00 BST 2007


At http://people.ubuntu.com/~robertc/baz2.0/pack

------------------------------------------------------------
revno: 2699
revision-id: robertc at robertcollins.net-20070815011257-kxmspgdwk2l36h0x
parent: pqm at pqm.ubuntu.com-20070814221506-6rw0b0oolfdeqrdw
committer: Robert Collins <robertc at robertcollins.net>
branch nick: pack
timestamp: Wed 2007-08-15 11:12:57 +1000
message:
  Add records_written attribute to ContainerWriter's. (Robert Collins).
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/pack.py                 container.py-20070607160755-tr8zc26q18rn0jnb-1
  bzrlib/tests/test_pack.py      test_container.py-20070607160755-tr8zc26q18rn0jnb-2
=== modified file 'NEWS'
--- a/NEWS	2007-08-14 19:29:56 +0000
+++ b/NEWS	2007-08-15 01:12:57 +0000
@@ -2,6 +2,11 @@
 
   BUGFIXES:
 
+  INTERNALS:
+
+    * ``bzrlib.pack.ContainerWriter`` now tracks how many records have been
+      added via a public attribute records_written. (Robert Collins)
+
 bzr 0.90rc1 2007-08-14
 
   BUGFIXES:

=== modified file 'bzrlib/pack.py'
--- a/bzrlib/pack.py	2007-08-08 02:57:22 +0000
+++ b/bzrlib/pack.py	2007-08-15 01:12:57 +0000
@@ -59,7 +59,12 @@
 
 
 class ContainerWriter(object):
-    """A class for writing containers."""
+    """A class for writing containers.
+
+    :attribute records_written: The number of user records added to the
+        container. This does not count the prelude or suffix of the container
+        introduced by the begin() and end() methods.
+    """
 
     def __init__(self, write_func):
         """Constructor.
@@ -69,6 +74,7 @@
         """
         self._write_func = write_func
         self.current_offset = 0
+        self.records_written = 0
 
     def begin(self):
         """Begin writing a container."""
@@ -112,6 +118,7 @@
         self.write_func("\n")
         # Finally, the contents.
         self.write_func(bytes)
+        self.records_written += 1
         # return a memo of where we wrote data to allow random access.
         return current_offset, self.current_offset - current_offset
 

=== modified file 'bzrlib/tests/test_pack.py'
--- a/bzrlib/tests/test_pack.py	2007-08-08 02:57:22 +0000
+++ b/bzrlib/tests/test_pack.py	2007-08-15 01:12:57 +0000
@@ -40,6 +40,13 @@
         self.assertEqual('Bazaar pack format 1 (introduced in 0.18)\n',
                          output.getvalue())
 
+    def test_zero_records_written_after_begin(self):
+        """After begin is written, 0 records have been written."""
+        output = StringIO()
+        writer = pack.ContainerWriter(output.write)
+        writer.begin()
+        self.assertEqual(0, writer.records_written)
+
     def test_end(self):
         """The end() method writes an End Marker record."""
         output = StringIO()
@@ -49,6 +56,23 @@
         self.assertEqual('Bazaar pack format 1 (introduced in 0.18)\nE',
                          output.getvalue())
 
+    def test_empty_end_does_not_add_a_record_to_records_written(self):
+        """The end() method does not count towards the records written."""
+        output = StringIO()
+        writer = pack.ContainerWriter(output.write)
+        writer.begin()
+        writer.end()
+        self.assertEqual(0, writer.records_written)
+
+    def test_non_empty_end_does_not_add_a_record_to_records_written(self):
+        """The end() method does not count towards the records written."""
+        output = StringIO()
+        writer = pack.ContainerWriter(output.write)
+        writer.begin()
+        writer.add_bytes_record('foo', names=[])
+        writer.end()
+        self.assertEqual(1, writer.records_written)
+
     def test_add_bytes_record_no_name(self):
         """Add a bytes record with no name."""
         output = StringIO()
@@ -131,6 +155,16 @@
             errors.InvalidRecordError,
             writer.add_bytes_record, 'abc', names=[('bad name', )])
 
+    def test_add_bytes_records_add_to_records_written(self):
+        """Adding a Bytes record increments the records_written counter."""
+        output = StringIO()
+        writer = pack.ContainerWriter(output.write)
+        writer.begin()
+        writer.add_bytes_record('foo', names=[])
+        self.assertEqual(1, writer.records_written)
+        writer.add_bytes_record('foo', names=[])
+        self.assertEqual(2, writer.records_written)
+
 
 class TestContainerReader(tests.TestCase):
 



More information about the bazaar-commits mailing list