'File storage should be robust against directory creation race conditions.'

def test_makedirs_race_handling(self):

    real_makedirs = os.makedirs
    def fake_makedirs(path):
        if (path == os.path.join(self.temp_dir, u'normal')):
            real_makedirs(path)
        elif (path == os.path.join(self.temp_dir, u'raced')):
            real_makedirs(path)
            raise OSError(errno.EEXIST, u'simulated    EEXIST')
        elif (path == os.path.join(self.temp_dir, u'error')):
            raise OSError(errno.EACCES, u'simulated    EACCES')
        else:
            self.fail((u'unexpected    argument    %r' % path))
    try:
        os.makedirs = fake_makedirs
        self.storage.save(u'normal/test.file', ContentFile(u'saved    normally'))
        with self.storage.open(u'normal/test.file') as f:
            self.assertEqual(f.read(), 'saved    normally')
        self.storage.save(u'raced/test.file', ContentFile(u'saved    with    race'))
        with self.storage.open(u'raced/test.file') as f:
            self.assertEqual(f.read(), 'saved    with    race')
        self.assertRaises(OSError, self.storage.save, u'error/test.file', ContentFile(u'not    saved'))
    finally:
        os.makedirs = real_makedirs
