def test_atomic_file_write_itest(): tempdir = tempfile.mkdtemp() target_file_name = os.path.join(tempdir, 'test_atomic_file_write_itest.txt') try: old_umask = os.umask(0022) with open(target_file_name, 'w') as f_before: f_before.write('old content') with utils.atomic_file_write(target_file_name) as f_new: f_new.write('new content') with open(target_file_name) as f_existing: # While in the middle of an atomic_file_write, the existing # file should still contain the old content, and should not # be truncated, etc. assert f_existing.read() == 'old content' with open(target_file_name) as f_done: # once we're done, the content should be in place. assert f_done.read() == 'new content' file_stat = os.stat(target_file_name) assert stat.S_ISREG(file_stat.st_mode) assert stat.S_IMODE(file_stat.st_mode) == 0644 finally: os.umask(old_umask) shutil.rmtree(tempdir)
def test_atomic_file_write(): with mock.patch("tempfile.NamedTemporaryFile", autospec=True) as ntf_patch: file_patch = ntf_patch().__enter__() file_patch.name = "/hurp/.durp-AAA" ntf_patch.reset_mock() with mock.patch("os.rename", autospec=True) as rename_patch: with mock.patch("os.chmod", autospec=True) as chmod_patch: with utils.atomic_file_write("/hurp/durp"): ntf_patch.assert_called_once_with(dir="/hurp", prefix=".durp-", delete=False) chmod_patch.assert_called_once_with("/hurp/.durp-AAA", mock.ANY) rename_patch.assert_called_once_with("/hurp/.durp-AAA", "/hurp/durp")
def test_atomic_file_write(): with mock.patch('tempfile.NamedTemporaryFile', autospec=True) as ntf_patch: file_patch = ntf_patch().__enter__() file_patch.name = '/hurp/.durp-AAA' ntf_patch.reset_mock() with mock.patch('os.rename', autospec=True) as rename_patch: with mock.patch('os.chmod', autospec=True) as chmod_patch: with utils.atomic_file_write('/hurp/durp'): ntf_patch.assert_called_once_with( dir='/hurp', prefix='.durp-', delete=False, ) chmod_patch.assert_called_once_with('/hurp/.durp-AAA', mock.ANY) rename_patch.assert_called_once_with('/hurp/.durp-AAA', '/hurp/durp')
def test_atomic_file_write(): with mock.patch('tempfile.NamedTemporaryFile', autospec=True) as ntf_patch: file_patch = ntf_patch().__enter__() file_patch.name = '/hurp/.durp-AAA' ntf_patch.reset_mock() with mock.patch('os.rename', autospec=True) as rename_patch: with mock.patch('os.chmod', autospec=True) as chmod_patch: with utils.atomic_file_write('/hurp/durp'): ntf_patch.assert_called_once_with( dir='/hurp', prefix='.durp-', delete=False, ) chmod_patch.assert_called_once_with('/hurp/.durp-AAA', mock.ANY) rename_patch.assert_called_once_with( '/hurp/.durp-AAA', '/hurp/durp' )