mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 10:44:36 +08:00
add resources to snippets
This commit is contained in:
1 parent
7d18a1ce60
commit
d96a5b11f7
4 files changed
+104
-2327
No files matched your search
@@ -9,3 +9,4 @@ __pycache__
|
||||
.vscode
|
||||
wasm-pack.log
|
||||
.idea/
|
||||
tests/snippets/resources
|
||||
@@ -78,53 +78,35 @@ def test_all_slices():
|
||||
"""
|
||||
test all possible slices except big number
|
||||
"""
|
||||
MODE = None # set to "build" to rebuild slice_res.py
|
||||
ll = [0, 1, 2, 3]
|
||||
start = list(range(-7, 7))
|
||||
end = list(range(-7, 7))
|
||||
step = list(range(-5, 5))
|
||||
step.pop(step.index(0))
|
||||
|
||||
for i in [start, end, step]:
|
||||
i.append(None)
|
||||
import resources
|
||||
from resources.cpython_generated_slices import SLICES_RES, START, END, STEP, LL
|
||||
|
||||
def build():
|
||||
# loop used to build slices_res.py with cpython
|
||||
with open("slice_res.py", "wt") as f:
|
||||
for s in start:
|
||||
for e in end:
|
||||
for t in step:
|
||||
f.write(str(ll[s:e:t]) + "\n")
|
||||
ll = LL
|
||||
start = START
|
||||
end = END
|
||||
step = STEP
|
||||
|
||||
def run():
|
||||
# test utility
|
||||
from slice_res import SLICES_RES
|
||||
|
||||
count = 0
|
||||
failures = []
|
||||
for s in start:
|
||||
for e in end:
|
||||
for t in step:
|
||||
lhs = ll[s:e:t]
|
||||
try:
|
||||
assert lhs == SLICES_RES[count]
|
||||
except AssertionError:
|
||||
failures.append(
|
||||
"start: {} ,stop: {}, step {}. Expected: {}, found: {}".format(
|
||||
s, e, t, lhs, SLICES_RES[count]
|
||||
)
|
||||
count = 0
|
||||
failures = []
|
||||
for s in start:
|
||||
for e in end:
|
||||
for t in step:
|
||||
lhs = ll[s:e:t]
|
||||
try:
|
||||
assert lhs == SLICES_RES[count]
|
||||
except AssertionError:
|
||||
failures.append(
|
||||
"start: {} ,stop: {}, step {}. Expected: {}, found: {}".format(
|
||||
s, e, t, lhs, SLICES_RES[count]
|
||||
)
|
||||
count += 1
|
||||
)
|
||||
count += 1
|
||||
|
||||
if failures:
|
||||
for f in failures:
|
||||
print(f)
|
||||
print(len(failures), "slices failed")
|
||||
|
||||
if MODE == "build":
|
||||
build()
|
||||
else:
|
||||
run()
|
||||
if failures:
|
||||
for f in failures:
|
||||
print(f)
|
||||
print(len(failures), "slices failed")
|
||||
|
||||
|
||||
test_all_slices()
|
||||
File diff suppressed because it is too large.
Load diff
+79
-33
@@ -1,4 +1,3 @@
|
||||
|
||||
# This is a python unittest class automatically populating with all tests
|
||||
# in the tests folder.
|
||||
|
||||
@@ -11,6 +10,8 @@ import logging
|
||||
import subprocess
|
||||
import contextlib
|
||||
import enum
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
|
||||
import compile_code
|
||||
|
||||
@@ -19,14 +20,15 @@ class _TestType(enum.Enum):
|
||||
functional = 1
|
||||
|
||||
|
||||
logger = logging.getLogger('tests')
|
||||
ROOT_DIR = '..'
|
||||
TEST_ROOT = os.path.abspath(os.path.join(ROOT_DIR, 'tests'))
|
||||
TEST_DIRS = {
|
||||
_TestType.functional: os.path.join(TEST_ROOT, 'snippets'),
|
||||
}
|
||||
CPYTHON_RUNNER_DIR = os.path.abspath(os.path.join(ROOT_DIR, 'py_code_object'))
|
||||
logger = logging.getLogger("tests")
|
||||
ROOT_DIR = ".."
|
||||
TEST_ROOT = os.path.abspath(os.path.join(ROOT_DIR, "tests"))
|
||||
TEST_DIRS = {_TestType.functional: os.path.join(TEST_ROOT, "snippets")}
|
||||
CPYTHON_RUNNER_DIR = os.path.abspath(os.path.join(ROOT_DIR, "py_code_object"))
|
||||
RUSTPYTHON_RUNNER_DIR = os.path.abspath(os.path.join(ROOT_DIR))
|
||||
RESOURCES_DIR = os.path.abspath(
|
||||
os.path.join(TEST_DIRS[_TestType.functional], "resources")
|
||||
)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
@@ -38,12 +40,12 @@ def pushd(path):
|
||||
|
||||
|
||||
def perform_test(filename, method, test_type):
|
||||
logger.info('Running %s via %s', filename, method)
|
||||
if method == 'cpython':
|
||||
logger.info("Running %s via %s", filename, method)
|
||||
if method == "cpython":
|
||||
run_via_cpython(filename)
|
||||
elif method == 'cpython_bytecode':
|
||||
elif method == "cpython_bytecode":
|
||||
run_via_cpython_bytecode(filename, test_type)
|
||||
elif method == 'rustpython':
|
||||
elif method == "rustpython":
|
||||
run_via_rustpython(filename, test_type)
|
||||
else:
|
||||
raise NotImplementedError(method)
|
||||
@@ -57,27 +59,27 @@ def run_via_cpython(filename):
|
||||
|
||||
def run_via_cpython_bytecode(filename, test_type):
|
||||
# Step1: Create bytecode file:
|
||||
bytecode_filename = filename + '.bytecode'
|
||||
with open(bytecode_filename, 'w') as f:
|
||||
bytecode_filename = filename + ".bytecode"
|
||||
with open(bytecode_filename, "w") as f:
|
||||
compile_code.compile_to_bytecode(filename, out_file=f)
|
||||
|
||||
# Step2: run cpython bytecode:
|
||||
env = os.environ.copy()
|
||||
env['RUST_LOG'] = 'info,cargo=error,jobserver=error'
|
||||
env['RUST_BACKTRACE'] = '1'
|
||||
env["RUST_LOG"] = "info,cargo=error,jobserver=error"
|
||||
env["RUST_BACKTRACE"] = "1"
|
||||
with pushd(CPYTHON_RUNNER_DIR):
|
||||
subprocess.check_call(['cargo', 'run', bytecode_filename], env=env)
|
||||
subprocess.check_call(["cargo", "run", bytecode_filename], env=env)
|
||||
|
||||
|
||||
def run_via_rustpython(filename, test_type):
|
||||
env = os.environ.copy()
|
||||
env['RUST_LOG'] = 'info,cargo=error,jobserver=error'
|
||||
env['RUST_BACKTRACE'] = '1'
|
||||
env["RUST_LOG"] = "info,cargo=error,jobserver=error"
|
||||
env["RUST_BACKTRACE"] = "1"
|
||||
|
||||
target = 'release'
|
||||
if env.get('CODE_COVERAGE', 'false') == 'true':
|
||||
target = 'debug'
|
||||
binary = os.path.abspath(os.path.join(ROOT_DIR, 'target', target, 'rustpython'))
|
||||
target = "release"
|
||||
if env.get("CODE_COVERAGE", "false") == "true":
|
||||
target = "debug"
|
||||
binary = os.path.abspath(os.path.join(ROOT_DIR, "target", target, "rustpython"))
|
||||
|
||||
subprocess.check_call([binary, filename], env=env)
|
||||
|
||||
@@ -85,15 +87,15 @@ def run_via_rustpython(filename, test_type):
|
||||
def create_test_function(cls, filename, method, test_type):
|
||||
""" Create a test function for a single snippet """
|
||||
core_test_directory, snippet_filename = os.path.split(filename)
|
||||
test_function_name = 'test_{}_'.format(method) \
|
||||
+ os.path.splitext(snippet_filename)[0] \
|
||||
.replace('.', '_').replace('-', '_')
|
||||
test_function_name = "test_{}_".format(method) + os.path.splitext(snippet_filename)[
|
||||
0
|
||||
].replace(".", "_").replace("-", "_")
|
||||
|
||||
def test_function(self):
|
||||
perform_test(filename, method, test_type)
|
||||
|
||||
if hasattr(cls, test_function_name):
|
||||
raise ValueError('Duplicate test case {}'.format(test_function_name))
|
||||
raise ValueError("Duplicate test case {}".format(test_function_name))
|
||||
setattr(cls, test_function_name, test_function)
|
||||
|
||||
|
||||
@@ -103,25 +105,69 @@ def populate(method):
|
||||
for test_type, filename in get_test_files():
|
||||
create_test_function(cls, filename, method, test_type)
|
||||
return cls
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def get_test_files():
|
||||
""" Retrieve test files """
|
||||
for test_type, test_dir in TEST_DIRS.items():
|
||||
for filepath in sorted(glob.iglob(os.path.join(test_dir, '*.py'))):
|
||||
for filepath in sorted(glob.iglob(os.path.join(test_dir, "*.py"))):
|
||||
filename = os.path.split(filepath)[1]
|
||||
if filename.startswith('xfail_'):
|
||||
if filename.startswith("xfail_"):
|
||||
continue
|
||||
|
||||
yield test_type, os.path.abspath(filepath)
|
||||
|
||||
|
||||
@populate('cpython')
|
||||
def generate_slices(path):
|
||||
# loop used to build slices_res.py with cpython
|
||||
ll = [0, 1, 2, 3]
|
||||
start = list(range(-7, 7))
|
||||
end = list(range(-7, 7))
|
||||
step = list(range(-5, 5))
|
||||
step.pop(step.index(0))
|
||||
for i in [start, end, step]:
|
||||
i.append(None)
|
||||
|
||||
slices_res = []
|
||||
for s in start:
|
||||
for e in end:
|
||||
for t in step:
|
||||
slices_res.append(ll[s:e:t])
|
||||
|
||||
path.write_text(
|
||||
"SLICES_RES={}\nSTART= {}\nEND= {}\nSTEP= {}\nLL={}\n".format(
|
||||
slices_res, start, end, step, ll
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@populate("cpython")
|
||||
# @populate('cpython_bytecode')
|
||||
@populate('rustpython')
|
||||
@populate("rustpython")
|
||||
class SampleTestCase(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
subprocess.check_call(['cargo', 'build'])
|
||||
subprocess.check_call(['cargo', 'build', '--release'])
|
||||
# setup resource dir
|
||||
cls.resources = Path(RESOURCES_DIR)
|
||||
if cls.resources.exists():
|
||||
shutil.rmtree(
|
||||
RESOURCES_DIR, ignore_errors=True
|
||||
) # we don't care if dir doesnt exist
|
||||
cls.resources.mkdir()
|
||||
(cls.resources / "__init__.py").touch()
|
||||
|
||||
# Here add resource files
|
||||
cls.slices_resource_path = Path(RESOURCES_DIR) / "cpython_generated_slices.py"
|
||||
generate_slices(cls.slices_resource_path)
|
||||
|
||||
# cargo stuff
|
||||
subprocess.check_call(["cargo", "build"])
|
||||
subprocess.check_call(["cargo", "build", "--release"])
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
shutil.rmtree(
|
||||
RESOURCES_DIR, ignore_errors=True
|
||||
) # we don't care if dir doesnt exist
|
||||
Reference in new issue
Block a user