test_config.py 13.8 KB
Newer Older
Kai Chen's avatar
Kai Chen committed
1
# Copyright (c) Open-MMLab. All rights reserved.
2
import argparse
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
3
import json
4
import os
Kai Chen's avatar
Kai Chen committed
5
import os.path as osp
6
import shutil
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
7
import tempfile
Kai Chen's avatar
Kai Chen committed
8
9

import pytest
10
import yaml
Kai Chen's avatar
Kai Chen committed
11

Kevin's avatar
Kevin committed
12
from mmcv import Config, DictAction, dump, load
Kai Chen's avatar
Kai Chen committed
13

14
15
data_path = osp.join(osp.dirname(osp.dirname(__file__)), 'data')

Kai Chen's avatar
Kai Chen committed
16

Kai Chen's avatar
Kai Chen committed
17
def test_construct():
Kai Chen's avatar
Kai Chen committed
18
19
20
21
22
23
    cfg = Config()
    assert cfg.filename is None
    assert cfg.text == ''
    assert len(cfg) == 0
    assert cfg._cfg_dict == {}

Kai Chen's avatar
Kai Chen committed
24
25
26
    with pytest.raises(TypeError):
        Config([0, 1])

Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
27
    cfg_dict = dict(item1=[1, 2], item2=dict(a=0), item3=True, item4='test')
28
    # test a.py
29
    cfg_file = osp.join(data_path, 'config/a.py')
30
31
32
33
34
35
36
37
38
39
40
41
    cfg = Config(cfg_dict, filename=cfg_file)
    assert isinstance(cfg, Config)
    assert cfg.filename == cfg_file
    assert cfg.text == open(cfg_file, 'r').read()
    assert cfg.dump() == cfg.pretty_text
    with tempfile.TemporaryDirectory() as temp_config_dir:
        dump_file = osp.join(temp_config_dir, 'a.py')
        cfg.dump(dump_file)
        assert cfg.dump() == open(dump_file, 'r').read()
        assert Config.fromfile(dump_file)

    # test b.json
42
    cfg_file = osp.join(data_path, 'config/b.json')
43
44
45
46
47
48
49
50
51
52
53
54
    cfg = Config(cfg_dict, filename=cfg_file)
    assert isinstance(cfg, Config)
    assert cfg.filename == cfg_file
    assert cfg.text == open(cfg_file, 'r').read()
    assert cfg.dump() == json.dumps(cfg_dict)
    with tempfile.TemporaryDirectory() as temp_config_dir:
        dump_file = osp.join(temp_config_dir, 'b.json')
        cfg.dump(dump_file)
        assert cfg.dump() == open(dump_file, 'r').read()
        assert Config.fromfile(dump_file)

    # test c.yaml
55
    cfg_file = osp.join(data_path, 'config/c.yaml')
56
57
58
59
60
61
62
63
64
65
    cfg = Config(cfg_dict, filename=cfg_file)
    assert isinstance(cfg, Config)
    assert cfg.filename == cfg_file
    assert cfg.text == open(cfg_file, 'r').read()
    assert cfg.dump() == yaml.dump(cfg_dict)
    with tempfile.TemporaryDirectory() as temp_config_dir:
        dump_file = osp.join(temp_config_dir, 'c.yaml')
        cfg.dump(dump_file)
        assert cfg.dump() == open(dump_file, 'r').read()
        assert Config.fromfile(dump_file)
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
66

67
    # test h.py
