Completed
Push — master ( 6cfd3b...95ab6f )
by Kale
64:30
created

test_remove_link_to_dir()   F

Complexity

Conditions 16

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 16
c 2
b 1
f 0
dl 0
loc 23
rs 3.0057

How to fix   Complexity   

Complexity

Complex classes like test_remove_link_to_dir() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
from __future__ import absolute_import, division, print_function, unicode_literals
3
4
from errno import ENOENT
5
import os
6
from os.path import isdir, isfile, islink, join, lexists
7
8
import pytest
9
10
from conda.compat import TemporaryDirectory
11
from conda.common.compat import on_win
12
from conda.gateways.disk.create import create_link, mkdir_p
13
from conda.gateways.disk.delete import move_to_trash, rm_rf
14
from conda.gateways.disk.link import islink, symlink
15
from conda.gateways.disk.test import softlink_supported
16
from conda.gateways.disk.update import touch
17
from conda.models.enums import LinkType
18
from .test_permissions import _make_read_only, _try_open, tempdir
19
20
21
def _write_file(path, content):
22
    with open(path, "a") as fh:
23
        fh.write(content)
24
        fh.close()
25
26
27
def test_remove_file():
28
    with tempdir() as td:
29
        test_path = join(td, 'test_path')
30
        touch(test_path)
31
        assert isfile(test_path)
32
        _try_open(test_path)
33
        _make_read_only(test_path)
34
        pytest.raises((IOError, OSError), _try_open, test_path)
35
        assert rm_rf(test_path)
36
        assert not isfile(test_path)
37
38
39
def test_remove_file_to_trash():
40
    with tempdir() as td:
41
        test_path = join(td, 'test_path')
42
        touch(test_path)
43
        assert isfile(test_path)
44
        _try_open(test_path)
45
        _make_read_only(test_path)
46
        pytest.raises((IOError, OSError), _try_open, test_path)
47
        assert rm_rf(test_path)
48
        assert not isfile(test_path)
49
50
51 View Code Duplication
def test_remove_dir():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
52
    with tempdir() as td:
53
        test_path = join(td, 'test_path')
54
        touch(test_path)
55
        _try_open(test_path)
56
        assert isfile(test_path)
57
        assert isdir(td)
58
        assert not islink(test_path)
59
        assert rm_rf(td)
60
        assert rm_rf(test_path)
61
        assert not isdir(td)
62
        assert not isfile(test_path)
63
        assert not lexists(test_path)
64
65
66 View Code Duplication
def test_remove_link_to_file():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
67
    with tempdir() as td:
68
        dst_link = join(td, "test_link")
69
        src_file = join(td, "test_file")
70
        _write_file(src_file, "welcome to the ministry of silly walks")
71
        if not softlink_supported(src_file, td) and on_win:
72
            pytest.skip("softlink not supported")
73
74
        symlink(src_file, dst_link)
75
        assert isfile(src_file)
76
        assert not islink(src_file)
77
        assert islink(dst_link)
78
        assert rm_rf(dst_link)
79
        assert isfile(src_file)
80
        assert rm_rf(src_file)
81
        assert not isfile(src_file)
82
        assert not islink(dst_link)
83
        assert not lexists(dst_link)
84
85
86
def test_remove_link_to_dir():
87
    with tempdir() as td:
88
        dst_link = join(td, "test_link")
89
        src_dir = join(td, "test_dir")
90
        test_file = join(td, "test_file")
91
        mkdir_p(src_dir)
92
        touch(test_file)
93
        assert isdir(src_dir)
94
        assert not islink(src_dir)
95
        assert not islink(dst_link)
96
        if not softlink_supported(test_file, td) and on_win:
97
            pytest.skip("softlink not supported")
98
99
        symlink(src_dir, dst_link)
100
        assert islink(dst_link)
101
        assert rm_rf(dst_link)
102
        assert not isdir(dst_link)
103
        assert not islink(dst_link)
104
        assert not lexists(dst_link)
105
        assert isdir(src_dir)
106
        assert rm_rf(src_dir)
107
        assert not isdir(src_dir)
108
        assert not islink(src_dir)
109
110
111
def test_rm_rf_does_not_follow_symlinks():
112
    with TemporaryDirectory() as tmp:
113
        # make a file in some temp folder
114
        real_file = os.path.join(tmp, 'testfile')
115
        with open(real_file, 'w') as f:
116
            f.write('weee')
117
        # make a subfolder
118
        subdir = os.path.join(tmp, 'subfolder')
119
        os.makedirs(subdir)
120
        # link to the file in the subfolder
121
        link_path = join(subdir, 'file_link')
122
        if not softlink_supported(real_file, tmp) and on_win:
123
            pytest.skip("softlink not supported")
124
125
        create_link(real_file, link_path, link_type=LinkType.softlink)
126
        assert islink(link_path)
127
        # rm_rf the subfolder
128
        rm_rf(subdir)
129
        # assert that the file still exists in the root folder
130
        assert os.path.isfile(real_file)
131
132
133
def test_move_to_trash():
134
    with tempdir() as td:
135
        test_path = join(td, 'test_path')
136
        touch(test_path)
137
        _try_open(test_path)
138
        assert isdir(td)
139
        assert isfile(test_path)
140
        move_to_trash(td, test_path)
141
        assert not isfile(test_path)
142
143
144
def test_move_path_to_trash_couldnt():
145
    from conda.gateways.disk.delete import move_path_to_trash
146
    with tempdir() as td:
147
        test_path = join(td, 'test_path')
148
        touch(test_path)
149
        _try_open(test_path)
150
        assert isdir(td)
151
        assert isfile(test_path)
152
        assert move_path_to_trash(test_path)
153
154
155
def test_backoff_unlink():
156
    from conda.gateways.disk.delete import backoff_rmdir
157
    with tempdir() as td:
158
        test_path = join(td, 'test_path')
159
        touch(test_path)
160
        _try_open(test_path)
161
        assert isdir(td)
162
        backoff_rmdir(td)
163
        assert not isdir(td)
164
165
166
def test_backoff_unlink_doesnt_exist():
167
    from conda.gateways.disk.delete import backoff_rmdir
168
    with tempdir() as td:
169
        test_path = join(td, 'test_path')
170
        touch(test_path)
171
        try:
172
            backoff_rmdir(join(test_path, 'some', 'path', 'in', 'utopia'))
173
        except Exception as e:
174
            assert e.value.errno == ENOENT
175
176
177
def test_try_rmdir_all_empty_doesnt_exist():
178
    from conda.gateways.disk.delete import try_rmdir_all_empty
179
    with tempdir() as td:
180
        assert isdir(td)
181
        try_rmdir_all_empty(td)
182
        assert not isdir(td)
183