示例#1
0
    def test_superfluous_arguments(self):
        # ARRANGE #
        arbitrary_transformer_symbol = StringTransformerSymbolContext.of_arbitrary_value(
            'TRANSFORMER_SYMBOL')

        src_file_relativity_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE)

        file_contents_builder = string_source_abs_stx.TransformableAbsStxBuilder(
            string_source_abs_stx.StringSourceOfFileAbsStx(
                src_file_relativity_conf.path_abs_stx_of_name('src-file.txt')))

        file_contents_cases = [
            NameAndValue('contents of existing file / without transformation',
                         file_contents_builder.without_transformation()),
            NameAndValue(
                'contents of existing file / with transformation',
                file_contents_builder.with_transformation(
                    arbitrary_transformer_symbol.abstract_syntax)),
        ]

        for file_contents_case in file_contents_cases:
            valid_instruction_syntax = instr_abs_stx.create_w_explicit_contents(
                path_abs_stx.DefaultRelPathAbsStx('dst-file.txt'),
                file_contents_case.value,
            )
            invalid_instruction_syntax = custom_abs_stx.SequenceAbsStx([
                valid_instruction_syntax,
                custom_abs_stx.CustomAbstractSyntax.singleton(
                    'superfluous_argument')
            ])
            with self.subTest(file_contents_variant=file_contents_case.name):
                # ACT & ASSERT #
                check_invalid_syntax__abs_stx(self, invalid_instruction_syntax)
