Files
langkit/testsuite/drivers/python_driver.py

107 lines
3.6 KiB
Python
Raw Permalink Normal View History

import os
import os.path
import sys
from drivers.base_driver import BaseDriver
class PythonDriver(BaseDriver):
2020-05-12 17:15:20 +02:00
default_process_timeout = 600
#
# Driver entry poins
#
2020-05-12 17:15:20 +02:00
def set_up(self):
super().set_up()
for f in self.mandatory_files:
self.check_file(f)
2020-05-12 17:15:20 +02:00
@property
def mandatory_files(self):
"""
Return the set of mandatory files for this driver. Meant to be
overloaded by subclasses.
"""
2020-05-12 17:15:20 +02:00
return ['test.py']
2020-05-12 17:15:20 +02:00
@property
def script_and_args(self):
"""
Return the name of the python script to run, as well as the arguments.
Meant to be overloaded by subclasses.
"""
2020-05-12 17:15:20 +02:00
return ['test.py']
def run(self):
# The "test.py" script will not import a generated library, however
# another spawned script could: provide the path to the interpreter in
# the environment so it can use it.
derived_env = dict(os.environ)
2020-05-12 17:15:20 +02:00
derived_env['PYTHON_INTERPRETER'] = self.python_interpreter
derived_env['LANGKIT_ROOT_DIR'] = self.langkit_root_dir
derived_env['LANGKIT_PRETTY_PRINT'] = str(
2020-05-12 17:15:20 +02:00
int(self.env.options.pretty_print))
# Unless this mechanism is specifically disabled, make the Langkit
# library relative to this testsuite available to tests.
if not self.env.options.no_auto_path:
self.add_path(derived_env, 'PYTHONPATH', self.langkit_root_dir)
# Make the common Python modules available from the testcase script
self.add_path(derived_env, 'PYTHONPATH', self.support_dir)
self.add_path(derived_env, 'PYTHONPATH', self.env.root_dir)
# If code coverage is requested, run the test script under the
# "coverage" program.
if self.coverage_enabled:
# Consider all Langkit Python source files, except modules which
# are currently not tested at all.
source = os.path.join(self.langkit_root_dir, 'langkit')
argv = [
'coverage', 'run', '--source={}'.format(source),
'--rcfile={}'.format(os.path.join(self.testsuite_dir,
'coverage.ini'))
]
derived_env['COVERAGE_FILE'] = self.coverage_file('coverage')
else:
argv = [sys.executable]
if self.valgrind_enabled:
derived_env['VALGRIND_ENABLED'] = '1'
2020-05-12 17:15:20 +02:00
self.run_and_check(argv + self.script_and_args, derived_env)
@property
def support_dir(self):
"""
Return the absolute path to the directory for support Python modules.
"""
2020-05-12 17:15:20 +02:00
return os.path.join(self.testsuite_dir, 'python_support')
2020-01-09 13:01:18 +01:00
2020-05-12 17:15:20 +02:00
def compute_failures(self):
2020-01-09 13:01:18 +01:00
# RA22-015: For the transition to the concrete syntax, we want to
# check-in and test unparsing results.
2020-05-12 17:15:20 +02:00
failures = super().compute_failures()
2020-01-09 13:01:18 +01:00
2020-05-12 17:15:20 +02:00
expected_lkt = self.test_dir('expected_concrete_syntax.lkt')
2020-01-09 13:01:18 +01:00
actual_lkt = self.working_dir('concrete_syntax.lkt')
2020-05-12 17:15:20 +02:00
# Some tests do not create a concrete_syntax.lkt file. There is nothing
# to compare for them.
2020-01-09 13:01:18 +01:00
if not os.path.exists(actual_lkt):
return failures
2020-01-09 13:01:18 +01:00
# We just open the file in append mode, to create it if it doesn't
# exist.
with open(expected_lkt, 'a+'):
pass
2020-05-12 17:15:20 +02:00
expected = self.read_file(expected_lkt)
2020-01-09 13:01:18 +01:00
2020-05-12 17:15:20 +02:00
failures += self.compute_diff(
expected_lkt, expected,
self.read_file(actual_lkt),
failure_message="unexpected concrete syntax"
)
return failures