1
-
2
1
# This is a python unittest class automatically populating with all tests
3
2
# in the tests folder.
4
3
11
10
import subprocess
12
11
import contextlib
13
12
import enum
13
+ from pathlib import Path
14
+ import shutil
14
15
15
16
import compile_code
16
17
@@ -19,13 +20,11 @@ class _TestType(enum.Enum):
19
20
functional = 1
20
21
21
22
22
- logger = logging .getLogger ('tests' )
23
- ROOT_DIR = '..'
24
- TEST_ROOT = os .path .abspath (os .path .join (ROOT_DIR , 'tests' ))
25
- TEST_DIRS = {
26
- _TestType .functional : os .path .join (TEST_ROOT , 'snippets' ),
27
- }
28
- CPYTHON_RUNNER_DIR = os .path .abspath (os .path .join (ROOT_DIR , 'py_code_object' ))
23
+ logger = logging .getLogger ("tests" )
24
+ ROOT_DIR = ".."
25
+ TEST_ROOT = os .path .abspath (os .path .join (ROOT_DIR , "tests" ))
26
+ TEST_DIRS = {_TestType .functional : os .path .join (TEST_ROOT , "snippets" )}
27
+ CPYTHON_RUNNER_DIR = os .path .abspath (os .path .join (ROOT_DIR , "py_code_object" ))
29
28
RUSTPYTHON_RUNNER_DIR = os .path .abspath (os .path .join (ROOT_DIR ))
30
29
RUSTPYTHON_LIB_DIR = os .path .abspath (os .path .join (ROOT_DIR , "Lib" ))
31
30
@@ -38,12 +37,12 @@ def pushd(path):
38
37
39
38
40
39
def perform_test (filename , method , test_type ):
41
- logger .info (' Running %s via %s' , filename , method )
42
- if method == ' cpython' :
40
+ logger .info (" Running %s via %s" , filename , method )
41
+ if method == " cpython" :
43
42
run_via_cpython (filename )
44
- elif method == ' cpython_bytecode' :
43
+ elif method == " cpython_bytecode" :
45
44
run_via_cpython_bytecode (filename , test_type )
46
- elif method == ' rustpython' :
45
+ elif method == " rustpython" :
47
46
run_via_rustpython (filename , test_type )
48
47
else :
49
48
raise NotImplementedError (method )
@@ -57,16 +56,16 @@ def run_via_cpython(filename):
57
56
58
57
def run_via_cpython_bytecode (filename , test_type ):
59
58
# Step1: Create bytecode file:
60
- bytecode_filename = filename + ' .bytecode'
61
- with open (bytecode_filename , 'w' ) as f :
59
+ bytecode_filename = filename + " .bytecode"
60
+ with open (bytecode_filename , "w" ) as f :
62
61
compile_code .compile_to_bytecode (filename , out_file = f )
63
62
64
63
# Step2: run cpython bytecode:
65
64
env = os .environ .copy ()
66
- env [' RUST_LOG' ] = ' info,cargo=error,jobserver=error'
67
- env [' RUST_BACKTRACE' ] = '1'
65
+ env [" RUST_LOG" ] = " info,cargo=error,jobserver=error"
66
+ env [" RUST_BACKTRACE" ] = "1"
68
67
with pushd (CPYTHON_RUNNER_DIR ):
69
- subprocess .check_call ([' cargo' , ' run' , bytecode_filename ], env = env )
68
+ subprocess .check_call ([" cargo" , " run" , bytecode_filename ], env = env )
70
69
71
70
72
71
def run_via_rustpython (filename , test_type ):
@@ -75,26 +74,26 @@ def run_via_rustpython(filename, test_type):
75
74
env ['RUST_BACKTRACE' ] = '1'
76
75
env ['PYTHONPATH' ] = RUSTPYTHON_LIB_DIR
77
76
78
- target = ' release'
79
- if env .get (' CODE_COVERAGE' , ' false' ) == ' true' :
80
- target = ' debug'
81
- binary = os .path .abspath (os .path .join (ROOT_DIR , ' target' , target , ' rustpython' ))
77
+ target = " release"
78
+ if env .get (" CODE_COVERAGE" , " false" ) == " true" :
79
+ target = " debug"
80
+ binary = os .path .abspath (os .path .join (ROOT_DIR , " target" , target , " rustpython" ))
82
81
83
82
subprocess .check_call ([binary , filename ], env = env )
84
83
85
84
86
85
def create_test_function (cls , filename , method , test_type ):
87
86
""" Create a test function for a single snippet """
88
87
core_test_directory , snippet_filename = os .path .split (filename )
89
- test_function_name = ' test_{}_' .format (method ) \
90
- + os . path . splitext ( snippet_filename )[ 0 ] \
91
- .replace ('.' , '_' ).replace ('-' , '_' )
88
+ test_function_name = " test_{}_" .format (method ) + os . path . splitext ( snippet_filename )[
89
+ 0
90
+ ] .replace ("." , "_" ).replace ("-" , "_" )
92
91
93
92
def test_function (self ):
94
93
perform_test (filename , method , test_type )
95
94
96
95
if hasattr (cls , test_function_name ):
97
- raise ValueError (' Duplicate test case {}' .format (test_function_name ))
96
+ raise ValueError (" Duplicate test case {}" .format (test_function_name ))
98
97
setattr (cls , test_function_name , test_function )
99
98
100
99
@@ -104,25 +103,61 @@ def wrapper(cls):
104
103
for test_type , filename in get_test_files ():
105
104
create_test_function (cls , filename , method , test_type )
106
105
return cls
106
+
107
107
return wrapper
108
108
109
109
110
110
def get_test_files ():
111
111
""" Retrieve test files """
112
112
for test_type , test_dir in TEST_DIRS .items ():
113
- for filepath in sorted (glob .iglob (os .path .join (test_dir , ' *.py' ))):
113
+ for filepath in sorted (glob .iglob (os .path .join (test_dir , " *.py" ))):
114
114
filename = os .path .split (filepath )[1 ]
115
- if filename .startswith (' xfail_' ):
115
+ if filename .startswith (" xfail_" ):
116
116
continue
117
117
118
118
yield test_type , os .path .abspath (filepath )
119
119
120
120
121
- @populate ('cpython' )
121
+ def generate_slices (path ):
122
+ # loop used to build slices_res.py with cpython
123
+ ll = [0 , 1 , 2 , 3 ]
124
+ start = list (range (- 7 , 7 ))
125
+ end = list (range (- 7 , 7 ))
126
+ step = list (range (- 5 , 5 ))
127
+ step .pop (step .index (0 ))
128
+ for i in [start , end , step ]:
129
+ i .append (None )
130
+
131
+ slices_res = []
132
+ for s in start :
133
+ for e in end :
134
+ for t in step :
135
+ slices_res .append (ll [s :e :t ])
136
+
137
+ path .write_text (
138
+ "SLICES_RES={}\n START= {}\n END= {}\n STEP= {}\n LL={}\n " .format (
139
+ slices_res , start , end , step , ll
140
+ )
141
+ )
142
+
143
+
144
+ @populate ("cpython" )
122
145
# @populate('cpython_bytecode')
123
- @populate (' rustpython' )
146
+ @populate (" rustpython" )
124
147
class SampleTestCase (unittest .TestCase ):
125
148
@classmethod
126
149
def setUpClass (cls ):
127
- subprocess .check_call (['cargo' , 'build' ])
128
- subprocess .check_call (['cargo' , 'build' , '--release' ])
150
+ # Here add resource files
151
+ cls .slices_resource_path = Path (TEST_DIRS [_TestType .functional ]) / "cpython_generated_slices.py"
152
+ if cls .slices_resource_path .exists ():
153
+ cls .slices_resource_path .unlink ()
154
+
155
+ generate_slices (cls .slices_resource_path )
156
+
157
+ # cargo stuff
158
+ subprocess .check_call (["cargo" , "build" ])
159
+ subprocess .check_call (["cargo" , "build" , "--release" ])
160
+
161
+ @classmethod
162
+ def tearDownClass (cls ):
163
+ cls .slices_resource_path .unlink ()
0 commit comments