370
def test_can_save_in_read_lock(self):
371
self.build_tree(['a-file'])
372
state = dirstate.DirState.initialize('dirstate')
374
# No stat and no sha1 sum.
375
state.add('a-file', 'a-file-id', 'file', None, '')
380
# Now open in readonly mode
381
state = dirstate.DirState.on_file('dirstate')
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',
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)
400
# Now, since we are the only one holding a lock, we should be able
401
# to save and have it written to disk
406
# Re-open the file, and ensure that the state has been updated.
407
state = dirstate.DirState.on_file('dirstate')
410
entry = state._get_entry(0, path_utf8='a-file')
411
self.assertEqual(sha1sum, entry[1][0][1])
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')
420
# No stat and no sha1 sum.
421
state.add('a-file', 'a-file-id', 'file', None, '')
426
state = dirstate.DirState.on_file('dirstate')
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',
434
self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
435
state._dirblock_state)
437
# Now, before we try to save, grab another dirstate, and take out a
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')
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
457
# The file on disk should not be modified.
458
state = dirstate.DirState.on_file('dirstate')
461
entry = state._get_entry(0, path_utf8='a-file')
462
self.assertEqual('', entry[1][0][1])
371
467
class TestDirStateInitialize(TestCaseWithDirState):