cookiecutter

Форк
0
/
test_utils.py 
111 строк · 3.2 Кб
1
"""Tests for `cookiecutter.utils` module."""
2

3
import stat
4
import sys
5
from pathlib import Path
6

7
import pytest
8

9
from cookiecutter import utils
10

11

12
def make_readonly(path) -> None:
13
    """Change the access permissions to readonly for a given file."""
14
    mode = Path.stat(path).st_mode
15
    Path.chmod(path, mode & ~stat.S_IWRITE)
16

17

18
def test_force_delete(mocker, tmp_path) -> None:
19
    """Verify `utils.force_delete` makes files writable."""
20
    ro_file = Path(tmp_path, 'bar')
21
    ro_file.write_text("Test data")
22
    make_readonly(ro_file)
23

24
    rmtree = mocker.Mock()
25
    utils.force_delete(rmtree, ro_file, sys.exc_info())
26

27
    assert (ro_file.stat().st_mode & stat.S_IWRITE) == stat.S_IWRITE
28
    rmtree.assert_called_once_with(ro_file)
29

30
    utils.rmtree(tmp_path)
31

32

33
def test_rmtree(tmp_path) -> None:
34
    """Verify `utils.rmtree` remove files marked as read-only."""
35
    file_path = Path(tmp_path, "bar")
36
    file_path.write_text("Test data")
37
    make_readonly(file_path)
38

39
    utils.rmtree(tmp_path)
40

41
    assert not Path(tmp_path).exists()
42

43

44
def test_make_sure_path_exists(tmp_path) -> None:
45
    """Verify correct True/False response from `utils.make_sure_path_exists`.
46

47
    Should return True if directory exist or created.
48
    Should return False if impossible to create directory (for example protected)
49
    """
50
    existing_directory = tmp_path
51
    directory_to_create = Path(tmp_path, "not_yet_created")
52

53
    utils.make_sure_path_exists(existing_directory)
54
    utils.make_sure_path_exists(directory_to_create)
55

56
    # Ensure by base system methods.
57
    assert existing_directory.is_dir()
58
    assert existing_directory.exists()
59
    assert directory_to_create.is_dir()
60
    assert directory_to_create.exists()
61

62

63
def test_make_sure_path_exists_correctly_handle_os_error(mocker) -> None:
64
    """Verify correct True/False response from `utils.make_sure_path_exists`.
65

66
    Should return True if directory exist or created.
67
    Should return False if impossible to create directory (for example protected)
68
    """
69
    mocker.patch("pathlib.Path.mkdir", side_effect=OSError)
70
    with pytest.raises(OSError) as err:
71
        utils.make_sure_path_exists(Path('protected_path'))
72
    assert str(err.value) == "Unable to create directory at protected_path"
73

74

75
def test_work_in(tmp_path) -> None:
76
    """Verify returning to original folder after `utils.work_in` use."""
77
    cwd = Path.cwd()
78
    ch_to = tmp_path
79

80
    assert ch_to != Path.cwd()
81

82
    # Under context manager we should work in tmp_path.
83
    with utils.work_in(ch_to):
84
        assert ch_to == Path.cwd()
85

86
    # Make sure we return to the correct folder
87
    assert cwd == Path.cwd()
88

89

90
def test_work_in_without_path() -> None:
91
    """Folder is not changed if no path provided."""
92
    cwd = Path.cwd()
93

94
    with utils.work_in():
95
        assert cwd == Path.cwd()
96

97
    assert cwd == Path.cwd()
98

99

100
def test_create_tmp_repo_dir(tmp_path) -> None:
101
    """Verify `utils.create_tmp_repo_dir` creates a copy."""
102
    repo_dir = Path(tmp_path) / 'bar'
103
    repo_dir.mkdir()
104
    subdirs = ('foo', 'bar', 'foobar')
105
    for name in subdirs:
106
        (repo_dir / name).mkdir()
107

108
    new_repo_dir = utils.create_tmp_repo_dir(repo_dir)
109

110
    assert new_repo_dir.exists()
111
    assert new_repo_dir.glob('*')
112

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.