cookiecutter
111 строк · 3.2 Кб
1"""Tests for `cookiecutter.utils` module."""
2
3import stat4import sys5from pathlib import Path6
7import pytest8
9from cookiecutter import utils10
11
12def make_readonly(path) -> None:13"""Change the access permissions to readonly for a given file."""14mode = Path.stat(path).st_mode15Path.chmod(path, mode & ~stat.S_IWRITE)16
17
18def test_force_delete(mocker, tmp_path) -> None:19"""Verify `utils.force_delete` makes files writable."""20ro_file = Path(tmp_path, 'bar')21ro_file.write_text("Test data")22make_readonly(ro_file)23
24rmtree = mocker.Mock()25utils.force_delete(rmtree, ro_file, sys.exc_info())26
27assert (ro_file.stat().st_mode & stat.S_IWRITE) == stat.S_IWRITE28rmtree.assert_called_once_with(ro_file)29
30utils.rmtree(tmp_path)31
32
33def test_rmtree(tmp_path) -> None:34"""Verify `utils.rmtree` remove files marked as read-only."""35file_path = Path(tmp_path, "bar")36file_path.write_text("Test data")37make_readonly(file_path)38
39utils.rmtree(tmp_path)40
41assert not Path(tmp_path).exists()42
43
44def test_make_sure_path_exists(tmp_path) -> None:45"""Verify correct True/False response from `utils.make_sure_path_exists`.46
47Should return True if directory exist or created.
48Should return False if impossible to create directory (for example protected)
49"""
50existing_directory = tmp_path51directory_to_create = Path(tmp_path, "not_yet_created")52
53utils.make_sure_path_exists(existing_directory)54utils.make_sure_path_exists(directory_to_create)55
56# Ensure by base system methods.57assert existing_directory.is_dir()58assert existing_directory.exists()59assert directory_to_create.is_dir()60assert directory_to_create.exists()61
62
63def test_make_sure_path_exists_correctly_handle_os_error(mocker) -> None:64"""Verify correct True/False response from `utils.make_sure_path_exists`.65
66Should return True if directory exist or created.
67Should return False if impossible to create directory (for example protected)
68"""
69mocker.patch("pathlib.Path.mkdir", side_effect=OSError)70with pytest.raises(OSError) as err:71utils.make_sure_path_exists(Path('protected_path'))72assert str(err.value) == "Unable to create directory at protected_path"73
74
75def test_work_in(tmp_path) -> None:76"""Verify returning to original folder after `utils.work_in` use."""77cwd = Path.cwd()78ch_to = tmp_path79
80assert ch_to != Path.cwd()81
82# Under context manager we should work in tmp_path.83with utils.work_in(ch_to):84assert ch_to == Path.cwd()85
86# Make sure we return to the correct folder87assert cwd == Path.cwd()88
89
90def test_work_in_without_path() -> None:91"""Folder is not changed if no path provided."""92cwd = Path.cwd()93
94with utils.work_in():95assert cwd == Path.cwd()96
97assert cwd == Path.cwd()98
99
100def test_create_tmp_repo_dir(tmp_path) -> None:101"""Verify `utils.create_tmp_repo_dir` creates a copy."""102repo_dir = Path(tmp_path) / 'bar'103repo_dir.mkdir()104subdirs = ('foo', 'bar', 'foobar')105for name in subdirs:106(repo_dir / name).mkdir()107
108new_repo_dir = utils.create_tmp_repo_dir(repo_dir)109
110assert new_repo_dir.exists()111assert new_repo_dir.glob('*')112