forked from team-discovery-channel/compose
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pytest.ts
87 lines (73 loc) · 2.48 KB
/
pytest.ts
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
/* Based on code from https://github.com/jasonrute/modulize
* */
export let example:any = {}
let main = ["import foo.bar","import os" ,"fb = foo.bar.bar_func(foo.foo_var)","print(fb) # foo bar"]
example["__main__.py"] = main
let init = ["foo_var = 'foo'"]
example["foo/__init__.py"] = init
let bar = ["def bar_func(x):", " return x + ' bar2'"]
example["foo/bar.py"] = bar
export let desiredOutput = `# -*- coding: utf-8 -*-
import sys
from types import ModuleType
import builtins
buildin_import = builtins.__import__
def compose_import(name, global_context=None, local_context=None, fromlist=(), level=0):
if name in MockModule.mocks:
modparts = name.split(".")
modparts.reverse()
mod = sys.modules[modparts.pop()]
while(len(modparts) != 0):
if level == 0:
break
mod = mod.__dict__[modparts.pop()]
level -= 1
return mod
return buildin_import(name,global_context,local_context,fromlist,level)
builtins.__import__ = compose_import
class MockModule(ModuleType):
mocks = {}
@staticmethod
def get_mock(name):
return MockModule.mocks[name]
def __init__(self, module_name, module_doc=None):
ModuleType.__init__(self, module_name, module_doc)
if '.' in module_name:
package, module = module_name.rsplit('.', 1)
get_mock_module(package).__path__ = []
setattr(get_mock_module(package), module, self)
def _initialize_(self, module_code):
self.__dict__.update(module_code(self.__name__))
self.__doc__ = module_code.__doc__
def get_mock_module(module_name):
if module_name not in sys.modules:
sys.modules[module_name] = MockModule(module_name)
MockModule.mocks[module_name] = sys.modules[module_name]
return sys.modules[module_name]
def modulize(module_name, dependencies=[]):
for d in dependencies:
get_mock_module(d)
return get_mock_module(module_name)._initialize_
##===========================================================================##
@modulize('foo')
def _foo(__name__):
#Begin foo/__init__.py
foo_var = 'foo'
#End foo/__init__.py
return locals()
@modulize('foo.bar')
def _bar(__name__):
#Begin foo/bar.py
def bar_func(x):
return x + ' bar2'
#End foo/bar.py
return locals()
def __main__():
#Begin __main__.py
import foo.bar
import os
fb = foo.bar.bar_func(foo.foo_var)
print(fb) # foo bar
#End __main__.py
__main__()
`