示例#1
0
def execute_and_check(put: unittest.TestCase, arrangement: Arrangement,
                      expectation: Expectation):
    with home_directory_structure() as hds:
        with preserved_cwd():
            partial_result = sut.execute(
                arrangement.test_case,
                ExecutionConfiguration(os_environ_getter, None,
                                       arrangement.timeout_in_seconds,
                                       arrangement.os_services,
                                       sandbox_root_name_resolver.for_test(),
                                       arrangement.mem_buff_size),
                ConfPhaseValues(NameAndValue('the actor', arrangement.actor),
                                hds),
                arrangement.settings_handler,
                is_keep_sandbox=True)

            expectation.phase_result.apply_with_message(
                put, partial_result, 'phase_result')
            result = Result(hds, partial_result)
            expectation.assertion_on_sds.apply_with_message(
                put, result.partial_result.sds, 'Sandbox Directory Structure')
    # CLEANUP #
    if result is not None and result.sds is not None:
        if result.sds.root_dir.exists():
            shutil.rmtree(str(result.sds.root_dir))
示例#2
0
def tmp_dir_as_cwd(contents: DirContents = empty_dir_contents()
                   ) -> ContextManager[pathlib.Path]:
    with temp_dir() as dir_path:
        with preserved_cwd():
            os.chdir(str(dir_path))
            contents.write_to(dir_path)
            yield dir_path
示例#3
0
def _check_contents_of_stdin_for_setup_settings(
        put: unittest.TestCase, settings_handler: MkSetupSettingsHandler,
        expected_contents_of_stdin: str) -> PartialExeResult:
    """
    Tests contents of stdin by executing a Python program that stores
    the contents of stdin in a file.
    """
    with tmp_dir() as tmp_dir_path:
        with preserved_cwd():
            # ARRANGE #
            output_file_path = tmp_dir_path / 'output.txt'
            python_program_file = fs.File(
                'logic_symbol_utils.py',
                _python_program_that_prints_stdin_to(output_file_path))
            python_program_file.write_to(tmp_dir_path)
            executor_that_records_contents_of_stdin = _AtcThatExecutesPythonProgramFile(
                tmp_dir_path / 'logic_symbol_utils.py')
            parser = ActorForConstantAtc(
                executor_that_records_contents_of_stdin)
            test_case = _empty_test_case()
            # ACT #
            result = _execute(parser, test_case, settings_handler)
            # ASSERT #
            file_checker = FileChecker(put)
            file_checker.assert_file_contents(output_file_path,
                                              expected_contents_of_stdin)
            return result
示例#4
0
    def execute(self):
        # SETUP #
        with preserved_cwd():
            with home_directory_structure() as hds:
                # ACT #
                partial_result = sut.execute(
                    self._test_case(),
                    ExecutionConfiguration(
                        os_environ_getter, None, None,
                        os_services_access.new_for_current_os(),
                        sandbox_root_name_resolver.for_test(), 2**10),
                    ConfPhaseValues(NameAndValue('the actor', self.__actor),
                                    hds), self._mk_settings_handler,
                    self.__dbg_do_not_delete_dir_structure)

                # ASSERT #
                self.__partial_result = partial_result
                self._assertions()
        # CLEANUP #
        if not self.__dbg_do_not_delete_dir_structure and self.sds:
            if self.sds.root_dir.exists():
                shutil.rmtree(str(self.sds.root_dir), ignore_errors=True)
        else:
            if self.sds:
                print(str(self.sds.root_dir))
示例#5
0
def tcds_with_act_as_curr_dir_2(
    hds_contents: hds_populators.HdsPopulator = hds_populators.empty(),
    sds_contents: sds_populator.SdsPopulator = sds_populator.empty(),
    non_hds_contents: non_hds_populator.NonHdsPopulator = non_hds_populator.
    empty(),
    tcds_contents: tcds_populators.TcdsPopulator = tcds_populators.empty(),
    act_result: Optional[ActResultProducer] = None,
    pre_contents_population_action: PlainTcdsAction = PlainTcdsAction(),
    post_contents_population_action: PlainTcdsAction = PlainTcdsAction(),
) -> ContextManager[TestCaseDs]:
    prefix = strftime(program_info.PROGRAM_NAME + '-test-%Y-%m-%d-%H-%M-%S',
                      localtime())
    with home_directory_structure(prefix=prefix + '-home') as hds:
        with sandbox_directory_structure(prefix=prefix + "-sds-") as sds:
            tcds = TestCaseDs(hds, sds)
            with preserved_cwd():
                os.chdir(str(sds.act_dir))
                pre_contents_population_action.apply(tcds)
                hds_contents.populate_hds(hds)
                sds_contents.populate_sds(sds)
                non_hds_contents.populate_non_hds(sds)
                tcds_contents.populate_tcds(tcds)
                if act_result:
                    actual_act_result = act_result.apply(ActEnvironment(tcds))
                    write_act_result(tcds.sds, actual_act_result)
                post_contents_population_action.apply(tcds)
                yield tcds