示例#2
0
    def runTest(self):
        non_zero_exit_code = 1
        program_string_source_syntax = string_source_abs_stx.StringSourceOfProgramAbsStx(
            ProcOutputFile.STDOUT,
            program_abs_stx.ProgramOfPythonInterpreterAbsStx.
            of_execute_python_src_string(
                py_programs.single_line_pgm_that_exists_with(
                    non_zero_exit_code)),
            ignore_exit_code=False,
        )
        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            conf_rel_non_hds(
                RelNonHdsOptionType.REL_TMP).path_abs_stx_of_name('dst.txt'),
            program_string_source_syntax,
        )

        for source_case in equivalent_source_variants__with_source_check__consume_last_line__abs_stx(
                instruction_syntax):
            with self.subTest(source_case.name):
                self.conf.run_test(
                    self,
                    source_case.value.source,
                    self.conf.arrangement(),
                    self.conf.expect_hard_error_of_main__any(),
                )
 def test_superfluous_arguments(self):
     program_w_superfluous_argument = program_abs_stx.FullProgramAbsStx(
         program_abs_stx.ARBITRARY_TRANSFORMABLE_PROGRAM,
         transformation=str_trans_abs_stx.StringTransformerCompositionAbsStx(
             [
                 StringTransformerSymbolReferenceAbsStx('str_trans_sym_1'),
                 StringTransformerSymbolReferenceAbsStx('str_trans_sym_1'),
             ],
             within_parens=False,
             allow_elements_on_separate_lines=False,
         )
     )
     for phase_is_after_act in [False, True]:
         for output_file in ProcOutputFile:
             for ignore_exit_code in [False, True]:
                 instruction_syntax = instr_abs_stx.create_w_explicit_contents(
                     ARBITRARY_ALLOWED_DST_FILE_RELATIVITY.path_abs_stx_of_name('dst-file'),
                     string_source_abs_stx.StringSourceOfProgramAbsStx(
                         output_file,
                         program_w_superfluous_argument,
                         ignore_exit_code=ignore_exit_code)
                 )
                 with self.subTest(output_file=output_file,
                                   phase_is_after_act=phase_is_after_act,
                                   ignore_exit_code=ignore_exit_code):
                     parse_check.check_invalid_syntax__abs_stx(
                         self,
                         instruction_syntax,
                     )
 def test_contents_from_here_doc(self):
     # ARRANGE #
     string_value = rich_str_abs_stx.HereDocAbsStx(
         'single line in here doc\n')
     expected_file = fs.File('a-file-name.txt', string_value.value)
     rel_opt_conf = ARBITRARY_ALLOWED_DST_FILE_RELATIVITY
     dst_path = rel_opt_conf.path_abs_stx_of_name(expected_file.name)
     instruction_syntax = instr_abs_stx.create_w_explicit_contents(
         dst_path,
         string_source_abs_stx.StringSourceOfStringAbsStx(string_value),
     )
     for phase_is_after_act in [False, True]:
         checker = integration_check.checker(phase_is_after_act)
         with self.subTest(phase_is_after_act=phase_is_after_act):
             # ACT & ASSERT #
             checker.check__abs_stx__std_layouts_and_source_variants(
                 self, instruction_syntax,
                 Arrangement.phase_agnostic(tcds=TcdsArrangement(
                     pre_population_action=
                     SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, ), ),
                 MultiSourceExpectation.phase_agnostic(
                     main_result=IS_SUCCESS,
                     side_effects_on_hds=f_asrt.dir_is_empty(),
                     symbol_usages=asrt.is_empty_sequence,
                     main_side_effects_on_sds=non_hds_dir_contains_exactly(
                         rel_opt_conf.root_dir__non_hds,
                         fs.DirContents([expected_file])),
                 ))
 def test_disallowed_relativities(self):
     # ARRANGE #
     arguments_cases = [
         NameAndValue(
             'here doc',
             string_source_abs_stx.StringSourceOfStringAbsStx(
                 rich_str_abs_stx.HereDocAbsStx('contents line\n'))),
         NameAndValue(
             'raw string',
             string_source_abs_stx.StringSourceOfStringAbsStx.of_str(
                 'raw_string_argument')),
         NameAndValue(
             'quoted string',
             string_source_abs_stx.StringSourceOfStringAbsStx.of_str(
                 'quoted string argument', QuoteType.SOFT)),
     ]
     for relativity in DISALLOWED_DST_RELATIVITIES:
         relativity_conf = conf_rel_any(relativity)
         for contents_case in arguments_cases:
             instruction_syntax = instr_abs_stx.create_w_explicit_contents(
                 relativity_conf.path_abs_stx_of_name('file-name'),
                 contents_case.value,
             )
             with self.subTest(relativity=str(relativity),
                               contents=contents_case.name):
                 # ACT & ASSERT #
                 check_invalid_syntax__abs_stx(self, instruction_syntax)
    def test_symbol_reference_in_dst_file_argument(self):
        dst_path_symbol = ConstantSuffixPathDdvSymbolContext(
            'dst_path_symbol', RelOptionType.REL_ACT, 'dst-file.txt',
            ACCEPTED_DST_RELATIVITY_VARIANTS)
        string_value = rich_str_abs_stx.HereDocAbsStx(
            'single line in here doc\n')
        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            dst_path_symbol.abstract_syntax,
            string_source_abs_stx.StringSourceOfStringAbsStx(string_value),
        )

        expected_file = fs.File(dst_path_symbol.path_suffix,
                                string_value.value)
        integration_check.CHECKER__BEFORE_ACT.check__abs_stx(
            self, instruction_syntax,
            Arrangement.phase_agnostic(
                tcds=TcdsArrangement(
                    pre_population_action=
                    SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, ),
                symbols=dst_path_symbol.symbol_table,
            ),
            Expectation.phase_agnostic(
                main_result=IS_SUCCESS,
                symbol_usages=asrt.matches_singleton_sequence(
                    dst_path_symbol.reference_assertion),
                main_side_effects_on_sds=dir_contains_exactly(
                    dst_path_symbol.rel_option_type,
                    fs.DirContents([expected_file])),
            ))
    def test_WHEN_program_is_non_non_existing_system_command_THEN_result_SHOULD_be_error_message(self):
        failing_program_builder = program_abs_stx.TransformableProgramAbsStxBuilder(
            program_abs_stx.ProgramOfSystemCommandLineAbsStx.of_str(NON_EXISTING_SYSTEM_PROGRAM)
        )
        transformer = TO_UPPER_TRANSFORMER_SYMBOL
        symbols = transformer.symbol_table

        cases = failing_program_builder.with_and_without_transformer_cases(transformer.abstract_syntax)

        for transformation_case in cases:
            instruction_syntax = instr_abs_stx.create_w_explicit_contents(
                path_abs_stx.DefaultRelPathAbsStx('dst-file'),
                string_source_abs_stx.StringSourceOfProgramAbsStx(
                    ProcOutputFile.STDOUT,
                    transformation_case.value,
                    ignore_exit_code=False)
            )
            for phase_is_after_act in [False, True]:
                checker = integration_check.checker(phase_is_after_act)
                with self.subTest(phase_is_after_act=phase_is_after_act,
                                  transformation=transformation_case.name):
                    checker.check__abs_stx__std_layouts_and_source_variants(
                        self,
                        instruction_syntax,
                        Arrangement.phase_agnostic(
                            symbols=symbols,
                            tcds=TcdsArrangement(),
                        ),
                        MultiSourceExpectation.phase_agnostic(
                            symbol_usages=asrt.anything_goes(),
                            main_result=IS_FAILURE,
                        )
                    )
