Failed Conditions
Pull Request — master (#1522)
by Abdeali
01:38
created

test_ignored()   B

Complexity

Conditions 5

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 39
rs 8.0894
1
import os
2
import unittest
3
from pyprint.ConsolePrinter import ConsolePrinter
4
5
from coalib.output.printers.LogPrinter import LogPrinter
6
from coalib.collecting.Collectors import (collect_files,
7
                                          collect_dirs,
8
                                          collect_bears,
9
                                          collect_all_bears_from_sections)
10
from coalib.misc.ContextManagers import retrieve_stdout
11
from coalib.settings.Section import Section
12
13
14
class CollectFilesTest(unittest.TestCase):
15
16
    def setUp(self):
17
        current_dir = os.path.split(__file__)[0]
18
        self.collectors_test_dir = os.path.join(current_dir,
19
                                                "collectors_test_dir")
20
        self.log_printer = LogPrinter(ConsolePrinter())
21
22
    def test_file_empty(self):
23
        self.assertRaises(TypeError, collect_files)
24
25
    def test_file_invalid(self):
26
        with retrieve_stdout() as sio:
27
            self.assertEqual(collect_files(["invalid_path"],
28
                                           self.log_printer), [])
29
            self.assertRegex(sio.getvalue(),
30
                             ".*\\[WARNING\\].*No files matching "
31
                             "'invalid_path' were found.\n")
32
33
    def test_file_collection(self):
34
        self.assertEqual(collect_files([os.path.join(self.collectors_test_dir,
35
                                                     "others",
36
                                                     "*",
37
                                                     "*2.py")],
38
                                       self.log_printer),
39
                         [os.path.normcase(os.path.join(
40
                             self.collectors_test_dir,
41
                             "others",
42
                             "py_files",
43
                             "file2.py"))])
44
45
    def test_file_string_collection(self):
46
        self.assertEqual(collect_files(os.path.join(self.collectors_test_dir,
47
                                                    "others",
48
                                                    "*",
49
                                                    "*2.py"),
50
                                       self.log_printer),
51
                         [os.path.normcase(os.path.join(
52
                             self.collectors_test_dir,
53
                             "others",
54
                             "py_files",
55
                             "file2.py"))])
56
57
    def test_ignored(self):
58
        self.assertEqual(collect_files([os.path.join(self.collectors_test_dir,
59
                                                     "others",
60
                                                     "*",
61
                                                     "*2.py"),
62
                                        os.path.join(self.collectors_test_dir,
63
                                                     "others",
64
                                                     "*",
65
                                                     "*2.py")],
66
                                       self.log_printer,
67
                                       ignored_file_paths=[os.path.join(
68
                                           self.collectors_test_dir,
69
                                           "others",
70
                                           "py_files",
71
                                           "file2.py")]),
72
                         [])
73
74
    def test_limited(self):
75
        self.assertEqual(
76
            collect_files([os.path.join(self.collectors_test_dir,
77
                                        "others",
78
                                        "*",
79
                                        "*py")],
80
                          self.log_printer,
81
                          limit_file_paths=[os.path.join(
82
                                                self.collectors_test_dir,
83
                                                "others",
84
                                                "*",
85
                                                "*2.py")]),
86
            [os.path.normcase(os.path.join(self.collectors_test_dir,
87
                                           "others",
88
                                           "py_files",
89
                                           "file2.py"))])
90
91
92
class CollectDirsTest(unittest.TestCase):
93
94
    def setUp(self):
95
        current_dir = os.path.split(__file__)[0]
96
        self.collectors_test_dir = os.path.join(current_dir,
97
                                                "collectors_test_dir")
98
99
    def test_dir_empty(self):
100
        self.assertRaises(TypeError, collect_dirs)
101
102
    def test_dir_invalid(self):
103
        self.assertEqual(collect_dirs(["invalid_path"]), [])
104
105
    def test_dir_collection(self):
106
        self.assertEqual(
107
            sorted(i for i in
108
                   collect_dirs([os.path.join(self.collectors_test_dir,
109
                                              "**")])
110
                   if "__pycache__" not in i),
111
            sorted([os.path.normcase(os.path.join(
112
                self.collectors_test_dir, "bears")),
113
                os.path.normcase(os.path.join(self.collectors_test_dir,
114
                                              "bears_local_global")),
115
                os.path.normcase(os.path.join(self.collectors_test_dir,
116
                                              "others")),
117
                os.path.normcase(os.path.join(self.collectors_test_dir,
118
                                              "others",
119
                                              "c_files")),
120
                os.path.normcase(os.path.join(self.collectors_test_dir,
121
                                              "others",
122
                                              "py_files")),
123
                os.path.normcase(self.collectors_test_dir+os.sep)]))
124
125
    def test_dir_string_collection(self):
126
        self.assertEqual(
127
            sorted(i for i in
128
                   collect_dirs(os.path.join(self.collectors_test_dir,
129
                                             "**"))
130
                   if "__pycache__" not in i),
131
            sorted([os.path.normcase(os.path.join(
132
                self.collectors_test_dir, "bears")),
133
                os.path.normcase(os.path.join(self.collectors_test_dir,
134
                                              "bears_local_global")),
135
                os.path.normcase(os.path.join(self.collectors_test_dir,
136
                                              "others")),
137
                os.path.normcase(os.path.join(self.collectors_test_dir,
138
                                              "others",
139
                                              "c_files")),
140
                os.path.normcase(os.path.join(self.collectors_test_dir,
141
                                              "others",
142
                                              "py_files")),
143
                os.path.normcase(self.collectors_test_dir+os.sep)]))
144
145
    def test_ignored(self):
146
        print(sorted(i for i in
147
                   collect_dirs([os.path.join(self.collectors_test_dir,
148
                                              "**")],
149
                                [os.path.normcase(os.path.join(
150
                                    self.collectors_test_dir,
151
                                    "others",
152
                                    "py_files"))])
153
                   if "__pycache__" not in i))
154
        print(sorted([os.path.normcase(os.path.join(
155
                self.collectors_test_dir, "bears")),
156
                os.path.normcase(os.path.join(self.collectors_test_dir,
157
                                              "bears_local_global")),
158
                os.path.normcase(os.path.join(self.collectors_test_dir,
159
                                              "others")),
160
                os.path.normcase(os.path.join(self.collectors_test_dir,
161
                                              "others",
162
                                              "c_files")),
163
                os.path.normcase(self.collectors_test_dir+os.sep)]))
164
        self.assertEqual(
165
            sorted(i for i in
166
                   collect_dirs([os.path.join(self.collectors_test_dir,
167
                                              "**")],
168
                                [os.path.normcase(os.path.join(
169
                                    self.collectors_test_dir,
170
                                    "others",
171
                                    "py_files"))])
172
                   if "__pycache__" not in i),
173
174
            sorted([os.path.normcase(os.path.join(
175
                self.collectors_test_dir, "bears")),
176
                os.path.normcase(os.path.join(self.collectors_test_dir,
177
                                              "bears_local_global")),
178
                os.path.normcase(os.path.join(self.collectors_test_dir,
179
                                              "others")),
180
                os.path.normcase(os.path.join(self.collectors_test_dir,
181
                                              "others",
182
                                              "c_files")),
183
                os.path.normcase(self.collectors_test_dir+os.sep)]))
184
185
186
class CollectBearsTest(unittest.TestCase):
187
188
    def setUp(self):
189
        current_dir = os.path.split(__file__)[0]
190
        self.collectors_test_dir = os.path.join(current_dir,
191
                                                "collectors_test_dir")
192
193
        self.log_printer = LogPrinter(ConsolePrinter())
194
195
    def test_bear_empty(self):
196
        self.assertRaises(TypeError, collect_bears)
197
198
    def test_bear_invalid(self):
199
        with retrieve_stdout() as sio:
200
            self.assertEqual(collect_bears(["invalid_paths"],
201
                                           ["invalid_name"],
202
                                           ["invalid kind"],
203
                                           self.log_printer), ([],))
204
            self.assertRegex(sio.getvalue(),
205
                             ".*\\[WARNING\\].*No bears matching "
206
                             "'invalid_name' were found.\n")
207
208
        self.assertEqual(collect_bears(["invalid_paths"],
209
                                       ["invalid_name"],
210
                                       ["invalid kind1", "invalid kind2"],
211
                                       self.log_printer), ([], []))
212
213
    def test_simple_single(self):
214
        self.assertEqual(len(collect_bears(
215
            [os.path.join(self.collectors_test_dir, "bears")],
216
            ["bear1"],
217
            ["kind"],
218
            self.log_printer)[0]), 1)
219
220
    def test_string_single(self):
221
        self.assertEqual(len(collect_bears(
222
            os.path.join(self.collectors_test_dir, "bears"),
223
            ["bear1"],
224
            ["kind"],
225
            self.log_printer)[0]), 1)
226
227
    def test_reference_single(self):
228
        self.assertEqual(len(collect_bears(
229
            [os.path.join(self.collectors_test_dir, "bears")],
230
            ["metabear"],
231
            ["kind"],
232
            self.log_printer)[0]), 1)
233
234
    def test_no_duplications(self):
235
        self.assertEqual(len(collect_bears(
236
            [os.path.join(self.collectors_test_dir, "bears", "**")],
237
            ["*"],
238
            ["kind"],
239
            self.log_printer)[0]), 2)
240
241
    def test_wrong_kind(self):
242
        self.assertEqual(len(collect_bears(
243
            [os.path.join(self.collectors_test_dir, "bears", "**")],
244
            ["*"],
245
            ["other_kind"],
246
            self.log_printer)[0]), 0)
247
248
    def test_all_bears_from_sections(self):
249
        test_section = Section("test_section")
250
        test_section.bear_dirs = lambda: os.path.join(self.collectors_test_dir,
251
                                                      "bears_local_global",
252
                                                      "**")
253
        local_bears, global_bears = collect_all_bears_from_sections(
254
            {'test_section': test_section},
255
            self.log_printer)
256
257
        self.assertEqual(len(local_bears['test_section']), 2)
258
        self.assertEqual(len(global_bears['test_section']), 2)
259