~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_dirstate.py

  • Committer: Jelmer Vernooij
  • Date: 2011-05-10 07:46:15 UTC
  • mfrom: (5844 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5845.
  • Revision ID: jelmer@samba.org-20110510074615-eptod049ndjxc4i7
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
527
527
 
528
528
class TestDirStateOnFile(TestCaseWithDirState):
529
529
 
 
530
    def create_updated_dirstate(self):
 
531
        self.build_tree(['a-file'])
 
532
        tree = self.make_branch_and_tree('.')
 
533
        tree.add(['a-file'], ['a-id'])
 
534
        tree.commit('add a-file')
 
535
        # Save and unlock the state, re-open it in readonly mode
 
536
        state = dirstate.DirState.from_tree(tree, 'dirstate')
 
537
        state.save()
 
538
        state.unlock()
 
539
        state = dirstate.DirState.on_file('dirstate')
 
540
        state.lock_read()
 
541
        return state
 
542
 
530
543
    def test_construct_with_path(self):
531
544
        tree = self.make_branch_and_tree('tree')
532
545
        state = dirstate.DirState.from_tree(tree, 'dirstate.from_tree')
561
574
            state.unlock()
562
575
 
563
576
    def test_can_save_in_read_lock(self):
564
 
        self.build_tree(['a-file'])
565
 
        state = dirstate.DirState.initialize('dirstate')
566
 
        try:
567
 
            # No stat and no sha1 sum.
568
 
            state.add('a-file', 'a-file-id', 'file', None, '')
569
 
            state.save()
570
 
        finally:
571
 
            state.unlock()
572
 
 
573
 
        # Now open in readonly mode
574
 
        state = dirstate.DirState.on_file('dirstate')
575
 
        state.lock_read()
 
577
        state = self.create_updated_dirstate()
576
578
        try:
577
579
            entry = state._get_entry(0, path_utf8='a-file')
578
580
            # The current size should be 0 (default)
579
581
            self.assertEqual(0, entry[1][0][2])
580
582
            # We should have a real entry.
581
583
            self.assertNotEqual((None, None), entry)
582
 
            # Make sure everything is old enough
 
584
            # Set the cutoff-time into the future, so things look cacheable
583
585
            state._sha_cutoff_time()
584
 
            state._cutoff_time += 10
585
 
            # Change the file length
586
 
            self.build_tree_contents([('a-file', 'shorter')])
587
 
            sha1sum = dirstate.update_entry(state, entry, 'a-file',
588
 
                os.lstat('a-file'))
589
 
            # new file, no cached sha:
590
 
            self.assertEqual(None, sha1sum)
 
586
            state._cutoff_time += 10.0
 
587
            st = os.lstat('a-file')
 
588
            sha1sum = dirstate.update_entry(state, entry, 'a-file', st)
 
589
            # We updated the current sha1sum because the file is cacheable
 
590
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
591
                             sha1sum)
591
592
 
592
593
            # The dirblock has been updated
593
 
            self.assertEqual(7, entry[1][0][2])
594
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
594
            self.assertEqual(st.st_size, entry[1][0][2])
 
595
            self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
595
596
                             state._dirblock_state)
596
597
 
597
598
            del entry
606
607
        state.lock_read()
607
608
        try:
608
609
            entry = state._get_entry(0, path_utf8='a-file')
609
 
            self.assertEqual(7, entry[1][0][2])
 
610
            self.assertEqual(st.st_size, entry[1][0][2])
610
611
        finally:
611
612
            state.unlock()
612
613
 
613
614
    def test_save_fails_quietly_if_locked(self):
614
615
        """If dirstate is locked, save will fail without complaining."""
615
 
        self.build_tree(['a-file'])
616
 
        state = dirstate.DirState.initialize('dirstate')
617
 
        try:
618
 
            # No stat and no sha1 sum.
619
 
            state.add('a-file', 'a-file-id', 'file', None, '')
620
 
            state.save()
621
 
        finally:
622
 
            state.unlock()
623
 
 
624
 
        state = dirstate.DirState.on_file('dirstate')
