Completed
Pull Request — master (#694)
by Eric
01:17
created

test_force_delete()   A

Complexity

Conditions 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
dl 0
loc 10
rs 9.4285
c 1
b 0
f 1
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
18
19
def make_readonly(path):
20
    """Helper function that is called in the tests to change the access
21
    permissions of the given file.
22
    """
23
    mode = os.stat(path).st_mode
24
    os.chmod(path, mode & ~stat.S_IWRITE)
25
26
27
def test_rmtree():
28
    os.mkdir('foo')
29
    with open('foo/bar', "w") as f:
30
        f.write("Test data")
31
    make_readonly('foo/bar')
32
    utils.rmtree('foo')
33
    assert not os.path.exists('foo')
34
35
36
def test_force_delete(mocker):
37
    path = 'foo'
38
    func = mocker.MagicMock(return_value=path)
39
    os.mkdir(path)
40
    make_readonly(path)
41
    utils.force_delete(func, path, None)
42
    mode = os.stat(path).st_mode
43
    func.assert_called_once_with(path)
44
    assert bool(mode & stat.S_IWRITE)
45
    os.rmdir(path)
46
47
48
def test_make_sure_path_exists():
49
    if sys.platform.startswith('win'):
50
        existing_directory = os.path.abspath(os.curdir)
51
        uncreatable_directory = 'a*b'
52
    else:
53
        existing_directory = '/usr/'
54
        uncreatable_directory = '/this-doesnt-exist-and-cant-be-created/'
55
56
    assert utils.make_sure_path_exists(existing_directory)
57
    assert utils.make_sure_path_exists('tests/blah')
58
    assert utils.make_sure_path_exists('tests/trailingslash/')
59
    assert not utils.make_sure_path_exists(uncreatable_directory)
60
    utils.rmtree('tests/blah/')
61
    utils.rmtree('tests/trailingslash/')
62
63
64
def test_workin():
65
    cwd = os.getcwd()
66
    ch_to = 'tests/files'
67
68
    class TestException(Exception):
69
        pass
70
71
    def test_work_in():
72
        with utils.work_in(ch_to):
73
            test_dir = os.path.join(cwd, ch_to).replace("/", os.sep)
74
            assert test_dir == os.getcwd()
75
            raise TestException()
76
77
    # Make sure we return to the correct folder
78
    assert cwd == os.getcwd()
79
80
    # Make sure that exceptions are still bubbled up
81
    with pytest.raises(TestException):
82
        test_work_in()
83