68
    cfg_file = osp.join(data_path, 'config/h.py')
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    cfg_dict = dict(
        item1='h.py',
        item2=f'{osp.dirname(__file__)}/data/config',
        item3='abc_h')
    cfg = Config(cfg_dict, filename=cfg_file)
    assert isinstance(cfg, Config)
    assert cfg.filename == cfg_file
    assert cfg.text == open(cfg_file, 'r').read()
    assert cfg.dump() == cfg.pretty_text
    with tempfile.TemporaryDirectory() as temp_config_dir:
        dump_file = osp.join(temp_config_dir, 'h.py')
        cfg.dump(dump_file)
        assert cfg.dump() == open(dump_file, 'r').read()
        assert Config.fromfile(dump_file)
        assert Config.fromfile(dump_file)['item1'] == cfg_dict['item1']
        assert Config.fromfile(dump_file)['item2'] == cfg_dict['item2']
        assert Config.fromfile(dump_file)['item3'] == cfg_dict['item3']

    # test no use_predefined_variable
    cfg_dict = dict(
        item1='{{fileBasename}}',
        item2='{{ fileDirname}}',
        item3='abc_{{ fileBasenameNoExtension }}')
    assert Config.fromfile(cfg_file, False)
    assert Config.fromfile(cfg_file, False)['item1'] == cfg_dict['item1']
    assert Config.fromfile(cfg_file, False)['item2'] == cfg_dict['item2']
    assert Config.fromfile(cfg_file, False)['item3'] == cfg_dict['item3']

    # test p.yaml
98
    cfg_file = osp.join(data_path, 'config/p.yaml')
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    cfg_dict = dict(item1=f'{osp.dirname(__file__)}/data/config')
    cfg = Config(cfg_dict, filename=cfg_file)
    assert isinstance(cfg, Config)
    assert cfg.filename == cfg_file
    assert cfg.text == open(cfg_file, 'r').read()
    assert cfg.dump() == yaml.dump(cfg_dict)
    with tempfile.TemporaryDirectory() as temp_config_dir:
        dump_file = osp.join(temp_config_dir, 'p.yaml')
        cfg.dump(dump_file)
        assert cfg.dump() == open(dump_file, 'r').read()
        assert Config.fromfile(dump_file)
        assert Config.fromfile(dump_file)['item1'] == cfg_dict['item1']

    # test no use_predefined_variable
    assert Config.fromfile(cfg_file, False)
    assert Config.fromfile(cfg_file, False)['item1'] == '{{ fileDirname }}'

    # test o.json
117
    cfg_file = osp.join(data_path, 'config/o.json')
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    cfg_dict = dict(item1=f'{osp.dirname(__file__)}/data/config')
    cfg = Config(cfg_dict, filename=cfg_file)
    assert isinstance(cfg, Config)
    assert cfg.filename == cfg_file
    assert cfg.text == open(cfg_file, 'r').read()
    assert cfg.dump() == json.dumps(cfg_dict)
    with tempfile.TemporaryDirectory() as temp_config_dir:
        dump_file = osp.join(temp_config_dir, 'o.json')
        cfg.dump(dump_file)
        assert cfg.dump() == open(dump_file, 'r').read()
        assert Config.fromfile(dump_file)
        assert Config.fromfile(dump_file)['item1'] == cfg_dict['item1']

    # test no use_predefined_variable
    assert Config.fromfile(cfg_file, False)
    assert Config.fromfile(cfg_file, False)['item1'] == '{{ fileDirname }}'

Kai Chen's avatar
Kai Chen committed
135
136

def test_fromfile():
lizz's avatar
lizz committed
137
    for filename in ['a.py', 'a.b.py', 'b.json', 'c.yaml']:
138
        cfg_file = osp.join(data_path, 'config', filename)
Kai Chen's avatar
Kai Chen committed
139
140
141
        cfg = Config.fromfile(cfg_file)
        assert isinstance(cfg, Config)
        assert cfg.filename == cfg_file
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
142
143
        assert cfg.text == osp.abspath(osp.expanduser(cfg_file)) + '\n' + \
            open(cfg_file, 'r').read()
Kai Chen's avatar
Kai Chen committed
144

