Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:11
created

tests/settings/ConfigurationGatheringTest.py (65 issues)

1
import os
2
import re
3
import tempfile
4
import unittest
5
6
from pyprint.ClosableObject import close_objects
7
from pyprint.NullPrinter import NullPrinter
8
9
from coalib.misc import Constants
10
from coalib.misc.ContextManagers import make_temp, change_directory
11
from coalib.output.printers.LogPrinter import LogPrinter
12
from coalib.parsing.StringProcessing import escape
13
from coalib.settings.ConfigurationGathering import (
14
    find_user_config, gather_configuration, load_configuration)
15
16
17
class ConfigurationGatheringTest(unittest.TestCase):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable unittest does not seem to be defined.
Loading history...
18
19
    def setUp(self):
20
        self.log_printer = LogPrinter(NullPrinter())
21
22
    def tearDown(self):
23
        close_objects(self.log_printer)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
24
25
    def test_gather_configuration(self):
26
        args = (lambda *args: True, self.log_printer)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
27
28
        # Passing the default coafile name only triggers a warning.
29
        gather_configuration(*args, arg_list=["-c abcdefghi/invalid/.coafile"])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable args does not seem to be defined.
Loading history...
30
31
        # Using a bad filename explicitly exits coala.
32
        with self.assertRaises(SystemExit):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SystemExit does not seem to be defined.
Loading history...
33
            gather_configuration(
34
                *args,
35
                arg_list=["-S", "test=5", "-c", "some_bad_filename"])
36
37
        with make_temp() as temporary:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable temporary does not seem to be defined.
Loading history...
38
            sections, local_bears, global_bears, targets = (
39
                gather_configuration(
40
                    *args,
41
                    arg_list=["-S",
42
                              "test=5",
43
                              "-c",
44
                              escape(temporary, "\\"),
45
                              "-s"]))
46
47
        self.assertEqual(str(sections["default"]),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sections does not seem to be defined.
Loading history...
48
                         "Default {config : " +
49
                         repr(temporary) + ", save : 'True', test : '5'}")
50
51
        with make_temp() as temporary:
52
            sections, local_bears, global_bears, targets = (
53
                gather_configuration(*args,
54
                                     arg_list=["-S test=5",
55
                                               "-c " + escape(temporary, "\\"),
56
                                               "-b LineCountBear -s"]))
57
58
        self.assertEqual(len(local_bears["default"]), 0)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable local_bears does not seem to be defined.
Loading history...
59
60 View Code Duplication
    def test_default_coafile_parsing(self):
61
        tmp = Constants.system_coafile
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Constants does not seem to be defined.
Loading history...
62
63
        Constants.system_coafile = os.path.abspath(os.path.join(
64
            os.path.dirname(os.path.realpath(__file__)),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable __file__ does not seem to be defined.
Loading history...
65
            "section_manager_test_files",
66
            "default_coafile"))
67
68
        sections, local_bears, global_bears, targets = gather_configuration(
69
            lambda *args: True,
70
            self.log_printer,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
71
            arg_list=[])
72
73
        self.assertEqual(str(sections["test"]),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sections does not seem to be defined.
Loading history...
74
                         "test {value : '1', testval : '5'}")
75
76
        Constants.system_coafile = tmp
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable tmp does not seem to be defined.
Loading history...
77
78 View Code Duplication
    def test_user_coafile_parsing(self):
79
        tmp = Constants.user_coafile
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Constants does not seem to be defined.
Loading history...
80
81
        Constants.user_coafile = os.path.abspath(os.path.join(
82
            os.path.dirname(os.path.realpath(__file__)),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable __file__ does not seem to be defined.
Loading history...
83
            "section_manager_test_files",
84
            "default_coafile"))
85
86
        sections, local_bears, global_bears, targets = gather_configuration(
87
            lambda *args: True,
88
            self.log_printer,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
89
            arg_list=[])
90
91
        self.assertEqual(str(sections["test"]),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sections does not seem to be defined.
Loading history...
92
                         "test {value : '1', testval : '5'}")
93
94
        Constants.user_coafile = tmp
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable tmp does not seem to be defined.
Loading history...
95
96
    def test_nonexistent_file(self):
97
        filename = "bad.one/test\neven with bad chars in it"
98
        with self.assertRaises(SystemExit):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SystemExit does not seem to be defined.
Loading history...
99
            gather_configuration(lambda *args: True,
100
                                 self.log_printer,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
101
                                 arg_list=['-S', "config=" + filename])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable filename does not seem to be defined.
