def test_transformer_component(self): checker = integration_check.checker__w_arbitrary_file_relativities() for validation_case in failing_validation_cases(): transformer_symbol = validation_case.value.symbol_context program_syntax = program_abs_stx.FullProgramAbsStx( program_abs_stx.ProgramOfSystemCommandLineAbsStx.of_str( 'a-system-command'), transformation=transformer_symbol.abstract_syntax, ) string_source_syntax = string_source_abs_stx.StringSourceOfProgramAbsStx( ProcOutputFile.STDOUT, program_syntax, ignore_exit_code=False, ) with self.subTest(validation_case.name): checker.check__abs_stx__layouts__source_variants__wo_input( self, equivalent_source_variants__for_expr_parse__s__nsc, OptionallyOnNewLine(string_source_syntax), arrangement_wo_tcds( symbols=transformer_symbol.symbol_table), MultiSourceExpectation( symbol_references=transformer_symbol. references_assertion, execution=ExecutionExpectation( validation=validation_case.value.expectation)))
def runTest(self): # ARRANGE # text_printed_by_program = StringConstantSymbolContext( 'STRING_TO_PRINT_SYMBOL', 'hello world') to_upper_transformer = TO_UPPER_TRANSFORMER_SYMBOL expected_output = text_printed_by_program.str_value.upper() 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, )) symbols = SymbolContext.symbol_table_of_contexts([ text_printed_by_program, to_upper_transformer, ]) checker = integration_check.checker__w_arbitrary_file_relativities() # ACT & ASSERT # checker.check__abs_stx__layouts__std_source_variants__wo_input( self, OptionallyOnNewLine(transformed_program_output_contents_syntax), arrangement_w_tcds(symbols=symbols, ), MultiSourceExpectation. of_prim__const(symbol_references=asrt.matches_sequence([ text_printed_by_program.reference_assertion__w_str_rendering, to_upper_transformer.reference_assertion, ]), primitive=asrt_string_source. pre_post_freeze__matches_str__const_2( expected_output, may_depend_on_external_resources=True, frozen_may_depend_on_external_resources=asrt. anything_goes(), )))
def test_WHEN_program_is_non_non_existing_system_command_THEN_result_SHOULD_be_hard_error(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 ignore_exit_code in [False, True]: for transformation_case in cases: syntax = string_source_abs_stx.StringSourceOfProgramAbsStx( ProcOutputFile.STDOUT, transformation_case.value, ignore_exit_code=ignore_exit_code, ) checker = integration_check.checker__w_arbitrary_file_relativities() with self.subTest(transformation=transformation_case.name, ignore_exit_code=ignore_exit_code): # ACT & ASSERT # 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=symbols, ), MultiSourceExpectation.of_const( symbol_references=asrt.anything_goes(), primitive=asrt_string_source.pre_post_freeze( asrt_str_src_contents.contents_raises_hard_error( may_depend_on_external_resources=asrt.equals(True) ), asrt_str_src_contents.contents_raises_hard_error__including_ext_deps(), ), ) )
# ACT & ASSERT # CHECKER.check__abs_stx__wo_input( self, StringSourceOfProgramAbsStx( output_file, FullProgramAbsStx( test_setup.program_that_copies_stdin_syntax(), stdin=stdin_string_source_syntax), ignore_exit_code, ), arrangement_w_tcds( symbols=SymbolContext.symbol_table_of_contexts( test_setup.symbols), ), Expectation.of_prim__const( parse=ParseExpectation( symbol_references=SymbolContext. references_assertion_of_contexts( test_setup.symbols)), primitive=asrt_string_source. pre_post_freeze__identical( asrt_contents.matches__str( asrt.equals(model_contents))), ), ) IS_EMPTY_STRING_SOURCE = asrt_string_source.pre_post_freeze__identical( asrt_contents.matches__str(asrt.equals(''))) CHECKER = integration_check.checker__w_arbitrary_file_relativities()
def test_non_transformer_components(self): # ARRANGE # relativity_cases = [ NArrEx( 'pre SDS validation failure', RelOptionType.REL_HDS_CASE, ValidationAssertions.pre_sds_fails__w_any_msg(), ), NArrEx( 'post SDS validation failure', RelOptionType.REL_ACT, ValidationAssertions.post_sds_fails__w_any_msg(), ), ] def make_pgm_w_ref_to_executable_file( relativity: RelOptionType) -> ProgramAbsStx: return program_abs_stx.ProgramOfExecutableFileCommandLineAbsStx( path_abs_stx.RelOptPathAbsStx(relativity, 'non-existing-file')) def make_pgm_w_ref_to_stdin_file( relativity: RelOptionType) -> ProgramAbsStx: return program_abs_stx.FullProgramAbsStx( program_abs_stx.ProgramOfSystemCommandLineAbsStx.of_str( 'a-system-command'), stdin=string_source_abs_stx.StringSourceOfFileAbsStx( path_abs_stx.RelOptPathAbsStx(relativity, 'non-existing-file'))) def make_pgm_w_ref_to_argument_file( relativity: RelOptionType) -> ProgramAbsStx: return program_abs_stx.ProgramOfSystemCommandLineAbsStx.of_str( 'a-system-command', [ ArgumentOfExistingPathAbsStx( path_abs_stx.RelOptPathAbsStx(relativity, 'non-existing-file')) ]) program_cases = [ NameAndValue( 'missing executable file', make_pgm_w_ref_to_executable_file, ), NameAndValue( 'missing argument file', make_pgm_w_ref_to_argument_file, ), NameAndValue( 'missing stdin file', make_pgm_w_ref_to_stdin_file, ), ] for relativity_case in relativity_cases: for program_case in program_cases: string_source_syntax = string_source_abs_stx.StringSourceOfProgramAbsStx( ProcOutputFile.STDOUT, program_case.value(relativity_case.arrangement), ignore_exit_code=False, ) # ACT & ASSERT # checker = integration_check.checker__w_arbitrary_file_relativities( ) with self.subTest(step=relativity_case.name, 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(string_source_syntax), arrangement_w_tcds(), MultiSourceExpectation(execution=ExecutionExpectation( validation=relativity_case.expectation)))
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 _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) ), ), ),
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(), ), ), ),
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]], ): 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) checker = integration_check.checker__w_arbitrary_file_relativities() for program_case in program_cases: program_syntax_builder = TransformableProgramAbsStxBuilder(program_case.source) program_syntax = make_arguments(program_syntax_builder) expected_symbol_references = program_case.expected_references + additional_symbol_references syntax = string_source_abs_stx.StringSourceOfProgramAbsStx( proc_output_file, program_syntax, ignore_exit_code=False, ) with self.subTest(program=program_case.name, output_channel=proc_output_file): 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=symbols, ), MultiSourceExpectation.of_prim__const( symbol_references=asrt.matches_sequence(expected_symbol_references), primitive=asrt_string_source.pre_post_freeze__matches_str__const_2( expected_file_contents, may_depend_on_external_resources=True, frozen_may_depend_on_external_resources=asrt.anything_goes(), ), ) )