145
146
147
148
149
150
151
152
153
154
155
156
157
    # test custom_imports for Config.fromfile
    cfg_file = osp.join(data_path, 'config', 'q.py')
    imported_file = osp.join(data_path, 'config', 'r.py')
    target_pkg = osp.join(osp.dirname(__file__), 'r.py')

    # Since the imported config will be regarded as a tmp file
    # it should be copied to the directory at the same level
    shutil.copy(imported_file, target_pkg)
    Config.fromfile(cfg_file, import_custom_modules=True)

    assert os.environ.pop('TEST_VALUE') == 'test'
    os.remove(target_pkg)

158
159
    with pytest.raises(FileNotFoundError):
        Config.fromfile('no_such_file.py')
Kai Chen's avatar
Kai Chen committed
160
    with pytest.raises(IOError):
161
        Config.fromfile(osp.join(data_path, 'color.jpg'))
Kai Chen's avatar
Kai Chen committed
162
163


Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
164
def test_merge_from_base():
165
    cfg_file = osp.join(data_path, 'config/d.py')
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
166
167
168
    cfg = Config.fromfile(cfg_file)
    assert isinstance(cfg, Config)
    assert cfg.filename == cfg_file
169
    base_cfg_file = osp.join(data_path, 'config/base.py')
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
170
171
172
173
174
175
176
177
178
179
180
    merge_text = osp.abspath(osp.expanduser(base_cfg_file)) + '\n' + \
        open(base_cfg_file, 'r').read()
    merge_text += '\n' + osp.abspath(osp.expanduser(cfg_file)) + '\n' + \
                  open(cfg_file, 'r').read()
    assert cfg.text == merge_text
    assert cfg.item1 == [2, 3]
    assert cfg.item2.a == 1
    assert cfg.item3 is False
    assert cfg.item4 == 'test_base'

    with pytest.raises(TypeError):
181
        Config.fromfile(osp.join(data_path, 'config/e.py'))
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
182
183
184


def test_merge_from_multiple_bases():
185
    cfg_file = osp.join(data_path, 'config/l.py')
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
186
187
188
189
190
191
192
193
    cfg = Config.fromfile(cfg_file)
    assert isinstance(cfg, Config)
    assert cfg.filename == cfg_file
    # cfg.field
    assert cfg.item1 == [1, 2]
    assert cfg.item2.a == 0
    assert cfg.item3 is False
    assert cfg.item4 == 'test'
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
194
195
196
    assert cfg.item5 == dict(a=0, b=1)
    assert cfg.item6 == [dict(a=0), dict(b=1)]
    assert cfg.item7 == dict(a=[0, 1, 2], b=dict(c=[3.1, 4.2, 5.3]))
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
197
198

    with pytest.raises(KeyError):
199
        Config.fromfile(osp.join(data_path, 'config/m.py'))
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
200
201
202


def test_merge_recursive_bases():
203
    cfg_file = osp.join(data_path, 'config/f.py')
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
204
205
206
207
208
209
210
211
212
213
214
    cfg = Config.fromfile(cfg_file)
    assert isinstance(cfg, Config)
    assert cfg.filename == cfg_file
    # cfg.field
    assert cfg.item1 == [2, 3]
    assert cfg.item2.a == 1
    assert cfg.item3 is False
    assert cfg.item4 == 'test_recursive_bases'


def test_merge_from_dict():
215
    cfg_file = osp.join(data_path, 'config/a.py')
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
216
    cfg = Config.fromfile(cfg_file)
217
    input_options = {'item2.a': 1, 'item2.b': 0.1, 'item3': False}
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
218
    cfg.merge_from_dict(input_options)
219
    assert cfg.item2 == dict(a=1, b=0.1)
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
220
221
    assert cfg.item3 is False

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
    cfg_file = osp.join(data_path, 'config/s.py')
    cfg = Config.fromfile(cfg_file)

    # Allow list keys
    input_options = {'item.0.a': 1, 'item.1.b': 1}
    cfg.merge_from_dict(input_options, allow_list_keys=True)
    assert cfg.item == [{'a': 1}, {'b': 1, 'c': 0}]

    # allow_list_keys is False
    input_options = {'item.0.a': 1, 'item.1.b': 1}
    with pytest.raises(TypeError):
        cfg.merge_from_dict(input_options, allow_list_keys=False)

    # Overflowed index number
    input_options = {'item.2.a': 1}
    with pytest.raises(KeyError):
        cfg.merge_from_dict(input_options, allow_list_keys=True)

Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
240
241

