1-
21# This is a python unittest class automatically populating with all tests
32# in the tests folder.
43
1110import subprocess
1211import contextlib
1312import enum
13+ from pathlib import Path
14+ import shutil
1415
1516import compile_code
1617
@@ -19,13 +20,11 @@ class _TestType(enum.Enum):
1920 functional = 1
2021
2122
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" ))
2928RUSTPYTHON_RUNNER_DIR = os .path .abspath (os .path .join (ROOT_DIR ))
3029RUSTPYTHON_LIB_DIR = os .path .abspath (os .path .join (ROOT_DIR , "Lib" ))
3130
@@ -38,12 +37,12 @@ def pushd(path):
3837
3938
4039def 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" :
4342 run_via_cpython (filename )
44- elif method == ' cpython_bytecode' :
43+ elif method == " cpython_bytecode" :
4544 run_via_cpython_bytecode (filename , test_type )
46- elif method == ' rustpython' :
45+ elif method == " rustpython" :
4746 run_via_rustpython (filename , test_type )
4847 else :
4948 raise NotImplementedError (method )
@@ -57,16 +56,16 @@ def run_via_cpython(filename):
5756
5857def run_via_cpython_bytecode (filename , test_type ):
5958 # 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 :
6261 compile_code .compile_to_bytecode (filename , out_file = f )
6362
6463 # Step2: run cpython bytecode:
6564 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"
6867 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 )
7069
7170
7271def run_via_rustpython (filename , test_type ):
@@ -75,26 +74,26 @@ def run_via_rustpython(filename, test_type):
7574 env ['RUST_BACKTRACE' ] = '1'
7675 env ['PYTHONPATH' ] = RUSTPYTHON_LIB_DIR
7776
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" ))
8281
8382 subprocess .check_call ([binary , filename ], env = env )
8483
8584
8685def create_test_function (cls , filename , method , test_type ):
8786 """ Create a test function for a single snippet """
8887 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 ("-" , "_" )
9291
9392 def test_function (self ):
9493 perform_test (filename , method , test_type )
9594
9695 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 ))
9897 setattr (cls , test_function_name , test_function )
9998
10099
@@ -104,25 +103,61 @@ def wrapper(cls):
104103 for test_type , filename in get_test_files ():
105104 create_test_function (cls , filename , method , test_type )
106105 return cls
106+
107107 return wrapper
108108
109109
110110def get_test_files ():
111111 """ Retrieve test files """
112112 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" ))):
114114 filename = os .path .split (filepath )[1 ]
115- if filename .startswith (' xfail_' ):
115+ if filename .startswith (" xfail_" ):
116116 continue
117117
118118 yield test_type , os .path .abspath (filepath )
119119
120120
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" )
122145# @populate('cpython_bytecode')
123- @populate (' rustpython' )
146+ @populate (" rustpython" )
124147class SampleTestCase (unittest .TestCase ):
125148 @classmethod
126149 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