Exemple #1
0
def environment_exe_case(
    environment: Dict[str, str],
    program_symbol_name: str,
) -> NExArr[PrimAndExeExpectation[MatcherWTrace, MatchingResult], Arrangement]:
    py_file = File(
        'environment-vars-check.py',
        py_programs.
        pgm_that_exists_with_zero_exit_code_iff_environment_vars_not_included(
            environment))

    py_file_rel_opt_conf = rel_opt.conf_rel_any(RelOptionType.REL_TMP)
    py_file_conf = py_file_rel_opt_conf.named_file_conf(py_file.name)

    program_symbol = ProgramSymbolContext.of_sdv(
        program_symbol_name,
        program_sdvs.interpret_py_source_file_that_must_exist(
            py_file_conf.path_sdv))

    return NExArr(
        'Environment: {}'.format(repr(environment)),
        PrimAndExeExpectation.of_exe(
            main_result=asrt_run.is_result_for_py_interpreter(
                py_run_programs.EXIT_CODE_FOR_SUCCESS)),
        arrangement_w_tcds(
            tcds_contents=py_file_rel_opt_conf.
            populator_for_relativity_option_root(DirContents([py_file])),
            symbols=program_symbol.symbol_table,
            process_execution=ProcessExecutionArrangement(
                process_execution_settings=ProcessExecutionSettings.
                with_environ(environment))))
    def runTest(self):
        output_to_stderr = 'on stderr'
        py_file = File(
            'exit-with-value-on-command-line.py',
            py_pgm_that_exits_with_1st_value_on_command_line(output_to_stderr))

        py_file_rel_opt_conf = relativity_options.conf_rel_any(
            RelOptionType.REL_TMP)
        py_file_conf = py_file_rel_opt_conf.named_file_conf(py_file.name)

        program_that_executes_py_pgm_symbol = ProgramSymbolContext.of_sdv(
            'PROGRAM_THAT_EXECUTES_PY_FILE',
            program_sdvs.interpret_py_source_file_that_must_exist(
                py_file_conf.path_sdv))

        symbols = program_that_executes_py_pgm_symbol.symbol_table

        instruction_arguments = [
            pgm_args.symbol_ref_command_line(
                program_that_executes_py_pgm_symbol.name),
            0,
        ]
        self.conf.run_test(
            self,
            single_line_source(args.sequence(instruction_arguments).as_str),
            self.conf.arrangement(tcds_contents=py_file_rel_opt_conf.
                                  populator_for_relativity_option_root(
                                      DirContents([py_file])),
                                  symbols=symbols),
            self.conf.expect_success(symbol_usages=asrt.matches_sequence(
                [program_that_executes_py_pgm_symbol.reference_assertion])),
        )
    def _check_with_exit_code(self, exit_code: int):
        # ARRANGE #
        result = SubProcessResult(
            exitcode=exit_code,
            stdout='output on stdout',
            stderr='output on stderr',
        )

        command_py_program = py_program.program_that_prints_and_exits_with_exit_code(result)

        py_file = fs.File(
            'the-program.py',
            command_py_program,
        )
        program_wo_transformation = ProgramSymbolContext.of_sdv(
            'PROGRAM_SYMBOL',
            program_sdvs.interpret_py_source_file_that_must_exist(
                path_sdvs.of_rel_option_with_const_file_name(
                    RelOptionType.REL_HDS_CASE,
                    py_file.name,
                )
            )
        )

        source = args.program(
            args.symbol_ref_command_line(program_wo_transformation.name),
            transformation=TO_UPPER_CASE_TRANSFORMER.name__sym_ref_syntax)

        symbols = [
            program_wo_transformation,
            TO_UPPER_CASE_TRANSFORMER,
        ]
        # ACT & ASSERT #

        integration_check.check_execution(
            self,
            sut.actor(),
            [instr(source.as_arguments.lines)],
            arrangement_w_tcds(
                symbol_table=SymbolContext.symbol_table_of_contexts(symbols),
                hds_contents=hds_populators.contents_in(
                    RelHdsOptionType.REL_HDS_CASE,
                    DirContents([py_file]),
                )
            ),
            Expectation(
                symbol_usages=SymbolContext.references_assertion_of_contexts(symbols),
                execute=asrt_eh.is_exit_code(result.exitcode),
                post_sds=PostSdsExpectation.constant(
                    sub_process_result_from_execute=asrt_proc_result.matches_proc_result(
                        exit_code=asrt.equals(result.exitcode),
                        stdout=asrt.equals(result.stdout.upper()),
                        stderr=asrt.equals(result.stderr),
                    )
                )
            ),
        )
    def runTest(self):
        # ARRANGE #

        command_py_program = py_program.exit_with_code(0)

        py_file = fs.File(
            'the-program.py',
            lines_content(command_py_program),
        )
        program_wo_transformation = ProgramSymbolContext.of_sdv(
            'PROGRAM_SYMBOL',
            program_sdvs.interpret_py_source_file_that_must_exist(
                path_sdvs.of_rel_option_with_const_file_name(
                    RelOptionType.REL_HDS_CASE,
                    py_file.name,
                )
            )
        )

        error_message = 'error message from transformer'
        transformer = StringTransformerPrimitiveSymbolContext(
            'HARD_ERROR_TRANSFORMER',
            string_transformers.model_access_raises_hard_error(error_message),
        )

        source = args.program(
            args.symbol_ref_command_line(program_wo_transformation.name),
            transformation=transformer.name__sym_ref_syntax)

        symbols = [
            program_wo_transformation,
            transformer,
        ]

        # ACT & ASSERT #

        integration_check.check_execution(
            self,
            sut.actor(),
            [instr(source.as_arguments.lines)],
            arrangement_w_tcds(
                symbol_table=SymbolContext.symbol_table_of_contexts(symbols),
                hds_contents=hds_populators.contents_in(
                    RelHdsOptionType.REL_HDS_CASE,
                    DirContents([py_file]),
                )
            ),
            Expectation(
                symbol_usages=SymbolContext.references_assertion_of_contexts(symbols),
                execute=asrt_eh.matches_hard_error(
                    asrt_failure_details.is_failure_message_matching__td(
                        asrt_text_doc.is_string_for_test_that_equals(error_message)
                    )
                ),
            ),
        )
