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