示例#8
0
    def runTest(self):
        # ARRANGE #

        src_file = fs.File('source-file.txt', 'contents of source file')
        src_rel_opt_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE)

        expected_file = fs.File('a-file-name.txt', src_file.contents.upper())
        dst_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_ACT)

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            to_uppercase(),
        )
        symbols = to_upper_transformer.symbol_table

        transformed_file_syntax = string_source_abs_stx.TransformedStringSourceAbsStx(
            string_source_abs_stx.StringSourceOfFileAbsStx(
                src_rel_opt_conf.path_abs_stx_of_name(src_file.name)),
            to_upper_transformer.abstract_syntax,
        )
        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            dst_rel_opt_conf.path_abs_stx_of_name(expected_file.name),
            transformed_file_syntax,
        )

        expected_non_hds_contents = dst_rel_opt_conf.assert_root_dir_contains_exactly(
            fs.DirContents([expected_file]))

        instruction_checker = self.conf.instruction_checker
        parser = self.conf.parser()
        for source_case in equivalent_source_variants__with_source_check__consume_last_line__abs_stx(
                instruction_syntax):
            with self.subTest(source_case.name):
                source = source_case.value.source
                # ACT #
                instruction = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
                # ASSERT #
                source_case.value.expectation.apply_with_message(
                    self, source, 'source-after-parse')
                # ACT & ASSERT #
                instruction_checker.check(
                    self, instruction,
                    self.conf.arrangement(
                        pre_contents_population_action=
                        SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR,
                        tcds_contents=src_rel_opt_conf.
                        populator_for_relativity_option_root(
                            DirContents([src_file])),
                        symbols=symbols,
                    ),
                    self.conf.expect_success(
                        symbol_usages=asrt.matches_sequence([
                            to_upper_transformer.reference_assertion,
                        ]),
                        main_side_effects_on_sds=expected_non_hds_contents,
                    ))
示例#9
0
def instruction_syntax_for_src_file_rel_result() -> AbstractSyntax:
    src_file_arg = path_abs_stx.PathWConstNameAbsStx.of_rel_opt(
        RelOptionType.REL_RESULT, 'src-file.txt')
    dst_file_arg = path_abs_stx.PathWConstNameAbsStx.of_rel_opt(
        RelOptionType.REL_ACT, 'dst-file.txt')

    return instr_abs_stx.create_w_explicit_contents(
        dst_file_arg,
        string_source_abs_stx.StringSourceOfFileAbsStx(src_file_arg),
    )
示例#10
0
    def runTest(self):
        to_upper_transformer = NameAndValue(
            'TRANSFORMER_SYMBOL',
            StringTransformerSdvConstantTestImpl(to_uppercase()))

        src_file = fs.File('src-file.txt', 'contents of source file')
        src_file_symbol = NameAndValue('SRC_FILE_SYMBOL', src_file.name)

        expected_dst_file = fs.File('dst-file-name.txt',
                                    src_file.contents.upper())
        dst_file_symbol = NameAndValue('DST_FILE_SYMBOL',
                                       expected_dst_file.name)

        transformed_file_syntax = string_source_abs_stx.TransformedStringSourceAbsStx(
            string_source_abs_stx.StringSourceOfFileAbsStx(
                PathSymbolReferenceAbsStx(src_file_symbol.name)),
            str_trans_abs_stx.StringTransformerSymbolReferenceAbsStx(
                to_upper_transformer.name))
        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            PathSymbolReferenceAbsStx(dst_file_symbol.name),
            transformed_file_syntax,
        )

        # ACT #
        for layout_case in tokens_layout.STANDARD_LAYOUT_SPECS:
            with self.subTest(layout_case.name):
                instruction = self.conf.parse_checker.parse__abs_stx(
                    self, instruction_syntax, layout_case.value)
                assert isinstance(
                    instruction,
                    TestCaseInstructionWithSymbols)  # Sanity check

                # ASSERT #

                expected_symbol_usages = [
                    asrt_sym_ref.matches_reference_2(
                        dst_file_symbol.name,
                        asrt_w_str_rend_rest.equals__w_str_rendering(
                            path_or_string_reference_restrictions(
                                new_file.REL_OPT_ARG_CONF.options.
                                accepted_relativity_variants))),
                    asrt_sym_ref.matches_reference_2(
                        src_file_symbol.name,
                        asrt_w_str_rend_rest.equals__w_str_rendering(
                            path_or_string_reference_restrictions(
                                src_rel_opt_arg_conf_for_phase(
                                    self.conf.phase_is_after_act()).options.
                                accepted_relativity_variants))),
                    is_reference_to_string_transformer__usage(
                        to_upper_transformer.name),
                ]
                expected_symbol_references = asrt.matches_sequence(
                    expected_symbol_usages)
                expected_symbol_references.apply_without_message(
                    self, instruction.symbol_usages())
