Completed
Push — master ( 419779...7515a9 )
by Charles
02:09
created

getFileContent()   F

Complexity

Conditions 13

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
cc 13
c 3
b 1
f 2
dl 0
loc 26
rs 2.7716

How to fix   Complexity   

Complexity

Complex classes like getFileContent() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
3
from mock import patch
4
import pytest
5
import os
6
import shutil
7
from pprint import pprint
8
9
from git_app_version.helper.pyversion import PY3
10
from git_app_version.dumper import FileDumper as AppDumper
11
import json
12
import xmltodict
13
14
# import configparser
15
16
# from backports import configparser
17
#
18
try:
19
    import ConfigParser as configparser
20
except ImportError:
21
    import configparser
22
23
import yaml
24
try:
25
    from yaml import CLoader as Loader, CDumper as Dumper
26
except ImportError:
27
    from yaml import Loader,Dumper
28
29
@pytest.fixture
30
def output_dir():
31
    cwd = os.path.realpath(os.path.dirname(__file__))
32
    path = cwd+'/output'
33
    if os.path.exists(path):
34
        shutil.rmtree(path)
35
    os.makedirs(path, 493)
36
37
    return path
38
39
def get_file_content(path, section=None, fileFormat=None):
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 config.has_section(section):
52
            for k,v in config.items(section):
53
                if PY3:
54
                    data[k] = v
55
                else:
56
                    data[k] = v.decode('utf-8')
57
58
        return data
59
    elif fileFormat == 'json':
60
        with open(path, 'r') as f:
61
            return json.load(f)
62
    else:
63
        with open(path, 'r') as f:
64
            return f.read()
