1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
test_utils |
5
|
|
|
------------ |
6
|
|
|
|
7
|
|
|
Tests for `cookiecutter.utils` module. |
8
|
|
|
""" |
9
|
|
|
|
10
|
|
|
import os |
11
|
|
|
import pytest |
12
|
|
|
import stat |
13
|
|
|
import sys |
14
|
|
|
|
15
|
|
|
from cookiecutter import utils |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def make_readonly(path): |
19
|
|
|
"""Helper function that is called in the tests to change the access |
20
|
|
|
permissions of the given file. |
21
|
|
|
""" |
22
|
|
|
mode = os.stat(path).st_mode |
23
|
|
|
os.chmod(path, mode & ~stat.S_IWRITE) |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def test_rmtree(): |
27
|
|
|
os.mkdir('foo') |
28
|
|
|
with open('foo/bar', "w") as f: |
29
|
|
|
f.write("Test data") |
30
|
|
|
make_readonly('foo/bar') |
31
|
|
|
utils.rmtree('foo') |
32
|
|
|
assert not os.path.exists('foo') |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def test_make_sure_path_exists(): |
36
|
|
|
if sys.platform.startswith('win'): |
37
|
|
|
existing_directory = os.path.abspath(os.curdir) |
38
|
|
|
uncreatable_directory = 'a*b' |
39
|
|
|
else: |
40
|
|
|
existing_directory = '/usr/' |
41
|
|
|
uncreatable_directory = '/this-doesnt-exist-and-cant-be-created/' |
42
|
|
|
|
43
|
|
|
assert utils.make_sure_path_exists(existing_directory) |
44
|
|
|
assert utils.make_sure_path_exists('tests/blah') |
45
|
|
|
assert utils.make_sure_path_exists('tests/trailingslash/') |
46
|
|
|
assert not utils.make_sure_path_exists(uncreatable_directory) |
47
|
|
|
utils.rmtree('tests/blah/') |
48
|
|
|
utils.rmtree('tests/trailingslash/') |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def test_workin(): |
52
|
|
|
cwd = os.getcwd() |
53
|
|
|
ch_to = 'tests/files' |
54
|
|
|
|
55
|
|
|
class TestException(Exception): |
56
|
|
|
pass |
57
|
|
|
|
58
|
|
|
def test_work_in(): |
59
|
|
|
with utils.work_in(ch_to): |
60
|
|
|
test_dir = os.path.join(cwd, ch_to).replace("/", os.sep) |
61
|
|
|
assert test_dir == os.getcwd() |
62
|
|
|
raise TestException() |
63
|
|
|
|
64
|
|
|
# Make sure we return to the correct folder |
65
|
|
|
assert cwd == os.getcwd() |
66
|
|
|
|
67
|
|
|
# Make sure that exceptions are still bubbled up |
68
|
|
|
with pytest.raises(TestException): |
69
|
|
|
test_work_in() |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def test_windows_error_reraised(): |
73
|
|
|
if sys.platform.startswith('win'): |
74
|
|
|
def win_error_reraise(): |
75
|
|
|
root = os.path.abspath(os.sep) |
76
|
|
|
# can't remove root on Windows, but path exists |
77
|
|
|
utils.force_delete(os.remove, root, None) |
78
|
|
|
|
79
|
|
|
with pytest.raises(WindowsError): |
80
|
|
|
win_error_reraise() |
81
|
|
|
else: |
82
|
|
|
pass |
83
|
|
|
|