/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_dirstate.py

Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
    osutils,
27
27
    )
28
28
from bzrlib.memorytree import MemoryTree
29
 
from bzrlib.tests import TestCase, TestCaseWithTransport
 
29
from bzrlib.osutils import has_symlinks
 
30
from bzrlib.tests import (
 
31
        TestCase,
 
32
        TestCaseWithTransport,
 
33
        TestSkipped,
 
34
        )
30
35
 
31
36
 
32
37
# TODO:
362
367
        finally:
363
368
            state.unlock()
364
369
 
 
370
    def test_can_save_in_read_lock(self):
 
371
        self.build_tree(['a-file'])
 
372
        state = dirstate.DirState.initialize('dirstate')
 
373
        try:
 
374
            # No stat and no sha1 sum.
 
375
            state.add('a-file', 'a-file-id', 'file', None, '')
 
376
            state.save()
 
377
        finally:
 
378
            state.unlock()
 
379
 
 
380
        # Now open in readonly mode
 
381
        state = dirstate.DirState.on_file('dirstate')
 
382
        state.lock_read()
 
383
        try:
 
384
            entry = state._get_entry(0, path_utf8='a-file')
 
385
            # The current sha1 sum should be empty
 
386
            self.assertEqual('', entry[1][0][1])
 
387
            # We should have a real entry.
 
388
            self.assertNotEqual((None, None), entry)
 
389
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
390
            # We should have gotten a real sha1
 
391
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
392
                             sha1sum)
 
393
 
 
394
            # The dirblock has been updated
 
395
            self.assertEqual(sha1sum, entry[1][0][1])
 
396
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
397
                             state._dirblock_state)
 
398
 
 
399
            del entry
 
400
            # Now, since we are the only one holding a lock, we should be able
 
401
            # to save and have it written to disk
 
402
            state.save()
 
403
        finally:
 
404
            state.unlock()
 
405
 
 
406
        # Re-open the file, and ensure that the state has been updated.
 
407
        state = dirstate.DirState.on_file('dirstate')
 
408
        state.lock_read()
 
409
        try:
 
410
            entry = state._get_entry(0, path_utf8='a-file')
 
411
            self.assertEqual(sha1sum, entry[1][0][1])
 
412
        finally:
 
413
            state.unlock()
 
414
 
 
415
    def test_save_fails_quietly_if_locked(self):
 
416
        """If dirstate is locked, save will fail without complaining."""
 
417
        self.build_tree(['a-file'])
 
418
        state = dirstate.DirState.initialize('dirstate')
 
419
        try:
 
420
            # No stat and no sha1 sum.
 
421
            state.add('a-file', 'a-file-id', 'file', None, '')
 
422
            state.save()
 
423
        finally:
 
424
            state.unlock()
 
425
 
 
426
        state = dirstate.DirState.on_file('dirstate')
 
427
        state.lock_read()
 
428
        try:
 
429
            entry = state._get_entry(0, path_utf8='a-file')
 
430
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
431
            # We should have gotten a real sha1
 
432
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
433
                             sha1sum)
 
434
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
435
                             state._dirblock_state)
 
436
 
 
437
            # Now, before we try to save, grab another dirstate, and take out a
 
438
            # read lock.
 
439
            # TODO: jam 20070315 Ideally this would be locked by another
 
440
            #       process. To make sure the file is really OS locked.
 
441
            state2 = dirstate.DirState.on_file('dirstate')
 
442
            state2.lock_read()
 
443
            try:
 
444
                # This won't actually write anything, because it couldn't grab
 
445
                # a write lock. But it shouldn't raise an error, either.
 
446
                # TODO: jam 20070315 We should probably distinguish between
 
447
                #       being dirty because of 'update_entry'. And dirty
 
448
                #       because of real modification. So that save() *does*
 
449
                #       raise a real error if it fails when we have real
 
450
                #       modifications.
 
451
                state.save()
 
452
            finally:
 
453
                state2.unlock()
 
454
        finally:
 
455
            state.unlock()
 
456
        
 
457
        # The file on disk should not be modified.
 
