Skip to content

Commit 9526f78

Browse files
committed
Add pytest runner for test snippets
1 parent b9170c0 commit 9526f78

File tree

8 files changed

+186
-5
lines changed

8 files changed

+186
-5
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
/target
22
**/*.rs.bk
3+
**/*.bytecode
4+
__pycache__
5+
**/*.pytest_cache
6+

py_code_object/Pipfile.lock

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ def parse_co_code_to_str(c):
4242
)
4343

4444

45-
def main():
46-
47-
filename = sys.argv[1]
45+
def compile_to_bytecode(filename, out_file=None):
4846
with open(filename, 'rU') as f:
4947
code = f.read()
5048

5149
code = compile(code, filename, "exec")
5250

53-
print(CodeEncoder().encode(code))
51+
print(CodeEncoder().encode(code), file=out_file)
52+
53+
54+
def main():
55+
filename = sys.argv[1]
56+
compile_to_bytecode(filename)
57+
5458

5559
if __name__ == "__main__":
5660
main()

py_code_object/src/convert.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
// TODO: create a function which takes CPython bytecode and transforms
3+
// this into RustPython bytecode. This to decouple RustPython from CPython
4+
// internal bytecode representations.
5+
6+
7+
pub fn convert(cpython_bytecode: CPythonByteCode) -> ByteCode {
8+
panic!("TODO");
9+
}
10+

vm/test_all.sh renamed to py_code_object/test_all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fail_titles=$""
1212
RED='\033[0;31m'
1313
NC='\033[0m' # No Color
1414

15-
for TESTCASE in $(find tests -name \*.py -print)
15+
for TESTCASE in $(find ../tests -name \*.py -print)
1616
do
1717
echo "TEST START: ${TESTCASE}"
1818
echo "--------------------------------"

py_code_object/test_snippets.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
# This is a python unittest class automatically populating with all tests
3+
# in the tests folder.
4+
5+
6+
import os
7+
import unittest
8+
import glob
9+
import logging
10+
import subprocess
11+
import contextlib
12+
13+
import compile_code
14+
15+
16+
logger = logging.getLogger('tests')
17+
TEST_DIR = os.path.abspath(os.path.join('..', 'tests'))
18+
CPYTHON_RUNNER_DIR = os.path.abspath(os.path.join('..', 'vm', 'RustPython'))
19+
20+
21+
@contextlib.contextmanager
22+
def pushd(path):
23+
old_dir = os.getcwd()
24+
os.chdir(path)
25+
yield
26+
os.chdir(old_dir)
27+
28+
29+
def perform_test(filename):
30+
logger.info('Running %s', filename)
31+
# Step1: Create bytecode file:
32+
bytecode_filename = filename + '.bytecode'
33+
with open(bytecode_filename, 'w') as f:
34+
compile_code.compile_to_bytecode(filename, out_file=f)
35+
36+
# Step2: run cpython bytecode:
37+
with pushd(CPYTHON_RUNNER_DIR):
38+
subprocess.check_call(['cargo', 'run', bytecode_filename])
39+
40+
41+
def create_test_function(cls, filename):
42+
""" Create a test function for a single snippet """
43+
core_test_directory, snippet_filename = os.path.split(filename)
44+
test_function_name = 'test_' \
45+
+ os.path.splitext(snippet_filename)[0] \
46+
.replace('.', '_').replace('-', '_')
47+
48+
def test_function(self):
49+
perform_test(filename)
50+
51+
if hasattr(cls, test_function_name):
52+
raise ValueError('Duplicate test case {}'.format(test_function_name))
53+
setattr(cls, test_function_name, test_function)
54+
55+
56+
def populate(cls):
57+
""" Decorator function which can populate a unittest.TestCase class """
58+
for filename in get_test_files():
59+
create_test_function(cls, filename)
60+
return cls
61+
62+
63+
def get_test_files():
64+
""" Retrieve test files """
65+
for filename in sorted(glob.iglob(os.path.join(
66+
TEST_DIR, '*.py'))):
67+
yield os.path.abspath(filename)
68+
69+
70+
@populate
71+
class SampleTestCase(unittest.TestCase):
72+
pass

0 commit comments

Comments
 (0)