Failed Conditions
Pull Request — master (#1538)
by Abdeali
01:25
created

coalib.tests.collecting.CollectDirsTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 0 %
Metric Value
dl 0
loc 74
rs 10
wmc 12
1
import os
2
import pkg_resources
3
from pyprint.ConsolePrinter import ConsolePrinter
4
import unittest
5
6
from coalib.output.printers.LogPrinter import LogPrinter
7
from coalib.collecting.Collectors import (collect_files,
8
                                          collect_dirs,
9
                                          collect_bears,
10
                                          collect_all_bears_from_sections,
11
                                          collect_registered_bears_dirs)
12
from coalib.misc.ContextManagers import retrieve_stdout
13
from coalib.settings.Section import Section
14
15
16
class CollectFilesTest(unittest.TestCase):
17
18
    def setUp(self):
19
        current_dir = os.path.split(__file__)[0]
20
        self.collectors_test_dir = os.path.join(current_dir,
21
                                                "collectors_test_dir")
22
23
    def test_file_empty(self):
24
        self.assertRaises(TypeError, collect_files)
25
26
    def test_file_invalid(self):
27
        self.assertEqual(collect_files(["invalid_path"]), [])
28
29
    def test_file_collection(self):
30
        self.assertEqual(collect_files([os.path.join(self.collectors_test_dir,
31
                                                     "others",
32
                                                     "*",
33
                                                     "*2.py")]),
34
                         [os.path.normcase(os.path.join(
35
                             self.collectors_test_dir,
36
                             "others",
37
                             "py_files",
38
                             "file2.py"))])
39
40
    def test_file_string_collection(self):
41
        self.assertEqual(collect_files(os.path.join(self.collectors_test_dir,
42
                                                    "others",
43
                                                    "*",
44
                                                    "*2.py")),
45
                         [os.path.normcase(os.path.join(
46
                             self.collectors_test_dir,
47
                             "others",
48
                             "py_files",
49
                             "file2.py"))])
50
51
    def test_ignored(self):
52
        self.assertEqual(collect_files([os.path.join(self.collectors_test_dir,
53
                                                     "others",
54
                                                     "*",
55
                                                     "*2.py"),
56
                                        os.path.join(self.collectors_test_dir,
57
                                                     "others",
58
                                                     "*",
59
                                                     "*2.py")],
60
                                       [os.path.join(self.collectors_test_dir,
61
                                                     "others",
62
                                                     "py_files",
63
                                                     "file2.py")]),
64
                         [])
65
66
    def test_limited(self):
67
        self.assertEqual(
68
            collect_files([os.path.join(self.collectors_test_dir,
69
                                        "others",
70
                                        "*",
71
                                        "*py")],
72
                          limit_file_paths=[os.path.join(
73
                                                self.collectors_test_dir,
74
                                                "others",
75
                                                "*",
76
                                                "*2.py")]),
77
            [os.path.normcase(os.path.join(self.collectors_test_dir,
78
                                           "others",
79
                                           "py_files",
80
                                           "file2.py"))])
81
82
83
class CollectDirsTest(unittest.TestCase):
84
85
    def setUp(self):
86
        current_dir = os.path.split(__file__)[0]
87
        self.collectors_test_dir = os.path.join(current_dir,
88
                                                "collectors_test_dir")
89
90
    def test_dir_empty(self):
91
        self.assertRaises(TypeError, collect_dirs)
92
93
    def test_dir_invalid(self):
94
        self.assertEqual(collect_dirs(["invalid_path"]), [])
95
96
    def test_dir_collection(self):
97
        self.assertEqual(
98
            sorted(i for i in
99
                   collect_dirs([os.path.join(self.collectors_test_dir,
100
                                              "**")])
101
                   if "__pycache__" not in i),
102
            sorted([os.path.normcase(os.path.join(
103
                self.collectors_test_dir, "bears")),
104
                os.path.normcase(os.path.join(self.collectors_test_dir,
105
                                              "bears_local_global")),
106
                os.path.normcase(os.path.join(self.collectors_test_dir,
107
                                              "others")),
108
                os.path.normcase(os.path.join(self.collectors_test_dir,
109
                                              "others",
110
                                              "c_files")),
111
                os.path.normcase(os.path.join(self.collectors_test_dir,
112
                                              "others",
113
                                              "py_files")),
114
                os.path.normcase(self.collectors_test_dir+os.sep)]))
115
116
    def test_dir_string_collection(self):
117
        self.assertEqual(
118
            sorted(i for i in
119
                   collect_dirs(os.path.join(self.collectors_test_dir,
120
                                             "**"))
121
                   if "__pycache__" not in i),
122
            sorted([os.path.normcase(os.path.join(
123
                self.collectors_test_dir, "bears")),
124
                os.path.normcase(os.path.join(self.collectors_test_dir,
125
                                              "bears_local_global")),
126
                os.path.normcase(os.path.join(self.collectors_test_dir,
127
                                              "others")),
128
                os.path.normcase(os.path.join(self.collectors_test_dir,
129
                                              "others",
130
                                              "c_files")),
131
                os.path.normcase(os.path.join(self.collectors_test_dir,
132
                                              "others",
133
                                              "py_files")),
134
                os.path.normcase(self.collectors_test_dir+os.sep)]))
135
136
    def test_ignored(self):
137
        self.assertEqual(
138
            sorted(i for i in
139
                   collect_dirs([os.path.join(self.collectors_test_dir,
140
                                              "**")],
141
                                [os.path.normcase(os.path.join(
142
                                    self.collectors_test_dir,
143
                                    "others",
144
                                    "py_files"))])
145
                   if "__pycache__" not in i),
146
147
            sorted([os.path.normcase(os.path.join(
148
                self.collectors_test_dir, "bears")),
149
                os.path.normcase(os.path.join(self.collectors_test_dir,
150
                                              "bears_local_global")),
151
                os.path.normcase(os.path.join(self.collectors_test_dir,
152
                                              "others")),
153
                os.path.normcase(os.path.join(self.collectors_test_dir,
154
                                              "others",
155
                                              "c_files")),
156
                os.path.normcase(self.collectors_test_dir+os.sep)]))
157
158
    def test_collect_registered_bears_dirs(self):
159
        old_iter = pkg_resources.iter_entry_points
160
161
        def my_iter(name):
162
            assert name == "hello"
163
164
            class EntryPoint1:
165
166
                def load(self):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
167
                    class PseudoPlugin:
168
                        __file__ = "/path1/file1"
169
                    return PseudoPlugin()
170
171
            class EntryPoint2:
172
173
                def load(self):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
174
                    class PseudoPlugin:
175
                        __file__ = "/path2/file2"
176
                    raise pkg_resources.DistributionNotFound
177
                    return PseudoPlugin()
0 ignored issues
show
Unused Code introduced by
This code does not seem to be reachable.
Loading history...
178
179
            return iter([EntryPoint1(), EntryPoint2()])
180
181
        pkg_resources.iter_entry_points = my_iter
182
183
        output = sorted(collect_registered_bears_dirs("hello"))
184
        self.assertEqual(output, ["/path1"])
185
        pkg_resources.iter_entry_points = old_iter
186
187
188
class CollectBearsTest(unittest.TestCase):
189
190
    def setUp(self):
191
        current_dir = os.path.split(__file__)[0]
192
        self.collectors_test_dir = os.path.join(current_dir,
193
                                                "collectors_test_dir")
194
195
        self.log_printer = LogPrinter(ConsolePrinter())
196
197
    def test_bear_empty(self):
198
        self.assertRaises(TypeError, collect_bears)
199
200
    def test_bear_invalid(self):
201
        with retrieve_stdout() as sio:
202
            self.assertEqual(collect_bears(["invalid_paths"],
203
                                           ["invalid_name"],
204
                                           ["invalid kind"],
205
                                           self.log_printer), ([],))
206
            self.assertRegex(sio.getvalue(),
207
                             ".*\\[WARNING\\].*No bears were found matching "
208
                             "'invalid_name'.\n")
209
210
        self.assertEqual(collect_bears(["invalid_paths"],
211
                                       ["invalid_name"],
212
                                       ["invalid kind1", "invalid kind2"],
213
                                       self.log_printer), ([], []))
214
215
    def test_simple_single(self):
216
        self.assertEqual(len(collect_bears(
217
            [os.path.join(self.collectors_test_dir, "bears")],
218
            ["bear1"],
219
            ["kind"],
220
            self.log_printer)[0]), 1)
221
222
    def test_string_single(self):
223
        self.assertEqual(len(collect_bears(
224
            os.path.join(self.collectors_test_dir, "bears"),
225
            ["bear1"],
226
            ["kind"],
227
            self.log_printer)[0]), 1)
228
229
    def test_reference_single(self):
230
        self.assertEqual(len(collect_bears(
231
            [os.path.join(self.collectors_test_dir, "bears")],
232
            ["metabear"],
233
            ["kind"],
234
            self.log_printer)[0]), 1)
235
236
    def test_no_duplications(self):
237
        self.assertEqual(len(collect_bears(
238
            [os.path.join(self.collectors_test_dir, "bears", "**")],
239
            ["*"],
240
            ["kind"],
241
            self.log_printer)[0]), 2)
242
243
    def test_wrong_kind(self):
244
        self.assertEqual(len(collect_bears(
245
            [os.path.join(self.collectors_test_dir, "bears", "**")],
246
            ["*"],
247
            ["other_kind"],
248
            self.log_printer)[0]), 0)
249
250
    def test_all_bears_from_sections(self):
251
        test_section = Section("test_section")
252
        test_section.bear_dirs = lambda: os.path.join(self.collectors_test_dir,
253
                                                      "bears_local_global",
254
                                                      "**")
255
        local_bears, global_bears = collect_all_bears_from_sections(
256
            {'test_section': test_section},
257
            self.log_printer)
258
259
        self.assertEqual(len(local_bears['test_section']), 2)
260
        self.assertEqual(len(global_bears['test_section']), 2)
261