示例#11
0
    def runTest(self):
        text_printed_by_program = 'single line of output'

        expected_file_contents = text_printed_by_program.upper()
        expected_file = fs.File('dst-file.txt', expected_file_contents)

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'TO_UPPER_CASE',
            to_uppercase(),
        )
        symbols = to_upper_transformer.symbol_table

        dst_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_TMP)

        program_syntax = string_source_abs_stx.StringSourceOfProgramAbsStx(
            ProcOutputFile.STDOUT,
            program_abs_stx.FullProgramAbsStx(
                program_abs_stx.ProgramOfPythonInterpreterAbsStx.
                of_execute_python_src_string(
                    py_programs.single_line_pgm_that_prints_to(
                        ProcOutputFile.STDOUT, text_printed_by_program)),
                transformation=to_upper_transformer.abstract_syntax,
            ))
        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            dst_rel_opt_conf.path_abs_stx_of_name(expected_file.name),
            program_syntax,
        )

        instruction_checker = self.conf.instruction_checker
        parser = self.conf.parser()
        for source_case in equivalent_source_variants__with_source_check__consume_last_line__abs_stx(
                instruction_syntax):
            with self.subTest(source_case.name):
                source = source_case.value.source
                # ACT #
                instruction = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
                # ASSERT #
                source_case.value.expectation.apply_with_message(
                    self, source, 'source-after-parse')
                # ACT & ASSERT #
                instruction_checker.check(
                    self, instruction,
                    self.conf.arrangement(
                        pre_contents_population_action=
                        SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR,
                        symbols=symbols),
                    self.conf.expect_success(
                        symbol_usages=asrt.matches_sequence([
                            to_upper_transformer.reference_assertion,
                        ]),
                        main_side_effects_on_sds=non_hds_dir_contains_exactly(
                            dst_rel_opt_conf.root_dir__non_hds,
                            fs.DirContents([expected_file])),
                    ))
示例#12
0
    def test__with_transformer(self):
        # ARRANGE #
        src_file = fs.File('source-file.txt', 'contents of source file')
        expected_file = fs.File('a-file-name.txt', src_file.contents.upper())

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            string_transformers.to_uppercase(),
        )

        src_rel_opt_conf = ALLOWED_SRC_FILE_RELATIVITIES__BEFORE_ACT[0]
        dst_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_ACT)

        expected_non_hds_contents = self._expected_non_hds_contents(
            dst_rel_opt_conf,
            expected_file,
            src_rel_opt_conf,
            src_file,
        )

        transformed_file_contents_abs_stx = string_source_abs_stx.TransformedStringSourceAbsStx(
            string_source_abs_stx.StringSourceOfFileAbsStx(
                src_rel_opt_conf.path_abs_stx_of_name(src_file.name)),
            to_upper_transformer.abstract_syntax,
        )
        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            dst_rel_opt_conf.path_abs_stx_of_name(expected_file.file_name),
            transformed_file_contents_abs_stx,
        )

        symbols = to_upper_transformer.symbol_table

        for phase_is_after_act in [False, True]:
            checker = integration_check.checker(phase_is_after_act)
            with self.subTest(phase_is_after_act=phase_is_after_act):
                # ACT & ASSERT #
                checker.check__abs_stx__std_layouts_and_source_variants(
                    self, instruction_syntax,
                    Arrangement.phase_agnostic(
                        tcds=TcdsArrangement(
                            pre_population_action=
                            SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN,
                            tcds_contents=src_rel_opt_conf.
                            populator_for_relativity_option_root(
                                DirContents([src_file])),
                        ),
                        symbols=symbols,
                    ),
                    MultiSourceExpectation.phase_agnostic(
                        main_result=IS_SUCCESS,
                        main_side_effects_on_sds=expected_non_hds_contents,
                        symbol_usages=to_upper_transformer.usages_assertion,
                    ))
示例#13
0
    def runTest(self):
        # ARRANGE #
        text_printed_by_program = StringSymbolContext.of_constant('STRING_TO_PRINT_SYMBOL', 'hello world')

        dst_file_symbol = ConstantSuffixPathDdvSymbolContext(
            'DST_FILE_SYMBOL',
            RelOptionType.REL_ACT,
            'dst-file-name.txt',
            sut.REL_OPT_ARG_CONF.options.accepted_relativity_variants,
        )

        to_upper_transformer = TO_UPPER_TRANSFORMER_SYMBOL

        transformed_program_output_contents_syntax = string_source_abs_stx.StringSourceOfProgramAbsStx(
            ProcOutputFile.STDOUT,
            program_abs_stx.FullProgramAbsStx(
                program_abs_stx.ProgramOfPythonInterpreterAbsStx.of_execute_python_src_string(
                    py_programs.single_line_pgm_that_prints_to(
                        ProcOutputFile.STDOUT,
                        text_printed_by_program.name__sym_ref_syntax
                    )
                ),
                transformation=to_upper_transformer.abstract_syntax,
            )
        )
        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            dst_file_symbol.abstract_syntax,
            transformed_program_output_contents_syntax
        )
        symbols = SymbolContext.symbol_table_of_contexts([
            dst_file_symbol,
            text_printed_by_program,
            to_upper_transformer,
        ])

        # ACT & ASSERT #
        checker = integration_check.checker(False)
        checker.check__abs_stx__std_layouts_and_source_variants(
            self,
            instruction_syntax,
            Arrangement.phase_agnostic(
                symbols=symbols,
                tcds=TcdsArrangement(),
            ),
            MultiSourceExpectation.phase_agnostic(
                main_result=IS_SUCCESS,
                symbol_usages=asrt.matches_sequence([
                    dst_file_symbol.reference_assertion__path_or_string,
                    text_printed_by_program.reference_assertion__w_str_rendering,
                    to_upper_transformer.reference_assertion,
                ]),
            )
        )
