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
|
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)
import os
import json
from subprocess import check_call
from shutil import rmtree
from itertools import chain
from powerline import Powerline
CONFIG_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'config')
class TestPowerline(Powerline):
def __init__(self, _paths, *args, **kwargs):
super(TestPowerline, self).__init__(*args, **kwargs)
self._paths = _paths
def get_config_paths(self):
return self._paths
def mkdir_recursive(directory):
if os.path.isdir(directory):
return
mkdir_recursive(os.path.dirname(directory))
os.mkdir(directory)
class FSTree(object):
__slots__ = ('tree', 'p', 'p_kwargs', 'create_p', 'get_config_paths', 'root')
def __init__(
self,
tree,
p_kwargs={'run_once': True},
root=CONFIG_DIR,
get_config_paths=lambda p: (p,),
create_p=False
):
self.tree = tree
self.root = root
self.get_config_paths = get_config_paths
self.create_p = create_p
self.p = None
self.p_kwargs = p_kwargs
def __enter__(self, *args):
os.mkdir(self.root)
for k, v in self.tree.items():
fname = os.path.join(self.root, k) + '.json'
mkdir_recursive(os.path.dirname(fname))
with open(fname, 'w') as F:
json.dump(v, F)
if self.create_p:
self.p = TestPowerline(
_paths=self.get_config_paths(self.root),
ext='test',
renderer_module='tests.modules.lib.config_mock',
**self.p_kwargs
)
if os.environ.get('POWERLINE_RUN_LINT_DURING_TESTS'):
try:
check_call(chain(['scripts/powerline-lint'], *[
('-p', d) for d in (
self.p.get_config_paths() if self.p
else self.get_config_paths(self.root)
)
]))
except:
self.__exit__()
raise
return self.p and self.p.__enter__(*args)
def __exit__(self, *args):
try:
rmtree(self.root)
finally:
if self.p:
self.p.__exit__(*args)
|