Exemple #5
0
class TestProgramViaSymbolReference(unittest.TestCase):
    output_to_stderr = 'on stderr'
    py_file = File(
        'exit-with-value-on-command-line.py',
        py_pgm_that_exits_with_1st_value_on_command_line(output_to_stderr))

    py_file_rel_opt_conf = relativity_options.conf_rel_any(
        RelOptionType.REL_TMP)
    py_file_conf = py_file_rel_opt_conf.named_file_conf(py_file.name)

    program_that_executes_py_pgm_symbol = ProgramSymbolContext.of_sdv(
        'PROGRAM_THAT_EXECUTES_PY_FILE',
        program_sdvs.interpret_py_source_file_that_must_exist(
            py_file_conf.path_sdv))

    symbols = program_that_executes_py_pgm_symbol.symbol_table

    def test_check_zero_exit_code(self):
        EXECUTION_CHECKER.check__w_source_variants(
            self,
            args.sequence([
                pgm_args.symbol_ref_command_line(
                    self.program_that_executes_py_pgm_symbol.name), 0
            ]).as_str,
            Arrangement.phase_agnostic(
                tcds=TcdsArrangement(tcds_contents=self.py_file_rel_opt_conf.
                                     populator_for_relativity_option_root(
                                         DirContents([self.py_file])), ),
                symbols=self.symbols),
            MultiSourceExpectation.phase_agnostic(
                main_result=result_assertions.equals(0, None),
                symbol_usages=asrt.matches_sequence([
                    self.program_that_executes_py_pgm_symbol.
                    reference_assertion
                ])))

    def test_check_non_zero_exit_code(self):
        exit_code = 87
        EXECUTION_CHECKER.check__w_source_variants(
            self,
            args.sequence([
                pgm_args.symbol_ref_command_line(
                    self.program_that_executes_py_pgm_symbol.name), exit_code
            ]).as_str,
            Arrangement.phase_agnostic(
                tcds=TcdsArrangement(tcds_contents=self.py_file_rel_opt_conf.
                                     populator_for_relativity_option_root(
                                         DirContents([self.py_file])), ),
                symbols=self.symbols),
            MultiSourceExpectation.phase_agnostic(
                main_result=result_assertions.equals(exit_code,
                                                     self.output_to_stderr),
                symbol_usages=asrt.matches_sequence([
                    self.program_that_executes_py_pgm_symbol.
                    reference_assertion
                ])))
Exemple #6
0
    def _check(
        self,
        output: StdOutputFilesContents,
        exit_code_cases: List[int],
        ignore_exit_code: bool,
        execution_expectation: ExecutionExpectation,
    ):
        # ARRANGE #

        py_file_rel_opt_conf = rel_opt.conf_rel_any(RelOptionType.REL_TMP)

        for exit_code in exit_code_cases:
            with self.subTest(non_zero_exit_code=exit_code):
                py_file = File(
                    'exit-with-hard-coded-exit-code.py',
                    py_programs.py_pgm_with_stdout_stderr_exit_code(
                        stdout_output=output.out,
                        stderr_output=output.err,
                        exit_code=exit_code,
                    ),
                )

                py_file_conf = py_file_rel_opt_conf.named_file_conf(
                    py_file.name)

                program_symbol = ProgramSymbolContext.of_sdv(
                    'PROGRAM_SYMBOL_NAME',
                    program_sdvs.interpret_py_source_file_that_must_exist(
                        py_file_conf.path_sdv))

                # ACT && ASSERT #

                integration_check.CHECKER__PARSE_FULL.check(
                    self,
                    args.syntax_for_run(
                        program_args.symbol_ref_command_elements(
                            program_symbol.name),
                        ignore_exit_code=ignore_exit_code,
                    ).as_remaining_source,
                    model_constructor.arbitrary(self),
                    arrangement_w_tcds(
                        symbols=program_symbol.symbol_table,
                        tcds_contents=py_file_rel_opt_conf.
                        populator_for_relativity_option_root(
                            DirContents([py_file])),
                    ),
                    Expectation(
                        ParseExpectation(
                            source=asrt_source.is_at_end_of_line(1),
                            symbol_references=program_symbol.
                            references_assertion),
                        execution_expectation,
                    ),
                )
