def test_write_file_error(self): """ write_file should return False if there is an IOError. """ from garage.utils import write_file, get_file_contents self._msg("test", "write_file", first=True) with patch("garage.utils.open_file") as mock_open_file: mock_open_file.side_effect = IOError path = "testfile.txt" result = write_file(path, TestData) calls = mock_open_file.mock_calls self._msg("calls", calls) self.assertFalse(result) self._msg("result", result)
def test_write_file(self): """ Ensure write_file function is working properly. """ from garage.utils import write_file, get_file_contents self._msg("test", "write_file", first=True) filename = "garage-testfile" tmpfile_dir = tempfile.gettempdir() path = os.path.join(tmpfile_dir, filename) result = write_file(path, TestData) self.assertTrue(result) # chect contents data = get_file_contents(path) self._msg("path", path) self._msg("data", data, linebreak=True) self.assertEqual(data, TestData) os.unlink(path)