def _parse_single_line(instructions: Sequence[ActPhaseInstruction]) -> str:
    non_empty_lines = all_source_code_lines(instructions)
    if not non_empty_lines:
        raise ParseException(svh.new_svh_validation_error('No lines with source code found'))
    if len(non_empty_lines) > 1:
        raise ParseException(svh.new_svh_validation_error('More than one line with source code found'))
    return non_empty_lines[0]
Exemple #2
0
def pre_or_post_sds_validate(file_ref_check: FileRefCheck,
                             environment: PathResolvingEnvironmentPreOrPostSds
                             ) -> svh.SuccessOrValidationErrorOrHardError:
    failure_message = pre_or_post_sds_failure_message_or_none(file_ref_check, environment)
    if failure_message is not None:
        return svh.new_svh_validation_error(failure_message)
    return svh.new_svh_success()
    def test_validation_error_in_validate_post_setup(self):
        test_case = _single_successful_instruction_in_each_phase()
        self._check(
            Arrangement(test_case,
                        act_executor_validate_post_setup=validate_action_that_returns(
                            svh.new_svh_validation_error('error in act/validate-post-setup'))),
            Expectation(
                asrt_result.matches2(PartialExeResultStatus.VALIDATION_ERROR,
                                     asrt_result.has_sds(),
                                     asrt_result.has_no_action_to_check_outcome(),
                                     ExpectedFailureForPhaseFailure.new_with_message(
                                         phase_step.ACT__VALIDATE_POST_SETUP,
                                         'error in act/validate-post-setup'),
                                     ),
                [phase_step.ACT__PARSE] +
                SYMBOL_VALIDATION_STEPS__ONCE +
                PRE_SDS_VALIDATION_STEPS__ONCE +
                [
                    phase_step.SETUP__MAIN,

                    phase_step.SETUP__VALIDATE_POST_SETUP,
                    phase_step.ACT__VALIDATE_POST_SETUP,

                    (phase_step.CLEANUP__MAIN, PreviousPhase.SETUP),
                ],
            ))
 def apply(self, instructions: Sequence[ActPhaseInstruction]) -> _SourceInfo:
     single_line_parser = ParserForSingleLineUsingStandardSyntax()
     single_line = single_line_parser.apply(instructions)
     single_line = single_line.strip()
     source = ParseSource(single_line)
     try:
         source_file_resolver = parse_file_ref.parse_file_ref_from_parse_source(source,
                                                                                RELATIVITY_CONFIGURATION)
         if self.is_shell:
             return self._shell(source_file_resolver, source)
         else:
             return self._executable_file(source_file_resolver, source)
     except TokenSyntaxError as ex:
         raise ParseException(
             svh.new_svh_validation_error(std_error_message_text_for_token_syntax_error_from_exception(ex)))
     except SingleInstructionInvalidArgumentException as ex:
         raise ParseException(svh.new_svh_validation_error(ex.error_message))
Exemple #5
0
def _validation_error(header: str, path: pathlib.Path) -> svh.SuccessOrValidationErrorOrHardError:
    return svh.new_svh_validation_error(
        rend_comb.SingletonSequenceR(
            header_rendering.HeaderValueRenderer(
                header,
                text_docs.minor_blocks_of_string_lines([str(path)])
            )
        )
    )
Exemple #6
0
 def _parse_shell_command(argument: str) -> CommandConfiguration:
     striped_argument = argument.strip()
     if not striped_argument:
         msg = SHELL_COMMAND_MARKER + ': {COMMAND} string is missing.'.format(
             COMMAND=texts.COMMAND)
         raise ParseException(svh.new_svh_validation_error(msg))
     arg_resolver = parse_string.string_resolver_from_string(striped_argument)
     command_resolver = command_resolvers.for_shell(arg_resolver)
     return CommandConfiguration(command_resolver)
Exemple #7
0
def pre_sds_validate(file_ref_check: FileRefCheck,
                     environment: PathResolvingEnvironmentPreSds) -> svh.SuccessOrValidationErrorOrHardError:
    validation_result = file_ref_check.pre_sds_condition_result(environment)
    if not validation_result.is_success:
        fr = file_ref_check.file_ref_resolver.resolve(environment.symbols)
        file_path = fr.value_pre_sds(environment.hds)
        return svh.new_svh_validation_error(render_failure(validation_result.cause,
                                                           file_path))
    return svh.new_svh_success()