Exemple #7
0
    def _check_failing_validation_of_referenced_program__for_relativity(self, missing_file_relativity: RelOptionType):
        relativity_conf = relativity_options.conf_rel_any(missing_file_relativity)

        program_symbol_with_ref_to_non_exiting_exe_file = ProgramSymbolContext.of_sdv(
            'PGM_WITH_REF_TO_EXE_FILE',
            program_sdvs.ref_to_exe_file(
                constant(simple_of_rel_option(relativity_conf.relativity_option,
                                              'non-existing-exe-file')))
        )

        program_symbol_with_ref_to_non_exiting_file_as_argument = ProgramSymbolContext.of_sdv(
            'PGM_WITH_ARG_WITH_REF_TO_FILE',
            program_sdvs.interpret_py_source_file_that_must_exist(
                constant(simple_of_rel_option(relativity_conf.relativity_option,
                                              'non-existing-python-file.py')))
        )

        expectation = MultiSourceExpectation.of_const(
            symbol_references=asrt.anything_goes(),
            primitive=asrt.anything_goes(),
            execution=ExecutionExpectation(
                validation=FAILING_VALIDATION_ASSERTION_FOR_PARTITION[relativity_conf.directory_structure_partition],
            )
        )

        symbols = SymbolContext.symbol_table_of_contexts([
            program_symbol_with_ref_to_non_exiting_exe_file,
            program_symbol_with_ref_to_non_exiting_file_as_argument,
        ])
        arrangement = arrangement_w_tcds(
            symbols=symbols,
        )

        cases = [
            NameAndValue(
                'executable does not exist',
                RawProgramOfSymbolReferenceAbsStx(program_symbol_with_ref_to_non_exiting_exe_file.name),
            ),
            NameAndValue(
                'file referenced from argument does not exist',
                RawProgramOfSymbolReferenceAbsStx(program_symbol_with_ref_to_non_exiting_file_as_argument.name),
            ),
        ]

        for case in cases:
            with self.subTest(case=case.name):
                # ACT & ASSERT #
                CHECKER_WO_EXECUTION.check__abs_stx__layouts__source_variants__wo_input(
                    self,
                    equivalent_source_variants__for_consume_until_end_of_last_line__s__nsc,
                    case.value,
                    arrangement,
                    expectation,
                )
Exemple #8
0
    def runTest(self):
        # ARRANGE #

        py_file = File('stdin-to-upper.py',
                       _PGM_THAT_OUTPUTS_STDIN_IN_UPPER_CASE)

        py_file_rel_opt_conf = rel_opt.conf_rel_any(RelOptionType.REL_TMP)
        py_file_conf = py_file_rel_opt_conf.named_file_conf(py_file.name)

        program_symbol = ProgramSymbolContext.of_sdv(
            'PROGRAM_THAT_EXECUTES_PY_FILE',
            program_sdvs.interpret_py_source_file_that_must_exist(py_file_conf.path_sdv)
        )

        input_model_lines = [
            'the\n',
            'input\n',
            'model',
        ]
        expected_output_model_lines = list(map(str.upper, input_model_lines))

        for with_ignored_exit_code in [False, True]:
            with self.subTest(with_ignored_exit_code=with_ignored_exit_code):
                # ACT && ASSERT #

                integration_check.CHECKER__PARSE_FULL.check__w_source_variants_for_full_line_parser(
                    self,
                    args.syntax_for_run(
                        program_args.symbol_ref_command_elements(program_symbol.name),
                        ignore_exit_code=with_ignored_exit_code,
                    ),
                    model_constructor.of_lines(self, input_model_lines),
                    arrangement_w_tcds(
                        tcds_contents=py_file_rel_opt_conf.populator_for_relativity_option_root(
                            DirContents([py_file])
                        ),
                        symbols=program_symbol.symbol_table,
                    ),
                    MultiSourceExpectation(
                        symbol_references=program_symbol.references_assertion,
                        execution=ExecutionExpectation(
                            main_result=asrt_string_source.pre_post_freeze__matches_lines(
                                asrt.equals(expected_output_model_lines),
                                may_depend_on_external_resources=asrt.equals(True),
                                frozen_may_depend_on_external_resources=asrt.anything_goes(),
                            )
                        ),
                        primitive=prim_asrt__constant(
                            asrt_string_transformer.is_identity_transformer(False)
                        ),
                    ),
                )
 def _instructions_for_executing_py_source(
         self, py_src: List[str]) -> ContextManager[TestCaseSourceSetup]:
     py_file = fs.File('the-program.py', lines_content(py_src))
     symbol = ProgramSymbolContext.of_sdv(
         'PROGRAM_SYMBOL',
         program_sdvs.interpret_py_source_file_that_must_exist(
             path_sdvs.of_rel_option_with_const_file_name(
                 RelOptionType.REL_HDS_ACT,
                 py_file.name,
             )))
     program_line = args.symbol_ref_command_line(symbol.name).as_str
     yield TestCaseSourceSetup(
         act_phase_instructions=[instr([program_line])],
         home_act_dir_contents=DirContents([py_file]),
         symbols=symbol.symbol_table,
         symbol_usages=symbol.usages_assertion)