示例#14
0
    def _check_of_invalid_src_file(
            self, is_after_act_2_every_src_file_rel_conf: Callable[
                [bool], Sequence[RelativityOptionConfiguration]],
            step_of_expected_failure: Step):
        # ARRANGE #
        transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            string_transformers.to_uppercase(),
        )
        symbols = transformer.symbol_table

        dst_file_rel_conf = conf_rel_any(RelOptionType.REL_TMP)

        expectation_ = self._expect_failure_in(step_of_expected_failure)

        for phase_is_after_act in [False, True]:
            checker = integration_check.checker(phase_is_after_act)
            for src_file_rel_conf in is_after_act_2_every_src_file_rel_conf(
                    phase_is_after_act):
                contents_builder = string_source_abs_stx.TransformableAbsStxBuilder(
                    string_source_abs_stx.StringSourceOfFileAbsStx(
                        src_file_rel_conf.path_abs_stx_of_name(
                            self.src_file_name)))
                for actual_src_file_variant in self.src_file_variants:
                    for contents_arguments in contents_builder.with_and_without_transformer_cases(
                            transformer.abstract_syntax):
                        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
                            dst_file_rel_conf.path_abs_stx_of_name(
                                'dst-file.txt'),
                            contents_arguments.value,
                        )
                        # ACT & ASSERT #
                        checker.check__abs_stx__std_layouts_and_source_variants(
                            self,
                            instruction_syntax,
                            Arrangement.phase_agnostic(
                                tcds=TcdsArrangement(
                                    pre_population_action=
                                    SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN,
                                    tcds_contents=src_file_rel_conf.
                                    populator_for_relativity_option_root(
                                        actual_src_file_variant.value),
                                ),
                                symbols=symbols,
                            ),
                            expectation_,
                            sub_test_identifiers={
                                'phase_is_after_act': phase_is_after_act,
                                'contents': contents_arguments.name,
                                'relativity_of_src_path':
                                src_file_rel_conf.name,
                            })
示例#15
0
    def _test_symbol_reference_in_dst_file_and_contents(
            self, symbol_ref_syntax_2_contents_arguments: Callable[
                [str], StringSourceAbsStx],
            symbol_value_2_expected_contents: Callable[[str], str]):
        sub_dir_symbol = ConstantSuffixPathDdvSymbolContext(
            'sub_dir_symbol',
            RelOptionType.REL_ACT,
            'sub-dir',
            ACCEPTED_DST_RELATIVITY_VARIANTS,
        )
        contents_symbol = StringConstantSymbolContext(
            'contents_symbol_name',
            'contents symbol value',
            default_restrictions=asrt_rest.is__w_str_rendering(),
        )

        expected_file_contents = symbol_value_2_expected_contents(
            contents_symbol.str_value)

        expected_file = fs.File('a-file-name.txt', expected_file_contents)

        symbols = [sub_dir_symbol, contents_symbol]
        expected_symbol_references = SymbolContext.references_assertion_of_contexts(
            symbols)
        symbol_table = SymbolContext.symbol_table_of_contexts(symbols)

        contents_arguments = symbol_ref_syntax_2_contents_arguments(
            symbol_reference_syntax_for_name(contents_symbol.name))

        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            path_abs_stx.PathStringAbsStx.of_plain_components(
                [sub_dir_symbol.name__sym_ref_syntax, expected_file.name]),
            contents_arguments,
        )

        integration_check.CHECKER__AFTER_ACT.check__abs_stx(
            self, instruction_syntax,
            Arrangement.phase_agnostic(
                tcds=TcdsArrangement(
                    pre_population_action=
                    SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, ),
                symbols=symbol_table,
            ),
            Expectation.phase_agnostic(
                main_result=IS_SUCCESS,
                symbol_usages=expected_symbol_references,
                main_side_effects_on_sds=dir_contains_exactly(
                    sub_dir_symbol.rel_option_type,
                    fs.DirContents(
                        [fs.Dir(sub_dir_symbol.path_suffix,
                                [expected_file])])),
            ))
示例#16
0
 def test_fail_when_superfluous_arguments(self):
     # ARRANGE #
     valid_instruction_syntax = instr_abs_stx.create_w_explicit_contents(
         path_abs_stx.DefaultRelPathAbsStx('file-name'),
         string_source_abs_stx.StringSourceOfStringAbsStx.of_str('string'),
     )
     invalid_instruction_syntax = custom_abs_stx.SequenceAbsStx([
         valid_instruction_syntax,
         custom_abs_stx.CustomAbstractSyntax.singleton(
             'superfluous_argument')
     ])
     # ACT & ASSERT #
     check_invalid_syntax__abs_stx(self, invalid_instruction_syntax)
