def result_to_sh(result_and_stderr: ResultAndStderr) -> sh.SuccessOrHardError: result = result_and_stderr.result if not result.is_success: return sh.new_sh_hard_error(result.error_message) if result.exit_code != 0: return sh.new_sh_hard_error(failure_message_for_nonzero_status(result_and_stderr)) return sh.new_sh_success()
def test_hard_error_in_setup_main_step(self): test_case = TestCaseGeneratorWithExtraInstrsBetweenRecordingInstr() \ .add(PartialPhase.SETUP, test.setup_phase_instruction_that( main=do_return(sh.new_sh_hard_error('hard error msg from setup')))) self._check( Arrangement(test_case), Expectation( asrt_result.matches2( PartialExeResultStatus.HARD_ERROR, asrt_result.has_sds(), asrt_result.has_no_action_to_check_outcome(), ExpectedFailureForInstructionFailure.new_with_message( phase_step.SETUP__MAIN, test_case.the_extra(PartialPhase.SETUP)[0].source, 'hard error msg from setup'), ), [phase_step.ACT__PARSE] + SYMBOL_VALIDATION_STEPS__TWICE + PRE_SDS_VALIDATION_STEPS__TWICE + [phase_step.SETUP__MAIN, (phase_step.CLEANUP__MAIN, PreviousPhase.SETUP), (phase_step.CLEANUP__MAIN, PreviousPhase.SETUP), ], ))
def test_hard_error_in_setup_main_step(self): py_pgm_setup = PyProgramSetup('some output to stdout', 'some output to stderr', 72) test_case = TestCaseGeneratorWithExtraInstrsBetweenRecordingInstr( act_phase_source=py_pgm_setup.as_line_sequence()) \ .add(PartialPhase.SETUP, test.setup_phase_instruction_that( main=do_return(sh.new_sh_hard_error('hard error msg from setup')))) self._check( arr_for_py3_source(test_case), Expectation( asrt_result.matches2( PartialExeResultStatus.HARD_ERROR, asrt_result.has_sds(), asrt_result.has_no_action_to_check_outcome(), ExpectedFailureForInstructionFailure.new_with_message( phase_step.SETUP__MAIN, test_case.the_extra(PartialPhase.SETUP)[0].source, 'hard error msg from setup'), ), atc_stdout_output=asrt.equals(''), atc_stderr_output=asrt.equals(''), step_recordings= [phase_step.ACT__PARSE] + SYMBOL_VALIDATION_STEPS__TWICE + PRE_SDS_VALIDATION_STEPS__TWICE + [phase_step.SETUP__MAIN, (phase_step.CLEANUP__MAIN, PreviousPhase.SETUP), (phase_step.CLEANUP__MAIN, PreviousPhase.SETUP), ], ))
def test_pass(self): the_error_message = 'error message' cases = [ ('no assertion on error message', sh.new_sh_hard_error(the_error_message), asrt.anything_goes(), ), ('assertion on error message', sh.new_sh_hard_error(the_error_message), asrt.equals(the_error_message), ), ] for name, actual, assertion_on_error_message in cases: with self.subTest(name=name): # ARRANGE # assertion = sut.is_hard_error(assertion_on_error_message) # ACT # assertion.apply_without_message(self, actual)
def test_pass__value_from_td(self): message_str = 'error message' the_error_message = asrt_text_doc.new_single_string_text_for_test(message_str) cases = [ NEA('no assertion on error message', asrt.anything_goes(), sh.new_sh_hard_error(the_error_message), ), NEA('assertion on error message', asrt_text_doc.is_single_pre_formatted_text_that_equals(message_str), sh.new_sh_hard_error(the_error_message), ), ] for case in cases: with self.subTest(case.name): # ARRANGE # assertion = sut.is_hard_error(case.expected) # ACT # assertion.apply_without_message(self, case.actual)
def main(self, environment: InstructionEnvironmentForPostSdsStep, settings: InstructionSettings, os_services: OsServices) -> sh.SuccessOrHardError: validation_result = self._validator.validate_post_sds_if_applicable( environment.path_resolving_environment) if not validation_result.is_success: return sh.new_sh_hard_error(validation_result.failure_message) return self.setup.executor.apply_as_non_assertion( environment, settings, os_services, None)
def prepare( self, environment: InstructionEnvironmentForPostSdsStep, os_services: OsServices, ) -> sh.SuccessOrHardError: try: self._construct_executor(environment, os_services) self._executor.prepare(environment) except HardErrorException as ex: return sh.new_sh_hard_error(ex.error) return sh.new_sh_success()
def prepare(self, environment: InstructionEnvironmentForPostSdsStep, script_output_dir_path: pathlib.Path) -> sh.SuccessOrHardError: script_file_path = self._source_file_path(script_output_dir_path) resolving_env = environment.path_resolving_environment_pre_or_post_sds source_code = self.source_code_resolver.resolve_value_of_any_dependency(resolving_env) try: with open(str(script_file_path), 'w') as f: f.write(source_code) return sh.new_sh_success() except OSError as ex: return sh.new_sh_hard_error(str(ex))
def return_success_or_hard_error(callable_block, *args, **kwargs) -> sh.SuccessOrHardError: """ Executes a callable (by invoking its __call__), and returns hard-error iff a `DetectedException` is raised, otherwise success. :param callable_block: :param args: Arguments given to callable_block :param kwargs: Arguments given to callable_block :return: success iff callable_block does not raise `DetectedException`, otherwise success """ try: callable_block(*args, **kwargs) return sh.new_sh_success() except DetectedException as ex: return sh.new_sh_hard_error(ex.failure_details.failure_message)
def return_success_or_hard_error(procedure: Callable, *args, **kwargs) -> sh.SuccessOrHardError: """ Executes a callable (by invoking its __call__), and returns hard-error iff a `HardErrorException` is raised, otherwise success. :param procedure: :param args: Arguments given to callable_block :param kwargs: Arguments given to callable_block :return: success iff callable_block does not raise `HardErrorException`, otherwise success """ try: procedure(*args, **kwargs) return sh.new_sh_success() except HardErrorException as ex: return sh.new_sh_hard_error(ex.error)
def apply_as_non_assertion( self, environment: InstructionEnvironmentForPostSdsStep, settings: InstructionSettings, os_services: OsServices, setup_phase_settings: Optional[SetupSettingsBuilder], ) -> sh.SuccessOrHardError: main_executor = _MainMethodExecutor(environment, settings, os_services, setup_phase_settings) try: result = self.main_step.main_method().accept(main_executor) except HardErrorException as ex: return sh.new_sh_hard_error(ex.error) return self.result_translator.translate_for_non_assertion(result)
def runTest(self): # ARRANGE # the_error_message = 'the error message' parts = instruction_parts.InstructionParts( ValidatorThat(), MainStepExecutorThat(assertion_return_value=pfh.new_pfh_fail(the_error_message), non_assertion_return_value=sh.new_sh_hard_error(the_error_message))) parser = self.conf.instruction_parser_from_parts_parser(PartsParserThatGives(parts)) source = remaining_source('ignored') # ACT & ASSERT # self.conf.run_test_with_parser(self, parser, source, self.conf.arrangement(), self.conf.expect_failure_of_main( assertion_on_error_message=asrt.equals(the_error_message) ))
def test_fail(self): the_error_message = 'error message' cases = [ ('is success', sh.new_sh_success(), asrt.anything_goes(), ), ('assertion on error message fails', sh.new_sh_hard_error(the_error_message), asrt.equals(the_error_message + ' - part of message not in actual'), ), ] for name, actual, assertion_on_error_message in cases: with self.subTest(name=name): # ARRANGE # assertion = sut.is_hard_error(assertion_on_error_message) # ACT # assert_that_assertion_fails(assertion, actual)
def test_hard_error_in_configuration_phase(self): test_case_generator = test_case_with_two_instructions_in_each_phase() \ .add(phase_identifier.CONFIGURATION, test.configuration_phase_instruction_that(do_return(sh.new_sh_hard_error('hard error msg')))) self._check( Arrangement(test_case_generator), Expectation( asrt_result.matches2( FullExeResultStatus.HARD_ERROR, asrt_result.has_no_sds(), asrt_result.has_no_action_to_check_outcome(), ExpectedFailureForInstructionFailure.new_with_message( phase_step.CONFIGURATION__MAIN, test_case_generator.the_extra(phase_identifier.CONFIGURATION)[ 0].source, 'hard error msg') ), [phase_step.CONFIGURATION__MAIN], ))
def test_hard_error_in_cleanup_main_step(self): test_case = TestCaseGeneratorWithExtraInstrsBetweenRecordingInstr() \ .add(PartialPhase.CLEANUP, test.cleanup_phase_instruction_that( main=do_return(sh.new_sh_hard_error('hard error msg from CLEANUP')))) self._check( Arrangement(test_case, act_executor_execute=execute_action_that_returns_exit_code(3)), Expectation( asrt_result.matches2( PartialExeResultStatus.HARD_ERROR, asrt_result.has_sds(), asrt_result.has_action_to_check_outcome_with_exit_code(3), ExpectedFailureForInstructionFailure.new_with_message( phase_step.CLEANUP__MAIN, test_case.the_extra(PartialPhase.CLEANUP)[0].source, 'hard error msg from CLEANUP'), ), [phase_step.ACT__PARSE] + SYMBOL_VALIDATION_STEPS__TWICE + PRE_SDS_VALIDATION_STEPS__TWICE + [phase_step.SETUP__MAIN, phase_step.SETUP__MAIN, phase_step.SETUP__VALIDATE_POST_SETUP, phase_step.SETUP__VALIDATE_POST_SETUP, phase_step.ACT__VALIDATE_POST_SETUP, phase_step.BEFORE_ASSERT__VALIDATE_POST_SETUP, phase_step.BEFORE_ASSERT__VALIDATE_POST_SETUP, phase_step.ASSERT__VALIDATE_POST_SETUP, phase_step.ASSERT__VALIDATE_POST_SETUP, phase_step.ACT__PREPARE, phase_step.ACT__EXECUTE, phase_step.BEFORE_ASSERT__MAIN, phase_step.BEFORE_ASSERT__MAIN, phase_step.ASSERT__MAIN, phase_step.ASSERT__MAIN, (phase_step.CLEANUP__MAIN, PreviousPhase.ASSERT), ], ))
def test_execution_mode_skipped_but_failing_instruction_in_configuration_phase_after_setting_execution_mode(self): test_case = test_case_with_two_instructions_in_each_phase() \ .add(phase_identifier.CONFIGURATION, test.ConfigurationPhaseInstructionThatSetsExecutionMode( TestCaseStatus.SKIP)) \ .add(phase_identifier.CONFIGURATION, test.configuration_phase_instruction_that(do_return(sh.new_sh_hard_error('hard error msg')))) self._check( Arrangement(test_case), Expectation( asrt_result.matches2( FullExeResultStatus.HARD_ERROR, asrt_result.has_no_sds(), asrt_result.has_no_action_to_check_outcome(), ExpectedFailureForInstructionFailure.new_with_message( phase_step.CONFIGURATION__MAIN, test_case.the_extra(phase_identifier.CONFIGURATION)[1].source, 'hard error msg')), [phase_step.CONFIGURATION__MAIN], ))
def test_fail__value_from_td(self): message_str = 'error message' the_error_message = asrt_text_doc.new_single_string_text_for_test(message_str) cases = [ NEA('is success', asrt.anything_goes(), sh.new_sh_success(), ), NEA('assertion on error message fails', asrt_text_doc.is_single_pre_formatted_text_that_equals( message_str + ' - part of message not in actual' ), sh.new_sh_hard_error(the_error_message), ), ] for case in cases: with self.subTest(case.name): # ARRANGE # assertion = sut.is_hard_error(case.expected) # ACT # assert_that_assertion_fails(assertion, case.actual)
def translate_for_non_assertion( self, error_message: TextRenderer) -> sh.SuccessOrHardError: return (sh.new_sh_success() if error_message is None else sh.new_sh_hard_error(error_message))
def test_fail(self): # ARRANGE # actual = sh.new_sh_hard_error('failure msg') assertion = sut.is_success() # ACT # assert_that_assertion_fails(assertion, actual)
def f(*args, **kwargs) -> sh.SuccessOrHardError: # return sh.new_sh_hard_error(new_failure_details_from_message(message)) return sh.new_sh_hard_error(message)
def result_to_sh(result: ExecutionResultAndStderr) -> sh.SuccessOrHardError: if result.exit_code != 0: return sh.new_sh_hard_error( top_lvl_error_msg_rendering.non_zero_exit_code_msg( result.program, result.exit_code, result.stderr_contents)) return sh.new_sh_success()
def translate_for_non_assertion(self, error_message) -> sh.SuccessOrHardError: return sh.new_sh_success() if error_message is None else sh.new_sh_hard_error(error_message)
def _translate( error_message_or_none: Optional[TextRenderer] ) -> sh.SuccessOrHardError: if error_message_or_none is not None: return sh.new_sh_hard_error(error_message_or_none) return sh.new_sh_success()
put.assertTrue(len(lines) > 0, 'There should be at least one line (found {})'.format(len(lines))) first_line = lines[0] put.assertEqual(expected_exit_value.exit_identifier, first_line, 'first line') DO_RAISES_EXCEPTION = do_raise(ValueError('implementation error msg')) SVH_VALIDATION_ERROR = do_return(svh.new_svh_validation_error('validation error msg')) SVH_HARD_ERROR = do_return(svh.new_svh_hard_error('hard error msg')) SH_HARD_ERROR = do_return(sh.new_sh_hard_error('hard error msg')) def output_is_sds_which_should_be_preserved(sds_dir_name: str) -> ValueAssertion[str]: return asrt.and_([ IsExistingDir(sds_dir_name), asrt.equals(sds_dir_name + '\n'), ]) def output_is_empty(sds_dir_name: str) -> ValueAssertion[str]: return asrt.equals('') class IsExistingDir(ValueAssertionBase[Any]): def __init__(self, path_name: str):