def check_both_single_and_multiple_line_source( self, put: unittest.TestCase, first_source_line_instruction_argument_source_template: str, act_phase_source_lines: List[str], symbol_usages: Assertion[Sequence[SymbolUsage]], expected_command: Callable[[TestCaseDs], Assertion[Command]], hds_contents: hds_populators.HdsPopulator = hds_populators.empty(), ): instruction_argument_source = first_source_line_instruction_argument_source_template.format_map( self.format_map_for_template_string) for source, source_assertion in equivalent_source_variants_with_assertion(put, instruction_argument_source): # ARRANGE # command_executor = CommandExecutorThatRecordsArguments() arrangement = Arrangement( source, act_phase_source_lines, os_services=os_services_access.new_for_cmd_exe(command_executor), hds_contents=hds_contents, ) expectation = Expectation( source_after_parse=source_assertion, symbol_usages=symbol_usages, after_execution=ExecutedCommandAssertion(command_executor, expected_command) ) # ACT & ASSERT # check_actor_execution(put, arrangement, expectation)
def runTest(self): # ARRANGE # expected_file_name = 'quoted file name.src' act_phase_instructions = [instr([str(surrounded_by_hard_quotes(expected_file_name))]), instr([''])] executor_that_records_arguments = CommandExecutorThatRecordsArguments() arrangement = integration_check.arrangement_w_tcds( hds_contents=contents_in(RelHdsOptionType.REL_HDS_ACT, DirContents([ File.empty(expected_file_name)])), process_execution=ProcessExecutionArrangement( os_services=os_services_access.new_for_cmd_exe(executor_that_records_arguments) ) ) expectation = integration_check.Expectation() # ACT # integration_check.check_execution(self, ACTOR_THAT_RUNS_PYTHON_PROGRAM_FILE, act_phase_instructions, arrangement, expectation) # ASSERT # expected_command = asrt_command.matches_command( driver=asrt_command.matches_executable_file_command_driver(asrt.anything_goes()), arguments=asrt.matches_sequence([ asrt_path.str_as_path(asrt_path.name_equals(expected_file_name)) ]) ) expected_command.apply_with_message( self, executor_that_records_arguments.command, 'command', )
def os_services_w_cmd_exe_counting__w_wrapped(counter: recording.Counter, wrapped: CommandExecutor) -> OsServices: return os_services_access.new_for_cmd_exe( CommandExecutorWInitialAction( wrapped, initial_action=counter.increase, ) )
def _run(self, suite_file_name: str, run_as_part_of_explicit_suite: Optional[Path]): # ARRANGE # default_test_case_handling = setup_with_null_act_phase_and_null_preprocessing( ) conf_parser_with_no_instructions = configuration_section_parser({}) test_case_definition = test_case_definition_with_only_assert_phase_instructions( []) case_file = File.empty('test.case') suite_file = File( suite_file_name, lines_content([ phase_names.ASSERT.syntax, ' '.join([ INCLUDING_DIRECTIVE_INFO.singular_name, 'non-existing-file.txt' ]), ])) suite_and_case_files = DirContents([ suite_file, case_file, ]) processor = sut.Processor( test_case_definition, os_services_access.new_for_cmd_exe( CommandExecutorThatJustReturnsConstant()), conf_parser_with_no_instructions, 2**10) with tmp_dir_as_cwd(suite_and_case_files) as tmp_dir: execution_settings = TestCaseExecutionSettings( case_file.name_as_path, tmp_dir, ReportingOption.STATUS_CODE, default_test_case_handling, run_as_part_of_explicit_suite=run_as_part_of_explicit_suite) # ACT # actual_result = capture_output_from_processor( processor, execution_settings) # ASSERT # expectation = is_result_for_exit_value( exit_values.NO_EXECUTION__FILE_ACCESS_ERROR) expectation.apply_without_message(self, actual_result)
def app_env_for_freeze( self, put: unittest.TestCase, message_builder: MessageBuilder, ) -> ContextManager[ApplicationEnvironment]: default_os_services = os_services_access.new_for_current_os() os_services_w_check = os_services_access.new_for_cmd_exe( CommandExecutorWithMaxNumInvocations( put, 1, default_os_services.command_executor, message_builder.apply(''), )) return application_environment.application_environment_with_existing_dir( os_services_w_check)
def runTest(self): # ARRANGE # program_symbol = ProgramSymbolContext.of_arbitrary_value( 'PROGRAM_SYMBOL') executor_that_do_not_run_any_program = CommandExecutorThatJustReturnsConstant( 0) for may_depend_on_external_resources in [False, True]: for with_ignored_exit_code in [False, True]: with self.subTest( with_ignored_exit_code=with_ignored_exit_code, may_depend_on_external_resources= may_depend_on_external_resources): # ACT && ASSERT # pass integration_check.CHECKER__PARSE_SIMPLE.check( self, args.syntax_for_run( program_args.symbol_ref_command_elements( program_symbol.name), ignore_exit_code=with_ignored_exit_code, ).as_remaining_source, model_constructor.empty(self, may_depend_on_external_resources= may_depend_on_external_resources), arrangement_w_tcds( symbols=program_symbol.symbol_table, process_execution=ProcessExecutionArrangement( os_services=os_services_access.new_for_cmd_exe( executor_that_do_not_run_any_program)), ), Expectation( ParseExpectation(symbol_references=program_symbol. references_assertion, ), ExecutionExpectation( main_result=asrt_string_source. pre_post_freeze__matches_lines( asrt.anything_goes(), 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)), ), )
def run(self, parsing_setup: TestCaseParsingSetup, test_case_handling_setup: TestCaseHandlingSetup, test_suite_definition: TestSuiteDefinition, case_file: Path, explicit_suite_file_path: Optional[Path]) -> SubProcessResult: processor = sut.Processor( TestCaseDefinition(parsing_setup, _predefined_properties.new_empty()), os_services_access.new_for_cmd_exe( CommandExecutorThatJustReturnsConstant()), test_suite_definition.configuration_section_parser, 2**10) execution_settings = TestCaseExecutionSettings( case_file, case_file.parent, ReportingOption.STATUS_CODE, test_case_handling_setup, run_as_part_of_explicit_suite=explicit_suite_file_path) return capture_output_from_processor(processor, execution_settings)
def runTest(self): CHECKER.check__w_source_variants( self, command_line( 'program', ).as_str, Arrangement.phase_agnostic( os_services=os_services_access.new_for_cmd_exe( CommandExecutorThatRaisesHardError( asrt_text_doc.new_single_string_text_for_test('the error message') ), ), tcds=TcdsArrangement(), ), MultiSourceExpectation.phase_agnostic( main_raises_hard_error=True ), )
def runTest(self): executor = CommandExecutorThatJustReturnsConstant( 1, string_to_write_to_stderr='output on stderr' ) CHECKER.check__w_source_variants( self, command_line( 'program', ).as_str, Arrangement.phase_agnostic( os_services=os_services_access.new_for_cmd_exe(executor), tcds=TcdsArrangement(), ), MultiSourceExpectation.phase_agnostic( main_result=result_assertions.equals(executor.constant_return_value, executor.string_to_write_to_stderr), ), )
def runTest(self): # ARRANGE # atc_file_name = 'existing-file.py' arg_1 = 'un-quoted' arg_2 = 'single quoted' arg_3 = 'double quoted' act_line_1 = '{} {} {} {}'.format( atc_file_name, arg_1, surrounded_by_hard_quotes(arg_2), surrounded_by_soft_quotes(arg_3), ) atc_line_2 = '' act_phase_instructions = [instr([act_line_1]), instr([atc_line_2])] executor_that_records_arguments = CommandExecutorThatRecordsArguments() arrangement = integration_check.arrangement_w_tcds( hds_contents=contents_in(RelHdsOptionType.REL_HDS_ACT, DirContents([ File.empty(atc_file_name)])), process_execution=ProcessExecutionArrangement( os_services=os_services_access.new_for_cmd_exe(executor_that_records_arguments) ) ) expected_command = asrt_command.matches_command( driver=asrt_command.matches_executable_file_command_driver(asrt.anything_goes()), arguments=asrt.matches_sequence([ asrt_path.str_as_path(asrt_path.name_equals(atc_file_name)), asrt.equals(arg_1), asrt.equals(arg_2), asrt.equals(arg_3), ]) ) expectation = integration_check.Expectation( after_execution=ExecutedCommandAssertion(executor_that_records_arguments, lambda tcds: expected_command) ) # ACT & ASSERT # integration_check.check_execution(self, ACTOR_THAT_RUNS_PYTHON_PROGRAM_FILE, act_phase_instructions, arrangement, expectation)
def runTest(self): # ARRANGE # executable_file = sys.executable command_executor = CommandExecutorThatRecordsArguments() arrangement = Arrangement( source=remaining_source('= ' + actor_utils.COMMAND_LINE_ACTOR_NAME), act_phase_source_lines=[executable_file], os_services=os_services_access.new_for_cmd_exe(command_executor), ) expected_command = asrt_command.matches_command( driver=asrt_command.matches_executable_file_command_driver( asrt_path.path_as_str(asrt.equals(executable_file)), ), arguments=asrt.is_empty_sequence) expectation = Expectation(symbol_usages=asrt.is_empty_sequence, after_execution=ExecutedCommandAssertion( command_executor, lambda tcds: expected_command)) # ACT & ASSERT # check_actor_execution(self, arrangement, expectation)
def runTest(self): hard_error_message = 'the err msg' with self.config.program_that_exits_with_code(0) as setup: self._check( setup.act_phase_instructions, arrangement_w_tcds( symbol_table=setup.symbols, hds_contents=_hds_pop_of(setup), process_execution=ProcessExecutionArrangement( os_services=os_services_access.new_for_cmd_exe( CommandExecutorThatRaisesHardError( asrt_text_doc.new_single_string_text_for_test( hard_error_message))), process_execution_settings=proc_exe_env_for_test( environ=setup.environ), )), expectation=Expectation.hard_error_from_execute( symbol_usages=setup.symbol_usages, error_message=asrt_text_doc. is_single_pre_formatted_text_that_equals( hard_error_message), ))
def runTest(self): # ARRANGE # program_symbol = ProgramSymbolContext.of_arbitrary_value( 'PROGRAM_SYMBOL') const_true_int_matcher_symbol = IntegerMatcherSymbolContext.of_primitive_constant( 'CONST_TRUE_INT_MATCHER', result=True, ) string_transformer_that_reports_failure_if_applied = StringTransformerSymbolContext.of_primitive( 'STRING_TRANSFORMER_THAT_MUST_NOT_BE_USED', StringTransformerThatFailsTestIfApplied(self)) all_symbols = [ program_symbol, string_transformer_that_reports_failure_if_applied, const_true_int_matcher_symbol, ] # ACT & ASSERT # CHECKER.check__abs_stx( self, InstructionArguments( FullProgramAbsStx( program_symbol.abstract_syntax, transformation= string_transformer_that_reports_failure_if_applied. abstract_syntax), const_true_int_matcher_symbol.abstract_syntax, ), ArrangementPostAct2( symbols=SymbolContext.symbol_table_of_contexts(all_symbols), process_execution=ProcessExecutionArrangement( os_services_access.new_for_cmd_exe( CommandExecutorThatJustReturnsConstant(1), ), ), ), Expectation2( ParseExpectation( symbol_usages=SymbolContext.usages_assertion_of_contexts( all_symbols), ), ExecutionExpectation(main_result=asrt_pfh.is_pass()), ))
def runTest(self): # ARRANGE # program_symbol = ProgramSymbolContext.of_arbitrary_value('PROGRAM_SYMBOL') const_true_int_matcher_symbol = IntegerMatcherSymbolContext.of_primitive_constant( 'CONST_TRUE_INT_MATCHER', result=True, ) all_symbols = [program_symbol, const_true_int_matcher_symbol, ] hard_error_message = 'the err msg' # ACT & ASSERT # CHECKER.check__abs_stx( self, InstructionArguments( program_symbol.abstract_syntax, const_true_int_matcher_symbol.abstract_syntax, ), ArrangementPostAct2( symbols=SymbolContext.symbol_table_of_contexts(all_symbols), process_execution=ProcessExecutionArrangement( os_services_access.new_for_cmd_exe( CommandExecutorThatRaisesHardError( asrt_text_doc.new_single_string_text_for_test(hard_error_message) ), ), ), ), Expectation2( ParseExpectation( symbol_usages=SymbolContext.usages_assertion_of_contexts(all_symbols), ), ExecutionExpectation( main_result=asrt_pfh.is_hard_error( asrt_text_doc.is_string_for_test_that_equals(hard_error_message) ) ), ) )
def test_successful_setting(self): # ARRANGE # command_executor = CommandExecutorThatRecordsArguments() arrangement = Arrangement( source=remaining_source('= ' + actor_utils.NULL_ACTOR_NAME), act_phase_source_lines=[ 'should', 'be', 'ignored', symbol_syntax.symbol_reference_syntax_for_name('symbol'), ], os_services=os_services_access.new_for_cmd_exe(command_executor), ) expectation = Expectation( symbol_usages=asrt.is_empty_sequence, sub_process_result_from_execute=asrt_proc_result. matches_proc_result( exit_code=asrt.equals(0), stdout=asrt.equals(''), stderr=asrt.equals(''), )) # ACT & ASSERT # check_actor_execution(self, arrangement, expectation)
def check_successful_execution(put: unittest.TestCase, arguments: ArgumentElementsRenderer, symbols: SymbolTable, symbol_usages: Assertion[Sequence[SymbolUsage]], expected_command: Assertion[Command], ): executor_that_records_arguments = CommandExecutorThatRecordsArguments() CHECKER.check__w_source_variants( put, arguments.as_str, Arrangement.phase_agnostic( os_services=os_services_access.new_for_cmd_exe(executor_that_records_arguments), symbols=symbols, tcds=TcdsArrangement(), ), MultiSourceExpectation.phase_agnostic( symbol_usages=symbol_usages, side_effects_on_tcds=ExecutedCommandAssertion( executor_that_records_arguments, lambda tcds: expected_command, ), main_result=result_assertions.equals(0, None), ), )
def os_services__w_settings_check(self) -> OsServices: return os_services_access.new_for_cmd_exe( self.command_executor_w_check)
Only TestCaseSetup:s that are included in this association (dict) can be executed. """ def __init__(self, test_case_id_2_result: Dict[int, tcp.Result]): self.test_case_id_2_result = test_case_id_2_result def apply(self, test_case: TestCaseFileReference) -> tcp.Result: return self.test_case_id_2_result[id(test_case)] DUMMY_TEST_CASE_DEFINITION = test_case_definition_with_no_instructions_and_no_preprocessor( ) DUMMY_CASE_PROCESSING = case_processing.Configuration( DUMMY_TEST_CASE_DEFINITION, test_case_handling_setup_with_identity_preprocessor(), os_services_access.new_for_cmd_exe( CommandExecutorThatJustReturnsConstant()), 2**10, False, ) FULL_RESULT_PASS = new_pass(DUMMY_SDS, ActionToCheckOutcome(0)) FULL_RESULT_SKIP = new_skipped() FULL_RESULT_FAIL = full_result_with_failure_info(FullExeResultStatus.FAIL) T_C_H_S = test_case_handling_setup_with_identity_preprocessor() def test_suite( source_file_name: str, sub_test_suites: List[structure.TestSuiteHierarchy], test_cases: List[TestCaseFileReference]
def _run(self, suite_file_name: str, suite_file_overriding: Optional[Path]): default_test_case_handling = setup_with_null_act_phase_and_null_preprocessing() suite_conf_instruction = SuiteConfInstructionThatSets( preprocessor=preprocessor_that_gives_const_source_with_single_assert_instruction( ASSERT_PHASE_INSTRUCTION_THAT_PASS_IFF_STDOUT_IS_SUCCESS_INDICATOR_STRING__NAME), act_phase_setup=act_setup_that_prints_single_string_on_stdout(SUCCESS_INDICATOR_STRING)) suite_conf_instructions = { SUITE_CONF_INSTRUCTION_THAT_SETS_PREPROCESSOR_AND_ACTOR__NAME: single_instruction_setup(SUITE_CONF_INSTRUCTION_THAT_SETS_PREPROCESSOR_AND_ACTOR__NAME, suite_conf_instruction) } suite_conf_parser = configuration_section_parser(suite_conf_instructions) test_case_definition = test_case_definition_with_only_assert_phase_instructions([ ( ASSERT_PHASE_INSTRUCTION_THAT_PASS_IFF_STDOUT_IS_SUCCESS_INDICATOR_STRING__NAME, assert_phase_instruction_that_pass_iff_stdout_is_success_indicator_string(SUCCESS_INDICATOR_STRING) ), ( ASSERT_PHASE_INSTRUCTION_THAT_FAILS_UNCONDITIONALLY__NAME, ASSERT_PHASE_INSTRUCTION_THAT_FAILS_UNCONDITIONALLY, ), ]) processor = sut.Processor(test_case_definition, os_services_access.new_for_cmd_exe(CommandExecutorThatJustReturnsConstant()), suite_conf_parser, 2 ** 10) suite_file = File( suite_file_name, test_suite_source_with_single_conf_instruction( SUITE_CONF_INSTRUCTION_THAT_SETS_PREPROCESSOR_AND_ACTOR__NAME) ) case_file = File( 'test.case', test_case_source_with_single_assert_phase_instruction( ASSERT_PHASE_INSTRUCTION_THAT_FAILS_UNCONDITIONALLY__NAME) ) sub_dir_path = Path('sub-dir') suite_and_case_files = DirContents([ suite_file, case_file, ]).in_dir(str(sub_dir_path)) explicit_suite_file_path = None if suite_file_overriding: explicit_suite_file_path = sub_dir_path / suite_file_overriding with tmp_dir_as_cwd(suite_and_case_files) as tmp_dir: execution_settings = TestCaseExecutionSettings(sub_dir_path / case_file.name_as_path, tmp_dir / sub_dir_path, ReportingOption.STATUS_CODE, default_test_case_handling, run_as_part_of_explicit_suite=explicit_suite_file_path) # ACT # actual_result = capture_output_from_processor(processor, execution_settings) # ASSERT # if actual_result.exitcode != exit_values.EXECUTION__PASS.exit_code: self.fail(_error_message(actual_result))
def runTest(self): # ARRANGE # actor = sut.actor(COMMAND_THAT_RUNS_PYTHON_PROGRAM_FILE) exe_file = fs.python_executable_file( 'program-name', py_programs.exit_with_0() ) argument_to_act_file = 'argument-to-act-file' act_contents = ab.sequence__r([ ab.singleton(exe_file.name), ab.singleton(argument_to_act_file) ]) interpreter_arguments_symbol = ListConstantSymbolContext('INTERPRETER_ARGS_LIST', ['1st', '2nd']) interpreter_arguments = ArgumentsSdv.new_without_validation( interpreter_arguments_symbol.sdv ) actor = sut.actor( command_sdvs.for_executable_file( path_sdvs.constant(path_ddvs.absolute_file_name(sys.executable)), interpreter_arguments ) ) def get_command_assertion(tcds: TestCaseDs) -> Assertion[Command]: symbols = SymbolTable.empty() return asrt_command.matches_command( asrt.anything_goes(), asrt.equals( interpreter_arguments_symbol.constant_list + [ str(relativity_configurations.ATC_FILE .named_file_conf(exe_file.name) .path_sdv .resolve(symbols) .value_of_any_dependency__d(tcds) .primitive ), argument_to_act_file, ] ) ) act_instruction = instr([act_contents.as_str]) executor_that_records_arguments = CommandExecutorThatRecordsArguments() # ACT & ASSERT # integration_check.check_execution( self, actor, [act_instruction], arrangement_w_tcds( process_execution=ProcessExecutionArrangement( os_services=os_services_access.new_for_cmd_exe(executor_that_records_arguments) ), hds_contents=relativity_configurations.ATC_FILE.populator_for_relativity_option_root__hds( fs.DirContents([exe_file]) ) ), Expectation( after_execution=ExecutedCommandAssertion( executor_that_records_arguments, get_command_assertion, ) ), )
def runTest(self): # ARRANGE # actor = sut.actor(COMMAND_THAT_RUNS_PYTHON_PROGRAM_FILE) exe_file = fs.python_executable_file( 'program-name', py_programs.exit_with_0() ) text_until_end_of_line = 'some {}invalidly quoted text'.format(SOFT_QUOTE_CHAR) existing_path_relativity = relativity_options.conf_rel_hds(RelHdsOptionType.REL_HDS_CASE) existing_path_argument = path_arguments.RelOptPathArgument( 'existing-path', existing_path_relativity.relativity, ) act_contents = ab.sequence__r([ ab.singleton(exe_file.name), program_arguments.existing_path( existing_path_argument ), program_arguments.remaining_part_of_current_line_as_literal(text_until_end_of_line), ]) def get_command_assertion(tcds: TestCaseDs) -> Assertion[Command]: symbols = SymbolTable.empty() return asrt_command.matches_command( asrt.anything_goes(), asrt.equals([ str(relativity_configurations.ATC_FILE .named_file_conf(exe_file.name) .path_sdv .resolve(symbols) .value_of_any_dependency__d(tcds) .primitive ), str(existing_path_relativity .named_file_conf(existing_path_argument.name) .path_sdv .resolve(symbols) .value_of_any_dependency__d(tcds) .primitive ), text_until_end_of_line, ]) ) act_instruction = instr([act_contents.as_str]) executor_that_records_arguments = CommandExecutorThatRecordsArguments() # ACT & ASSERT # integration_check.check_execution( self, actor, [act_instruction], arrangement_w_tcds( process_execution=ProcessExecutionArrangement( os_services=os_services_access.new_for_cmd_exe(executor_that_records_arguments) ), hds_contents=hds_populators.multiple([ relativity_configurations.ATC_FILE.populator_for_relativity_option_root__hds( fs.DirContents([exe_file]) ), existing_path_relativity.populator_for_relativity_option_root__hds( fs.DirContents([fs.File.empty(existing_path_argument.name)]) ), ]) ), Expectation( after_execution=ExecutedCommandAssertion( executor_that_records_arguments, get_command_assertion, ) ), )