示例#17
0
    def test_all_relativities__without_transformer(self):
        # ARRANGE #

        src_file = fs.File('source-file.txt', 'contents of source file')
        expected_file = fs.File('a-file-name.txt', src_file.contents)

        for phase_is_after_act in [False, True]:
            checker = integration_check.checker(phase_is_after_act)
            for dst_rel_opt_conf in ALLOWED_DST_FILE_RELATIVITIES:
                for src_rel_opt_conf in accepted_src_file_relativities(
                        phase_is_after_act):
                    file_contents_abs_stx = string_source_abs_stx.StringSourceOfFileAbsStx(
                        src_rel_opt_conf.path_abs_stx_of_name(src_file.name))
                    instruction_syntax = instr_abs_stx.create_w_explicit_contents(
                        dst_rel_opt_conf.path_abs_stx_of_name(
                            expected_file.file_name),
                        file_contents_abs_stx,
                    )
                    expected_non_hds_contents = self._expected_non_hds_contents(
                        dst_rel_opt_conf, expected_file, src_rel_opt_conf,
                        src_file)

                    with self.subTest(phase_is_after_act=phase_is_after_act,
                                      relativity_dst=dst_rel_opt_conf.
                                      option_argument.as_str,
                                      relativity_src=src_rel_opt_conf.
                                      option_argument.as_str):
                        # ACT & ASSERT #
                        checker.check__abs_stx__std_layouts_and_source_variants(
                            self, instruction_syntax,
                            Arrangement.phase_agnostic(tcds=TcdsArrangement(
                                pre_population_action=
                                SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN,
                                tcds_contents=src_rel_opt_conf.
                                populator_for_relativity_option_root(
                                    DirContents([src_file]))), ),
                            MultiSourceExpectation.phase_agnostic(
                                main_result=IS_SUCCESS,
                                symbol_usages=asrt.is_empty_sequence,
                                main_side_effects_on_sds=
                                expected_non_hds_contents,
                            ))
示例#18
0
 def test_string_transformer_should_be_parsed_as_simple_expression(self):
     # ARRANGE #
     transformation_w_infix_op = str_trans_abs_stx.StringTransformerCompositionAbsStx(
         [
             StringTransformerSymbolReferenceAbsStx('str_trans_1'),
             StringTransformerSymbolReferenceAbsStx('str_trans_2'),
         ],
         within_parens=False,
         allow_elements_on_separate_lines=False,
     )
     src_rel_opt_conf = conf_rel_sds(RelSdsOptionType.REL_ACT)
     file_contents_arg = string_source_abs_stx.TransformedStringSourceAbsStx(
         string_source_abs_stx.StringSourceOfFileAbsStx(
             src_rel_opt_conf.path_abs_stx_of_name('src-file')),
         transformation_w_infix_op)
     instruction_syntax = instr_abs_stx.create_w_explicit_contents(
         path_abs_stx.DefaultRelPathAbsStx('dst-file'),
         file_contents_arg,
     )
     # ACT & ASSERT #
     check_invalid_syntax__abs_stx(self, instruction_syntax)
示例#19
0
 def runTest(self):
     # ARRANGE #
     cases = [
         NArrEx(
             'pre SDS validation failure SHOULD cause validation error',
             RelOptionType.REL_HDS_CASE,
             MultiSourceExpectation.phase_agnostic(
                 validation=ValidationAssertions.pre_sds_fails__w_any_msg()
             ),
         ),
         NArrEx(
             'post SDS validation failure SHOULD cause main error',
             RelOptionType.REL_ACT,
             MultiSourceExpectation.phase_agnostic(
                 validation=ValidationAssertions.post_sds_fails__w_any_msg()
             ),
         ),
     ]
     for case in cases:
         program_with_ref_to_non_existing_file = program_abs_stx.ProgramOfExecutableFileCommandLineAbsStx(
             path_abs_stx.RelOptPathAbsStx(case.arrangement, 'non-existing-file')
         )
         instruction_syntax = instr_abs_stx.create_w_explicit_contents(
             path_abs_stx.DefaultRelPathAbsStx('dst-file'),
             string_source_abs_stx.StringSourceOfProgramAbsStx(ProcOutputFile.STDOUT,
                                                               program_with_ref_to_non_existing_file,
                                                               ignore_exit_code=False)
         )
         # ACT & ASSERT #
         for phase_is_after_act in [False, True]:
             checker = integration_check.checker(phase_is_after_act)
             with self.subTest(phase_is_after_act=phase_is_after_act,
                               step=case.name):
                 checker.check__abs_stx__std_layouts_and_source_variants(
                     self,
                     instruction_syntax,
                     Arrangement.phase_agnostic(),
                     case.expectation,
                 )