def test_merge_delete():
242
    cfg_file = osp.join(data_path, 'config/delete.py')
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
243
244
245
246
247
248
249
250
251
    cfg = Config.fromfile(cfg_file)
    # cfg.field
    assert cfg.item1 == [1, 2]
    assert cfg.item2 == dict(b=0)
    assert cfg.item3 is True
    assert cfg.item4 == 'test'
    assert '_delete_' not in cfg.item2


252
253
def test_merge_intermediate_variable():

254
    cfg_file = osp.join(data_path, 'config/i_child.py')
255
256
257
258
259
260
261
262
263
264
265
    cfg = Config.fromfile(cfg_file)
    # cfg.field
    assert cfg.item1 == [1, 2]
    assert cfg.item2 == dict(a=0)
    assert cfg.item3 is True
    assert cfg.item4 == 'test'
    assert cfg.item_cfg == dict(b=2)
    assert cfg.item5 == dict(cfg=dict(b=1))
    assert cfg.item6 == dict(cfg=dict(b=2))


266
def test_fromfile_in_config():
267
    cfg_file = osp.join(data_path, 'config/code.py')
268
269
270
271
272
273
274
275
276
    cfg = Config.fromfile(cfg_file)
    # cfg.field
    assert cfg.cfg.item1 == [1, 2]
    assert cfg.cfg.item2 == dict(a=0)
    assert cfg.cfg.item3 is True
    assert cfg.cfg.item4 == 'test'
    assert cfg.item5 == 1


Kai Chen's avatar
Kai Chen committed
277
278
279
280
def test_dict():
    cfg_dict = dict(item1=[1, 2], item2=dict(a=0), item3=True, item4='test')

    for filename in ['a.py', 'b.json', 'c.yaml']:
281
        cfg_file = osp.join(data_path, 'config', filename)
Kai Chen's avatar
Kai Chen committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
        cfg = Config.fromfile(cfg_file)

        # len(cfg)
        assert len(cfg) == 4
        # cfg.keys()
        assert set(cfg.keys()) == set(cfg_dict.keys())
        assert set(cfg._cfg_dict.keys()) == set(cfg_dict.keys())
        # cfg.values()
        for value in cfg.values():
            assert value in cfg_dict.values()
        # cfg.items()
        for name, value in cfg.items():
            assert name in cfg_dict
            assert value in cfg_dict.values()
        # cfg.field
        assert cfg.item1 == cfg_dict['item1']
        assert cfg.item2 == cfg_dict['item2']
        assert cfg.item2.a == 0
        assert cfg.item3 == cfg_dict['item3']
        assert cfg.item4 == cfg_dict['item4']
302
303
        with pytest.raises(AttributeError):
            cfg.not_exist
Kai Chen's avatar
Kai Chen committed
304
305
306
307
308
        # field in cfg, cfg[field], cfg.get()
        for name in ['item1', 'item2', 'item3', 'item4']:
            assert name in cfg
            assert cfg[name] == cfg_dict[name]
            assert cfg.get(name) == cfg_dict[name]
309
            assert cfg.get('not_exist') is None
Kai Chen's avatar
Kai Chen committed
310
            assert cfg.get('not_exist', 0) == 0
311
312
313
            with pytest.raises(KeyError):
                cfg['not_exist']
        assert 'item1' in cfg
Kai Chen's avatar
Kai Chen committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
        assert 'not_exist' not in cfg
        # cfg.update()
        cfg.update(dict(item1=0))
        assert cfg.item1 == 0
        cfg.update(dict(item2=dict(a=1)))
        assert cfg.item2.a == 1


