import sys
from pathlib import Path
is_monty = sys.platform == 'monty'
is_windows = sys.platform == 'win32'
too_many_path = root / 'mkdir_too_many_args'
try:
too_many_path.mkdir(0o777, False, False, 'extra')
assert False, 'expected TypeError on mkdir too many positional args'
except TypeError as exc:
if is_monty:
assert str(exc) == 'Path.mkdir() takes from 0 to 3 positional arguments but 4 were given', (
f'unexpected message: {exc}'
)
else:
assert str(exc) == 'Path.mkdir() takes from 1 to 4 positional arguments but 5 were given', (
f'unexpected message: {exc}'
)
assert too_many_path.exists() == False
unknown_kw_path = root / 'mkdir_unknown_kw'
try:
unknown_kw_path.mkdir(bogus=True)
assert False, 'expected TypeError on mkdir unknown kwarg'
except TypeError as exc:
assert str(exc) == "Path.mkdir() got an unexpected keyword argument 'bogus'", f'unexpected message: {exc}'
assert unknown_kw_path.exists() == False
duplicate_arg_path = root / 'mkdir_duplicate_arg'
try:
duplicate_arg_path.mkdir(0o777, True, parents=False)
assert False, 'expected TypeError on mkdir duplicate parents'
except TypeError as exc:
assert str(exc) == "Path.mkdir() got multiple values for argument 'parents'", f'unexpected message: {exc}'
assert duplicate_arg_path.exists() == False
try:
(root / 'nonexistent.txt').read_text()
assert False, 'expected FileNotFoundError on read_text'
except FileNotFoundError as exc:
if is_monty:
assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent.txt'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'
try:
(root / 'nonexistent.bin').read_bytes()
assert False, 'expected FileNotFoundError on read_bytes'
except FileNotFoundError as exc:
if is_monty:
assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent.bin'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'
try:
(root / 'nonexistent.txt').unlink()
assert False, 'expected FileNotFoundError on unlink'
except FileNotFoundError as exc:
if is_monty:
assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent.txt'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'
try:
(root / 'nonexistent.txt').stat()
assert False, 'expected FileNotFoundError on stat'
except FileNotFoundError as exc:
if is_monty:
assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent.txt'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'
try:
(root / 'nonexistent' / 'child.txt').stat()
assert False, 'expected FileNotFoundError on stat'
except FileNotFoundError as exc:
if is_monty:
assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent/child.txt'", (
f'unexpected message: {exc}'
)
elif not is_windows:
assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'
try:
(root / 'nonexistent_dir').rmdir()
assert False, 'expected FileNotFoundError on rmdir nonexistent'
except FileNotFoundError as exc:
if is_monty:
assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent_dir'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'
try:
(root / 'nonexistent.txt').rename(root / 'new.txt')
assert False, 'expected FileNotFoundError on rename nonexistent'
except FileNotFoundError as exc:
if is_monty:
assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent.txt'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'
try:
(root / 'missing_parent' / 'child').mkdir()
assert False, 'expected error on mkdir without parents'
except FileNotFoundError as exc:
if is_monty:
assert str(exc) == "[Errno 2] No such file or directory: '/mnt/missing_parent/child'", (
f'unexpected message: {exc}'
)
elif not is_windows:
assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'
try:
(root / 'no_such_parent' / 'child.txt').write_text('should fail')
assert False, 'expected FileNotFoundError on write_text with missing parent'
except FileNotFoundError as exc:
if is_monty:
assert str(exc) == "[Errno 2] No such file or directory: '/mnt/no_such_parent/child.txt'", (
f'unexpected message: {exc}'
)
elif not is_windows:
assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'
try:
(root / 'no_such_parent' / 'child.bin').write_bytes(b'should fail')
assert False, 'expected FileNotFoundError on write_bytes with missing parent'
except FileNotFoundError as exc:
if is_monty:
assert str(exc) == "[Errno 2] No such file or directory: '/mnt/no_such_parent/child.bin'", (
f'unexpected message: {exc}'
)
elif not is_windows:
assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'
try:
list((root / 'nonexistent').iterdir())
assert False, 'expected FileNotFoundError on iterdir nonexistent'
except FileNotFoundError as exc:
if is_monty:
assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'
try:
(root / 'subdir').mkdir()
assert False, 'expected FileExistsError on mkdir existing'
except FileExistsError as exc:
if is_monty:
assert str(exc) == "[Errno 17] File exists: '/mnt/subdir'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 17] File exists: '"), f'exc message: {exc}'
try:
(root / 'subdir').mkdir(parents=True, exist_ok=False)
assert False, 'expected FileExistsError on mkdir parents=True exist_ok=False'
except FileExistsError as exc:
if is_monty:
assert str(exc) == "[Errno 17] File exists: '/mnt/subdir'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 17] File exists: '"), f'exc message: {exc}'
try:
(root / 'hello.txt').mkdir(exist_ok=True)
assert False, 'expected FileExistsError on mkdir exist_ok=True on a file'
except FileExistsError as exc:
if is_monty:
assert str(exc) == "[Errno 17] File exists: '/mnt/hello.txt'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 17] File exists: '"), f'exc message: {exc}'
(root / 'rename_src_dir').mkdir()
(root / 'rename_src_dir' / 'moved.txt').write_text('moved')
(root / 'rename_dst_dir').mkdir()
(root / 'rename_dst_dir' / 'existing.txt').write_text('existing')
try:
(root / 'rename_src_dir').rename(root / 'rename_dst_dir')
assert False, 'expected OSError on rename dir onto non-empty dir'
except OSError as exc:
if is_monty:
assert str(exc) == "[Errno 39] Directory not empty: '/mnt/rename_dst_dir'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith(('[Errno 66] Directory not empty:', '[Errno 39] Directory not empty:')), (
f'exc message: {exc}'
)
if not is_windows:
(root / 'rename_dst_empty').mkdir()
(root / 'rename_src_dir').rename(root / 'rename_dst_empty')
assert (root / 'rename_dst_empty' / 'moved.txt').read_text() == 'moved'
assert not (root / 'rename_src_dir').exists(), 'source dir gone after rename'
try:
(root / 'subdir').read_text()
assert False, 'expected IsADirectoryError on read_text of dir'
except (IsADirectoryError, PermissionError) as exc:
if is_monty:
assert str(exc) == "[Errno 21] Is a directory: '/mnt/subdir'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 21] Is a directory: '"), f'exc message: {exc}'
try:
(root / 'subdir').read_bytes()
assert False, 'expected IsADirectoryError on read_bytes of dir'
except (IsADirectoryError, PermissionError) as exc:
if is_monty:
assert str(exc) == "[Errno 21] Is a directory: '/mnt/subdir'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 21] Is a directory: '"), f'exc message: {exc}'
try:
(root / 'subdir').write_text('test')
assert False, 'expected IsADirectoryError on write_text to dir'
except (IsADirectoryError, PermissionError) as exc:
if is_monty:
assert str(exc) == "[Errno 21] Is a directory: '/mnt/subdir'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 21] Is a directory: '"), f'exc message: {exc}'
try:
(root / 'subdir').write_bytes(b'test')
assert False, 'expected IsADirectoryError on write_bytes to dir'
except (IsADirectoryError, PermissionError) as exc:
if is_monty:
assert str(exc) == "[Errno 21] Is a directory: '/mnt/subdir'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 21] Is a directory: '"), f'exc message: {exc}'
try:
(root / 'subdir').unlink()
assert False, 'expected error on unlink of dir'
except (IsADirectoryError, PermissionError):
pass
try:
list((root / 'hello.txt').iterdir())
assert False, 'expected NotADirectoryError on iterdir of file'
except NotADirectoryError as exc:
if is_monty:
assert str(exc) == "[Errno 20] Not a directory: '/mnt/hello.txt'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 20] Not a directory: '"), f'exc message: {exc}'
try:
(root / 'hello.txt').rmdir()
assert False, 'expected NotADirectoryError on rmdir of file'
except NotADirectoryError as exc:
if is_monty:
assert str(exc) == "[Errno 20] Not a directory: '/mnt/hello.txt'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith("[Errno 20] Not a directory: '"), f'exc message: {exc}'
try:
(root / 'subdir').rmdir()
assert False, 'expected error on rmdir non-empty'
except OSError as exc:
if is_monty:
assert str(exc) == "[Errno 39] Directory not empty: '/mnt/subdir'", f'unexpected message: {exc}'
elif not is_windows:
assert str(exc).startswith(('[Errno 66] Directory not empty:', '[Errno 39] Directory not empty:')), (
f'exc message: {exc}'
)
(root / 'bad_utf8.bin').write_bytes(b'\x80\x81\x82')
try:
(root / 'bad_utf8.bin').read_text()
assert False, 'expected UnicodeDecodeError on read_text of non-UTF-8'
except UnicodeDecodeError as exc:
if is_monty:
assert str(exc) == "'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte", (
f'unexpected message: {exc}'
)
elif not is_windows:
assert 'utf-8' in str(exc), f'exc message: {exc}'
long_name = 'a' * 256
long_component_path = Path(str(root) + '/' + long_name)
try:
long_component_path.write_text('test')
assert False, 'expected OSError on long component'
except OSError as exc:
if not is_windows:
assert str(exc).startswith(("[Errno 36] File name too long: '", "[Errno 63] File name too long: '")), (
f'exc message: {exc}'
)
ok_name = 'b' * 255
assert Path(str(root) + '/' + ok_name).exists() == False
long_path_str = str(root) + '/' + '/'.join(['x' * 200] * 21)
try:
Path(long_path_str).write_text('test')
assert False, 'expected OSError on long total path'
except OSError as exc:
if not is_windows:
assert str(exc).startswith(("[Errno 36] File name too long: '", "[Errno 63] File name too long: '")), (
f'exc message: {exc}'
)
try:
long_component_path.read_text()
assert False, 'expected OSError on read_text with long component'
except OSError as exc:
if not is_windows:
assert str(exc).startswith(("[Errno 36] File name too long: '", "[Errno 63] File name too long: '")), (
f'exc message: {exc}'
)
try:
long_component_path.stat()
assert False, 'expected OSError on stat with long component'
except OSError as exc:
if not is_windows:
assert str(exc).startswith(("[Errno 36] File name too long: '", "[Errno 63] File name too long: '")), (
f'exc message: {exc}'
)
try:
long_component_path.mkdir()
assert False, 'expected OSError on mkdir with long component'
except OSError as exc:
if not is_windows:
assert str(exc).startswith(("[Errno 36] File name too long: '", "[Errno 63] File name too long: '")), (
f'exc message: {exc}'
)
try:
(root / 'hello.txt').write_text(123)
assert False, 'expected TypeError on write_text with int'
except TypeError as exc:
assert str(exc) == 'data must be str, not int', f'unexpected message: {exc}'
try:
(root / 'hello.txt').write_text()
assert False, 'expected TypeError on write_text with no args'
except TypeError as exc:
assert str(exc) == "Path.write_text() missing 1 required positional argument: 'data'", f'unexpected message: {exc}'
try:
(root / 'hello.txt').write_bytes(123)
assert False, 'expected TypeError on write_bytes with int'
except TypeError as exc:
assert str(exc) == "memoryview: a bytes-like object is required, not 'int'", f'unexpected message: {exc}'
try:
(root / 'hello.txt').write_bytes()
assert False, 'expected TypeError on write_bytes with no args'
except TypeError as exc:
assert str(exc) == "Path.write_bytes() missing 1 required positional argument: 'data'", f'unexpected message: {exc}'