Exemple #10
0
def exit_code_exe_cases(
    program_symbol_name: str,
    exit_code_symbol_name: str,
) -> Sequence[NExArr[PrimAndExeExpectation[MatcherWTrace, MatchingResult],
                     Arrangement]]:
    py_file = File(
        'exit-with-value-on-command-line.py',
        py_programs.py_pgm_that_exits_with_1st_value_on_command_line(''))

    py_file_rel_opt_conf = rel_opt.conf_rel_any(RelOptionType.REL_TMP)
    py_file_conf = py_file_rel_opt_conf.named_file_conf(py_file.name)

    program_symbol = ProgramSymbolContext.of_sdv(
        program_symbol_name,
        program_sdvs.interpret_py_source_file_that_must_exist(
            py_file_conf.path_sdv))

    def case(
        exit_code_from_program: int
    ) -> NExArr[PrimAndExeExpectation[MatcherWTrace, MatchingResult],
                Arrangement]:
        exit_code_symbol = StringIntConstantSymbolContext(
            exit_code_symbol_name, exit_code_from_program)

        return NExArr(
            'Exit code {}'.format(exit_code_from_program),
            PrimAndExeExpectation.of_exe(
                main_result=asrt_run.is_result_for_py_interpreter(
                    exit_code_from_program)),
            arrangement_w_tcds(
                tcds_contents=py_file_rel_opt_conf.
                populator_for_relativity_option_root(DirContents([py_file])),
                symbols=SymbolContext.symbol_table_of_contexts([
                    program_symbol,
                    exit_code_symbol,
                ]),
            ))

    exit_code_from_program_cases = [0, 1, 3, 69, 72]

    return [
        case(exit_code_from_program)
        for exit_code_from_program in exit_code_from_program_cases
    ]
Exemple #11
0
def argument_list_exe_case(
    command_line_arguments: List[str],
    expected_program_arguments: List[str],
    program_symbol_name: str,
    list_arg_symbol_name: str,
) -> NExArr[PrimAndExeExpectation[MatcherWTrace, MatchingResult], Arrangement]:
    py_file = File(
        'arguments-check.py',
        py_run_programs.
        pgm_that_exists_with_zero_exit_code_iff_arguments_are_expected(
            expected_program_arguments))

    py_file_rel_opt_conf = rel_opt.conf_rel_any(RelOptionType.REL_TMP)
    py_file_conf = py_file_rel_opt_conf.named_file_conf(py_file.name)

    program_symbol = ProgramSymbolContext.of_sdv(
        program_symbol_name,
        program_sdvs.interpret_py_source_file_that_must_exist(
            py_file_conf.path_sdv))

    arg_list_symbol = ListSymbolContext.of_constants(list_arg_symbol_name,
                                                     command_line_arguments)

    return NExArr(
        'Command line arguments: {}'.format(repr(command_line_arguments), ),
        PrimAndExeExpectation.of_exe(
            main_result=asrt_run.is_result_for_py_interpreter(
                py_run_programs.EXIT_CODE_FOR_SUCCESS)),
        arrangement_w_tcds(
            tcds_contents=py_file_rel_opt_conf.
            populator_for_relativity_option_root(DirContents([py_file])),
            symbols=SymbolContext.symbol_table_of_contexts([
                program_symbol,
                arg_list_symbol,
            ]),
        ))
    def runTest(self):
        # ARRANGE #
        exit_code_from_program = 0
        py_file = fs.File(
            'the-program',
            lines_content(py_program.exit_with_code(exit_code_from_program)),
        )
        program_symbol = ProgramSymbolContext.of_sdv(
            'PROGRAM_SYMBOL',
            program_sdvs.interpret_py_source_file_that_must_exist(
                path_sdvs.of_rel_option_with_const_file_name(
                    RelOptionType.REL_HDS_CASE,
                    py_file.name,
                )))
        program_line = args.symbol_ref_command_line(program_symbol.name).as_str

        with tmp_dir_in_path_with_files(DirContents([py_file])) as environ:
            for source_case in valid_source_variants(program_line):
                with self.subTest(source_case.name):
                    # ACT & ASSERT #
                    integration_check.check_execution(
                        self,
                        sut.actor(),
                        [instr([program_line])],
                        arrangement_w_tcds(
                            symbol_table=program_symbol.symbol_table,
                            act_exe_input=AtcExeInputArr(environ=environ),
                            hds_contents=hds_populators.contents_in(
                                RelHdsOptionType.REL_HDS_CASE,
                                DirContents([py_file])),
                        ),
                        Expectation(
                            symbol_usages=program_symbol.usages_assertion,
                            execute=asrt_eh.is_exit_code(
                                exit_code_from_program)),
                    )
