def test_write(tmp_path: Path, mode: str, contents: t.Union[str, bytes]): file = tmp_path / "test_file" sh.write(file, contents, mode) actual_contents = file.read_bytes() if isinstance(contents, str): actual_contents = actual_contents.decode() # type: ignore assert contents == actual_contents
def test_write__can_atomically_create_file( tmp_path: Path, mock_atomicfile: mock.MagicMock, mode: str, contents: t.Union[str, bytes], expected_mode: str, expected_overwrite: bool, ): file = tmp_path / "test_file" sh.write(file, contents, mode, atomic=True) assert mock_atomicfile.called assert mock_atomicfile.call_args == mock.call(file, expected_mode, overwrite=expected_overwrite) args, kwargs = mock_atomicfile.call_args with mock_atomicfile(*args, **kwargs) as fp: assert fp.write.called assert fp.write.call_args == mock.call(contents)
def test_writetext__accepts_valid_mode(tmp_path: Path, valid_write_only_text_mode: str): sh.write(tmp_path / "test_file", "", valid_write_only_text_mode)
def test_writebytes__accepts_valid_mode(tmp_path: Path, valid_write_only_bin_mode: str): sh.write(tmp_path / "test_file", b"", valid_write_only_bin_mode)
def test_write__raises_when_mode_invalid(tmp_path: Path, invalid_write_only_mode: str): file = tmp_path / "test_file" with pytest.raises(ValueError): sh.write(file, "", invalid_write_only_mode)
def test_write__accepts_valid_mode(tmp_path: Path, valid_write_only_mode: str): contents: t.Union[str, bytes] = b"" if "b" in valid_write_only_mode else "" sh.write(tmp_path / "test_file", contents, valid_write_only_mode)