示例#20
0
    def runTest(self):
        # ARRANGE #
        dst_file = PathArgumentWithRelativity(
            'dst-file.txt', conf_rel_any(RelOptionType.REL_TMP))

        cases = [
            NArrEx(
                'pre sds validation failure SHOULD cause validation error',
                RelOptionType.REL_HDS_CASE,
                self.conf.expect_failing_validation_pre_sds(),
            ),
            NArrEx(
                'post sds validation failure SHOULD cause main hard error',
                RelOptionType.REL_ACT,
                self.conf.expect_hard_error_of_main__any(),
            ),
        ]
        for case in cases:
            src_file_rel_conf = conf_rel_any(case.arrangement)
            instruction_syntax = instr_abs_stx.create_w_explicit_contents(
                dst_file.argument_abs_stx,
                string_source_abs_stx.StringSourceOfFileAbsStx(
                    src_file_rel_conf.path_abs_stx_of_name(
                        'non-existing-source-file.txt')),
            )

            for source_case in equivalent_source_variants__with_source_check__consume_last_line__abs_stx(
                    instruction_syntax):
                with self.subTest(validation_case=case.name,
                                  source_case=source_case.name):
                    # ACT & ASSERT#
                    self.conf.run_test(
                        self,
                        source_case.value.source,
                        arrangement=self.conf.arrangement(),
                        expectation=case.expectation,
                    )
示例#21
0
    def _test(self,
              text_printed_by_program: str,
              expected_file_contents: str,
              make_arguments: Callable[[TransformableProgramAbsStxBuilder], ProgramAbsStx],
              additional_symbols: Dict[str, SymbolContainer],
              additional_symbol_references: List[Assertion[SymbolReference]],
              ):
        expected_file = fs.File('a-file-name.txt', expected_file_contents)

        for proc_output_file in ProcOutputFile:
            python_source = py_programs.single_line_pgm_that_prints_to(proc_output_file,
                                                                       text_printed_by_program)

            program_that_executes_py_source_symbol = ProgramSymbolContext.of_sdv(
                'PROGRAM_THAT_EXECUTES_PY_SOURCE',
                program_sdvs.for_py_source_on_command_line(python_source)
            )

            program_cases = [
                ProgramCase(
                    'python interpreter',
                    program_abs_stx.ProgramOfPythonInterpreterAbsStx.of_execute_python_src_string(python_source),
                    []
                ),
                ProgramCase(
                    'symbol reference program',
                    ProgramOfSymbolReferenceAbsStx(program_that_executes_py_source_symbol.name),
                    [program_that_executes_py_source_symbol.reference_assertion],
                ),
            ]

            symbols_dict = {
                program_that_executes_py_source_symbol.name:
                    program_that_executes_py_source_symbol.symbol_table_container,
            }
            symbols_dict.update(additional_symbols)
            symbols = SymbolTable(symbols_dict)

            for program_case in program_cases:
                program_syntax_builder = TransformableProgramAbsStxBuilder(program_case.source)
                program_syntax = make_arguments(program_syntax_builder)

                rel_opt_conf = ARBITRARY_ALLOWED_DST_FILE_RELATIVITY

                expected_symbol_references = program_case.expected_references + additional_symbol_references

                instruction_syntax = instr_abs_stx.create_w_explicit_contents(
                    rel_opt_conf.path_abs_stx_of_name(expected_file.name),
                    string_source_abs_stx.StringSourceOfProgramAbsStx(proc_output_file, program_syntax,
                                                                      ignore_exit_code=False)
                )

                with self.subTest(relativity_option_string=str(rel_opt_conf.option_argument),
                                  program=program_case.name,
                                  output_channel=proc_output_file):
                    integration_check.CHECKER__BEFORE_ACT.check__abs_stx__layout_and_source_variants(
                        self,
                        instruction_syntax,
                        Arrangement.phase_agnostic(
                            symbols=symbols,
                            tcds=TcdsArrangement(),
                        ),
                        MultiSourceExpectation.phase_agnostic(
                            main_result=IS_SUCCESS,
                            side_effects_on_hds=f_asrt.dir_is_empty(),
                            symbol_usages=asrt.matches_sequence(expected_symbol_references),
                            main_side_effects_on_sds=non_hds_dir_contains_exactly(rel_opt_conf.root_dir__non_hds,
                                                                                  fs.DirContents([expected_file])),
                        )
                    )
示例#22
0
    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,
                            },
                        )