Exemple #13
0
    def runTest(self):
        # ARRANGE #

        py_file = File('output-env-vars.py',
                       _PGM_THAT_OUTPUTS_ENVIRONMENT_VARS)

        py_file_rel_opt_conf = rel_opt.conf_rel_any(RelOptionType.REL_TMP)
        py_file_conf = py_file_rel_opt_conf.named_file_conf(py_file.name)

        program_symbol = ProgramSymbolContext.of_sdv(
            'PROGRAM_THAT_EXECUTES_PY_FILE',
            program_sdvs.interpret_py_source_file_that_must_exist(
                py_file_conf.path_sdv))
        environment_cases = [
            {
                '1': 'one',
            },
            {
                '1': 'one',
                '2': 'two',
            },
        ]

        for with_ignored_exit_code in [False, True]:
            with self.subTest(with_ignored_exit_code=with_ignored_exit_code):
                # ACT && ASSERT #

                integration_check.CHECKER__PARSE_FULL.check_multi(
                    self,
                    args.syntax_for_run(
                        program_args.symbol_ref_command_elements(
                            program_symbol.name),
                        ignore_exit_code=with_ignored_exit_code,
                    ),
                    ParseExpectation(
                        source=asrt_source.is_at_end_of_line(1),
                        symbol_references=program_symbol.references_assertion,
                    ),
                    model_constructor.arbitrary(self),
                    [
                        NExArr(
                            'Environment: {}'.format(repr(environment)),
                            PrimAndExeExpectation(
                                ExecutionExpectation(
                                    main_result=asrt_string_source.
                                    pre_post_freeze__matches_lines(
                                        _AssertLinesRepresentSubSetOfDict(
                                            environment),
                                        may_depend_on_external_resources=asrt.
                                        equals(True),
                                        frozen_may_depend_on_external_resources
                                        =asrt.anything_goes(),
                                    )),
                                prim_asrt__constant(
                                    asrt_string_transformer.
                                    is_identity_transformer(False)),
                            ),
                            arrangement_w_tcds(
                                tcds_contents=py_file_rel_opt_conf.
                                populator_for_relativity_option_root(
                                    DirContents([py_file])),
                                symbols=program_symbol.symbol_table,
                                process_execution=ProcessExecutionArrangement(
                                    process_execution_settings=
                                    ProcessExecutionSettings.with_environ(
                                        environment))))
                        for environment in environment_cases
                    ],
                )
    def _check_exit_codes(self,
                          exit_code_cases: List[int],
                          ignore_exit_code: bool,
                          expected_primitive: Callable[[str], Assertion[StringSource]],
                          ):
        # ARRANGE #

        program_output = {
            ProcOutputFile.STDOUT: 'output on stdout',
            ProcOutputFile.STDERR: 'output on stderr',
        }
        transformer = TO_UPPER_TRANSFORMER_SYMBOL

        sym_ref_program = ProgramOfSymbolReferenceAbsStx('PROGRAM_SYMBOL_NAME')
        program_builder = program_abs_stx.TransformableProgramAbsStxBuilder(
            ProgramOfSymbolReferenceAbsStx(sym_ref_program.symbol_name)
        )
        program_cases = [
            ProgramAndSymbolsCase(
                'without transformation',
                program_builder.without_transformation(),
                [],
                adapt_expected_program_output=lambda s: s
            ),
            ProgramAndSymbolsCase(
                'with transformation',
                program_builder.with_transformation(transformer.abstract_syntax),
                [transformer],
                adapt_expected_program_output=str.upper
            ),
        ]
        program_builder.with_and_without_transformer_cases(transformer.abstract_syntax)

        py_file_rel_conf = rel_opt.conf_rel_any(RelOptionType.REL_HDS_CASE)

        for exit_code in exit_code_cases:
            py_file = File('exit-with-hard-coded-exit-code.py',
                           py_programs.py_pgm_with_stdout_stderr_exit_code(
                               exit_code=exit_code,
                               stdout_output=program_output[ProcOutputFile.STDOUT],
                               stderr_output=program_output[ProcOutputFile.STDERR],
                           ),
                           )

            py_file_conf = py_file_rel_conf.named_file_conf(py_file.name)

            program_symbol = ProgramSymbolContext.of_sdv(
                sym_ref_program.symbol_name,
                program_sdvs.interpret_py_source_file_that_must_exist(py_file_conf.path_sdv)
            )
            for output_file in ProcOutputFile:
                for program_case in program_cases:
                    syntax = string_source_abs_stx.StringSourceOfProgramAbsStx(
                        output_file,
                        program_case.syntax,
                        ignore_exit_code=ignore_exit_code,
                    )
                    expected_program_output = program_case.adapt_expected_program_output(program_output[output_file])
                    symbol_contexts = [program_symbol] + program_case.additional_symbols
                    # ACT && ASSERT #
                    checker = integration_check.checker__w_arbitrary_file_relativities()
                    with self.subTest(exit_code=exit_code,
                                      output_file=output_file,
                                      program=program_case.name):
                        checker.check__abs_stx(
                            self,
                            syntax,
                            None,
                            arrangement_w_tcds(
                                symbols=SymbolContext.symbol_table_of_contexts(symbol_contexts),
                                tcds_contents=py_file_rel_conf.populator_for_relativity_option_root(
                                    DirContents([py_file])
                                )
                            ),
                            Expectation(
                                ParseExpectation(
                                    symbol_references=SymbolContext.references_assertion_of_contexts(symbol_contexts),
                                ),
                                primitive=prim_asrt__constant(
                                    expected_primitive(expected_program_output)
                                ),
                            ),
                        ),