625
 
        state.lock_read()
 
616
        state = self.create_updated_dirstate()
626
617
        try:
627
618
            entry = state._get_entry(0, path_utf8='a-file')
628
 
            sha1sum = dirstate.update_entry(state, entry, 'a-file',
629
 
                os.lstat('a-file'))
630
 
            # No sha - too new
631
 
            self.assertEqual(None, sha1sum)
632
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
619
            # No cached sha1 yet.
 
620
            self.assertEqual('', entry[1][0][1])
 
621
            # Set the cutoff-time into the future, so things look cacheable
 
622
            state._sha_cutoff_time()
 
623
            state._cutoff_time += 10.0
 
624
            st = os.lstat('a-file')
 
625
            sha1sum = dirstate.update_entry(state, entry, 'a-file', st)
 
626
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
627
                             sha1sum)
 
628
            self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
633
629
                             state._dirblock_state)
634
630
 
635
631
            # Now, before we try to save, grab another dirstate, and take out a
1343
1339
            tree1.unlock()
1344
1340
 
1345
1341
 
 
1342
class TestDirStateHashUpdates(TestCaseWithDirState):
 
1343
 
 
1344
    def do_update_entry(self, state, path):
 
1345
        entry = state._get_entry(0, path_utf8=path)
 
1346
        stat = os.lstat(path)
 
1347
        return dirstate.update_entry(state, entry, os.path.abspath(path), stat)
 
1348
 
 
1349
    def test_worth_saving_limit_avoids_writing(self):
 
1350
        tree = self.make_branch_and_tree('.')
 
1351
        self.build_tree(['c', 'd'])
 
1352
        tree.lock_write()
 
1353
        tree.add(['c', 'd'], ['c-id', 'd-id'])
 
1354
        tree.commit('add c and d')
 
1355
        state = InstrumentedDirState.on_file(tree.current_dirstate()._filename,
 
1356
                                             worth_saving_limit=2)
 
1357
        tree.unlock()
 
1358
        state.lock_write()
 
1359
        self.addCleanup(state.unlock)
 
1360
        state._read_dirblocks_if_needed()
 
1361
        state.adjust_time(+20) # Allow things to be cached
 
1362
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1363
                         state._dirblock_state)
 
1364
        f = open(state._filename, 'rb')
 
1365
        try:
 
1366
            content = f.read()
 
1367
        finally:
 
1368
            f.close()
 
1369
        self.do_update_entry(state, 'c')
 
1370
        self.assertEqual(1, len(state._known_hash_changes))
 
1371
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
1372
                         state._dirblock_state)
 
1373
        state.save()
 
1374
        # It should not have set the state to IN_MEMORY_UNMODIFIED because the
 
1375
        # hash values haven't been written out.
 
1376
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
1377
                         state._dirblock_state)
 
1378
        self.assertFileEqual(content, state._filename)
 
1379
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
1380
                         state._dirblock_state)
 
1381
        self.do_update_entry(state, 'd')
 
1382
        self.assertEqual(2, len(state._known_hash_changes))
 
1383
        state.save()
 
1384
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1385
                         state._dirblock_state)
 
1386
        self.assertEqual(0, len(state._known_hash_changes))
 
1387
 
 
1388
 
1346
1389
class TestGetLines(TestCaseWithDirState):
1347
1390
 
1348
1391
    def test_get_line_with_2_rows(self):
1741
1784
class InstrumentedDirState(dirstate.DirState):
1742
1785
    """An DirState with instrumented sha1 functionality."""
1743
1786
 
1744
 
    def __init__(self, path, sha1_provider):
1745
 
        super(InstrumentedDirState, self).__init__(path, sha1_provider)
 
1787
    def __init__(self, path, sha1_provider, worth_saving_limit=0):
 
1788
        super(InstrumentedDirState, self).__init__(path, sha1_provider,
 
1789
            worth_saving_limit=worth_saving_limit)
1746
1790
        self._time_offset = 0
1747
1791
        self._log = []
1748
1792
        # member is dynamically set in DirState.__init__ to turn on trace