def new_w_defaults_of_hard_error(
        may_depend_on_external_resources: bool,
        as_str: Callable[[], str] = do_raise(
            HardErrorException(
                asrt_text_doc.new_single_string_text_for_test(
                    'should not be called'))),
        as_lines: Callable[[], Iterator[str]] = do_raise(
            HardErrorException(
                asrt_text_doc.new_single_string_text_for_test(
                    'should not be called'))),
        as_file: Callable[[DirFileSpace], Path] = do_raise(
            HardErrorException(
                asrt_text_doc.new_single_string_text_for_test(
                    'should not be called'))),
        write_to: Callable[[TextIO], None] = do_raise(
            HardErrorException(
                asrt_text_doc.new_single_string_text_for_test(
                    'should not be called'))),
    ) -> 'StringSourceContentsData':
        def _may_depend_on_external_resources() -> bool:
            return may_depend_on_external_resources

        return StringSourceContentsData(
            may_depend_on_external_resources=_may_depend_on_external_resources,
            as_str=as_str,
            as_lines=as_lines,
            as_file=as_file,
            write_to=write_to,
        )
Exemple #2
0
    def matches_w_trace(self, model: FileMatcherModel) -> MatchingResult:
        if not isinstance(model, FileMatcherModel):
            raise HardErrorException(
                text_doc_assertions.new_single_string_text_for_test(
                    'Model is not a {}: {}'.format(type(FileMatcherModel),
                                                   model)))

        if not model.file_type_access.is_type(FileType.DIRECTORY):
            raise HardErrorException(
                text_doc_assertions.new_single_string_text_for_test(
                    'Test failure: File is not a directory'))

        return matching_result.of(True)
Exemple #3
0
def _install_into_directory(os_services: OsServices,
                            src_file_path: pathlib.Path,
                            dst_file_name: str,
                            dst_container_path: pathlib.Path):
    target = dst_container_path / dst_file_name
    if target.exists():
        raise HardErrorException(
            failure_details.FailureDetailsRenderer(
                failure_details.FailureDetails.new_message(
                    text_docs.single_line(
                        str_constructor.FormatMap(
                            '{dst} already exists: {target}',
                            {
                                'dst': instruction_arguments.DESTINATION_PATH_ARGUMENT.name,
                                'target': target,
                            }))
                )
            )
        )
    src = str(src_file_path)
    dst = str(target)
    if src_file_path.is_dir():
        os_services.copy_tree__preserve_as_much_as_possible(src, dst)
    else:
        os_services.copy_file__preserve_as_much_as_possible(src, dst)
 def _raise_file_name_clash(
     self,
     src_file_name: str,
     dst_dir_path: DescribedPath,
 ):
     rendering = _FileNameClashRendering(self._src_dir, dst_dir_path,
                                         src_file_name)
     err_msg_renderer = rendering.renderer()
     raise HardErrorException(err_msg_renderer)
Exemple #5
0
    def _hard_error(ex: Exception):
        from exactly_lib.test_case.hard_error import HardErrorException
        from exactly_lib.common.report_rendering.parts.failure_details import FailureDetailsRenderer
        from exactly_lib.test_case.result.failure_details import FailureDetails
        from exactly_lib.execution.phase_step import ACT__PREPARE

        msg = 'Error in {}'.format(ACT__PREPARE.step)
        raise HardErrorException(
            FailureDetailsRenderer(
                FailureDetails.new_exception(ex, message=msg)))
Exemple #6
0
 def test_fail_due_to_unexpected_hard_error_exception(self):
     self._check_source_and_exe_variants__failing_assertions(
         ParserThatGives(instruction_embryo_that__phase_agnostic(
             main=do_raise(HardErrorException(new_single_string_text_for_test('hard error message'))))
         ),
         Arrangement.phase_agnostic(),
         MultiSourceExpectation.phase_agnostic(
             main_result=asrt.anything_goes()
         ),
     )