Loading history...
102
103
        tmp = Constants.system_coafile
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Constants does not seem to be defined.
Loading history...
104
        Constants.system_coafile = filename
105
106
        with self.assertRaises(SystemExit):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SystemExit does not seem to be defined.
Loading history...
107
            gather_configuration(lambda *args: True,
108
                                 self.log_printer,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
109
                                 arg_list=[])
110
111
        Constants.system_coafile = tmp
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable tmp does not seem to be defined.
Loading history...
112
113
    def test_merge(self):
114
        tmp = Constants.system_coafile
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Constants does not seem to be defined.
Loading history...
115
        Constants.system_coafile = os.path.abspath(os.path.join(
116
            os.path.dirname(os.path.realpath(__file__)),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable __file__ does not seem to be defined.
Loading history...
117
            "section_manager_test_files",
118
            "default_coafile"))
119
120
        config = os.path.abspath(os.path.join(
121
            os.path.dirname(os.path.realpath(__file__)),
122
            "section_manager_test_files",
123
            ".coafile"))
124
125
        # Check merging of default_coafile and .coafile
126
        sections, local_bears, global_bears, targets = gather_configuration(
127
            lambda *args: True,
128
            self.log_printer,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
129
            arg_list=["-c", re.escape(config)])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable config does not seem to be defined.
Loading history...
130
131
        self.assertEqual(str(sections["test"]),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sections does not seem to be defined.
Loading history...
132
                         "test {value : '2'}")
133
        self.assertEqual(str(sections["test-2"]),
134
                         "test-2 {files : '.', bears : 'LineCountBear'}")
135
136
        # Check merging of default_coafile, .coafile and cli
137
        sections, local_bears, global_bears, targets = gather_configuration(
138
            lambda *args: True,
139
            self.log_printer,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
140
            arg_list=["-c",
141
                      re.escape(config),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable config does not seem to be defined.
Loading history...
142
                      "-S",
143
                      "test.value=3",
144
                      "test-2.bears=",
145
                      "test-5.bears=TestBear2"])
146
147
        self.assertEqual(str(sections["test"]), "test {value : '3'}")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sections does not seem to be defined.
Loading history...
148
        self.assertEqual(str(sections["test-2"]),
149
                         "test-2 {files : '.', bears : ''}")
150
        self.assertEqual(str(sections["test-3"]),
151
                         "test-3 {files : 'MakeFile'}")
152
        self.assertEqual(str(sections["test-4"]),
153
                         "test-4 {bears : 'TestBear'}")
154
        self.assertEqual(str(sections["test-5"]),
155
                         "test-5 {bears : 'TestBear2'}")
156
157
        Constants.system_coafile = tmp
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable tmp does not seem to be defined.
Loading history...
158
159
    def test_merge_defaults(self):
160
        with make_temp() as temporary:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable temporary does not seem to be defined.
Loading history...
161
            sections, local_bears, global_bears, targets = (
162
                gather_configuration(lambda *args: True,
163
                                     self.log_printer,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
164
                                     arg_list=["-S",
165
                                               "value=1",
166
                                               "test.value=2",
167
                                               "-c",
168
                                               escape(temporary, "\\")]))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable temporary does not seem to be defined.
Loading history...
169
170
        self.assertEqual(sections["default"],
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sections does not seem to be defined.
Loading history...
171
                         sections["test"].defaults)
172
173
    def test_back_saving(self):
174
        filename = os.path.join(tempfile.gettempdir(),
175
                                "SectionManagerTestFile")
176
177
        # We need to use a bad filename or this will parse coalas .coafile
178
        gather_configuration(
179
            lambda *args: True,
180
            self.log_printer,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
181
            arg_list=['-S',
182
                      "save=" + escape(filename, '\\'),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable filename does not seem to be defined.
Loading history...
183
                      "-c=some_bad_filename"])
184
185
        with open(filename, "r") as f:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable f does not seem to be defined.
Loading history...
186
            lines = f.readlines()
187
        self.assertEqual(["[Default]\n", "config = some_bad_filename\n"], lines)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable lines does not seem to be defined.
Loading history...
188
189
        gather_configuration(
190
            lambda *args: True,
191
            self.log_printer,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
192
            arg_list=['-S',
193
                      "save=true",
194
                      "config=" + escape(filename, '\\'),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable filename does not seem to be defined.
Loading history...
195
                      "test.value=5"])
196
197
        with open(filename, "r") as f:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable f does not seem to be defined.
Loading history...
198
            lines = f.readlines()
199
        os.remove(filename)
