def test_non_callable_text_func_raises_an_error(self): with tempfile.TemporaryDirectory() as tmp_dir: file = Path(tmp_dir, "foo.bar") lines = sutils.write_to_file([], file, text_func=1) self.assertRaises(TypeError, lambda: list(lines)) self.assertFalse(file.exists())
def test_iterable_is_written_to_file_when_processed(self): with tempfile.TemporaryDirectory() as tmp_dir: file = Path(tmp_dir, "foo.bar") # call list to exhaust the iterable list(sutils.write_to_file(["kissa"], file)) with file.open("r") as f: self.assertEqual(["kissa"], f.readlines())
def test_file_exist_after_an_empty_list_is_processed(self): with tempfile.TemporaryDirectory() as tmp_dir: file = Path(tmp_dir, "foo.bar") lines = sutils.write_to_file([], file) self.assertEqual([], list(lines)) self.assertTrue(file.exists()) with file.open("r") as f: self.assertEqual([], f.readlines())
def test_file_exists_in_case_text_func_fails(self): with tempfile.TemporaryDirectory() as tmp_dir: file = Path(tmp_dir, "foo.bar") lines = sutils.write_to_file(["kissa"], file, text_func=lambda x, y: str(x + y)) self.assertRaises(TypeError, lambda: list(lines)) self.assertTrue(file.exists())
def test_existing_file_is_overwritten(self): with tempfile.TemporaryDirectory() as tmp_dir: file = Path(tmp_dir, "foo.bar") with file.open("w") as f: f.write("istuu") list(sutils.write_to_file(["kissa"], file)) with file.open("r") as f: self.assertEqual(["kissa"], f.readlines())
def test_text_func_does_not_change_output(self): with tempfile.TemporaryDirectory() as tmp_dir: file = Path(tmp_dir, "foo.bar") xs = list( sutils.write_to_file(["kissa"], file, text_func=lambda x: x[::-1])) self.assertEqual(["kissa"], xs)
def test_text_func(self): with tempfile.TemporaryDirectory() as tmp_dir: file = Path(tmp_dir, "foo.bar") list( sutils.write_to_file(["kissa"], file, text_func=lambda x: x[::-1])) with file.open("r") as f: self.assertEqual(["assik"], f.readlines())
def test_readlines_returns_empty_list_after_partial_exhaustion(self): # Note: OS differences make it difficult to test what happens # when a file handle is being kept open in a generator. If this # test fails on some platform, feel free to add a skip decorator # (or write a better test!) with tempfile.TemporaryDirectory() as tmp_dir: file = Path(tmp_dir, "foo.bar") xs = sutils.write_to_file(["kissa", "istuu"], file) # advance the iterator by one self.assertEqual("kissa", next(xs)) with file.open("r") as f: self.assertEqual([], f.readlines()) # exhaust the iterator to avoid PermissionErrors on Windows when # the tmp_dir context manager closes list(xs)
def test_giving_a_folder_as_input_raises_error(self): with tempfile.TemporaryDirectory() as tmp_dir: file = Path(tmp_dir) lines = sutils.write_to_file(["kissa"], file) self.assertRaises(OSError, lambda: list(lines))
def test_writing_to_non_existing_folder_raises_exception(self): with tempfile.TemporaryDirectory() as tmp_dir: file = Path(tmp_dir, "baz", "foo.bar") lines = sutils.write_to_file(["kissa"], file) self.assertRaises(OSError, lambda: list(lines))
def test_write_to_file_does_not_add_newline_characters(self): with tempfile.TemporaryDirectory() as tmp_dir: file = Path(tmp_dir, "foo.bar") list(sutils.write_to_file(["kissa", "istuu"], file)) with file.open("r") as f: self.assertEqual(["kissaistuu"], f.readlines())
def test_file_is_not_written_until_iterable_is_processed(self): with tempfile.TemporaryDirectory() as tmp_dir: file = Path(tmp_dir, "foo.bar") sutils.write_to_file(["kissa"], file) self.assertFalse(file.exists())