Example #1
0
    def run(*argv, **kwargs):
        valgrind = kwargs.pop('valgrind', False)
        suppressions = kwargs.pop('valgrind_suppressions', [])
        assert not kwargs

        if valgrind_enabled and valgrind:
            argv = valgrind_cmd(list(argv), suppressions)

        subprocess.check_call(argv, env=env)
Example #2
0
    def run_and_check(self,
                      argv,
                      env=None,
                      for_coverage=False,
                      memcheck=False):
        """
        Run a subprocess with `argv` and check it completes with status code 0.

        In case of failure, the test output is appended to the actual output
        and a TestError is raised.

        :param list[str] argv: List of arguments to pass to the subprocess.
        :param None|dict[str, str] env: If provided, environment variables to
            pass to the subprocess.
        :param bool for_coverage: If true and if coverage is enabled, produce a
            trace file.
        :param bool memcheck: If true and if Valgrind runs are requested, run
            this process under Valgrind. If there are memory issues, they be
            reported on the testcase output and the process will return
            non-zero.
        """
        program = argv[0]

        if for_coverage and self.coverage_enabled:
            trace_file = self.coverage_file('trace')
            argv = ['gnatcov', 'run', '-o', trace_file] + argv

        if memcheck and self.valgrind_enabled:
            argv = valgrind_cmd(argv)

        p = Run(argv,
                cwd=self.working_dir(),
                timeout=self.TIMEOUT,
                output=self.output_file,
                error=STDOUT,
                env=env)

        if p.status != 0:
            self.result.actual_output += (
                '{} returned status code {}\n'.format(program, p.status))
            self.result.actual_output += self.read_file(self.output_file)
            raise TestError('{} returned status code {}'.format(
                program, p.status))
Example #3
0
def build_and_run(grammar,
                  py_script=None,
                  ada_main=None,
                  lexer=None,
                  warning_set=default_warning_set,
                  generate_unparser=False,
                  symbol_canonicalizer=None):
    """
    Compile and emit code for `ctx` and build the generated library. Then,
    execute the provided scripts/programs, if any.

    An exception is raised if any step fails (the script must return code 0).

    :param langkit.lexer.Lexer lexer: The lexer to use along with the grammar.
        See emit_and_print_errors.
    :param None|str py_script: If not None, name of the Python script to run
        with the built library available.
    :param None|str|list[str] ada_main: If not None, list of name of main
        source files for Ada programs to build and run with the generated
        library. If the input is a single string, consider it's a single mail
        source file.
    :param WarningSet warning_set: Set of warnings to emit.
    :param bool generate_unparser: Whether to generate unparser.
    :param langkit.compile_context.LibraryEntity|None symbol_canonicalizer:
        Symbol canoncalizes to use for this context, if any.
    """

    if lexer is None:
        from lexer_example import foo_lexer
        lexer = foo_lexer

    ctx = prepare_context(grammar,
                          lexer,
                          warning_set,
                          symbol_canonicalizer=symbol_canonicalizer)

    class Manage(ManageScript):
        def create_context(self, args):
            return ctx

    m = Manage(override_lang_source_dir=False)

    extensions_dir = os.path.abspath('extensions')
    if os.path.isdir(extensions_dir):
        ctx.extensions_dir = extensions_dir

    # First build the library. Forward all test.py's arguments to the libmanage
    # call so that manual testcase runs can pass "-g", for instance.
    argv = sys.argv[1:] + ['--full-error-traces', '-vnone', 'make']
    for w in WarningSet.available_warnings:
        argv.append('-{}{}'.format('W' if w in warning_set else 'w', w.name))
    if not pretty_print:
        argv.append('--no-pretty-print')
    if generate_unparser:
        argv.append('--generate-unparser')
    m.run(argv)

    # Flush stdout and stderr, so that diagnostics appear deterministically
    # before the script/program output.
    sys.stdout.flush()
    sys.stderr.flush()

    # Write a "setenv" script to make developper investigation convenient
    with open('setenv.sh', 'w') as f:
        m.write_setenv(f)

    env = m.derived_env()

    def run(*argv):
        subprocess.check_call(argv, env=env)

    if py_script is not None:
        # Run the Python script. Note that in order to use the generated
        # library, we have to use the special Python interpreter the testsuite
        # provides us. See the corresponding code in
        # testuite_support/python_driver.py.
        python_interpreter = os.environ['PYTHON_INTERPRETER']
        run(python_interpreter, py_script)

    if ada_main is not None:
        if isinstance(ada_main, str):
            ada_main = [ada_main]

        # Generate a project file to build the given Ada main and then run
        # the program.
        with open('gen.gpr', 'w') as f:
            f.write(
                project_template.format(main_sources=', '.join(
                    '"{}"'.format(m) for m in ada_main)))
        run('gprbuild', '-Pgen', '-q', '-p', '-XLIBRARY_TYPE=relocatable',
            '-XXMLADA_BUILD=relocatable')

        for i, m in enumerate(ada_main):
            assert m.endswith('.adb')
            if i > 0:
                print('')
            if len(ada_main) > 1:
                print('== {} =='.format(m))
            sys.stdout.flush()
            argv = [os.path.join('obj', m[:-4])]
            if os.environ.get('VALGRIND_ENABLED'):
                argv = valgrind_cmd(argv)
            run(*argv)