示例#6
0
def execute(test_case: TestCase, full_exe_input_conf: ExecutionConfiguration,
            conf_phase_values: ConfPhaseValues,
            setup_handler: MkSetupSettingsHandler,
            is_keep_sandbox: bool) -> PartialExeResult:
    """
    Part of execution that is independent of the "status" (SKIP, PASS, FAIL, ...)

    Takes care of construction of the Sandbox directory structure, including
    the root directory, and executes a given Test Case in this directory.

    Preserves Current Working Directory.

    Perhaps the test case should be executed in a sub process, so that
    Environment Variables and Current Working Directory of the process that executes
    the main program is not modified.

    The responsibility of this method is not the most natural!!
    Please refactor if a more natural responsibility evolves!
    """
    ret_val = None
    try:
        with preserved_cwd():
            ret_val = executor.execute(
                executor.Configuration(full_exe_input_conf, conf_phase_values,
                                       setup_handler),
                test_case,
            )
            return ret_val
    finally:
        if not is_keep_sandbox:
            if ret_val is not None and ret_val.has_sds:
                shutil.rmtree(str(ret_val.sds.root_dir), ignore_errors=True)
示例#7
0
def test__va(put: unittest.TestCase,
             test_case: TestCase,
             arrangement: Arrangement,
             assertions_on_result: Assertion[Result],
             is_keep_sandbox_during_assertions: bool = False):
    with preserved_cwd():
        result = _execute(test_case,
                          arrangement,
                          is_keep_sandbox=is_keep_sandbox_during_assertions)

        assertions_on_result.apply(put, result, asrt.MessageBuilder('Result'))
    # CLEANUP #
    if result.sds.root_dir.exists():
        shutil.rmtree(str(result.sds.root_dir), ignore_errors=True)
示例#8
0
def test(put: unittest.TestCase,
         test_case: TestCase,
         actor: Actor,
         assertions: Callable[[unittest.TestCase, Result], None],
         is_keep_sandbox: bool = True):
    with preserved_cwd():
        result = _execute(test_case,
                          Arrangement(actor=actor),
                          is_keep_sandbox=is_keep_sandbox)

        assertions(put, result)
    # CLEANUP #
    if result.sds.root_dir.exists():
        shutil.rmtree(str(result.sds.root_dir))
示例#9
0
def sds_with_act_as_curr_dir(
    contents: sds_populator.SdsPopulator = sds_populator.empty(),
    pre_contents_population_action: SdsAction = SdsAction(),
    symbols: SymbolTable = None,
) -> PathResolvingEnvironmentPostSds:
    with tempfile.TemporaryDirectory(prefix=program_info.PROGRAM_NAME +
                                     '-test-sds-') as sds_root_dir:
        sds = sds_module.construct_at(resolved_path_name(sds_root_dir))
        environment = PathResolvingEnvironmentPostSds(
            sds, symbol_table_from_none_or_value(symbols))
        with preserved_cwd():
            os.chdir(str(sds.act_dir))
            pre_contents_population_action.apply(environment)
            contents.populate_sds(sds)
            yield environment
示例#10
0
def check(setup: Setup, put: unittest.TestCase):
    # ARRANGE #
    with tempfile.TemporaryDirectory(prefix=program_info.PROGRAM_NAME +
                                     '-test-') as tmp_dir:
        with preserved_cwd():
            tmp_dir_path = resolved_path(tmp_dir)
            os.chdir(str(tmp_dir_path))

            setup.file_structure_to_read().write_to(tmp_dir_path)
            # ACT & ASSERT #
            with put.assertRaises(Exception) as cm:
                # ACT #
                Reader(default_environment()).apply(
                    setup.root_suite_based_at(tmp_dir_path))
            # ASSERT #
            setup.check_exception(tmp_dir_path, cm.exception, put)
