def resolving_helper( symbols: Optional[SymbolTable] = None, tcds: TestCaseDs = fake_tcds(), file_space: DirFileSpace = DirFileSpaceThatMustNoBeUsed(), os_services_: OsServices = os_services_access.new_for_current_os(), process_execution_settings: ProcessExecutionSettings = ProcessExecutionSettings.null(), mem_buff_size: int = 2**10, ) -> LogicTypeResolvingHelper: return LogicTypeResolvingHelper( (symbols if symbols is not None else empty_symbol_table()), tcds, ApplicationEnvironment(os_services_, process_execution_settings, file_space, mem_buff_size), )
def test_manipulation_of_environment_variables(self): env_var = NameAndValue('env_var_name', 'env var value') expected_environment_variables = { env_var.name: env_var.value } instruction = InstructionThatSetsEnvironmentVariable(env_var) self._check_source_and_exe_variants( ParserThatGives(instruction), Arrangement.phase_agnostic( process_execution_settings=ProcessExecutionSettings.with_environ({}) ), MultiSourceExpectation.phase_agnostic( main_side_effect_on_environment_variables=asrt.equals(expected_environment_variables) ), )
def arrangement(self, pre_contents_population_action: TcdsAction = TcdsAction(), hds_contents: hds_populators.HdsPopulator = hds_populators.empty(), sds_contents_before_main: sds_populator.SdsPopulator = sds_populator.empty(), tcds_contents: tcds_populators.TcdsPopulator = tcds_populators.empty(), environ: Optional[Dict[str, str]] = None, default_environ_getter: DefaultEnvironGetter = get_empty_environ, os_services: OsServices = new_for_current_os(), symbols: SymbolTable = None): return ic.Arrangement(pre_contents_population_action=pre_contents_population_action, hds_contents=hds_contents, sds_contents_before_main=sds_contents_before_main, tcds_contents=tcds_contents, process_execution_settings=ProcessExecutionSettings.from_non_immutable(environ=environ), default_environ_getter=default_environ_getter, os_services=os_services, symbols=symbols)
def test_invalid_executable_SHOULD_raise_hard_error(self): # ARRANGE # with tmp_dir.tmp_dir() as tmp_dir_path: with self.assertRaises(HardErrorException) as ctx: executor = sut.ProcessorThatStoresResultInFilesInDir(COMMAND_EXECUTOR, tmp_dir_path, file_ctx_managers.dev_null(), ) # ACT & ASSERT # executor.process( ProcessExecutionSettings.null(), command_for_exe_file(tmp_dir_path / 'non-existing-program'), ) assert isinstance(ctx.exception, HardErrorException) asrt_text_doc.is_any_text().apply_with_message( self, ctx.exception.error, 'exception error message' )
def test_exit_code(self): # ARRANGE # exit_code = 3 program_file = File( 'program.py', py_programs.py_pgm_with_stdout_stderr_exit_code(exit_code=exit_code), ) with tmp_dir.tmp_dir(DirContents([program_file])) as tmp_dir_path: executor = sut.ProcessorThatStoresResultInFilesInDir(COMMAND_EXECUTOR, tmp_dir_path, file_ctx_managers.dev_null(), ) # ACT # result = executor.process(ProcessExecutionSettings.null(), py_exe.command_for_interpreting(tmp_dir_path / program_file.name)) # ASSERT # self.assertEqual(exit_code, result.exit_code, 'Exit code')
def test_propagate_environment_variables_from_arrangement(self): env_var_in_arrangement = NameAndValue('env_var_in_arr_name', 'env var in arr value') env_var_to_set = NameAndValue('env_var_name', 'env var value') environ_of_arrangement = { env_var_in_arrangement.name: env_var_in_arrangement.value, } expected_environment_variables = { env_var_in_arrangement.name: env_var_in_arrangement.value, env_var_to_set.name: env_var_to_set.value } instruction = InstructionThatSetsEnvironmentVariable(env_var_to_set) self._check_source_and_exe_variants( ParserThatGives(instruction), Arrangement.phase_agnostic( process_execution_settings=ProcessExecutionSettings.with_environ(environ_of_arrangement)), MultiSourceExpectation.phase_agnostic( main_side_effect_on_environment_variables= asrt.equals(expected_environment_variables)), )
def test_populate_environ(self): default_from_default_getter = {'default': 'value of default'} default_environs = {'in_environs': 'value of var in environs'} def default_environ_getter() -> Dict[str, str]: return dict(default_from_default_getter) self._check_source_and_exe_variants( PARSER_THAT_GIVES_SUCCESSFUL_INSTRUCTION, Arrangement.phase_agnostic( default_environ_getter=default_environ_getter, process_execution_settings=ProcessExecutionSettings.from_non_immutable(environ=default_environs), ), MultiSourceExpectation.phase_agnostic( instruction_settings=asrt_instr_settings.matches( environ=asrt.equals(default_environs), return_value_from_default_getter=asrt.equals(default_from_default_getter) ), proc_exe_settings=asrt_pes.matches( environ=asrt.equals(default_environs) ) ), )
def test_storage_of_result_in_files__existing_dir(self): # ARRANGE # py_pgm_file = File( 'program.py', program_that_prints_and_exits_with_exit_code(PROCESS_OUTPUT_WITH_NON_ZERO_EXIT_STATUS), ) with tmp_dir.tmp_dir(DirContents([py_pgm_file])) as pgm_dir_path: with tmp_dir.tmp_dir() as output_dir_path: executor = sut.ProcessorThatStoresResultInFilesInDir(COMMAND_EXECUTOR, output_dir_path, file_ctx_managers.dev_null(), ) # ACT # result = executor.process(ProcessExecutionSettings.null(), py_exe.command_for_interpreting(pgm_dir_path / py_pgm_file.name) ) # ASSERT # assert_is_success_and_output_dir_contains_at_exactly_result_files( self, output_dir_path, PROCESS_OUTPUT_WITH_NON_ZERO_EXIT_STATUS, result, )
def test_stdin_SHOULD_be_passed_to_process(self): # ARRANGE # program_file = File( 'stdin-2-stdout.py', py_programs.py_pgm_that_copies_stdin_to_stdout(), ) stdin_file = File( 'stdin.txt', 'contents of stdin', ) dir_contents = DirContents([ program_file, stdin_file, ]) with tmp_dir.tmp_dir_as_cwd(dir_contents): output_dir = pathlib.Path('output') executor = sut.ProcessorThatStoresResultInFilesInDir( COMMAND_EXECUTOR, output_dir, file_ctx_managers.open_file(stdin_file.name_as_path, 'r'), ) # ACT # result = executor.process(ProcessExecutionSettings.null(), py_exe.command_for_interpreting(program_file.name)) # ASSERT # assert_is_success_and_output_dir_contains_at_exactly_result_files( self, output_dir, SubProcessResult( exitcode=0, stdout=stdin_file.contents, stderr='', ), result, )
def test_fail_WHEN_storage_dir_is_an_existing_regular_file(self): # ARRANGE # successful_py_program = File( 'successful.py', py_programs.py_pgm_with_stdout_stderr_exit_code(exit_code=0), ) existing_file = File.empty('a-file') dir_contents = DirContents([successful_py_program, existing_file]) with tmp_dir.tmp_dir(dir_contents) as tmp_dir_path: path_of_existing_regular_file = tmp_dir_path / existing_file.name executor = sut.ProcessorThatStoresResultInFilesInDir( COMMAND_EXECUTOR, path_of_existing_regular_file, file_ctx_managers.dev_null(), ) with self.assertRaises(exception.ImplementationError) as ctx: # ACT & ASSERT # executor.process( ProcessExecutionSettings.null(), command_for_exe_file(tmp_dir_path / successful_py_program.name), ) assert isinstance(ctx.exception, exception.ImplementationError) self.assertIsInstance(ctx.exception.message, str, 'exception info')
def test_populate_environ(self): default_from_default_getter = {'default': 'value of default'} default_environs = {'in_environs': 'value of var in environs'} def default_environ_getter() -> Dict[str, str]: return default_from_default_getter self._check( PARSER_THAT_GIVES_SUCCESSFUL_INSTRUCTION, utils.single_line_source(), sut.arrangement( default_environ_getter=default_environ_getter, process_execution_settings=ProcessExecutionSettings.from_non_immutable(environ=default_environs), ), sut.Expectation( instruction_settings=asrt_instr_settings.matches( environ=asrt.equals(default_environs), return_value_from_default_getter=asrt.equals(default_from_default_getter) ), proc_exe_settings=asrt_pes.matches( environ=asrt.equals(default_environs) ) ), )
def check(self, instruction: InstructionEmbryo[T]): with tcds_with_act_as_curr_dir_3(self.arrangement.tcds) as tcds: environment_builder = InstructionEnvironmentPostSdsBuilder.new_tcds( tcds, self.arrangement.symbols, ProcessExecutionSettings.from_non_immutable( timeout_in_seconds=self.arrangement. process_execution_settings.timeout_in_seconds, environ=self.arrangement.process_execution_settings. environ, ), ) environment = environment_builder.build_pre_sds() validate_result = self._execute_validate_pre_sds( environment, instruction) self.symbols_after_parse.apply_with_message( self.put, instruction.symbol_usages, 'symbol-usages after ' + phase_step.STEP__VALIDATE_PRE_SDS) if validate_result is not None: return environment = environment_builder.build_post_sds() validate_result = self._execute_validate_post_setup( environment, instruction) self.symbols_after_parse.apply_with_message( self.put, instruction.symbol_usages, 'symbol-usages after ' + phase_step.STEP__VALIDATE_POST_SETUP) if validate_result is not None: return instr_settings = _instruction_settings.from_proc_exe_settings( self.arrangement.process_execution_settings, self.arrangement.default_environ_getter) setup_settings = self._setup_settings_from_arrangement() result_of_main = self._execute_main(environment, instr_settings, setup_settings, instruction) self.expectation.main_side_effect_on_environment_variables.apply_with_message( self.put, instr_settings.environ(), 'main side effects on environment variables') self.symbols_after_parse.apply_with_message( self.put, instruction.symbol_usages, 'symbol-usages after ' + phase_step.STEP__MAIN) self.symbols_after_main.apply_with_message( self.put, environment.symbols, 'symbols in symbol table after ' + phase_step.STEP__MAIN) self.expectation.proc_exe_settings.apply_with_message( self.put, environment.proc_exe_settings, 'proc exe settings') self.expectation.instruction_settings.apply_with_message( self.put, instr_settings, 'instruction settings') self.expectation.setup_settings.apply_with_message( self.put, self._setup_settings_assertion_model( setup_settings, environment, ), 'setup settings') self.expectation.main_side_effects_on_sds.apply_with_message( self.put, tcds.sds, 'side_effects_on_files') self.expectation.side_effects_on_tcds.apply_with_message( self.put, tcds, 'side_effects_on_tcds') self.expectation.side_effects_on_hds.apply_with_message( self.put, tcds.hds.case_dir, 'side_effects_on_home') application_environment = InstructionApplicationEnvironment( self.arrangement.os_services, environment, instr_settings, ) self.expectation.instruction_application_environment.apply_with_message( self.put, application_environment, 'assertion_on_environment') self.expectation.main_result.apply_with_message( self.put, result_of_main, 'result of main (wo access to TCDS)')
def __init__(self, os_services: OsServices = os_services_access.new_for_current_os(), process_execution_settings: ProcessExecutionSettings = ProcessExecutionSettings.null(), ): self.os_services = os_services self.process_execution_settings = process_execution_settings
def _proc_exe_settings_w_expected_value(self) -> ProcessExecutionSettings: return ProcessExecutionSettings.with_environ(self.expected_environ)
def _proc_exe_settings_w_expected_value(self) -> ProcessExecutionSettings: return ProcessExecutionSettings.with_timeout(self.expected_timeout)
def runTest(self): # ARRANGE # py_file = File('output-env-vars.py', _PGM_THAT_OUTPUTS_ENVIRONMENT_VARS) py_file_rel_opt_conf = rel_opt.conf_rel_any(RelOptionType.REL_TMP) py_file_conf = py_file_rel_opt_conf.named_file_conf(py_file.name) program_symbol = ProgramSymbolContext.of_sdv( 'PROGRAM_THAT_EXECUTES_PY_FILE', program_sdvs.interpret_py_source_file_that_must_exist( py_file_conf.path_sdv)) environment_cases = [ { '1': 'one', }, { '1': 'one', '2': 'two', }, ] for with_ignored_exit_code in [False, True]: with self.subTest(with_ignored_exit_code=with_ignored_exit_code): # ACT && ASSERT # integration_check.CHECKER__PARSE_FULL.check_multi( self, args.syntax_for_run( program_args.symbol_ref_command_elements( program_symbol.name), ignore_exit_code=with_ignored_exit_code, ), ParseExpectation( source=asrt_source.is_at_end_of_line(1), symbol_references=program_symbol.references_assertion, ), model_constructor.arbitrary(self), [ NExArr( 'Environment: {}'.format(repr(environment)), PrimAndExeExpectation( ExecutionExpectation( main_result=asrt_string_source. pre_post_freeze__matches_lines( _AssertLinesRepresentSubSetOfDict( environment), 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)), ), arrangement_w_tcds( tcds_contents=py_file_rel_opt_conf. populator_for_relativity_option_root( DirContents([py_file])), symbols=program_symbol.symbol_table, process_execution=ProcessExecutionArrangement( process_execution_settings= ProcessExecutionSettings.with_environ( environment)))) for environment in environment_cases ], )