Exemple #15
0
    def runTest(self):
        # ARRANGE #

        py_file = File(
            'print-arguments-on-stdout.py',
            _PY_PROGRAM_THAT_PRINTS_ONE_ARGUMENT_PER_LINE_ON_STDOUT,
        )
        py_file_rel_opt_conf = rel_opt.conf_rel_any(RelOptionType.REL_TMP)

        py_file_conf = py_file_rel_opt_conf.named_file_conf(py_file.name)

        program_symbol = ProgramSymbolContext.of_sdv(
            'PROGRAM_SYMBOL_NAME',
            program_sdvs.interpret_py_source_file_that_must_exist(
                py_file_conf.path_sdv))

        command_line_arg_list_symbol_name = 'COMMAND_LINE_ARGUMENTS_LIST'

        command_line_arguments_cases = [
            [],
            ['one'],
            ['one', 'two'],
        ]

        def arguments_case(
            command_line_arguments: List[str],
        ) -> NExArr[PrimAndExeExpectation[StringTransformer, StringSource],
                    Arrangement]:
            arg_list_symbol = ListSymbolContext.of_constants(
                command_line_arg_list_symbol_name, command_line_arguments)

            symbols = [
                program_symbol,
                arg_list_symbol,
            ]

            expected_lines_on_stdout = asrt.matches_sequence(
                [asrt.equals(arg + '\n') for arg in command_line_arguments])

            return NExArr(
                'Arguments: ' + repr(command_line_arguments),
                PrimAndExeExpectation(
                    ExecutionExpectation(
                        main_result=asrt_string_source.
                        pre_post_freeze__matches_lines(
                            expected_lines_on_stdout,
                            may_depend_on_external_resources=asrt.equals(True),
                            frozen_may_depend_on_external_resources=asrt.
                            anything_goes(),
                        )),
                    prim_asrt__constant(
                        asrt_string_transformer.is_identity_transformer(
                            False)),
                ),
                arrangement_w_tcds(
                    symbols=SymbolContext.symbol_table_of_contexts(symbols),
                    tcds_contents=py_file_rel_opt_conf.
                    populator_for_relativity_option_root(DirContents([py_file
                                                                      ])),
                ),
            )

        for with_ignored_exit_code in [False, True]:
            with self.subTest(with_ignored_exit_code=with_ignored_exit_code):
                # ACT && ASSERT #

                integration_check.CHECKER__PARSE_FULL.check_multi(
                    self,
                    args.syntax_for_run(
                        program_args.symbol_ref_command_elements(
                            program_symbol.name,
                            arguments=[
                                symbol_reference_syntax_for_name(
                                    command_line_arg_list_symbol_name)
                            ],
                        ),
                        ignore_exit_code=with_ignored_exit_code,
                    ),
                    ParseExpectation(
                        source=asrt_source.is_at_end_of_line(1),
                        symbol_references=asrt.matches_sequence([
                            program_symbol.reference_assertion,
                            is_reference_to__w_str_rendering(
                                command_line_arg_list_symbol_name),
                        ]),
                    ),
                    model_constructor.arbitrary(self),
                    [
                        arguments_case(command_line_arguments) for
                        command_line_arguments in command_line_arguments_cases
                    ],
                )
    def _check_exit_codes(self,
                          exit_code_cases: List[int],
                          ignore_exit_code: bool,
                          main_result: Assertion[Optional[TextRenderer]],
                          expected_output_dir_contents: Optional[Callable[[str, str], DirContents]],
                          ):
        # ARRANGE #
        destination_file_name = 'dst-file.txt'

        program_output = {
            ProcOutputFile.STDOUT: 'output on stdout',
            ProcOutputFile.STDERR: 'output on stderr',
        }
        transformer = TO_UPPER_TRANSFORMER_SYMBOL

        sym_ref_program = ProgramOfSymbolReferenceAbsStx('PROGRAM_SYMBOL_NAME')
        program_builder = program_abs_stx.TransformableProgramAbsStxBuilder(
            ProgramOfSymbolReferenceAbsStx(sym_ref_program.symbol_name)
        )
        program_cases = [
            ProgramAndSymbolsCase(
                'without transformation',
                program_builder.without_transformation(),
                [],
                adapt_expected_program_output=lambda s: s
            ),
            ProgramAndSymbolsCase(
                'with transformation',
                program_builder.with_transformation(transformer.abstract_syntax),
                [transformer],
                adapt_expected_program_output=str.upper
            ),
        ]
        program_builder.with_and_without_transformer_cases(transformer.abstract_syntax)

        py_file_rel_conf = rel_opt.conf_rel_any(RelOptionType.REL_HDS_CASE)
        dst_file_rel_conf = ARBITRARY_ALLOWED_DST_FILE_RELATIVITY

        for output_file in ProcOutputFile:
            for exit_code in exit_code_cases:
                py_file = File('exit-with-hard-coded-exit-code.py',
                               py_programs.py_pgm_with_stdout_stderr_exit_code(
                                   exit_code=exit_code,
                                   stdout_output=program_output[ProcOutputFile.STDOUT],
                                   stderr_output=program_output[ProcOutputFile.STDERR],
                               ),
                               )

                py_file_conf = py_file_rel_conf.named_file_conf(py_file.name)
                dst_file_conf = dst_file_rel_conf.named_file_conf(destination_file_name)

                program_symbol = ProgramSymbolContext.of_sdv(
                    sym_ref_program.symbol_name,
                    program_sdvs.interpret_py_source_file_that_must_exist(py_file_conf.path_sdv)
                )

                for program_case in program_cases:
                    instruction_syntax = instr_abs_stx.create_w_explicit_contents(
                        dst_file_conf.abstract_syntax,
                        string_source_abs_stx.StringSourceOfProgramAbsStx(
                            output_file,
                            program_case.syntax,
                            ignore_exit_code=ignore_exit_code)
                    )
                    expected_program_output = program_case.adapt_expected_program_output(program_output[output_file])
                    main_side_effects_on_sds = (
                        dst_file_rel_conf.assert_root_dir_contains_exactly(
                            expected_output_dir_contents(dst_file_conf.name, expected_program_output)
                        )
                        if expected_output_dir_contents is not None
                        else
                        asrt.anything_goes()
                    )
                    symbol_contexts = [program_symbol] + program_case.additional_symbols
                    # ACT && ASSERT #
                    for phase_is_after_act in [False, True]:
                        checker = integration_check.checker(phase_is_after_act)
                        checker.check__abs_stx__std_layouts_and_source_variants(
                            self,
                            instruction_syntax,
                            Arrangement.phase_agnostic(
                                symbols=SymbolContext.symbol_table_of_contexts(symbol_contexts),
                                tcds=TcdsArrangement(
                                    tcds_contents=py_file_rel_conf.populator_for_relativity_option_root(
                                        DirContents([py_file])
                                    )
                                ),
                            ),
                            MultiSourceExpectation.phase_agnostic(
                                symbol_usages=SymbolContext.usages_assertion_of_contexts(symbol_contexts),
                                main_result=main_result,
                                main_side_effects_on_sds=main_side_effects_on_sds,
                            ),
                            sub_test_identifiers={
                                'exit_code': exit_code,
                                'output_file': output_file,
                                'program': program_case.name,
                                'phase_is_after_act': phase_is_after_act,
                            },
                        )