Exemple #7
0
 def test_succeed_due_to_expected_hard_error_exception(self):
     self._check_source_and_exe_variants(
         ParserThatGives(instruction_embryo_that__phase_agnostic(
             main=do_raise(HardErrorException(new_single_string_text_for_test('hard error message'))))
         ),
         Arrangement.phase_agnostic(),
         MultiSourceExpectation.phase_agnostic(
             main_raises_hard_error=True
         ),
     )
Exemple #8
0
def _raise_he__single_line(
    format_str: str,
    format_map: Mapping[str, ToStringObject],
    ex: Exception,
):
    raise HardErrorException(
        failure_details.FailureDetailsRenderer(
            FailureDetails.new_message(
                text_docs.single_line(
                    str_constructor.FormatMap(format_str, format_map)), ex)))
Exemple #9
0
 def add_dir_to_process_if_is_dir(maybe_entry_for_dir):
     try:
         if (maybe_entry_for_dir.is_dir()
                 and not directory_prune.matches_w_trace(
                     current_file_model.as_file_matcher_model()).value):
             remaining_dirs.append(
                 current_file.new_for_sub_dir(maybe_entry_for_dir))
     except OSError as ex:
         raise HardErrorException(
             text_docs.os_exception_error_message(ex))
Exemple #10
0
 def write(self, tmp_file_space: DirFileSpace, output: TextIO):
     processor = self._processor(tmp_file_space, output)
     result = processor.process(self._proc_exe_settings, self._command.command)
     if result.exit_code != 0:
         raise HardErrorException(
             top_lvl_error_msg_rendering.non_zero_exit_code_msg(
                 self._command.structure(),
                 result.exit_code,
                 result.stderr,
             )
         )
Exemple #11
0
 def _raise_hard_error(self,
                       exit_code: int,
                       stderr: TextIO,
                       ):
     raise HardErrorException(
         top_lvl_error_msg_rendering.non_zero_exit_code_msg(
             self._command.structure(),
             exit_code,
             self._err_str_from(stderr),
         )
     )
Exemple #12
0
def _raise_hard_error(command: Command,
                      ex: process_executor.ProcessExecutionException):
    from exactly_lib.test_case.hard_error import HardErrorException
    from exactly_lib.impls.types.program import top_lvl_error_msg_rendering
    from exactly_lib.util.simple_textstruct.rendering import blocks
    from exactly_lib.util.simple_textstruct.rendering import line_objects
    raise HardErrorException(
        top_lvl_error_msg_rendering.unable_to_execute_program(
            command.new_structure_builder().build(),
            blocks.MajorBlocksOfSingleLineObject(
                line_objects.PreFormattedString.of_str(str(ex.cause)))))
Exemple #13
0
 def make(self, path: DescribedPath):
     self._assert_is_valid_path(path)
     try:
         self._maker(path)
     except OSError as ex:
         failure = failure_details.FailureDetails(
             path_err_msgs.line_header__primitive__path(
                 self._PATH_CREATION_FAILURE,
                 path),
             ex
         )
         raise HardErrorException(_fd_rendering.FailureDetailsRenderer(failure))
Exemple #14
0
    def _get_exit_code(self) -> int:
        sds = self._sds
        try:
            f = sds.result.exitcode_file.open()
        except IOError:
            rel_path = sds.relative_to_sds_root(sds.result.exitcode_file)
            err_msg = text_docs.single_line(
                str_constructor.FormatMap(
                    'Cannot read {exit_code} from file {file}', {
                        'exit_code': texts.ATTRIBUTE__EXIT_CODE,
                        'file': rel_path,
                    }))
            raise HardErrorException(err_msg)
        try:
            contents = f.read()
        except IOError:
            raise HardErrorException(
                text_docs.single_line(
                    str_constructor.Concatenate([
                        _FAILED_TO_READ_CONTENTS_FROM,
                        sds.result.exitcode_file,
                    ])))
        finally:
            f.close()

        try:
            return int(contents)
        except ValueError:
            msg = text_docs.single_line(
                str_constructor.FormatMap(
                    'The contents of the file for {exit_code} ("{file}") is not an integer: "{contents}"',
                    {
                        'exit_code': texts.ATTRIBUTE__EXIT_CODE,
                        'file': sds.result.exitcode_file,
                        'contents': contents,
                    }))
            raise HardErrorException(msg)
    def _handle_result(
        self,
        result: RET,
        command: Command,
    ):
        exit_code = self.get_exit_code(result)

        if exit_code == 0:
            return

        raise HardErrorException(
            top_lvl_error_msg_rendering.non_zero_exit_code_msg(
                command.new_structure_builder().build(),
                exit_code,
                self._stderr_part(result),
            ))