示例#11
0
def tcds_with_act_as_curr_dir(
        hds_contents: hds_populators.HdsPopulator = hds_populators.empty(),
        sds_contents: sds_populator.SdsPopulator = sds_populator.empty(),
        non_hds_contents: non_hds_populator.NonHdsPopulator = non_hds_populator.empty(),
        tcds_contents: tcds_populators.TcdsPopulator = tcds_populators.empty(),
        pre_contents_population_action: TcdsAction = TcdsAction(),
        symbols: SymbolTable = None) -> ContextManager[PathResolvingEnvironmentPreOrPostSds]:
    symbols = symbol_table_from_none_or_value(symbols)
    prefix = strftime(program_info.PROGRAM_NAME + '-test-%Y-%m-%d-%H-%M-%S', localtime())
    with home_directory_structure(prefix=prefix + '-home') as hds:
        with sandbox_directory_structure(prefix=prefix + "-sds-") as sds:
            tcds = TestCaseDs(hds, sds)
            ret_val = PathResolvingEnvironmentPreOrPostSds(tcds, symbols)
            with preserved_cwd():
                os.chdir(str(sds.act_dir))
                pre_contents_population_action.apply(ret_val)
                hds_contents.populate_hds(hds)
                sds_contents.populate_sds(sds)
                non_hds_contents.populate_non_hds(sds)
                tcds_contents.populate_tcds(tcds)
                yield ret_val
示例#12
0
    def check(self, instruction: AssertPhaseInstruction):
        with tcds_with_act_as_curr_dir__post_act(self.arrangement.tcds) as tcds:
            with preserved_cwd():
                os.chdir(str(tcds.hds.case_dir))
                proc_execution = self.arrangement.process_execution

                environment_builder = InstructionEnvironmentPostSdsBuilder.new_tcds(
                    tcds,
                    self.arrangement.symbols,
                    proc_execution.process_execution_settings,
                )

                environment = environment_builder.build_pre_sds()
                validate_result = self._execute_validate_pre_sds(environment, instruction)
                if not validate_result.is_success:
                    return

            environment = environment_builder.build_post_sds()
            validate_result = self._execute_validate_post_setup(environment, instruction)
            if not validate_result.is_success:
                return

            instruction_settings = instr_settings.from_proc_exe_settings(
                self.arrangement.process_execution.process_execution_settings,
                self.arrangement.default_environ_getter,
            )

            try:
                main_result = self._execute_main(environment, instruction_settings, instruction)
            except StopAssertion:
                return

            self.expectation.instruction_settings.apply_with_message(self.put, instruction_settings,
                                                                     'instruction settings')
            self.expectation.proc_exe_settings.apply_with_message(self.put, environment.proc_exe_settings,
                                                                  'proc exe settings')
            self.expectation.main_side_effects_on_sds.apply_with_message(self.put, environment.sds, 'SDS')
            self.expectation.main_side_effects_on_tcds.apply_with_message(self.put, tcds, 'TCDS')

        self.expectation.main_result.apply(self.put, main_result)
示例#13
0
def _execute(
    actor: Actor,
    test_case: TestCase,
    setup_settings_handler: Optional[MkSetupSettingsHandler] = None,
    is_keep_sandbox: bool = False,
    current_directory: pathlib.Path = None,
    mem_buff_size: int = 2**10,
) -> PartialExeResult:
    if current_directory is None:
        current_directory = pathlib.Path.cwd()
    with home_directory_structure() as hds:
        with preserved_cwd():
            os.chdir(str(current_directory))
            return sut.execute(
                test_case,
                ExecutionConfiguration(os_environ_getter, None, None,
                                       os_services_access.new_for_current_os(),
                                       sandbox_root_name_resolver.for_test(),
                                       mem_buff_size),
                ConfPhaseValues(NameAndValue('the actor', actor), hds),
                settings_handlers.mk_from_optional(setup_settings_handler),
                is_keep_sandbox)
示例#14
0
    def check(self, instruction: Instruction):
        self._check_instruction(CleanupPhaseInstruction, instruction)
        assert isinstance(instruction, CleanupPhaseInstruction)
        self.expectation.symbol_usages.apply_with_message(
            self.put, instruction.symbol_usages(), 'symbol-usages after parse')
        with tcds_with_act_as_curr_dir(
                pre_contents_population_action=self.arrangement.
                pre_contents_population_action,
                hds_contents=self.arrangement.hds_contents,
                sds_contents=self.arrangement.sds_contents,
                non_hds_contents=self.arrangement.non_hds_contents,
                tcds_contents=self.arrangement.tcds_contents,
                symbols=self.arrangement.symbols
        ) as path_resolving_environment:
            tcds = path_resolving_environment.tcds
            self.arrangement.post_sds_population_action.apply(
                path_resolving_environment)
            environment_builder = InstructionEnvironmentPostSdsBuilder.new_tcds(
                tcds,
                self.arrangement.symbols,
                self.arrangement.process_execution_settings,
            )

            with preserved_cwd():
                os.chdir(str(tcds.hds.case_dir))

                environment = environment_builder.build_pre_sds()

                result_of_validate_pre_sds = self._execute_pre_validate(
                    environment, instruction)
                self.expectation.symbol_usages.apply_with_message(
                    self.put, instruction.symbol_usages(),
                    'symbol-usages after ' + phase_step.STEP__VALIDATE_PRE_SDS)
                if not result_of_validate_pre_sds.is_success:
                    return

            environment = environment_builder.build_post_sds()
            instruction_settings = instr_settings.from_proc_exe_settings(
                self.arrangement.process_execution_settings,
                self.arrangement.default_environ_getter)

            result_of_main = self._execute_main(environment,
                                                instruction_settings,
                                                instruction)

            self.expectation.instruction_settings.apply_with_message(
                self.put, instruction_settings, 'instruction settings')
            self.expectation.proc_exe_settings.apply_with_message(
                self.put, environment.proc_exe_settings, 'proc exe settings')
            self.expectation.main_side_effects_on_sds.apply_with_message(
                self.put, environment.sds, 'SDS')
            self.expectation.main_side_effects_on_tcds.apply_with_message(
                self.put, tcds, 'TCDS')

        self.expectation.main_result.apply_with_message(
            self.put, result_of_main,
            'result of main (without access to TCDS)')

        self.expectation.symbol_usages.apply_with_message(
            self.put, instruction.symbol_usages(),
            'symbol-usages after ' + phase_step.STEP__MAIN)
