def test_symlink_to_should_create_symbolic_link(fs): link2 = os.path.join(fs, "link2") Path(link2).symlink_to(Path(fs, "file2.txt")) with open(link2, "r", encoding="utf-8") as f: content = f.read() assert content == "abcöüçğış" os.unlink(link2)
def test_rename_should_fail_for_existing_target(fs): src = os.path.join(fs, "copy1.txt") shutil.copyfile(os.path.join(fs, "file1.txt"), src) dst = os.path.join(fs, "copy2.txt") shutil.copyfile(os.path.join(fs, "file2.txt"), dst) with raises(FileExistsError): Path(src).rename(Path(dst)) os.unlink(src)
def test_rename_should_rename_file_for_nonexisting_target(fs): src = os.path.join(fs, "copy1.txt") shutil.copyfile(os.path.join(fs, "file1.txt"), src) dst = os.path.join(fs, "renamed1.txt") Path(src).rename(Path(dst)) with open(dst, "rb") as f: content = f.read() assert content == b"file1" os.unlink(dst)
def test_unlink_should_remove_symlink(fs): file2 = os.path.join(fs, "file2.txt") link2 = os.path.join(fs, "link2") os.symlink(file2, link2) Path(link2).unlink() assert not os.path.exists(link2) assert os.path.exists(file2)
def test_touch_should_create_empty_file(fs): path = os.path.join(fs, "tmp") assert not os.path.exists(path) Path(path).touch() assert os.path.exists(path) assert os.stat(path).st_size == 0 os.unlink(path)
def test_mkdir_should_succeed_nested_creation_if_parents_set(fs): path1 = os.path.join(fs, "tmp1") path2 = os.path.join(path1, "tmp2") assert not os.path.exists(path1) Path(fs, "tmp1", "tmp2").mkdir(parents=True) assert os.path.exists(path2) os.rmdir(path2) os.rmdir(path1)
def test_shutil_rmtree_should_take_path_as_parameter(fs): tmp1 = os.path.join(fs, "tmp1") os.mkdir(tmp1) tmp2 = os.path.join(tmp1, "tmp2") os.mkdir(tmp2) tmp3 = os.path.join(tmp2, "tmp3.txt") shutil.copyfile(os.path.join(fs, "file1.txt"), tmp3) shutil.rmtree(Path(tmp2)) assert not os.path.exists(tmp2) assert os.path.exists(tmp1) os.rmdir(tmp1)
def test_rmtree_should_remove_tree_recursively(fs): tmp1 = os.path.join(fs, "tmp1") os.mkdir(tmp1) tmp2 = os.path.join(tmp1, "tmp2") os.mkdir(tmp2) tmp3 = os.path.join(tmp2, "tmp3.txt") shutil.copyfile(os.path.join(fs, "file1.txt"), tmp3) Path(tmp2).rmtree() assert not os.path.exists(tmp2) assert os.path.exists(tmp1) os.rmdir(tmp1)
def test_mkdir_should_create_directory_with_given_permissions(fs): path = os.path.join(fs, "tmp") Path(fs, "tmp").mkdir(mode=0o555) assert os.stat(path).st_mode == 16749 os.rmdir(path)
def test_is_mount_should_be_false_for_regular_directories(fs): assert not Path(fs).is_mount()
def test_group_should_get_group_name_from_group_database(fs): assert Path(fs, "file1.txt").group() == grp.getgrgid(os.getgid()).gr_name
def test_stat_should_contain_permissions(fs): assert Path(fs, "file1.txt").stat().st_mode == 33188
def test_is_not_absolute_when_no_root(): assert not Path("a/b").is_absolute()
def test_as_uri_should_return_file_uri(): assert Path("/etc/passwd").as_uri() == "file:///etc/passwd"
def test_is_absolute_when_starts_with_unc_share(): assert Path("\\\\some\\share").is_absolute()
def test_drive_should_be_empty(): assert Path("/etc").drive == ""
def test_as_posix_should_return_same_result(): assert Path("/etc/passwd").as_posix() == "/etc/passwd"
def test_is_reserved_should_be_true_for_reserved_paths(): assert Path("nul").is_reserved()
def test_is_absolute_when_starts_with_root(): assert Path("/a/b").is_absolute()
def test_is_reserved_should_not_be_true_for_regular_paths(): assert not Path("c:\\\\windows").is_reserved()
def test_is_reserved_should_be_false(): assert not Path("nul").is_reserved()
def test_joinpath_should_extend_drive_with_path(): assert Path("c:").joinpath("\\Program Files") == "c:\\Program Files"
def test_chmod_should_change_permissions(fs): Path(fs, "file1.txt").chmod(0o444) assert Path(fs, "file1.txt").stat().st_mode == 33060 Path(fs, "file1.txt").chmod(0o644) assert Path(fs, "file1.txt").stat().st_mode == 33188
def test_relative_to_on_different_drive_should_fail_even_when_not_strict(): with raises(ValueError): Path("c:\\windows").relative_to(Path("d:\\"), strict=False)
def test_is_mount_should_be_true_for_mount_points(fs): assert Path("/").is_mount()
def test_symlink_to_directory_should_create_symbolic_link(fs): sublink1 = os.path.join(fs, "sublink1") Path(sublink1).symlink_to(Path(fs, "sub1")) assert os.path.exists(os.path.join(sublink1, "mod2.py"))
def test_mkdir_should_create_directory_with_default_permissions(fs): path = os.path.join(fs, "tmp") Path(fs, "tmp").mkdir() assert os.stat(path).st_mode == 16877 os.rmdir(path)
def test_unlink_should_not_remove_directory_even_if_empty(fs): sub2 = os.path.join(fs, "sub2") os.mkdir(sub2) with raises(IsADirectoryError): Path(sub2).unlink()
def test_owner_should_get_user_name_from_operating_system(fs): assert Path(fs, "file1.txt").owner() == pwd.getpwuid(os.getuid()).pw_name
def test_multiple_absolute_paths_should_anchor_to_last(): assert Path("/etc", "/usr", "lib64") == "/usr/lib64"