summaryrefslogtreecommitdiffstats
path: root/tests/test_python/test_config_reload.py
blob: a418d4966ed8fd41d74c4fb742a1bee387acb288 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)

from time import sleep
from copy import deepcopy
from functools import wraps

from tests.modules import TestCase
from tests.modules.lib.config_mock import get_powerline, add_watcher_events, UT


config = {
	'config': {
		'common': {
			'interval': 0,
			'watcher': 'test',
		},
		'ext': {
			'test': {
				'theme': 'default',
				'colorscheme': 'default',
			},
		},
	},
	'colors': {
		'colors': {
			"col1": 1,
			"col2": 2,
			"col3": 3,
			"col4": 4,
		},
		'gradients': {
		},
	},
	'colorschemes/test/default': {
		'groups': {
			'str1': {'fg': 'col1', 'bg': 'col2', 'attrs': ['bold']},
			'str2': {'fg': 'col3', 'bg': 'col4', 'attrs': ['underline']},
		},
	},
	'colorschemes/test/2': {
		'groups': {
			'str1': {'fg': 'col2', 'bg': 'col3', 'attrs': ['bold']},
			'str2': {'fg': 'col1', 'bg': 'col4', 'attrs': ['underline']},
		},
	},
	'themes/test/default': {
		'segments': {
			"left": [
				{
					"type": "string",
					"contents": "s",
					"highlight_groups": ["str1"],
				},
				{
					"type": "string",
					"contents": "g",
					"highlight_groups": ["str2"],
				},
			],
			"right": [
			],
		},
	},
	'themes/' + UT: {
		'dividers': {
			"left": {
				"hard": ">>",
				"soft": ">",
			},
			"right": {
				"hard": "<<",
				"soft": "<",
			},
		},
		'spaces': 0,
	},
	'themes/other': {
		'dividers': {
			"left": {
				"hard": ">>",
				"soft": ">",
			},
			"right": {
				"hard": "<<",
				"soft": "<",
			},
		},
		'spaces': 1,
	},
	'themes/test/2': {
		'segments': {
			"left": [
				{
					"type": "string",
					"contents": "t",
					"highlight_groups": ["str1"],
				},
				{
					"type": "string",
					"contents": "b",
					"highlight_groups": ["str2"],
				},
			],
			"right": [
			],
		},
	},
}


def with_new_config(func):
	@wraps(func)
	def f(self):
		return func(self, deepcopy(config))
	return f