Exemple #16
0
 def __call__(self, *args, **kwargs):
     src_basename = self.src_path.name
     if self.dst_path.exists():
         if self.dst_path.is_dir():
             _install_into_directory(self.os_services,
                                     self.src_path,
                                     src_basename,
                                     self.dst_path)
         else:
             err_msg = '{} file already exists but is not a directory: {}'.format(
                 instruction_arguments.DESTINATION_PATH_ARGUMENT.name,
                 self.dst_path)
             raise HardErrorException(
                 failure_details.FailureDetailsRenderer(
                     failure_details.FailureDetails.new_constant_message(err_msg))
             )
     else:
         self.os_services.make_dir_if_not_exists(self.dst_path.parent)
         _install_into_directory(self.os_services,
                                 self.src_path,
                                 self.dst_path.name,
                                 self.dst_path.parent)
Exemple #17
0
    def test_hard_error_exception_from_executor_execute_SHOULD_be_handled(
            self):
        # ARRANGE #
        act_phase_instructions = []
        hard_error_message = 'the err msg'

        actor = sut.ActorFromParts(
            ParserWithConstantResult(SymbolUserWithConstantSymbolReferences(
                ())),
            sut.UnconditionallySuccessfulValidatorConstructor(),
            _ExecutorConstructorForConstant(
                ExecutorThat(execute_initial_action=do_raise(
                    HardErrorException(
                        asrt_text_doc.new_single_string_text_for_test(
                            hard_error_message))))),
        )

        arrangement = arrangement_w_tcds()
        expectation = Expectation.hard_error_from_execute(
            error_message=asrt_text_doc.
            is_single_pre_formatted_text_that_equals(hard_error_message))
        # ACT & ASSERT #
        check_execution(self, actor, act_phase_instructions, arrangement,
                        expectation)
Exemple #18
0
def hard_error_ex(err_msg: str) -> HardErrorException:
    return HardErrorException(
        asrt_text_doc.new_pre_formatted_str_for_test(err_msg)
    )
 def _validate_post_setup(validator: DdvValidator,
                          tcds: TestCaseDs,
                          ):
     err_msg = validator.validate_post_sds_if_applicable(tcds)
     if err_msg:
         raise HardErrorException(err_msg)
 def _raise_hard_error(self):
     raise HardErrorException(
         text_doc_assertions.new_single_string_text_for_test(self._err_msg))
Exemple #21
0
 def _hard_error_if_file_is_not_existing_of_expected_type(
         self, model: FileMatcherModel):
     mb_failure = path_check.failure_message_or_none(
         self._expected_file_check, model.path)
     if mb_failure:
         raise HardErrorException(mb_failure)
Exemple #22
0
 def freeze(self):
     raise HardErrorException(
         asrt_text_doc.new_single_string_text_for_test(
             'freeze not allowed here'))
Exemple #23
0
 def may_depend_on_external_resources() -> bool:
     raise HardErrorException(
         asrt_text_doc.new_single_string_text_for_test('hard err msg'))
Exemple #24
0
 def matches_w_trace(self, model: MODEL) -> MatchingResult:
     raise HardErrorException(new_single_string_text_for_test(self.error_message))
Exemple #25
0
def _raise_hard_error_if_not_success(result: CheckResult, path: DescribedPath):
    if not result.is_success:
        raise HardErrorException(
            file_properties.render_failure__d(result.cause, path)
        )
Exemple #26
0
 def _assert_is_valid_path(self, path: DescribedPath):
     result = self._FILE_EXISTENCE_CHECK.apply(path.primitive)
     if result.is_success:
         raise HardErrorException(
             file_properties.render_failure__d(self._PROPERTIES_FOR_ERR_MSG, path)
         )