65
66
@pytest.mark.parametrize("data,data_format,target,section,expected_target,expected_data", [
67
    (
68
        {
69
            'version': 'v1.1.0-3-g439e52',
70
            'abbrev_commit': '40aaf83',
71
            'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
72
            'author_name': u'Se\u0301bastien Dupond',
73
            'commit_date': '2016-03-01T09:33:33+0000',
74
            'commit_timestamp': '1456824813',
75
            'deploy_date': '2016-03-02T11:33:45+0000',
76
            'deploy_timestamp': '1456918425',
77
            'branches': ['master', 'feature/my_feature']
78
        },
79
        'ini',
80
        'version',
81
        'app_version',
82
        'version.ini',
83
        {
84
            'version': 'v1.1.0-3-g439e52',
85
            'abbrev_commit': '40aaf83',
86
            'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
87
            'author_name': u'Se\u0301bastien Dupond',
88
            'commit_date': '2016-03-01T09:33:33+0000',
89
            'commit_timestamp': '1456824813',
90
            'deploy_date': '2016-03-02T11:33:45+0000',
91
            'deploy_timestamp': '1456918425',
92
            'branches': "['master', 'feature/my_feature']"
93
        },
94
    ),
95
    (
96
        {
97
            'version': 'v1.1.0-3-g439e52',
98
            'abbrev_commit': '40aaf83',
99
            'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
100
            'author_name': u'Se\u0301bastien Dupond',
101
            'commit_date': '2016-03-01T09:33:33+0000',
102
            'commit_timestamp': '1456824813',
103
            'deploy_date': '2016-03-02T11:33:45+0000',
104
            'deploy_timestamp': '1456918425',
105
        },
106
        'ini',
107
        'version',
108
        'parameters.git',
109
        'version.ini',
110
        {
111
            'version': 'v1.1.0-3-g439e52',
112
            'abbrev_commit': '40aaf83',
113
            'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
114
            'author_name': u'Se\u0301bastien Dupond',
115
            'commit_date': '2016-03-01T09:33:33+0000',
116
            'commit_timestamp': '1456824813',
117
            'deploy_date': '2016-03-02T11:33:45+0000',
118
            'deploy_timestamp': '1456918425',
119
        },
120
    ),
121
    (
122
        {
123
            'version': 'v1.1.0-3-g439e52',
124
            'abbrev_commit': '40aaf83',
125
            'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
126
            'commit_date': '2016-03-01T09:33:33+0000',
127
            'commit_timestamp': '1456824813',
128
            'deploy_date': '2016-03-02T11:33:45+0000',
129
            'deploy_timestamp': '1456918425',
130
        },
131
        'json',
132
        'version',
133
        '',
134
        'version.json',
135
        {
136
            'version': 'v1.1.0-3-g439e52',
137
            'abbrev_commit': '40aaf83',
138
            'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
139
            'commit_date': '2016-03-01T09:33:33+0000',
140
            'commit_timestamp': '1456824813',
141
            'deploy_date': '2016-03-02T11:33:45+0000',
142
            'deploy_timestamp': '1456918425',
143
        },
144
    ),
145
    (
146
        {
147
            'version': 'v1.1.0-3-g439e52',
148
            'abbrev_commit': '40aaf83',
149
            'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
150
            'author_name': u'Se\u0301bastien Dupond',
151
            'commit_date': '2016-03-01T09:33:33+0000',
152
            'commit_timestamp': '1456824813',
153
            'deploy_date': '2016-03-02T11:33:45+0000',
154
            'deploy_timestamp': '1456918425',
155
        },
156
        'json',
157
        'version',
158
        'parameters.git',
159
        'version.json',
160
        {
161
            'parameters': {
162
                'git': {
163
                    'version': 'v1.1.0-3-g439e52',
164
                    'abbrev_commit': '40aaf83',
165
                    'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
166
                    'author_name': u'Se\u0301bastien Dupond',
167
                    'commit_date': '2016-03-01T09:33:33+0000',
168
                    'commit_timestamp': '1456824813',
169
                    'deploy_date': '2016-03-02T11:33:45+0000',
170
                    'deploy_timestamp': '1456918425',
171
                }
172
            }
173
        }
174
    ),
175
    (
176
        {
177
            'version': 'v1.1.0-3-g439e52',
178
            'abbrev_commit': '40aaf83',
179
            'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
180
            'commit_date': '2016-03-01T09:33:33+0000',
181
            'commit_timestamp': '1456824813',
182
            'deploy_date': '2016-03-02T11:33:45+0000',
183
            'deploy_timestamp': '1456918425',
184
        },
185
        'yml',
186
        'version',
187
        '',
188
        'version.yml',
189
        {
190
            'version': 'v1.1.0-3-g439e52',
191
            'abbrev_commit': '40aaf83',
192
            'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
193
            'commit_date': '2016-03-01T09:33:33+0000',
194
            'commit_timestamp': '1456824813',
195
            'deploy_date': '2016-03-02T11:33:45+0000',
196
            'deploy_timestamp': '1456918425',
197
        },
198
    ),
199
    (
200
        {
201
            'version': 'v1.1.0-3-g439e52',
202
            'abbrev_commit': '40aaf83',
203
            'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
204
            'author_name': u'Se\u0301bastien Dupond',
205
            'commit_date': '2016-03-01T09:33:33+0000',
206
            'commit_timestamp': '1456824813',
207
            'deploy_date': '2016-03-02T11:33:45+0000',
208
            'deploy_timestamp': '1456918425',
209
        },
210
        'yml',
211
        'version',
212
        'parameters.git',
213
        'version.yml',
214
        {
215
            'parameters': {
216
                'git': {
217
                    'version': 'v1.1.0-3-g439e52',
218
                    'abbrev_commit': '40aaf83',
219
                    'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
220
                    'author_name': u'Se\u0301bastien Dupond',
221
                    'commit_date': '2016-03-01T09:33:33+0000',
222
                    'commit_timestamp': '1456824813',
223
                    'deploy_date': '2016-03-02T11:33:45+0000',
224
                    'deploy_timestamp': '1456918425',
225
                }
226
            }
227
        },
228
    ),
229
    (
230
        {},
231
        'yml',
232
        'version',
233
        '',
234
        'version.yml',
235
        None,
236
    ),
237
    (
238
        {
239
            'version': 'v1.1.0-3-g439e52',
240
            'abbrev_commit': '40aaf83',
241
            'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
242
            'commit_date': '2016-03-01T09:33:33+0000',
243
            'commit_timestamp': '1456824813',
244
            'deploy_date': '2016-03-02T11:33:45+0000',
245
            'deploy_timestamp': '1456918425',
246
        },
247
        'xml',
248
        'version',
249
        '',
250
        'version.xml',
251
        {
252
            'app_version': {
253
                'version': 'v1.1.0-3-g439e52',
254
                'abbrev_commit': '40aaf83',
255
                'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
256
                'commit_date': '2016-03-01T09:33:33+0000',
257
                'commit_timestamp': '1456824813',
258
                'deploy_date': '2016-03-02T11:33:45+0000',
259
                'deploy_timestamp': '1456918425',
260
            }
261
        },
262
    ),
263
    (
264
        {
265
            'version': 'v1.1.0-3-g439e52',
266
            'abbrev_commit': '40aaf83',
267
            'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
268
            'author_name': u'Se\u0301bastien Dupond',
269
            'commit_date': '2016-03-01T09:33:33+0000',
270
            'commit_timestamp': '1456824813',
271
            'deploy_date': '2016-03-02T11:33:45+0000',
272
            'deploy_timestamp': '1456918425',
273
        },
274
        'xml',
275
        'version',
276
        'parameters.git',
277
        'version.xml',
278
        {
279
            'parameters': {
280
                'git': {
281
                    'version': 'v1.1.0-3-g439e52',
282
                    'abbrev_commit': '40aaf83',
283
                    'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c',
284
                    'author_name': u'Se\u0301bastien Dupond',
285
                    'commit_date': '2016-03-01T09:33:33+0000',
286
                    'commit_timestamp': '1456824813',
287
                    'deploy_date': '2016-03-02T11:33:45+0000',
288
                    'deploy_timestamp': '1456918425',
289
                }
290
            }
291
        },
292
    )
293
])
294
def test_dump(output_dir, data, data_format, target, section, expected_target, expected_data):
295
    appdumper = AppDumper()
296
297
    cwd = os.path.realpath(os.path.dirname(__file__))
298
    resultTarget = appdumper.dump(data, data_format, target, output_dir, section)
299
300
    assert output_dir+'/'+expected_target == resultTarget
301
    assert expected_data == get_file_content(output_dir+'/'+expected_target, section, data_format)
302