|
1
|
|
|
from __future__ import absolute_import |
|
2
|
|
|
# Project imports |
|
3
|
|
|
|
|
4
|
|
|
import os |
|
5
|
|
|
import sys |
|
6
|
|
|
import unittest |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
from mock import patch |
|
10
|
|
|
from tempfile import gettempdir |
|
11
|
|
|
|
|
12
|
|
|
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) |
|
13
|
|
|
|
|
14
|
|
|
from elodie import constants |
|
15
|
|
|
from elodie.config import load_config, load_plugin_config |
|
16
|
|
|
|
|
17
|
|
|
@patch('elodie.config.config_file', '%s/config.ini-singleton-success' % gettempdir()) |
|
18
|
|
|
def test_load_config_singleton_success(): |
|
19
|
|
|
with open('%s/config.ini-singleton-success' % gettempdir(), 'w') as f: |
|
20
|
|
|
f.write(""" |
|
21
|
|
|
[MapQuest] |
|
22
|
|
|
key=your-api-key-goes-here |
|
23
|
|
|
prefer_english_names=False |
|
24
|
|
|
""") |
|
25
|
|
|
if hasattr(load_config, 'config'): |
|
26
|
|
|
del load_config.config |
|
27
|
|
|
|
|
28
|
|
|
config = load_config() |
|
29
|
|
|
assert config['MapQuest']['key'] == 'your-api-key-goes-here', config.get('MapQuest', 'key') |
|
30
|
|
|
config.set('MapQuest', 'key', 'new-value') |
|
31
|
|
|
|
|
32
|
|
|
config = load_config() |
|
33
|
|
|
|
|
34
|
|
|
if hasattr(load_config, 'config'): |
|
35
|
|
|
del load_config.config |
|
36
|
|
|
|
|
37
|
|
|
assert config['MapQuest']['key'] == 'new-value', config.get('MapQuest', 'key') |
|
38
|
|
|
|
|
39
|
|
|
@patch('elodie.config.config_file', '%s/config.ini-does-not-exist' % gettempdir()) |
|
40
|
|
|
def test_load_config_singleton_no_file(): |
|
41
|
|
|
if hasattr(load_config, 'config'): |
|
42
|
|
|
del load_config.config |
|
43
|
|
|
|
|
44
|
|
|
config = load_config() |
|
45
|
|
|
|
|
46
|
|
|
if hasattr(load_config, 'config'): |
|
47
|
|
|
del load_config.config |
|
48
|
|
|
|
|
49
|
|
|
assert config == {}, config |
|
50
|
|
|
|
|
51
|
|
|
@patch('elodie.config.config_file', '%s/config.ini-load-plugin-config-unset-backwards-compat' % gettempdir()) |
|
52
|
|
|
def test_load_plugin_config_unset_backwards_compat(): |
|
53
|
|
|
with open('%s/config.ini-load-plugin-config-unset-backwards-compat' % gettempdir(), 'w') as f: |
|
54
|
|
|
f.write(""" |
|
55
|
|
|
""") |
|
56
|
|
|
if hasattr(load_config, 'config'): |
|
57
|
|
|
del load_config.config |
|
58
|
|
|
|
|
59
|
|
|
plugins = load_plugin_config() |
|
60
|
|
|
|
|
61
|
|
|
if hasattr(load_config, 'config'): |
|
62
|
|
|
del load_config.config |
|
63
|
|
|
|
|
64
|
|
|
assert plugins == [], plugins |
|
65
|
|
|
|
|
66
|
|
|
@patch('elodie.config.config_file', '%s/config.ini-load-plugin-config-exists-not-set' % gettempdir()) |
|
67
|
|
|
def test_load_plugin_config_exists_not_set(): |
|
68
|
|
|
with open('%s/config.ini-load-plugin-config-exists-not-set' % gettempdir(), 'w') as f: |
|
69
|
|
|
f.write(""" |
|
70
|
|
|
[Plugins] |
|
71
|
|
|
""") |
|
72
|
|
|
if hasattr(load_config, 'config'): |
|
73
|
|
|
del load_config.config |
|
74
|
|
|
|
|
75
|
|
|
plugins = load_plugin_config() |
|
76
|
|
|
|
|
77
|
|
|
if hasattr(load_config, 'config'): |
|
78
|
|
|
del load_config.config |
|
79
|
|
|
|
|
80
|
|
|
assert plugins == [], plugins |
|
81
|
|
|
|
|
82
|
|
|
@patch('elodie.config.config_file', '%s/config.ini-load-plugin-config-one' % gettempdir()) |
|
83
|
|
|
def test_load_plugin_config_one(): |
|
84
|
|
|
with open('%s/config.ini-load-plugin-config-one' % gettempdir(), 'w') as f: |
|
85
|
|
|
f.write(""" |
|
86
|
|
|
[Plugins] |
|
87
|
|
|
plugins=Dummy |
|
88
|
|
|
""") |
|
89
|
|
|
if hasattr(load_config, 'config'): |
|
90
|
|
|
del load_config.config |
|
91
|
|
|
|
|
92
|
|
|
plugins = load_plugin_config() |
|
93
|
|
|
|
|
94
|
|
|
if hasattr(load_config, 'config'): |
|
95
|
|
|
del load_config.config |
|
96
|
|
|
|
|
97
|
|
|
assert plugins == ['Dummy'], plugins |
|
98
|
|
|
|
|
99
|
|
|
@patch('elodie.config.config_file', '%s/config.ini-load-plugin-config-one-with-invalid' % gettempdir()) |
|
100
|
|
|
def test_load_plugin_config_one_with_invalid(): |
|
101
|
|
|
with open('%s/config.ini-load-plugin-config-one' % gettempdir(), 'w') as f: |
|
102
|
|
|
f.write(""" |
|
103
|
|
|
[Plugins] |
|
104
|
|
|
plugins=DNE |
|
105
|
|
|
""") |
|
106
|
|
|
if hasattr(load_config, 'config'): |
|
107
|
|
|
del load_config.config |
|
108
|
|
|
|
|
109
|
|
|
plugins = load_plugin_config() |
|
110
|
|
|
|
|
111
|
|
|
if hasattr(load_config, 'config'): |
|
112
|
|
|
del load_config.config |
|
113
|
|
|
|
|
114
|
|
|
assert plugins == [], plugins |
|
115
|
|
|
|
|
116
|
|
|
@patch('elodie.config.config_file', '%s/config.ini-load-plugin-config-many' % gettempdir()) |
|
117
|
|
|
def test_load_plugin_config_many(): |
|
118
|
|
|
with open('%s/config.ini-load-plugin-config-many' % gettempdir(), 'w') as f: |
|
119
|
|
|
f.write(""" |
|
120
|
|
|
[Plugins] |
|
121
|
|
|
plugins=GooglePhotos,Dummy |
|
122
|
|
|
""") |
|
123
|
|
|
if hasattr(load_config, 'config'): |
|
124
|
|
|
del load_config.config |
|
125
|
|
|
|
|
126
|
|
|
plugins = load_plugin_config() |
|
127
|
|
|
|
|
128
|
|
|
if hasattr(load_config, 'config'): |
|
129
|
|
|
del load_config.config |
|
130
|
|
|
|
|
131
|
|
|
assert plugins == ['GooglePhotos','Dummy'], plugins |
|
132
|
|
|
|