Completed
Push — master ( 7e5a08...0b8668 )
by Charles
01:53
created

test_dump()   B

Complexity

Conditions 3

Size

Total Lines 372

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 4
Metric Value
cc 3
dl 0
loc 372
rs 8.2857
c 7
b 1
f 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# -*- coding: utf-8 -*-
2
3
import csv
4
import json
5
import os
6
import re
7
import shutil
8
9
import pytest
10
import xmltodict
11
import yaml
12
13
from git_app_version.dumper import FileDumper as AppDumper
14
from git_app_version.helper.pyversion import PY3
15
16
try:
17
    import ConfigParser as configparser
18
except ImportError:
19
    import configparser
20
21
try:
22
    from yaml import CLoader as Loader, CDumper as Dumper
23
except ImportError:
24
    from yaml import Loader, Dumper
25
26
27
@pytest.fixture
28
def output_dir():
29
    cwd = os.path.realpath(os.path.dirname(__file__))
30
    path = cwd + '/output'
31
    if os.path.exists(path):
32
        shutil.rmtree(path)
33
    os.makedirs(path, 493)
34
35
    return path
36
37
38
def get_file_content(path, section=None, fileFormat=None,
39
                     csv_delimiter=',', csv_quote='"', csv_eol='lf'):
40
    if fileFormat == 'yml' or fileFormat == 'yaml':
41
        with open(path, 'r') as f:
42
            return yaml.load(f)
43
    elif fileFormat == 'xml':
44
        with open(path, 'r') as f:
45
            return xmltodict.parse(f.read(), dict_constructor=dict)
46
    elif fileFormat == 'ini':
47
        config = configparser.RawConfigParser()
48
        config.read(path)
49
50
        data = {}
51
        if not section:
52
            section = 'app_version'
53
54
        if config.has_section(section):
55
            for k, v in config.items(section):
56
                if PY3:
57
                    data[k] = v
58
                else:
59
                    data[k] = v.decode('utf-8')
60
61
        return data
62
    elif fileFormat == 'csv':
63
        data = {}
64
65
        eol = "\r\n" if csv_eol == 'crlf' or csv_eol == "\r\n" else "\n"
66
67
        if PY3:
68
            with open(path, 'r', encoding='utf-8') as f:
69
                reader = csv.reader(
70
                    f,
71
                    delimiter=csv_delimiter,
72
                    quotechar=csv_quote,
73
                    lineterminator=eol,
74
                    quoting=csv.QUOTE_MINIMAL)
75
                for row in reader:
76
                    data[row[0]] = row[1]
77
        else:
78
            with open(path, 'rb') as f:
79
                reader = csv.reader(
80
                    f,
81
                    delimiter=csv_delimiter,
82
                    quotechar=csv_quote,
83
                    lineterminator=eol,
84
                    quoting=csv.QUOTE_MINIMAL)
85
                for row in reader:
86
                    data[row[0]] = row[1].decode('utf-8')
87
88
        return data
89
    elif fileFormat == 'sh':
90
        data = {}
91
        pattern = re.compile(r'^([^=]+)="(.*)"$')
92
        with open(path, 'r') as f:
93
            for line in f:
94
                match = pattern.match(line)
95
                if match:
96
                    k = match.group(1)
97
                    v = match.group(2)
98
99
                    if PY3:
100
                        data[k] = v
101
                    else:
102
                        data[k] = v.decode('utf-8')
103
104
        return data
105
    elif fileFormat == 'json':
106
        with open(path, 'r') as f:
107
            return json.load(f)
108
    else:
109
        with open(path, 'r') as f:
110
            return f.read()