Exemple #8
0
 def test_unsuccessful_WHEN_actual_is_not_expected(self):
     # ARRANGE #
     expected_err_msg = 'expected error message'
     actual_err_msg = 'actual error message'
     cases = [
         NEA(
             'VALIDATION - SUCCESS/any error message',
             sut.status_is_not_success(
                 svh.SuccessOrValidationErrorOrHardErrorEnum.
                 VALIDATION_ERROR),
             svh.new_svh_success(),
         ),
         NEA(
             'VALIDATION - VALIDATION/non-matching error message',
             sut.status_is_not_success(
                 svh.SuccessOrValidationErrorOrHardErrorEnum.
                 VALIDATION_ERROR,
                 asrt_text_doc.is_single_pre_formatted_text_that_equals(
                     expected_err_msg)),
             svh.new_svh_validation_error(_renderer_of_str(actual_err_msg)),
         ),
         NEA(
             'VALIDATION - VALIDATION/non-matching error message/const msg',
             sut.status_is_not_success(
                 svh.SuccessOrValidationErrorOrHardErrorEnum.
                 VALIDATION_ERROR,
                 asrt_text_doc.is_single_pre_formatted_text_that_equals(
                     expected_err_msg)),
             svh.new_svh_validation_error__str(actual_err_msg),
         ),
         NEA(
             'HARD_ERROR - SUCCESS/any error message',
             sut.status_is_not_success(
                 svh.SuccessOrValidationErrorOrHardErrorEnum.HARD_ERROR),
             svh.new_svh_success(),
         ),
         NEA(
             'HARD_ERROR - HARD_ERROR/non-matching error message',
             sut.status_is_not_success(
                 svh.SuccessOrValidationErrorOrHardErrorEnum.HARD_ERROR,
                 asrt_text_doc.is_single_pre_formatted_text_that_equals(
                     expected_err_msg)),
             svh.new_svh_hard_error(_renderer_of_str(actual_err_msg)),
         ),
         NEA(
             'HARD_ERROR - HARD_ERROR/non-matching error message/const msg',
             sut.status_is_not_success(
                 svh.SuccessOrValidationErrorOrHardErrorEnum.HARD_ERROR,
                 asrt_text_doc.is_single_pre_formatted_text_that_equals(
                     expected_err_msg)),
             svh.new_svh_hard_error__str(actual_err_msg),
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             # ACT & ASSERT #
             assert_that_assertion_fails(case.expected, case.actual)
Exemple #9
0
 def _parse_executable_file(argument: str) -> CommandConfiguration:
     try:
         source = ParseSource(argument)
         executable = parse_file_ref_from_parse_source(source,
                                                       RELATIVITY_CONFIGURATION)
         arguments = parse_list(source)
         command_resolver = command_resolvers.for_executable_file(executable)
         executable_file = command_resolver.new_with_additional_argument_list(arguments)
         return CommandConfiguration(executable_file)
     except SingleInstructionInvalidArgumentException as ex:
         raise ParseException(svh.new_svh_validation_error(ex.error_message))
Exemple #10
0
def translate_svh_exception_to_svh(action,
                                   *args, **kwargs) -> svh.SuccessOrValidationErrorOrHardError:
    try:
        action(*args, **kwargs)
        return svh.new_svh_success()

    except SvhValidationException as ex:
        return svh.new_svh_validation_error(ex.err_msg)

    except SvhHardErrorException as ex:
        return svh.new_svh_hard_error(ex.err_msg)
def translate_svh_exception_to_svh(action,
                                   *args, **kwargs) -> svh.SuccessOrValidationErrorOrHardError:
    try:
        action(*args, **kwargs)
        return svh.new_svh_success()

    except SvhValidationException as ex:
        return svh.new_svh_validation_error(ex.err_msg)

    except SvhHardErrorException as ex:
        return svh.new_svh_hard_error(ex.err_msg)
    def parse(self, instructions: Sequence[ActPhaseInstruction]) -> ActionToCheck:
        try:
            source_lines = list(itertools.chain.from_iterable(map(self._get_source_code_lines, instructions)))
            reference_instructions_arguments = self._get_reference_instruction_arguments(source_lines)
            references = list(
                itertools.chain.from_iterable(map(_parse_reference_arguments, reference_instructions_arguments)))

            return ActionToCheckThatRunsConstantActions(
                symbol_usages_action=do_return(references)
            )
        except SingleInstructionInvalidArgumentException as ex:
            raise ParseException(svh.new_svh_validation_error(ex.error_message))
    def _get_reference_instruction_arguments(self, lines: Sequence[str]) -> List[str]:
        ret_val = []
        for line in lines:
            if not line or line.isspace():
                continue
            parts = line.split(maxsplit=1)
            if len(parts) == 2 and parts[0] == self._reference_instruction_name:
                ret_val.append(parts[1])
            else:
                err_msg = 'Invalid act phase instruction: {}\nExpecting: {}'.format(
                    line,
                    self._reference_instruction_name)
                raise ParseException(svh.new_svh_validation_error(err_msg))

        return ret_val
Exemple #14
0
 def test_WHEN_parse_raises_unknown_exception_THEN_execution_SHOULD_stop_with_result_of_implementation_error(self):
     # ARRANGE #
     expected_cause = svh.new_svh_validation_error('failure message')
     atc_that_does_nothing = ActionToCheckThatRunsConstantActions()
     step_recorder = ListRecorder()
     recording_atc = ActionToCheckWrapperThatRecordsSteps(step_recorder,
                                                          atc_that_does_nothing)
     actor = ActorForConstantAtc(
         recording_atc,
         parse_atc=do_raise(ValueError(expected_cause))
     )
     arrangement = Arrangement(test_case=_empty_test_case(),
                               actor=actor)
     # ASSERT #
     expectation = Expectation(phase_result=asrt_result.status_is(PartialExeResultStatus.IMPLEMENTATION_ERROR))
     # APPLY #
     execute_and_check(self, arrangement, expectation)
     self.assertEqual([],
                      step_recorder.recorded_elements,
                      'executed steps')
 def runTest(self):
     conf = self.configuration
     test_case = TestCaseGeneratorWithExtraInstrsBetweenRecordingInstr() \
         .add(conf.phase,
              conf.instruction_that_returns(svh.new_svh_validation_error('validation error message')))
     execute_test_case_with_recording(
         self,
         Arrangement(test_case),
         Expectation(
             asrt_result.matches2(
                 PartialExeResultStatus.VALIDATION_ERROR,
                 asrt_result.has_sds(),
                 asrt_result.has_no_action_to_check_outcome(),
                 ExpectedFailureForInstructionFailure.new_with_message(
                     conf.step,
                     test_case.the_extra(conf.phase)[0].source,
                     'validation error message'),
             ),
             conf.expected_steps,
         )
     )
Exemple #16
0
 def test_success_WHEN_actual_is_expected(self):
     # ARRANGE #
     cases = [
         NEA(
             'SUCCESS',
             svh.SuccessOrValidationErrorOrHardErrorEnum.SUCCESS,
             svh.new_svh_success(),
         ),
         NEA(
             'VALIDATION_ERROR',
             svh.SuccessOrValidationErrorOrHardErrorEnum.VALIDATION_ERROR,
             svh.new_svh_validation_error(
                 asrt_text_doc.new_single_string_text_for_test(
                     'validation msg')),
         ),
         NEA(
             'VALIDATION_ERROR/const msg',
             svh.SuccessOrValidationErrorOrHardErrorEnum.VALIDATION_ERROR,
             svh.new_svh_validation_error__str('validation msg'),
         ),
         NEA(
             'HARD_ERROR',
             svh.SuccessOrValidationErrorOrHardErrorEnum.HARD_ERROR,
             svh.new_svh_hard_error(
                 asrt_text_doc.new_single_string_text_for_test(
                     'hard err msg')),
         ),
         NEA(
             'HARD_ERROR/const msg',
             svh.SuccessOrValidationErrorOrHardErrorEnum.HARD_ERROR,
             svh.new_svh_hard_error__str('hard err msg'),
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             assertion = sut.status_is(case.expected)
             # ACT & ASSERT #
             assertion.apply_without_message(self, case.actual)
Exemple #17
0
 def test_success_WHEN_actual_is_not_success(self):
     # ARRANGE #
     cases = [
         NameAndValue(
             'VALIDATION_ERROR',
             svh.new_svh_validation_error(
                 asrt_text_doc.new_single_string_text_for_test(
                     'failure msg'))),
         NameAndValue('VALIDATION_ERROR/const msg',
                      svh.new_svh_validation_error__str('failure msg')),
         NameAndValue(
             'HARD_ERROR',
             svh.new_svh_hard_error(
                 asrt_text_doc.new_single_string_text_for_test(
                     'hard error msg'))),
         NameAndValue('HARD_ERROR/const msg',
                      svh.new_svh_hard_error__str('hard error msg')),
     ]
     assertion = sut.is_success()
     for case in cases:
         with self.subTest(case.name):
             # ACT #
             assert_that_assertion_fails(assertion, case.value)
Exemple #18
0
 def __init__(self):
     self.do_return_validation_error = do_return(svh.new_svh_validation_error('validation error message'))
Exemple #19
0
 def parse(self, instructions: Sequence[ActPhaseInstruction]):
     raise ParseException(svh.new_svh_validation_error('unconditional parse failure'))
Exemple #20
0
                                         output: str,
                                         expected_exit_value: ExitValue):
    lines = output.split()
    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('')
Exemple #21
0
 def validate_pre_sds(self,
                      environment: InstructionEnvironmentForPreSdsStep) -> svh.SuccessOrValidationErrorOrHardError:
     path = self._src_path(environment)
     if not path.exists():
         return svh.new_svh_validation_error('File does not exist: {}'.format(str(path)))
     return svh.new_svh_success()
Exemple #22
0
 def _translate(error_message_or_none: Optional[TextRenderer]) -> svh.SuccessOrValidationErrorOrHardError:
     if error_message_or_none is not None:
         return svh.new_svh_validation_error(error_message_or_none)
     return svh.new_svh_success()