示例#23
0
    def runTest(self):
        non_zero_exit_code = 1
        text_printed_by_program = 'the output from the program'

        py_file = File(
            'exit-with-hard-coded-exit-code.py',
            py_programs.py_pgm_with_stdout_stderr_exit_code(
                stdout_output=text_printed_by_program,
                stderr_output='',
                exit_code=non_zero_exit_code,
            ),
        )

        expected_file = fs.File(
            'dst-file.txt',
            text_printed_by_program.upper(),
        )

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'TO_UPPER_CASE',
            to_uppercase(),
        )
        symbols = to_upper_transformer.symbol_table

        py_src_file_rel_opt_conf = rel_opt.conf_rel_any(
            RelOptionType.REL_HDS_CASE)
        dst_file_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_TMP)

        program_string_source_syntax = string_source_abs_stx.StringSourceOfProgramAbsStx(
            ProcOutputFile.STDOUT,
            program_abs_stx.FullProgramAbsStx(
                program_abs_stx.ProgramOfPythonInterpreterAbsStx.
                of_execute_python_src_file(
                    py_src_file_rel_opt_conf.path_abs_stx_of_name(
                        py_file.name)),
                transformation=to_upper_transformer.abstract_syntax,
            ),
            ignore_exit_code=True,
        )
        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            dst_file_rel_opt_conf.path_abs_stx_of_name(expected_file.name),
            program_string_source_syntax,
        )

        instruction_checker = self.conf.instruction_checker
        parser = self.conf.parser()
        for source_case in equivalent_source_variants__with_source_check__consume_last_line__abs_stx(
                instruction_syntax):
            with self.subTest(source_case.name):
                source = source_case.value.source
                # ACT #
                instruction = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
                # ASSERT #
                source_case.value.expectation.apply_with_message(
                    self, source, 'source-after-parse')
                # ACT & ASSERT #
                instruction_checker.check(
                    self, instruction,
                    self.conf.arrangement(
                        pre_contents_population_action=
                        SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR,
                        symbols=symbols,
                        tcds_contents=py_src_file_rel_opt_conf.
                        populator_for_relativity_option_root(
                            DirContents([py_file]))),
                    self.conf.expect_success(
                        symbol_usages=asrt.matches_sequence([
                            to_upper_transformer.reference_assertion,
                        ]),
                        main_side_effects_on_sds=non_hds_dir_contains_exactly(
                            dst_file_rel_opt_conf.root_dir__non_hds,
                            fs.DirContents([expected_file])),
                    ))
示例#24
0
    def test_symbol_usages(self):
        # ARRANGE #

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            string_transformers.to_uppercase(),
        )

        src_file = fs.File('src-file.txt', 'contents of source file')
        src_file_rel_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE)

        expected_dst_file = fs.File('dst-file-name.txt',
                                    src_file.contents.upper())

        dst_file_symbol = ConstantSuffixPathDdvSymbolContext(
            'DST_FILE_SYMBOL',
            RelOptionType.REL_TMP,
            expected_dst_file.name,
            sut.REL_OPT_ARG_CONF.options.accepted_relativity_variants,
        )

        for phase_is_after_act in [False, True]:
            checker = integration_check.checker(phase_is_after_act)
            src_file_rel_opt_conf = src_rel_opt_arg_conf_for_phase(
                phase_is_after_act)

            src_file_symbol = ConstantSuffixPathDdvSymbolContext(
                'SRC_FILE_SYMBOL',
                src_file_rel_conf.relativity_option,
                src_file.name,
                src_file_rel_opt_conf.options.accepted_relativity_variants,
            )
            transformed_file_contents = string_source_abs_stx.TransformedStringSourceAbsStx(
                string_source_abs_stx.StringSourceOfFileAbsStx(
                    src_file_symbol.abstract_syntax),
                StringTransformerSymbolReferenceAbsStx(
                    to_upper_transformer.name))
            instruction_syntax = instr_abs_stx.create_w_explicit_contents(
                dst_file_symbol.abstract_syntax,
                transformed_file_contents,
            )
            symbols = SymbolContext.symbol_table_of_contexts([
                dst_file_symbol,
                src_file_symbol,
                to_upper_transformer,
            ])

            with self.subTest(phase_is_after_act=phase_is_after_act):
                # ACT & ASSERT #
                checker.check__abs_stx__std_layouts_and_source_variants(
                    self,
                    instruction_syntax,
                    Arrangement.phase_agnostic(
                        symbols=symbols,
                        tcds=TcdsArrangement(
                            hds_contents=src_file_rel_conf.
                            populator_for_relativity_option_root__hds(
                                DirContents([src_file]))),
                    ),
                    MultiSourceExpectation.phase_agnostic(
                        main_result=IS_SUCCESS,
                        symbol_usages=asrt.matches_sequence([
                            dst_file_symbol.
                            reference_assertion__path_or_string,
                            src_file_symbol.
                            reference_assertion__path_or_string,
                            is_reference_to_string_transformer__usage(
                                to_upper_transformer.name),
                        ]),
                    ),
                )