458
        state = dirstate.DirState.on_file('dirstate')
 
459
        state.lock_read()
 
460
        try:
 
461
            entry = state._get_entry(0, path_utf8='a-file')
 
462
            self.assertEqual('', entry[1][0][1])
 
463
        finally:
 
464
            state.unlock()
 
465
 
365
466
 
366
467
class TestDirStateInitialize(TestCaseWithDirState):
367
468
 
684
785
    def test_add_symlink_to_root_no_parents_all_data(self):
685
786
        # The most trivial addition of a symlink when there are no parents and
686
787
        # its in the root and all data about the file is supplied
687
 
        ## TODO: windows: dont fail this test. Also, how are symlinks meant to
688
 
        # be represented on windows.
 
788
        # bzr doesn't support fake symlinks on windows, yet.
 
789
        if not has_symlinks():
 
790
            raise TestSkipped("No symlink support")
689
791
        os.symlink('target', 'a link')
690
792
        stat = os.lstat('a link')
691
793
        expected_entries = [
1201
1303
    def test_update_entry_symlink(self):
1202
1304
        """Update entry should read symlinks."""
1203
1305
        if not osutils.has_symlinks():
1204
 
            return # PlatformDeficiency / TestSkipped
 
1306
            # PlatformDeficiency / TestSkipped
 
1307
            raise TestSkipped("No symlink support")
1205
1308
        state, entry = self.get_state_with_a()
1206
1309
        state.save()
1207
1310
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1286
1389
        This should not be called if this platform does not have symlink
1287
1390
        support.
1288
1391
        """
 
1392
        # caller should care about skipping test on platforms without symlinks
1289
1393
        os.symlink('path/to/foo', 'a')
1290
1394
 
1291
1395
        stat_value = os.lstat('a')
1320
1424
 
1321
1425
    def test_update_missing_symlink(self):
1322
1426
        if not osutils.has_symlinks():
1323
 
            return # PlatformDeficiency / TestSkipped
 
1427
            # PlatformDeficiency / TestSkipped
 
1428
            raise TestSkipped("No symlink support")
1324
1429
        state, entry = self.get_state_with_a()
1325
1430
        packed_stat = self.create_and_test_symlink(state, entry)
1326
1431
        os.remove('a')
1341
1446
    def test_update_file_to_symlink(self):
1342
1447
        """File becomes a symlink"""
1343
1448
        if not osutils.has_symlinks():
1344
 
            return # PlatformDeficiency / TestSkipped
 
1449
            # PlatformDeficiency / TestSkipped
 
1450
            raise TestSkipped("No symlink support")
1345
1451
        state, entry = self.get_state_with_a()
1346
1452
        self.create_and_test_file(state, entry)
1347
1453
        os.remove('a')
1357
1463
    def test_update_dir_to_symlink(self):
1358
1464
        """Directory becomes a symlink"""
1359
1465
        if not osutils.has_symlinks():
1360
 
            return # PlatformDeficiency / TestSkipped
 
1466
            # PlatformDeficiency / TestSkipped
 
1467
            raise TestSkipped("No symlink support")
1361
1468
        state, entry = self.get_state_with_a()
1362
1469
        self.create_and_test_dir(state, entry)
1363
1470
        os.rmdir('a')
1365
1472
 
1366
1473
    def test_update_symlink_to_file(self):
1367
1474
        """Symlink becomes a file"""
 
1475
        if not has_symlinks():
 
1476
            raise TestSkipped("No symlink support")
1368
1477
        state, entry = self.get_state_with_a()
1369
1478
        self.create_and_test_symlink(state, entry)
1370
1479
        os.remove('a')
1372
1481
 
1373
1482
    def test_update_symlink_to_dir(self):
1374
1483
        """Symlink becomes a directory"""
 
1484
        if not has_symlinks():
 
1485
            raise TestSkipped("No symlink support")
1375
1486
        state, entry = self.get_state_with_a()
1376
1487
        self.create_and_test_symlink(state, entry)
1377
1488
        os.remove('a')