class TestConfigReload(TestCase):
	def assertAccessEvents(self, p, *args):
		events = set()
		for event in args:
			if ':' not in event:
				events.add('check:' + event)
				events.add('load:' + event)
			else:
				events.add(event)
		self.assertEqual(set(p._pop_events()), events)

	@with_new_config
	def test_noreload(self, config):
		with get_powerline(config, run_once=True) as p:
			self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'colors', 'check:colorschemes/default', 'check:colorschemes/test/__main__', 'colorschemes/test/default', 'themes/test/default', 'themes/' + UT, 'check:themes/test/__main__')
			config['config']['common']['spaces'] = 1
			add_watcher_events(p, 'config', wait=False, interval=0.05)
			# When running once thread should not start
			self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p)
			self.assertEqual(p.logger._pop_msgs(), [])

	@with_new_config
	def test_reload_main(self, config):
		with get_powerline(config, run_once=False) as p:
			self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'colors', 'check:colorschemes/default', 'check:colorschemes/test/__main__', 'colorschemes/test/default', 'themes/test/default', 'themes/' + UT, 'check:themes/test/__main__')

			config['config']['common']['default_top_theme'] = 'other'
			add_watcher_events(p, 'config')
			p.render()
			self.assertEqual(p.render(), '<1 2 1> s <2 4 False>>><3 4 4>g <4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'themes/other', 'check:themes/test/__main__', 'themes/test/default')
			self.assertEqual(p.logger._pop_msgs(), [])

			config['config']['ext']['test']['theme'] = 'nonexistent'
			add_watcher_events(p, 'config')
			self.assertEqual(p.render(), '<1 2 1> s <2 4 False>>><3 4 4>g <4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'check:themes/test/nonexistent', 'themes/other', 'check:themes/test/__main__')
			# It should normally handle file missing error
			self.assertEqual(p.logger._pop_msgs(), [
				'exception:test:powerline:Failed to load theme: themes/test/__main__',
				'exception:test:powerline:Failed to load theme: themes/test/nonexistent',
				'exception:test:powerline:Failed to create renderer: themes/test/nonexistent'
			])

			config['config']['ext']['test']['theme'] = 'default'
			add_watcher_events(p, 'config')
			self.assertEqual(p.render(), '<1 2 1> s <2 4 False>>><3 4 4>g <4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'themes/test/default', 'themes/other', 'check:themes/test/__main__')
			self.assertEqual(p.logger._pop_msgs(), [])

			config['config']['ext']['test']['colorscheme'] = 'nonexistent'
			add_watcher_events(p, 'config')
			self.assertEqual(p.render(), '<1 2 1> s <2 4 False>>><3 4 4>g <4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'check:colorschemes/nonexistent', 'check:colorschemes/test/__main__', 'check:colorschemes/test/nonexistent')
			# It should normally handle file missing error
			self.assertEqual(p.logger._pop_msgs(), [
				'exception:test:powerline:Failed to load colorscheme: colorschemes/nonexistent',
				'exception:test:powerline:Failed to load colorscheme: colorschemes/test/__main__',
				'exception:test:powerline:Failed to load colorscheme: colorschemes/test/nonexistent',
				'exception:test:powerline:Failed to create renderer: colorschemes/test/nonexistent'
			])

			config['config']['ext']['test']['colorscheme'] = '2'
			add_watcher_events(p, 'config')
			self.assertEqual(p.render(), '<2 3 1> s <3 4 False>>><1 4 4>g <4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'check:colorschemes/2', 'check:colorschemes/test/__main__', 'colorschemes/test/2')
			self.assertEqual(p.logger._pop_msgs(), [])

			config['config']['ext']['test']['theme'] = '2'
			add_watcher_events(p, 'config')
			self.assertEqual(p.render(), '<2 3 1> t <3 4 False>>><1 4 4>b <4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'themes/test/2', 'themes/other', 'check:themes/test/__main__')
			self.assertEqual(p.logger._pop_msgs(), [])

			self.assertEqual(p.renderer.local_themes, None)
			config['config']['ext']['test']['local_themes'] = 'something'
			add_watcher_events(p, 'config')
			self.assertEqual(p.render(), '<2 3 1> t <3 4 False>>><1 4 4>b <4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config')
			self.assertEqual(p.logger._pop_msgs(), [])
			self.assertEqual(p.renderer.local_themes, 'something')

	@with_new_config
	def test_reload_unexistent(self, config):
		with get_powerline(config, run_once=False) as p:
			self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'colors', 'check:colorschemes/default', 'check:colorschemes/test/__main__', 'colorschemes/test/default', 'themes/test/default', 'themes/' + UT, 'check:themes/test/__main__')

			config['config']['ext']['test']['colorscheme'] = 'nonexistentraise'
			add_watcher_events(p, 'config')
			# It may appear that p.logger._pop_msgs() is called after given 
			# exception is added to the mesagges, but before config_loader 
			# exception was added (this one: 
			# “exception:test:config_loader:Error while running condition 
			# function for key colorschemes/test/nonexistentraise: 
			# fcf:colorschemes/test/nonexistentraise”).
			# sleep(0.1)
			self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			# For colorschemes/{test/,}*raise find_config_file raises 
			# IOError, but it does not do so for check:colorschemes/test/__main__, 
			# so powerline is trying to load this, but not other 
			# colorschemes/*
			self.assertAccessEvents(p, 'config', 'check:colorschemes/test/__main__', 'check:colorschemes/nonexistentraise', 'check:colorschemes/test/nonexistentraise')
			self.assertIn('exception:test:powerline:Failed to create renderer: fcf:colorschemes/test/nonexistentraise', p.logger._pop_msgs())

			config['colorschemes/nonexistentraise'] = {}
			config['colorschemes/test/nonexistentraise'] = {
				'groups': {
					'str1': {'fg': 'col1', 'bg': 'col3', 'attrs': ['bold']},
					'str2': {'fg': 'col2', 'bg': 'col4', 'attrs': ['underline']},
				},
			}
			while not p._will_create_renderer():
				sleep(0.1)
			self.assertEqual(p.render(), '<1 3 1> s<3 4 False>>><2 4 4>g<4 False False>>><None None None>')
			# Same as above
			self.assertAccessEvents(p, 'colorschemes/nonexistentraise', 'colorschemes/test/nonexistentraise', 'check:colorschemes/test/__main__')
			self.assertEqual(p.logger._pop_msgs(), [])

	@with_new_config
	def test_reload_colors(self, config):
		with get_powerline(config, run_once=False) as p:
			self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'colors', 'check:colorschemes/default', 'check:colorschemes/test/__main__', 'colorschemes/test/default', 'themes/test/default', 'themes/' + UT, 'check:themes/test/__main__')

			config['colors']['colors']['col1'] = 5
			add_watcher_events(p, 'colors')
			self.assertEqual(p.render(), '<5 2 1> s<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p, 'colors')
			self.assertEqual(p.logger._pop_msgs(), [])

	@with_new_config
	def test_reload_colorscheme(self, config):
		with get_powerline(config, run_once=False) as p:
			self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'colors', 'check:colorschemes/default', 'check:colorschemes/test/__main__', 'colorschemes/test/default', 'themes/test/default', 'themes/' + UT, 'check:themes/test/__main__')

			config['colorschemes/test/default']['groups']['str1']['bg'] = 'col3'
			add_watcher_events(p, 'colorschemes/test/default')
			self.assertEqual(p.render(), '<1 3 1> s<3 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p, 'check:colorschemes/default', 'check:colorschemes/test/__main__', 'colorschemes/test/default')
			self.assertEqual(p.logger._pop_msgs(), [])

	@with_new_config
	def test_reload_theme(self, config):
		with get_powerline(config, run_once=False) as p:
			self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'colors', 'check:colorschemes/default', 'check:colorschemes/test/__main__', 'colorschemes/test/default', 'themes/test/default', 'themes/' + UT, 'check:themes/test/__main__')

			config['themes/test/default']['segments']['left'][0]['contents'] = 'col3'
			add_watcher_events(p, 'themes/test/default')
			self.assertEqual(p.render(), '<1 2 1> col3<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p, 'themes/test/default', 'themes/' + UT, 'check:themes/test/__main__')
			self.assertEqual(p.logger._pop_msgs(), [])

	@with_new_config
	def test_reload_top_theme(self, config):
		with get_powerline(config, run_once=False) as p:
			self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'colors', 'check:colorschemes/default', 'check:colorschemes/test/__main__', 'colorschemes/test/default', 'themes/test/default', 'themes/' + UT, 'check:themes/test/__main__')

			config['themes/' + UT]['dividers']['left']['hard'] = '|>'
			add_watcher_events(p, 'themes/' + UT)
			self.assertEqual(p.render(), '<1 2 1> s<2 4 False>|><3 4 4>g<4 False False>|><None None None>')
			self.assertAccessEvents(p, 'themes/test/default', 'themes/' + UT, 'check:themes/test/__main__')
			self.assertEqual(p.logger._pop_msgs(), [])

	@with_new_config
	def test_reload_theme_main(self, config):
		config['config']['common']['interval'] = None
		with get_powerline(config, run_once=False) as p:
			self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'colors', 'check:colorschemes/default', 'check:colorschemes/test/__main__', 'colorschemes/test/default', 'themes/test/default', 'themes/' + UT, 'check:themes/test/__main__')

			config['themes/test/default']['segments']['left'][0]['contents'] = 'col3'
			add_watcher_events(p, 'themes/test/default', wait=False)
			self.assertEqual(p.render(), '<1 2 1> col3<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p, 'themes/test/default', 'themes/' + UT, 'check:themes/test/__main__')
			self.assertEqual(p.logger._pop_msgs(), [])
			self.assertTrue(p._watcher._calls)

	@with_new_config
	def test_run_once_no_theme_reload(self, config):
		config['config']['common']['interval'] = None
		with get_powerline(config, run_once=True) as p:
			self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p, 'config', 'colors', 'check:colorschemes/default', 'check:colorschemes/test/__main__', 'colorschemes/test/default', 'themes/test/default', 'themes/' + UT, 'check:themes/test/__main__')

			config['themes/test/default']['segments']['left'][0]['contents'] = 'col3'
			add_watcher_events(p, 'themes/test/default', wait=False)
			self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>><None None None>')
			self.assertAccessEvents(p)
			self.assertEqual(p.logger._pop_msgs(), [])


if __name__ == '__main__':
	from tests.modules import main
	main()