200
        if os.path.sep == '\\':
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable os does not seem to be defined.
Loading history...
201
            filename = escape(filename, '\\')
202
        self.assertEqual(["[Default]\n",
203
                          "config = " + filename + "\n",
204
                          "\n",
205
                          "[test]\n",
206
                          "value = 5\n"], lines)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable lines does not seem to be defined.
Loading history...
207
208
    def test_targets(self):
209
        sections, local_bears, global_bears, targets = gather_configuration(
210
            lambda *args: True,
211
            self.log_printer,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
212
            arg_list=["default", "test1", "test2"])
213
214
        self.assertEqual(targets, ["default", "test1", "test2"])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable targets does not seem to be defined.
Loading history...
215
216
    def test_find_user_config(self):
217
        current_dir = os.path.abspath(os.path.dirname(__file__))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable __file__ does not seem to be defined.
Loading history...
218
        c_file = os.path.join(current_dir,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable current_dir does not seem to be defined.
Loading history...
219
                              "section_manager_test_files",
220
                              "project",
221
                              "test.c")
222
223
        retval = find_user_config(c_file, 1)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable c_file does not seem to be defined.
Loading history...
224
        self.assertEqual("", retval)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable retval does not seem to be defined.
Loading history...
225
226
        retval = find_user_config(c_file, 2)
227
        self.assertEqual(os.path.join(current_dir,
228
                                      "section_manager_test_files",
229
                                      ".coafile"), retval)
230
231
        child_dir = os.path.join(current_dir,
232
                                 "section_manager_test_files",
233
                                 "child_dir")
234
        retval = find_user_config(child_dir, 2)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable child_dir does not seem to be defined.
Loading history...
235
        self.assertEqual(os.path.join(current_dir,
236
                                      "section_manager_test_files",
237
                                      "child_dir",
238
                                      ".coafile"), retval)
239
240
        with change_directory(child_dir):
241
            sections, _, _, _ = gather_configuration(
242
                lambda *args: True,
243
                self.log_printer,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
244
                arg_list=["--find-config"])
245
            self.assertEqual(bool(sections["default"]['find_config']), True)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sections does not seem to be defined.
Loading history...
246
247
    def test_no_config(self):
248
        current_dir = os.path.abspath(os.path.dirname(__file__))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable __file__ does not seem to be defined.
Loading history...
249
        child_dir = os.path.join(current_dir,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable current_dir does not seem to be defined.
Loading history...
250
                                 "section_manager_test_files",
251
                                 "child_dir")
252
        with change_directory(child_dir):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable child_dir does not seem to be defined.
Loading history...
253
            sections, targets = load_configuration([], self.log_printer)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
254
            self.assertIn('value', sections["default"])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sections does not seem to be defined.
Loading history...
255
256
            sections, targets = load_configuration(
257
                ['--no-config'],
258
                self.log_printer)
259
            self.assertNotIn('value', sections["default"])
260
261
            sections, targets = load_configuration(
262
                ['--no-config', '-S', 'use_spaces=True'],
263
                self.log_printer)
264
            self.assertIn('use_spaces', sections["default"])
265
            self.assertNotIn('values', sections["default"])
266
267
            sections, targets = load_configuration(
268
                ['--no-config', 'False', '-S', 'use_spaces=True'],
269
                self.log_printer)
270
            self.assertIn('use_spaces', sections["default"])
271
            self.assertIn('value', sections["default"])
272
273
            with self.assertRaises(SystemExit) as cm:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable cm does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable SystemExit does not seem to be defined.
Loading history...
274
                sections, target = load_configuration(
275
                    ['--no-config', '--save'],
276
                    self.log_printer)
277
                self.assertEqual(cm.exception.code, 2)
278
279
            with self.assertRaises(SystemExit) as cm:
280
                sections, target = load_configuration(
281
                    ['--no-config', '--find-config'],
282
                    self.log_printer)
283
                self.assertEqual(cm.exception.code, 2)
284
285
    def test_autoapply_arg(self):
286
        sections, _, _, _ = gather_configuration(
287
            lambda *args: True,
288
            self.log_printer,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
289
            autoapply=False,
290
            arg_list=[])
291
292
        self.assertEqual(str(sections['default'].get('autoapply', None)),
293
                         'False')
294
295
        sections, _, _, _ = gather_configuration(
296
            lambda *args: True,
297
            self.log_printer,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
298
            autoapply=True,
299
            arg_list=[])
300
301
        self.assertEqual(str(sections['default'].get('autoapply', None)),
302
                         'None')
303