示例#15
0
    def execute(self):
        self.put.assertIsNotNone(self.instruction,
                                 'Result from parser cannot be None')
        self.put.assertIsInstance(
            self.instruction, SetupPhaseInstruction,
            'The instruction must be an instance of ' +
            str(SetupPhaseInstruction))
        assert isinstance(self.instruction, SetupPhaseInstruction)
        self.expectation.symbols_after_parse.apply_with_message(
            self.put, self.instruction.symbol_usages(),
            'symbol-usages after parse')

        with tcds_with_act_as_curr_dir(
                pre_contents_population_action=self.arrangement.
                pre_contents_population_action,
                hds_contents=self.arrangement.hds_contents,
                sds_contents=self.arrangement.sds_contents,
                non_hds_contents=self.arrangement.non_hds_contents,
                tcds_contents=self.arrangement.tcds_contents,
                symbols=self.arrangement.symbols
        ) as path_resolving_environment:

            self.arrangement.post_sds_population_action.apply(
                path_resolving_environment)

            environment_builder = InstructionEnvironmentPostSdsBuilder.new_tcds(
                path_resolving_environment.tcds,
                self.arrangement.symbols,
                self.arrangement.process_execution_settings,
            )

            with preserved_cwd():
                os.chdir(str(path_resolving_environment.hds.case_dir))

                environment = environment_builder.build_pre_sds()
                pre_validate_result = self._execute_pre_validate(
                    environment, self.instruction)
                self.expectation.symbols_after_parse.apply_with_message(
                    self.put, self.instruction.symbol_usages(),
                    'symbol-usages after ' + phase_step.STEP__VALIDATE_PRE_SDS)
                if not pre_validate_result.is_success:
                    return

            instruction_environment = environment_builder.build_post_sds()
            instruction_settings = instr_settings.from_proc_exe_settings(
                self.arrangement.process_execution_settings,
                self.arrangement.default_environ_getter)

            tcds = path_resolving_environment.tcds
            sds = tcds.sds

            main_result = self._execute_main(instruction_environment,
                                             instruction_settings,
                                             self.instruction)

            self.expectation.instruction_settings.apply_with_message(
                self.put, instruction_settings, 'instruction settings')
            self.expectation.main_side_effects_on_sds.apply(self.put, sds)
            self.expectation.symbols_after_main.apply_with_message(
                self.put, self.instruction.symbol_usages(),
                'symbol-usages after ' + phase_step.STEP__MAIN)
            if not main_result.is_success:
                return
            self.expectation.symbols_after_main.apply_with_message(
                self.put, instruction_environment.symbols,
                'symbols_after_main')
            self._execute_post_validate(instruction_environment,
                                        self.instruction)
            self.expectation.symbols_after_main.apply_with_message(
                self.put, self.instruction.symbol_usages(),
                'symbol-usages after ' + phase_step.STEP__VALIDATE_POST_SETUP)
            self.expectation.proc_exe_settings.apply_with_message(
                self.put, instruction_environment.proc_exe_settings,
                'proc exe settings')
            self.expectation.main_side_effects_on_tcds.apply_with_message(
                self.put, instruction_environment.tcds, 'TCDS')
            self.expectation.settings_builder.apply_with_message(
                self.put,
                SettingsBuilderAssertionModel(
                    self.arrangement.initial_settings_builder,
                    instruction_environment, self.arrangement.os_services),
                'settings builder')
        self.expectation.main_result.apply_with_message(
            self.put, main_result, 'main-result (wo access to TCDS)')