1
|
|
|
from __future__ import absolute_import |
2
|
|
|
# Project imports |
3
|
|
|
import mock |
4
|
|
|
import os |
5
|
|
|
import sys |
6
|
|
|
from tempfile import gettempdir |
7
|
|
|
|
8
|
|
|
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) |
9
|
|
|
|
10
|
|
|
from . import helper |
11
|
|
|
from elodie.config import load_config |
12
|
|
|
from elodie.plugins.plugins import Plugins |
13
|
|
|
|
14
|
|
|
@mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-unset-backwards-compat' % gettempdir()) |
15
|
|
|
def test_load_plugins_unset_backwards_compat(): |
16
|
|
|
with open('%s/config.ini-load-plugins-unset-backwards-compat' % gettempdir(), 'w') as f: |
17
|
|
|
f.write(""" |
18
|
|
|
""") |
19
|
|
|
if hasattr(load_config, 'config'): |
20
|
|
|
del load_config.config |
21
|
|
|
|
22
|
|
|
plugins = Plugins() |
23
|
|
|
plugins.load() |
24
|
|
|
|
25
|
|
|
if hasattr(load_config, 'config'): |
26
|
|
|
del load_config.config |
27
|
|
|
|
28
|
|
|
assert plugins.plugins == [], plugins.plugins |
29
|
|
|
|
30
|
|
|
@mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-exists-not-set' % gettempdir()) |
31
|
|
|
def test_load_plugins_exists_not_set(): |
32
|
|
|
with open('%s/config.ini-load-plugins-exists-not-set' % gettempdir(), 'w') as f: |
33
|
|
|
f.write(""" |
34
|
|
|
[Plugins] |
35
|
|
|
""") |
36
|
|
|
if hasattr(load_config, 'config'): |
37
|
|
|
del load_config.config |
38
|
|
|
|
39
|
|
|
plugins = Plugins() |
40
|
|
|
plugins.load() |
41
|
|
|
|
42
|
|
|
if hasattr(load_config, 'config'): |
43
|
|
|
del load_config.config |
44
|
|
|
|
45
|
|
|
assert plugins.plugins == [], plugins.plugins |
46
|
|
|
|
47
|
|
View Code Duplication |
@mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-one' % gettempdir()) |
|
|
|
|
48
|
|
|
def test_load_plugins_one(): |
49
|
|
|
with open('%s/config.ini-load-plugins-one' % gettempdir(), 'w') as f: |
50
|
|
|
f.write(""" |
51
|
|
|
[Plugins] |
52
|
|
|
plugins=Dummy |
53
|
|
|
""") |
54
|
|
|
if hasattr(load_config, 'config'): |
55
|
|
|
del load_config.config |
56
|
|
|
|
57
|
|
|
plugins = Plugins() |
58
|
|
|
plugins.load() |
59
|
|
|
|
60
|
|
|
if hasattr(load_config, 'config'): |
61
|
|
|
del load_config.config |
62
|
|
|
|
63
|
|
|
assert plugins.plugins == ['Dummy'], plugins.plugins |
64
|
|
|
assert len(plugins.classes) == 1, len(plugins.classes) |
65
|
|
|
|
66
|
|
View Code Duplication |
@mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-one-with-invalid' % gettempdir()) |
|
|
|
|
67
|
|
|
def test_load_plugins_one_with_invalid(): |
68
|
|
|
with open('%s/config.ini-load-plugins-one' % gettempdir(), 'w') as f: |
69
|
|
|
f.write(""" |
70
|
|
|
[Plugins] |
71
|
|
|
plugins=DNE |
72
|
|
|
""") |
73
|
|
|
if hasattr(load_config, 'config'): |
74
|
|
|
del load_config.config |
75
|
|
|
|
76
|
|
|
plugins = Plugins() |
77
|
|
|
plugins.load() |
78
|
|
|
|
79
|
|
|
if hasattr(load_config, 'config'): |
80
|
|
|
del load_config.config |
81
|
|
|
|
82
|
|
|
assert plugins.plugins == [], plugins.plugins |
83
|
|
|
assert len(plugins.classes) == 0, len(plugins.classes) |
84
|
|
|
|
85
|
|
View Code Duplication |
@mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-many' % gettempdir()) |
|
|
|
|
86
|
|
|
def test_load_plugins_many(): |
87
|
|
|
with open('%s/config.ini-load-plugins-many' % gettempdir(), 'w') as f: |
88
|
|
|
f.write(""" |
89
|
|
|
[Plugins] |
90
|
|
|
plugins=GooglePhotos,Dummy |
91
|
|
|
""") |
92
|
|
|
if hasattr(load_config, 'config'): |
93
|
|
|
del load_config.config |
94
|
|
|
|
95
|
|
|
plugins = Plugins() |
96
|
|
|
plugins.load() |
97
|
|
|
|
98
|
|
|
if hasattr(load_config, 'config'): |
99
|
|
|
del load_config.config |
100
|
|
|
|
101
|
|
|
assert plugins.plugins == ['GooglePhotos','Dummy'], plugins.plugins |
102
|
|
|
assert len(plugins.classes) == 2, len(plugins.classes) |
103
|
|
|
|
104
|
|
|
@mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-many-with-invalid' % gettempdir()) |
105
|
|
|
def test_load_plugins_set_many_with_invalid(): |
106
|
|
|
with open('%s/config.ini-load-plugins-many-with-invalid' % gettempdir(), 'w') as f: |
107
|
|
|
f.write(""" |
108
|
|
|
[Plugins] |
109
|
|
|
plugins=GooglePhotos,Dummy,DNE |
110
|
|
|
""") |
111
|
|
|
if hasattr(load_config, 'config'): |
112
|
|
|
del load_config.config |
113
|
|
|
|
114
|
|
|
plugins = Plugins() |
115
|
|
|
plugins.load() |
116
|
|
|
|
117
|
|
|
if hasattr(load_config, 'config'): |
118
|
|
|
del load_config.config |
119
|
|
|
|
120
|
|
|
assert plugins.plugins == ['GooglePhotos','Dummy'], plugins.plugins |
121
|
|
|
|
122
|
|
|
@mock.patch('elodie.config.config_file', '%s/config.ini-run-before' % gettempdir()) |
123
|
|
|
def test_run_before(): |
124
|
|
|
with open('%s/config.ini-run-before' % gettempdir(), 'w') as f: |
125
|
|
|
f.write(""" |
126
|
|
|
[Plugins] |
127
|
|
|
plugins=Dummy |
128
|
|
|
""") |
129
|
|
|
if hasattr(load_config, 'config'): |
130
|
|
|
del load_config.config |
131
|
|
|
|
132
|
|
|
plugins = Plugins() |
133
|
|
|
plugins.load() |
134
|
|
|
print(plugins.classes) |
135
|
|
|
before_ran_1 = plugins.classes['Dummy'].before_ran |
136
|
|
|
plugins.run_all_before('', '', '') |
137
|
|
|
before_ran_2 = plugins.classes['Dummy'].before_ran |
138
|
|
|
|
139
|
|
|
if hasattr(load_config, 'config'): |
140
|
|
|
del load_config.config |
141
|
|
|
|
142
|
|
|
assert before_ran_1 == False, before_ran_1 |
143
|
|
|
assert before_ran_2 == True, before_ran_2 |
144
|
|
|
|