Failed Conditions
Pull Request — master (#1487)
by Abdeali
01:42
created

coalib.tests.collecting.CollectBearsTest.setUp()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

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