def test_setattr():
    cfg = Config()
    cfg.item1 = [1, 2]
    cfg.item2 = {'a': 0}
    cfg['item5'] = {'a': {'b': None}}
    assert cfg._cfg_dict['item1'] == [1, 2]
    assert cfg.item1 == [1, 2]
    assert cfg._cfg_dict['item2'] == {'a': 0}
    assert cfg.item2.a == 0
    assert cfg._cfg_dict['item5'] == {'a': {'b': None}}
    assert cfg.item5.a.b is None
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
333
334
335


def test_pretty_text():
336
    cfg_file = osp.join(data_path, 'config/l.py')
Jerry Jiarui XU's avatar
Jerry Jiarui XU committed
337
338
339
340
341
342
343
    cfg = Config.fromfile(cfg_file)
    with tempfile.TemporaryDirectory() as temp_config_dir:
        text_cfg_filename = osp.join(temp_config_dir, '_text_config.py')
        with open(text_cfg_filename, 'w') as f:
            f.write(cfg.pretty_text)
        text_cfg = Config.fromfile(text_cfg_filename)
    assert text_cfg._cfg_dict == cfg._cfg_dict
344
345
346
347
348
349
350
351
352
353
354


def test_dict_action():
    parser = argparse.ArgumentParser(description='Train a detector')
    parser.add_argument(
        '--options', nargs='+', action=DictAction, help='custom options')
    args = parser.parse_args(
        ['--options', 'item2.a=1', 'item2.b=0.1', 'item2.c=x', 'item3=false'])
    out_dict = {'item2.a': 1, 'item2.b': 0.1, 'item2.c': 'x', 'item3': False}
    assert args.options == out_dict

355
    cfg_file = osp.join(data_path, 'config/a.py')
356
357
358
359
    cfg = Config.fromfile(cfg_file)
    cfg.merge_from_dict(args.options)
    assert cfg.item2 == dict(a=1, b=0.1, c='x')
    assert cfg.item3 is False
360
361
362


def test_dump_mapping():
363
    cfg_file = osp.join(data_path, 'config/n.py')
364
365
366
367
368
369
370
371
    cfg = Config.fromfile(cfg_file)

    with tempfile.TemporaryDirectory() as temp_config_dir:
        text_cfg_filename = osp.join(temp_config_dir, '_text_config.py')
        cfg.dump(text_cfg_filename)
        text_cfg = Config.fromfile(text_cfg_filename)

    assert text_cfg._cfg_dict == cfg._cfg_dict
372
373
374


def test_reserved_key():
375
    cfg_file = osp.join(data_path, 'config/g.py')
376
377
    with pytest.raises(KeyError):
        Config.fromfile(cfg_file)
378
379
380
381
382
383
384
385
386
387
388
389
390
391


def test_syntax_error():
    temp_cfg_file = tempfile.NamedTemporaryFile(suffix='.py')
    temp_cfg_path = temp_cfg_file.name
    # write a file with syntax error
    with open(temp_cfg_path, 'w') as f:
        f.write('a=0b=dict(c=1)')
    with pytest.raises(
            SyntaxError,
            match='There are syntax errors in config '
            f'file {temp_cfg_path}'):
        Config.fromfile(temp_cfg_path)
    temp_cfg_file.close()
Kevin's avatar
Kevin committed
392
393
394


def test_pickle_support():
395
    cfg_file = osp.join(data_path, 'config/n.py')
Kevin's avatar
Kevin committed
396
397
398
399
400
401
402
403
    cfg = Config.fromfile(cfg_file)

    with tempfile.TemporaryDirectory() as temp_config_dir:
        pkl_cfg_filename = osp.join(temp_config_dir, '_pickle.pkl')
        dump(cfg, pkl_cfg_filename)
        pkl_cfg = load(pkl_cfg_filename)

    assert pkl_cfg._cfg_dict == cfg._cfg_dict