Exemple #17
0
    def test_string_transformer_should_be_parsed_as_simple_expression(self):
        the_layout = LayoutSpec.of_default()

        output_from_program = 'untransformed output from the program'

        sym_ref_program_syntax = ProgramOfSymbolReferenceAbsStx(
            'PROGRAM_THAT_EXECUTES_PY_FILE')

        str_trans__unused = StringTransformerSymbolReferenceAbsStx(
            'UNUSED_TRANSFORMER')

        program_w_complex_str_trans_wo_parentheses = program_abs_stx.FullProgramAbsStx(
            ProgramOfSymbolReferenceAbsStx(sym_ref_program_syntax.symbol_name),
            transformation=str_trans_abs_stx.
            StringTransformerCompositionAbsStx(
                [
                    TO_UPPER_TRANSFORMER_SYMBOL.abstract_syntax,
                    str_trans__unused,
                ],
                within_parens=False,
                allow_elements_on_separate_lines=False,
            ))
        expected_remaining_tokens = TokenSequence.concat([
            TokenSequence.singleton(
                str_trans_abs_stx.names.SEQUENCE_OPERATOR_NAME),
            str_trans__unused.tokenization(),
        ])
        expected_remaining_source = expected_remaining_tokens.layout(
            the_layout)

        checker = integration_check.checker__w_arbitrary_file_relativities()

        py_program_file = File(
            'program.py',
            py_programs.py_pgm_with_stdout_stderr_exit_code(
                exit_code=0,
                stdout_output=output_from_program,
                stderr_output=output_from_program,
            ),
        )
        py_file_rel_conf = rel_opt.conf_rel_any(RelOptionType.REL_HDS_CASE)
        py_file_conf = py_file_rel_conf.named_file_conf(py_program_file.name)

        program_symbol__that_executes_py_file = ProgramSymbolContext.of_sdv(
            sym_ref_program_syntax.symbol_name,
            program_sdvs.interpret_py_source_file_that_must_exist(
                py_file_conf.path_sdv))
        symbols = [
            program_symbol__that_executes_py_file,
            TO_UPPER_TRANSFORMER_SYMBOL,
        ]

        for output_file in ProcOutputFile:
            for ignore_exit_code in [False, True]:
                syntax = string_source_abs_stx.StringSourceOfProgramAbsStx(
                    output_file,
                    program_w_complex_str_trans_wo_parentheses,
                    ignore_exit_code=ignore_exit_code,
                )
                with self.subTest(output_file=output_file,
                                  ignore_exit_code=ignore_exit_code):
                    checker.check__abs_stx__wo_input(
                        self,
                        syntax,
                        arrangement_w_tcds(
                            symbols=SymbolContext.symbol_table_of_contexts(
                                symbols),
                            tcds_contents=py_file_rel_conf.
                            populator_for_relativity_option_root(
                                DirContents([py_program_file]))),
                        Expectation(
                            ParseExpectation(
                                source=asrt_source.source_is_not_at_end(
                                    remaining_source=asrt.equals(
                                        expected_remaining_source)),
                                symbol_references=SymbolContext.
                                references_assertion_of_contexts(symbols),
                            )),
                        the_layout,
                    )
    def test_syntax_layout_variants(self):
        # ARRANGE #
        exit_code_from_program = 1
        output_from_program = 'the output from the program'
        transformer = TO_UPPER_TRANSFORMER_SYMBOL

        sym_ref_program = ProgramOfSymbolReferenceAbsStx('PROGRAM_SYMBOL_NAME')
        program_builder = program_abs_stx.TransformableProgramAbsStxBuilder(
            ProgramOfSymbolReferenceAbsStx(sym_ref_program.symbol_name)
        )
        output_cases = [
            OutputFileCase(
                ProcOutputFile.STDOUT,
                {
                    ProcOutputFile.STDOUT: output_from_program,
                    ProcOutputFile.STDERR: '',
                }
            ),
            OutputFileCase(
                ProcOutputFile.STDERR,
                {
                    ProcOutputFile.STDOUT: '',
                    ProcOutputFile.STDERR: output_from_program,
                }
            ),
        ]
        program_cases = [
            ProgramAndSymbolsCase(
                'without transformation',
                program_builder.without_transformation(),
                [],
                adapt_expected_program_output=lambda s: s
            ),
            ProgramAndSymbolsCase(
                'with transformation',
                program_builder.with_transformation(transformer.abstract_syntax),
                [transformer],
                adapt_expected_program_output=str.upper
            ),
        ]
        py_file_rel_conf = rel_opt.conf_rel_any(RelOptionType.REL_HDS_CASE)

        for output_case in output_cases:
            py_file = File('exit-with-hard-coded-exit-code.py',
                           py_programs.py_pgm_with_stdout_stderr_exit_code_2(
                               exit_code=exit_code_from_program,
                               output=output_case.program_output,
                           ),
                           )

            py_file_conf = py_file_rel_conf.named_file_conf(py_file.name)

            program_symbol = ProgramSymbolContext.of_sdv(
                sym_ref_program.symbol_name,
                program_sdvs.interpret_py_source_file_that_must_exist(py_file_conf.path_sdv)
            )
            for program_case in program_cases:
                syntax = string_source_abs_stx.StringSourceOfProgramAbsStx(
                    output_case.output_file,
                    program_case.syntax,
                    ignore_exit_code=True,
                )
                expected_program_output = program_case.adapt_expected_program_output(output_from_program)
                symbol_contexts = [program_symbol] + program_case.additional_symbols
                # ACT && ASSERT #
                checker = integration_check.checker__w_arbitrary_file_relativities()
                with self.subTest(output_file=output_case.output_file,
                                  program=program_case.name):
                    checker.check__abs_stx__layouts__source_variants__wo_input(
                        self,
                        equivalent_source_variants__for_full_line_expr_parse__s__nsc,
                        OptionallyOnNewLine(syntax),
                        arrangement_w_tcds(
                            symbols=SymbolContext.symbol_table_of_contexts(symbol_contexts),
                            tcds_contents=py_file_rel_conf.populator_for_relativity_option_root(
                                DirContents([py_file])
                            )
                        ),
                        MultiSourceExpectation.of_const(
                            symbol_references=SymbolContext.references_assertion_of_contexts(symbol_contexts),
                            primitive=asrt_string_source.pre_post_freeze__matches_str__const_2(
                                expected_program_output,
                                may_depend_on_external_resources=True,
                                frozen_may_depend_on_external_resources=asrt.anything_goes(),
                            ),
                        ),
                    ),