111
112
113
@pytest.mark.parametrize(
114
    'data,data_format,target,section,expected_target,expected_data,'
115
    'csv_delimiter,csv_quote,csv_eol',
116
    [
117
        (
118
            {
119
                'version': 'v1.1.0-3-g439e52',
120
                'abbrev_commit': '40aaf83',
121
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
122
                'author_name': u'Se\u0301bastien Dupond',
123
                'commit_date': '2016-03-01T09:33:33+0000',
124
                'commit_timestamp': '1456824813',
125
                'deploy_date': '2016-03-02T11:33:45+0000',
126
                'deploy_timestamp': '1456918425',
127
                'branches': ['master', 'feature/my_feature']
128
            },
129
            'sh',
130
            'version',
131
            '',
132
            'version.sh',
133
            {
134
                'version': 'v1.1.0-3-g439e52',
135
                'abbrev_commit': '40aaf83',
136
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
137
                'author_name': u'Se\u0301bastien Dupond',
138
                'commit_date': '2016-03-01T09:33:33+0000',
139
                'commit_timestamp': '1456824813',
140
                'deploy_date': '2016-03-02T11:33:45+0000',
141
                'deploy_timestamp': '1456918425',
142
                'branches': "['master', 'feature/my_feature']"
143
            },
144
            None,
145
            None,
146
            None,
147
        ),
148
        (
149
            {
150
                'version': 'v1.1.0-3-g439e52',
151
                'abbrev_commit': '40aaf83',
152
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
153
                'author_name': u'Se\u0301bastien Dupond',
154
                'commit_date': '2016-03-01T09:33:33+0000',
155
                'commit_timestamp': '1456824813',
156
                'deploy_date': '2016-03-02T11:33:45+0000',
157
                'deploy_timestamp': '1456918425',
158
                'branches': ['master', 'feature/my_feature']
159
            },
160
            'csv',
161
            'version',
162
            '',
163
            'version.csv',
164
            {
165
                'version': 'v1.1.0-3-g439e52',
166
                'abbrev_commit': '40aaf83',
167
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
168
                'author_name': u'Se\u0301bastien Dupond',
169
                'commit_date': '2016-03-01T09:33:33+0000',
170
                'commit_timestamp': '1456824813',
171
                'deploy_date': '2016-03-02T11:33:45+0000',
172
                'deploy_timestamp': '1456918425',
173
                'branches': "['master', 'feature/my_feature']"
174
            },
175
            ',',
176
            '"',
177
            'lf',
178
        ),
179
        (
180
            {
181
                'version': 'v1.1.0-3-g439e52',
182
                'abbrev_commit': '40aaf83',
183
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
184
                'author_name': u'Se\u0301bastien Dupond',
185
                'commit_date': '2016-03-01T09:33:33+0000',
186
                'commit_timestamp': '1456824813',
187
                'deploy_date': '2016-03-02T11:33:45+0000',
188
                'deploy_timestamp': '1456918425',
189
                'branches': ['master', 'feature/my_feature']
190
            },
191
            'csv',
192
            'version',
193
            '',
194
            'version.csv',
195
            {
196
                'version': 'v1.1.0-3-g439e52',
197
                'abbrev_commit': '40aaf83',
198
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
199
                'author_name': u'Se\u0301bastien Dupond',
200
                'commit_date': '2016-03-01T09:33:33+0000',
201
                'commit_timestamp': '1456824813',
202
                'deploy_date': '2016-03-02T11:33:45+0000',
203
                'deploy_timestamp': '1456918425',
204
                'branches': "['master', 'feature/my_feature']"
205
            },
206
            ';',
207
            '\'',
208
            'crlf',
209
        ),
210
        (
211
            {
212
                'version': 'v1.1.0-3-g439e52',
213
                'abbrev_commit': '40aaf83',
214
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
215
                'author_name': u'Se\u0301bastien Dupond',
216
                'commit_date': '2016-03-01T09:33:33+0000',
217
                'commit_timestamp': '1456824813',
218
                'deploy_date': '2016-03-02T11:33:45+0000',
219
                'deploy_timestamp': '1456918425',
220
                'branches': ['master', 'feature/my_feature']
221
            },
222
            'ini',
223
            'version',
224
            '',
225
            'version.ini',
226
            {
227
                'version': 'v1.1.0-3-g439e52',
228
                'abbrev_commit': '40aaf83',
229
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
230
                'author_name': u'Se\u0301bastien Dupond',
231
                'commit_date': '2016-03-01T09:33:33+0000',
232
                'commit_timestamp': '1456824813',
233
                'deploy_date': '2016-03-02T11:33:45+0000',
234
                'deploy_timestamp': '1456918425',
235
                'branches': "['master', 'feature/my_feature']"
236
            },
237
            None,
238
            None,
239
            None,
240
        ),
241
        (
242
            {
243
                'version': 'v1.1.0-3-g439e52',
244
                'abbrev_commit': '40aaf83',
245
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
246
                'author_name': u'Se\u0301bastien Dupond',
247
                'commit_date': '2016-03-01T09:33:33+0000',
248
                'commit_timestamp': '1456824813',
249
                'deploy_date': '2016-03-02T11:33:45+0000',
250
                'deploy_timestamp': '1456918425',
251
            },
252
            'ini',
253
            'version',
254
            'parameters.git',
255
            'version.ini',
256
            {
257
                'version': 'v1.1.0-3-g439e52',
258
                'abbrev_commit': '40aaf83',
259
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
260
                'author_name': u'Se\u0301bastien Dupond',
261
                'commit_date': '2016-03-01T09:33:33+0000',
262
                'commit_timestamp': '1456824813',
263
                'deploy_date': '2016-03-02T11:33:45+0000',
264
                'deploy_timestamp': '1456918425',
265
            },
266
            None,
267
            None,
268
            None,
269
        ),
270
        (
271
            {
272
                'version': 'v1.1.0-3-g439e52',
273
                'abbrev_commit': '40aaf83',
274
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
275
                'commit_date': '2016-03-01T09:33:33+0000',
276
                'commit_timestamp': '1456824813',
277
                'deploy_date': '2016-03-02T11:33:45+0000',
278
                'deploy_timestamp': '1456918425',
279
            },
280
            'json',
281
            'version',
282
            '',
283
            'version.json',
284
            {
285
                'version': 'v1.1.0-3-g439e52',
286
                'abbrev_commit': '40aaf83',
287
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
288
                'commit_date': '2016-03-01T09:33:33+0000',
289
                'commit_timestamp': '1456824813',
290
                'deploy_date': '2016-03-02T11:33:45+0000',
291
                'deploy_timestamp': '1456918425',
292
            },
293
            None,
294
            None,
295
            None,
296
        ),
297
        (
298
            {
299
                'version': 'v1.1.0-3-g439e52',
300
                'abbrev_commit': '40aaf83',
301
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
302
                'author_name': u'Se\u0301bastien Dupond',
303
                'commit_date': '2016-03-01T09:33:33+0000',
304
                'commit_timestamp': '1456824813',
305
                'deploy_date': '2016-03-02T11:33:45+0000',
306
                'deploy_timestamp': '1456918425',
307
            },
308
            'json',
309
            'version',
310
            'parameters.git',
311
            'version.json',
312
            {
313
                'parameters': {
314
                    'git': {
315
                        'version': 'v1.1.0-3-g439e52',
316
                        'abbrev_commit': '40aaf83',
317
                        'full_commit':
318
                            '40aaf83894b98898895d478f8b7cc4a866b1d62c',
319
                        'author_name': u'Se\u0301bastien Dupond',
320
                        'commit_date': '2016-03-01T09:33:33+0000',
321
                        'commit_timestamp': '1456824813',
322
                        'deploy_date': '2016-03-02T11:33:45+0000',
323
                        'deploy_timestamp': '1456918425',
324
                    }
325
                }
326
            },
327
            None,
328
            None,
329
            None,
330
        ),
331
        (
332
            {
333
                'version': 'v1.1.0-3-g439e52',
334
                'abbrev_commit': '40aaf83',
335
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
336
                'commit_date': '2016-03-01T09:33:33+0000',
337
                'commit_timestamp': '1456824813',
338
                'deploy_date': '2016-03-02T11:33:45+0000',
339
                'deploy_timestamp': '1456918425',
340
            },
341
            'yml',
342
            'version',
343
            '',
344
            'version.yml',
345
            {
346
                'version': 'v1.1.0-3-g439e52',
347
                'abbrev_commit': '40aaf83',
348
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
349
                'commit_date': '2016-03-01T09:33:33+0000',
350
                'commit_timestamp': '1456824813',
351
                'deploy_date': '2016-03-02T11:33:45+0000',
352
                'deploy_timestamp': '1456918425',
353
            },
354
            None,
355
            None,
356
            None,
357
        ),
358
        (
359
            {
360
                'version': 'v1.1.0-3-g439e52',
361
                'abbrev_commit': '40aaf83',
362
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
363
                'author_name': u'Se\u0301bastien Dupond',
364
                'commit_date': '2016-03-01T09:33:33+0000',
365
                'commit_timestamp': '1456824813',
366
                'deploy_date': '2016-03-02T11:33:45+0000',
367
                'deploy_timestamp': '1456918425',
368
            },
369
            'yml',
370
            'version',
371
            'parameters.git',
372
            'version.yml',
373
            {
374
                'parameters': {
375
                    'git': {
376
                        'version': 'v1.1.0-3-g439e52',
377
                        'abbrev_commit': '40aaf83',
378
                        'full_commit':
379
                            '40aaf83894b98898895d478f8b7cc4a866b1d62c',
380
                        'author_name': u'Se\u0301bastien Dupond',
381
                        'commit_date': '2016-03-01T09:33:33+0000',
382
                        'commit_timestamp': '1456824813',
383
                        'deploy_date': '2016-03-02T11:33:45+0000',
384
                        'deploy_timestamp': '1456918425',
385
                    }
386
                }
387
            },
388
            None,
389
            None,
390
            None,
391
        ),
392
        (
393
            {},
394
            'yml',
395
            'version',
396
            '',
397
            'version.yml',
398
            None,
399
            None,
400
            None,
401
            None,
402
        ),
403
        (
404
            {
405
                'version': 'v1.1.0-3-g439e52',
406
                'abbrev_commit': '40aaf83',
407
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
408
                'commit_date': '2016-03-01T09:33:33+0000',
409
                'commit_timestamp': '1456824813',
410
                'deploy_date': '2016-03-02T11:33:45+0000',
411
                'deploy_timestamp': '1456918425',
412
            },
413
            'xml',
414
            'version',
415
            '',
416
            'version.xml',
417
            {
418
                'app_version': {
419
                    'version': 'v1.1.0-3-g439e52',
420
                    'abbrev_commit': '40aaf83',
421
                    'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
422
                    'commit_date': '2016-03-01T09:33:33+0000',
423
                    'commit_timestamp': '1456824813',
424
                    'deploy_date': '2016-03-02T11:33:45+0000',
425
                    'deploy_timestamp': '1456918425',
426
                }
427
            },
428
            None,
429
            None,
430
            None,
431
        ),
432
        (
433
            {
434
                'version': 'v1.1.0-3-g439e52',
435
                'abbrev_commit': '40aaf83',
436
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
437
                'author_name': u'Se\u0301bastien Dupond',
438
                'commit_date': '2016-03-01T09:33:33+0000',
439
                'commit_timestamp': '1456824813',
440
                'deploy_date': '2016-03-02T11:33:45+0000',
441
                'deploy_timestamp': '1456918425',
442
            },
443
            'xml',
444
            'version',
445
            'parameters.git',
446
            'version.xml',
447
            {
448
                'parameters': {
449
                    'git': {
450
                        'version': 'v1.1.0-3-g439e52',
451
                        'abbrev_commit': '40aaf83',
452
                        'full_commit':
453
                            '40aaf83894b98898895d478f8b7cc4a866b1d62c',
454
                        'author_name': u'Se\u0301bastien Dupond',
455
                        'commit_date': '2016-03-01T09:33:33+0000',
456
                        'commit_timestamp': '1456824813',
457
                        'deploy_date': '2016-03-02T11:33:45+0000',
458
                        'deploy_timestamp': '1456918425',
459
                    }
460
                }
461
            },
462
            None,
463
            None,
464
            None,
465
        )
466
    ]
467
)
468
def test_dump(output_dir, data, data_format, target,
469
              section, expected_target, expected_data,
470
              csv_delimiter, csv_quote, csv_eol):
471
    appdumper = AppDumper()
472
473
    resultTarget = appdumper.dump(
474
        data=data,
475
        fileformat=data_format,
476
        target=target,
477
        cwd=output_dir,
478
        namespace=section,
479
        csv_delimiter=csv_delimiter,
480
        csv_quote=csv_quote,
481
        csv_eol=csv_eol)
482
483
    assert output_dir + '/' + expected_target == resultTarget
484
    assert expected_data == get_file_content(
485
        output_dir + '/' + expected_target, section, data_format,
486
        csv_delimiter=csv_delimiter, csv_quote=csv_quote, csv_eol=csv_eol)
487