def runTest(self): # ARRANGE # expected_file_name = 'quoted file name.src' act_phase_instructions = [instr([str(surrounded_by_hard_quotes(expected_file_name))]), instr([''])] executor_that_records_arguments = CommandExecutorThatRecordsArguments() arrangement = integration_check.arrangement_w_tcds( hds_contents=contents_in(RelHdsOptionType.REL_HDS_ACT, DirContents([ File.empty(expected_file_name)])), process_execution=ProcessExecutionArrangement( os_services=os_services_access.new_for_cmd_exe(executor_that_records_arguments) ) ) expectation = integration_check.Expectation() # ACT # integration_check.check_execution(self, ACTOR_THAT_RUNS_PYTHON_PROGRAM_FILE, act_phase_instructions, arrangement, expectation) # ASSERT # expected_command = asrt_command.matches_command( driver=asrt_command.matches_executable_file_command_driver(asrt.anything_goes()), arguments=asrt.matches_sequence([ asrt_path.str_as_path(asrt_path.name_equals(expected_file_name)) ]) ) expected_command.apply_with_message( self, executor_that_records_arguments.command, 'command', )
def runTest(self): cases = [ NameAndValue( 'no instructions', (), ), NameAndValue( 'single instruction', (instr([]), ), ), NameAndValue( 'two instructions', (instr([]), instr([])), ), ] for case in cases: with self.subTest(case.name): # ACT # actual__as_lines = sut.all_source_code_lines__std_syntax( case.value) actual__as_str = sut.all_source_code_lines_str__std_syntax( case.value) # ASSERT # self.assertEqual(actual__as_lines, [], 'as lines') self.assertEqual(actual__as_str, '', 'as str')
def of_two_instruction(lines: LinesSetup) -> List['InstructionsSetup']: n = len(lines.lines) return [ InstructionsSetup( [ instr(lines.lines[:i]), instr(lines.lines[i:]), ], [ lines.lines[:i], lines.lines[i:], ], lines.expected, ) for i in range(n + 1) ]
def test_symbol_reference_on_command_line_SHOULD_be_reported_and_used_in_execution(self): symbol = NameAndValue('symbol_name', 'symbol value') string_to_print_template = 'constant and {symbol}' expected_output_template = string_to_print_template + '\n' shell_source_line = shell_commands.command_that_prints_to_stdout( string_to_print_template.format(symbol=symbol_reference_syntax_for_name(symbol.name)) ) act_phase_instructions = [instr([shell_command_source_line_for(shell_source_line)])] expected_symbol_references = [ SymbolReference(symbol.name, is_any_data_type()), ] check_execution( self, sut.Parser(), act_phase_instructions, Arrangement( symbol_table=SymbolTable({ symbol.name: data_symbol_utils.string_constant_container(symbol.value) }) ), Expectation( symbol_usages=equals_symbol_references(expected_symbol_references), sub_process_result_from_execute= pr.stdout(asrt.equals(expected_output_template.format(symbol=symbol.value))) ), )
def test_arguments_of_program_file(self): # ARRANGE # exe_file = fs.python_executable_file( 'program-name', py_programs.exit_with(0) ) actor = sut.actor(COMMAND_THAT_RUNS_PYTHON_PROGRAM_FILE) for case in VALIDATION_CASES: with self.subTest(case.name): act_contents = ab.sequence__r([ ab.singleton(exe_file.name), program_arguments.existing_file( path_arguments.RelOptPathArgument('non-existing', case.path_relativity) ) ]) act_instruction = instr([act_contents.as_str]) # ACT & ASSERT # integration_check.check_execution( self, actor, [act_instruction], arrangement_w_tcds( hds_contents=relativity_configurations.ATC_FILE.populator_for_relativity_option_root__hds( fs.DirContents([exe_file]) ) ), Expectation(validation=case.expectation), )
def runTest(self): # ARRANGE # source_file = fs.File.empty('source-file.src') a_dir = fs.Dir.empty('a-dir') interpreter_with_program_that_is_a_dir = command_sdvs.for_executable_file( path_sdvs.constant( path_ddvs.rel_hds(relativity_configurations.INTERPRETER_FILE.relativity_option_rel_hds, path_ddvs.constant_path_part(a_dir.name))), ) command_line = source_file arrangement = arrangement_w_tcds( hds_contents=hds_populators.multiple([ relativity_configurations.ATC_FILE.populator_for_relativity_option_root__hds( fs.DirContents([source_file]) ), relativity_configurations.INTERPRETER_FILE.populator_for_relativity_option_root__hds( fs.DirContents([a_dir]) ), ]) ) expectation = Expectation( validation=ValidationExpectationSvh.fails__pre_sds() ) # ACT & ASSERT # check_execution(self, sut.actor(interpreter_with_program_that_is_a_dir), [instr([command_line.name])], arrangement, expectation)
def runTest(self): # ARRANGE # source_file = fs.File.empty('source-file.src') python_interpreter_symbol = PathSymbolContext.of_sdv( 'PYTHON_INTERPRETER_SYMBOL', path_sdvs.constant( path_ddvs.absolute_path(pathlib.Path(sys.executable)))) interpreter_with_symbol_reference = command_sdvs.for_executable_file( python_interpreter_symbol.reference_sdv__path_or_string( relativity_configurations.INTERPRETER_FILE.relativity)) arrangement = arrangement_w_tcds( symbol_table=python_interpreter_symbol.symbol_table, hds_contents=relativity_configurations.ATC_FILE. populator_for_relativity_option_root__hds( fs.DirContents([source_file]))) expectation = Expectation( symbol_usages=asrt.matches_singleton_sequence( python_interpreter_symbol.reference_assertion__path_or_string), post_sds=PostSdsExpectation.constant( sub_process_result_from_execute=asrt_pr.sub_process_result( exitcode=asrt.equals(0), stdout=asrt.equals(''), stderr=asrt.equals(''), )), ) # ACT & ASSERT # check_execution(self, sut.actor(interpreter_with_symbol_reference), [instr([])], arrangement, expectation)
def runTest(self): # ARRANGE # source_file = fs.File.empty('source-file.src') interpreter_with_non_existing_program_file = command_sdvs.for_executable_file( path_sdvs.constant( path_ddvs.rel_hds(relativity_configurations.INTERPRETER_FILE.relativity_option_rel_hds, path_ddvs.constant_path_part('non-existing'))), ) arrangement = arrangement_w_tcds( hds_contents=relativity_configurations.ATC_FILE.populator_for_relativity_option_root__hds( fs.DirContents([source_file]) ) ) expectation = Expectation( validation=ValidationExpectationSvh.fails__pre_sds() ) # ACT & ASSERT # check_execution(self, sut.actor(interpreter_with_non_existing_program_file), [instr([source_file.name])], arrangement, expectation)
def test_symbol_reference_on_command_line_SHOULD_be_reported_and_used_in_execution( self): symbol = StringConstantSymbolContext('symbol_name', 'symbol value') string_to_print_template = 'constant and {symbol}' expected_output_template = string_to_print_template + '\n' shell_source_line = shell_commands.command_that_prints_to_stdout( string_to_print_template.format( symbol=symbol.name__sym_ref_syntax)) act_phase_instructions = [ instr([shell_command_source_line_for(shell_source_line)]) ] check_execution( self, sut.actor(), act_phase_instructions, arrangement_w_tcds(symbol_table=symbol.symbol_table), Expectation(symbol_usages=asrt.matches_singleton_sequence( symbol.reference_assertion__w_str_rendering), post_sds=PostSdsExpectation.constant( sub_process_result_from_execute=pr.stdout( asrt.equals( expected_output_template.format( symbol=symbol.str_value))))), )
def test_possibility_to_have_sds_path_references_in_argument(self): file_name_of_referenced_file = 'file-name.txt' symbol = ConstantSuffixPathDdvSymbolContext( 'symbol_name', RelOptionType.REL_TMP, file_name_of_referenced_file) executable = 'the-executable' command_line = '{executable} {symbol}'.format( executable=executable, symbol=symbol.name__sym_ref_syntax, ) arrangement = arrangement_w_tcds( hds_contents=relativity_configurations.ATC_FILE. populator_for_relativity_option_root__hds( fs.DirContents([ fs.python_executable_file( executable, PYTHON_PROGRAM_THAT_PRINTS_COMMAND_LINE_ARGUMENTS_ON_SEPARATE_LINES ) ])), symbol_table=symbol.symbol_table) expectation = Expectation( execute=eh_assertions.is_exit_code(0), symbol_usages=asrt.matches_singleton_sequence( symbol.reference_assertion__w_str_rendering), post_sds=PostSdsExpectation.constant( sub_process_result_from_execute=pr.stdout( str_asrt.contains(file_name_of_referenced_file))), ) check_execution(self, sut.actor(), [instr([command_line])], arrangement, expectation)
def runTest(self): absolute_name_of_non_existing_file = str(pathlib.Path().resolve() / 'non' / 'existing' / 'file' / 'oiasdlkv') act_phase_instructions = [instr([absolute_name_of_non_existing_file])] actual = self._do_parse_and_validate_pre_sds(act_phase_instructions) self.assertIs(svh.SuccessOrValidationErrorOrHardErrorEnum.VALIDATION_ERROR, actual.status, 'Validation result')
def check_actor_execution(put: unittest.TestCase, arrangement: Arrangement, expectation: Expectation): instruction = sut.Parser().parse(ARBITRARY_FS_LOCATION_INFO, arrangement.source) configuration_builder = _configuration_builder_with_exception_throwing_act_phase_setup() assert isinstance(instruction, ConfigurationPhaseInstruction) instruction.main(configuration_builder) act_phase_instructions = [instr(arrangement.act_phase_source_lines)] integration_check.check_execution( put, configuration_builder.actor.value, act_phase_instructions, integration_check.arrangement_w_tcds( hds_contents=arrangement.hds_contents, process_execution=ProcessExecutionArrangement( os_services=arrangement.os_services, )), integration_check.Expectation( symbol_usages=expectation.symbol_usages, post_sds=PostSdsExpectation.constant( sub_process_result_from_execute=expectation.sub_process_result_from_execute ), after_execution=expectation.after_execution ) ) expectation.source_after_parse.apply_with_message(put, arrangement.source, 'source after parse')
def test_arguments_of_interpreter(self): # ARRANGE # exe_file = fs.python_executable_file( 'program-name', py_programs.exit_with(0) ) for case in VALIDATION_CASES: with self.subTest(case.name): interpreter_arguments = arguments_sdvs.ref_to_path_that_must_exist( path_sdvs.of_rel_option_with_const_file_name(case.path_relativity, 'non-existing-file') ) actor = sut.actor( command_sdvs.for_executable_file( path_sdvs.constant(path_ddvs.absolute_file_name(sys.executable)), interpreter_arguments ) ) act_instruction = instr([exe_file.name]) # ACT & ASSERT # integration_check.check_execution( self, actor, [act_instruction], arrangement_w_tcds( hds_contents=relativity_configurations.ATC_FILE.populator_for_relativity_option_root__hds( fs.DirContents([exe_file]) ) ), Expectation(validation=case.expectation), )
def runTest(self): existing_file = abs_path_to_interpreter_quoted_for_exactly() act_phase_instructions = [instr([existing_file])] actual = self._do_parse_and_validate_pre_sds(act_phase_instructions) self.assertIs(svh.SuccessOrValidationErrorOrHardErrorEnum.SUCCESS, actual.status, 'Validation result')
def test_executable_file_must_exist(self): act_phase_instructions = [instr(['non-existing-executable'])] arrangement = arrangement_w_tcds() expectation = Expectation( validation=ValidationExpectationSvh.fails__pre_sds()) check_execution(self, sut.actor(), act_phase_instructions, arrangement, expectation)
def test_symbol_value_with_space_SHOULD_be_given_as_single_argument_to_program( self): symbol = StringConstantSymbolContext('symbol_name', 'symbol value with space') expected_output = lines_content([symbol.str_value]) executable_file_name = 'the-executable_file_name' command_line = '{executable_file_name} {symbol}'.format( executable_file_name=executable_file_name, symbol=symbol.name__sym_ref_syntax, ) arrangement = arrangement_w_tcds( hds_contents=relativity_configurations.ATC_FILE. populator_for_relativity_option_root__hds( fs.DirContents([ fs.python_executable_file( executable_file_name, PYTHON_PROGRAM_THAT_PRINTS_COMMAND_LINE_ARGUMENTS_ON_SEPARATE_LINES ) ])), symbol_table=symbol.symbol_table) expectation = Expectation( symbol_usages=asrt.matches_sequence( [symbol.reference_assertion__w_str_rendering]), execute=eh_assertions.is_exit_code(0), post_sds=PostSdsExpectation. constant(sub_process_result_from_execute=pr.stdout( asrt.Equals(expected_output, 'CLI arguments, one per line'))), ) check_execution(self, sut.actor(), [instr([command_line])], arrangement, expectation)
def runTest(self): # ARRANGE # exit_code_from_program = 0 exe_file_in_path = fs.python_executable_file( 'the-program', lines_content(py_program.exit_with_code(exit_code_from_program)), ) program_symbol = StringConstantSymbolContext( 'PROGRAM_NAME_SYMBOL', exe_file_in_path.name, ) program_line = args.system_program_command_line( program_symbol.name__sym_ref_syntax).as_str with tmp_dir_in_path_with_files(DirContents([exe_file_in_path ])) as environ: for source_case in valid_source_variants(program_line): with self.subTest(source_case.name): # ACT & ASSERT # integration_check.check_execution( self, sut.actor(), [instr([program_line])], arrangement_w_tcds( symbol_table=program_symbol.symbol_table, act_exe_input=AtcExeInputArr(environ=environ), ), Expectation( symbol_usages=asrt.matches_singleton_sequence( program_symbol. reference_assertion__string__w_all_indirect_refs_are_strings ), execute=asrt_eh.is_exit_code( exit_code_from_program)), )
def runTest(self): existing_file = abs_path_to_interpreter_quoted_for_exactly() abs_path_and_arguments = ' '.join( [existing_file, 'arg1', '"quoted arg"']) act_phase_instructions = [instr([abs_path_and_arguments])] actual = self._do_parse_and_validate_pre_sds(act_phase_instructions) self.assertIs(svh.SuccessOrValidationErrorOrHardErrorEnum.SUCCESS, actual.status, 'Validation result')
def _instructions_for_executing_py_source( self, py_src: List[str]) -> ContextManager[TestCaseSourceSetup]: exe_file = fs.python_executable_file('the-program', lines_content(py_src)) yield TestCaseSourceSetup( act_phase_instructions=[instr([exe_file.name])], home_act_dir_contents=DirContents([exe_file]))
def test_parse_fails_when_command_is_only_space(self): act_phase_instructions = [ instr([ shell_command_source_line_for(' '), ]) ] with self.assertRaises(ParseException): self._do_parse(act_phase_instructions)
def runTest(self): absolute_name_of_non_existing_file = str(pathlib.Path().resolve() / 'non' / 'existing' / 'file' / 'oia-sdl-kv') act_phase_instructions = [instr([absolute_name_of_non_existing_file])] actual = self._do_parse_and_validate_pre_sds(act_phase_instructions) self.assertIs( svh.SuccessOrValidationErrorOrHardErrorEnum.VALIDATION_ERROR, actual.status, 'Validation result')
def _check_with_exit_code(self, exit_code: int): # ARRANGE # result = SubProcessResult( exitcode=exit_code, stdout='output on stdout', stderr='output on stderr', ) command_py_program = py_program.program_that_prints_and_exits_with_exit_code(result) py_file = fs.File( 'the-program.py', command_py_program, ) program_wo_transformation = ProgramSymbolContext.of_sdv( 'PROGRAM_SYMBOL', program_sdvs.interpret_py_source_file_that_must_exist( path_sdvs.of_rel_option_with_const_file_name( RelOptionType.REL_HDS_CASE, py_file.name, ) ) ) source = args.program( args.symbol_ref_command_line(program_wo_transformation.name), transformation=TO_UPPER_CASE_TRANSFORMER.name__sym_ref_syntax) symbols = [ program_wo_transformation, TO_UPPER_CASE_TRANSFORMER, ] # ACT & ASSERT # integration_check.check_execution( self, sut.actor(), [instr(source.as_arguments.lines)], arrangement_w_tcds( symbol_table=SymbolContext.symbol_table_of_contexts(symbols), hds_contents=hds_populators.contents_in( RelHdsOptionType.REL_HDS_CASE, DirContents([py_file]), ) ), Expectation( symbol_usages=SymbolContext.references_assertion_of_contexts(symbols), execute=asrt_eh.is_exit_code(result.exitcode), post_sds=PostSdsExpectation.constant( sub_process_result_from_execute=asrt_proc_result.matches_proc_result( exit_code=asrt.equals(result.exitcode), stdout=asrt.equals(result.stdout.upper()), stderr=asrt.equals(result.stderr), ) ) ), )
def _check(self, command_line: str, arrangement: Arrangement, expectation: Expectation): check_execution(self, sut.actor(COMMAND_THAT_RUNS_PYTHON_PROGRAM_FILE), [instr([command_line])], arrangement, expectation)
def _check(self, source_line: str, arrangement: Arrangement, expectation: Expectation): check_execution(self, self.parser_that_executes_python_program, [instr([source_line])], arrangement, expectation)
def _check(self, command_line: str, arrangement: Arrangement, expectation: Expectation): check_execution(self, sut.parser(self.command_that_runs_python_file), [instr([command_line])], arrangement, expectation)
def invalid_source_variants( valid_program_line: str ) -> List[NameAndValue[List[ActPhaseInstruction]]]: return [ NameAndValue( 'superfluous line after program', [instr([valid_program_line, 'superfluous'])], ), NameAndValue( 'superfluous line after program and comment line', [ instr([ valid_program_line, LINE_COMMENT_MARKER + ' line comment', 'superfluous' ]) ], ), ]
def runTest(self): # ARRANGE # command_py_program = py_program.exit_with_code(0) py_file = fs.File( 'the-program.py', lines_content(command_py_program), ) program_wo_transformation = ProgramSymbolContext.of_sdv( 'PROGRAM_SYMBOL', program_sdvs.interpret_py_source_file_that_must_exist( path_sdvs.of_rel_option_with_const_file_name( RelOptionType.REL_HDS_CASE, py_file.name, ) ) ) error_message = 'error message from transformer' transformer = StringTransformerPrimitiveSymbolContext( 'HARD_ERROR_TRANSFORMER', string_transformers.model_access_raises_hard_error(error_message), ) source = args.program( args.symbol_ref_command_line(program_wo_transformation.name), transformation=transformer.name__sym_ref_syntax) symbols = [ program_wo_transformation, transformer, ] # ACT & ASSERT # integration_check.check_execution( self, sut.actor(), [instr(source.as_arguments.lines)], arrangement_w_tcds( symbol_table=SymbolContext.symbol_table_of_contexts(symbols), hds_contents=hds_populators.contents_in( RelHdsOptionType.REL_HDS_CASE, DirContents([py_file]), ) ), Expectation( symbol_usages=SymbolContext.references_assertion_of_contexts(symbols), execute=asrt_eh.matches_hard_error( asrt_failure_details.is_failure_message_matching__td( asrt_text_doc.is_string_for_test_that_equals(error_message) ) ), ), )
def runTest(self): # ARRANGE # atc_file_name = 'existing-file.py' arg_1 = 'un-quoted' arg_2 = 'single quoted' arg_3 = 'double quoted' act_line_1 = '{} {} {} {}'.format( atc_file_name, arg_1, surrounded_by_hard_quotes(arg_2), surrounded_by_soft_quotes(arg_3), ) atc_line_2 = '' act_phase_instructions = [instr([act_line_1]), instr([atc_line_2])] executor_that_records_arguments = CommandExecutorThatRecordsArguments() arrangement = integration_check.arrangement_w_tcds( hds_contents=contents_in(RelHdsOptionType.REL_HDS_ACT, DirContents([ File.empty(atc_file_name)])), process_execution=ProcessExecutionArrangement( os_services=os_services_access.new_for_cmd_exe(executor_that_records_arguments) ) ) expected_command = asrt_command.matches_command( driver=asrt_command.matches_executable_file_command_driver(asrt.anything_goes()), arguments=asrt.matches_sequence([ asrt_path.str_as_path(asrt_path.name_equals(atc_file_name)), asrt.equals(arg_1), asrt.equals(arg_2), asrt.equals(arg_3), ]) ) expectation = integration_check.Expectation( after_execution=ExecutedCommandAssertion(executor_that_records_arguments, lambda tcds: expected_command) ) # ACT & ASSERT # integration_check.check_execution(self, ACTOR_THAT_RUNS_PYTHON_PROGRAM_FILE, act_phase_instructions, arrangement, expectation)
def test_executable_file_must_be_executable(self): executable_file_name = 'existing-executable' act_phase_instructions = [instr([executable_file_name])] arrangement = arrangement_w_tcds( hds_contents=relativity_configurations.ATC_FILE. populator_for_relativity_option_root__hds( fs.DirContents([File.empty(executable_file_name)]))) expectation = Expectation( validation=ValidationExpectationSvh.fails__pre_sds()) check_execution(self, sut.actor(), act_phase_instructions, arrangement, expectation)
def runTest(self): act_phase_instructions = [instr(['system-under-test'])] with home_directory_structure( contents=contents_in(RelHomeOptionType.REL_HOME_ACT, fs.DirContents([ fs.executable_file('system-under-test')]))) as hds: environment = InstructionEnvironmentForPreSdsStep(hds, {}) executor = self.actor.parse(act_phase_instructions) actual = executor.validate_pre_sds(environment) self.assertIs(svh.SuccessOrValidationErrorOrHardErrorEnum.SUCCESS, actual.status, 'Validation result')
def runTest(self): existing_file = abs_path_to_interpreter_quoted_for_exactly() act_phase_instructions = [ instr([ '', ' ', LINE_COMMENT_MARKER + ' line comment text', existing_file, LINE_COMMENT_MARKER + ' line comment text', '' ]) ] actual = self._do_parse_and_validate_pre_sds(act_phase_instructions) self.assertIs(svh.SuccessOrValidationErrorOrHardErrorEnum.SUCCESS, actual.status, 'Validation result')
def test_succeeds_when_there_is_exactly_one_statement_but_surrounded_by_empty_and_comment_lines(self): existing_file = abs_path_to_interpreter_quoted_for_exactly() act_phase_instructions = [instr(['', ' ', LINE_COMMENT_MARKER + ' line comment text', shell_command_source_line_for(existing_file), LINE_COMMENT_MARKER + ' line comment text', ''])] actual = self._do_parse_and_validate(act_phase_instructions) self.assertIs(svh.SuccessOrValidationErrorOrHardErrorEnum.SUCCESS, actual.status, 'Validation result')
def _instructions_for_executing_py_source( self, py_src: List[str]) -> ContextManager[TestCaseSourceSetup]: exe_file_in_path = fs.python_executable_file('the-program', lines_content(py_src)) with tmp_dir_in_path_with_files(DirContents([exe_file_in_path ])) as environ: program_line = args.system_program_command_line( exe_file_in_path.name).as_str yield TestCaseSourceSetup( act_phase_instructions=[instr([program_line])], environ=environ)
def runTest(self): # ARRANGE # program_line = args.system_program_command_line( 'non-existing-system-program').as_str # ACT & ASSERT # integration_check.check_execution( self, sut.actor(), [instr([program_line])], arrangement_w_tcds(), Expectation.hard_error_from_execute(), )
def runTest(self): act_phase_instructions = [instr(['system-under-test'])] with home_directory_structure(contents=contents_in( RelHdsOptionType.REL_HDS_ACT, fs.DirContents([fs.executable_file('system-under-test') ]))) as hds: environment = InstructionEnvironmentPreSdsBuilder.of_empty_env( hds=hds).build executor = self.actor.parse(act_phase_instructions) actual = executor.validate_pre_sds(environment) self.assertIs(svh.SuccessOrValidationErrorOrHardErrorEnum.SUCCESS, actual.status, 'Validation result')
def runTest(self): expected_output = pr.sub_process_result(exitcode=asrt.equals(0), stdout=asrt.equals(''), stderr=asrt.equals('')) cases = [ ( 'no act phase contents', [] ), ( 'act phase contents of just comments', [instr([LINE_COMMENT_MARKER + ' a comment'])] ), ( 'act phase contents of just comments and empty lines', [instr([LINE_COMMENT_MARKER + ' a comment', '', LINE_COMMENT_MARKER + ' a second comment', ])] ), ( 'act phase contents with non-comment and non-empty line', [instr(['not a comment and not empty', ])] ), ] actor = sut.actor() for case_name, act_phase_instructions in cases: with self.subTest(case_name=case_name): arrangement = arrangement_w_tcds() expectation = Expectation( post_sds=PostSdsExpectation.constant( sub_process_result_from_execute=expected_output ) ) check_execution(self, actor, act_phase_instructions, arrangement, expectation)
def test_multiple_symbol_references_in_executable(self): sub_dir_of_home = 'sub-dir' dir_symbol = NameAndValue('dir_symbol_name', file_refs.rel_home_act(file_refs.constant_path_part(sub_dir_of_home))) executable_file_name_symbol = NameAndValue('executable_file_name_symbol_name', 'the-executable-file') argument = 'argument_string' expected_output = lines_content([argument]) command_line = '{dir}/{file_name} {argument} '.format( dir=symbol_reference_syntax_for_name(dir_symbol.name), file_name=symbol_reference_syntax_for_name(executable_file_name_symbol.name), argument=argument, ) executable_file = fs.python_executable_file( executable_file_name_symbol.value, PYTHON_PROGRAM_THAT_PRINTS_COMMAND_LINE_ARGUMENTS_ON_SEPARATE_LINES) arrangement = Arrangement( hds_contents=contents_in(RelHomeOptionType.REL_HOME_ACT, fs.DirContents([ fs.Dir(sub_dir_of_home, [executable_file]) ])), symbol_table=SymbolTable({ dir_symbol.name: su.file_ref_constant_container(dir_symbol.value), executable_file_name_symbol.name: su.string_constant_container(executable_file_name_symbol.value), }) ) expectation = Expectation( result_of_execute=eh_assertions.is_exit_code(0), sub_process_result_from_execute=pr.stdout(asrt.Equals(expected_output, 'CLI arguments, one per line')), symbol_usages=equals_symbol_references([ SymbolReference(dir_symbol.name, path_or_string_reference_restrictions(PATH_RELATIVITY_VARIANTS_FOR_FILE_TO_RUN)), SymbolReference(executable_file_name_symbol.name, PATH_COMPONENT_STRING_REFERENCES_RESTRICTION), ]), ) check_execution(self, sut.Parser(), [instr([command_line])], arrangement, expectation)
def test_executable_file_must_exist(self): act_phase_instructions = [ instr(['non-existing-executable']) ] arrangement = Arrangement() expectation = Expectation( result_of_validate_pre_sds=svh_assertions.is_validation_error() ) check_execution(self, sut.Parser(), act_phase_instructions, arrangement, expectation)
def test_succeeds_when_there_is_exactly_one_statement_but_surrounded_by_empty_and_comment_lines( self): existing_file = abs_path_to_interpreter_quoted_for_exactly() act_phase_instructions = [ instr([ '', ' ', LINE_COMMENT_MARKER + ' line comment text', shell_command_source_line_for(existing_file), LINE_COMMENT_MARKER + ' line comment text', '' ]) ] actual = self._do_parse_and_validate(act_phase_instructions) self.assertIs(svh.SuccessOrValidationErrorOrHardErrorEnum.SUCCESS, actual.status, 'Validation result')
def runTest(self): expected_output = pr.sub_process_result(exitcode=asrt.equals(0), stdout=asrt.equals(''), stderr=asrt.equals('')) cases = [ ( 'no act phase contents', [] ), ( 'act phase contents of just comments', [instr([LINE_COMMENT_MARKER + ' a comment'])] ), ( 'act phase contents of just comments and empty lines', [instr([LINE_COMMENT_MARKER + ' a comment', '', LINE_COMMENT_MARKER + ' a second comment', ])] ), ( 'act phase contents with non-comment and non-empty line', [instr(['not a comment and not empty', ])] ), ] executor_parser = sut.Parser() for case_name, act_phase_instructions in cases: with self.subTest(case_name=case_name): arrangement = Arrangement() expectation = Expectation(sub_process_result_from_execute=expected_output) check_execution(self, executor_parser, act_phase_instructions, arrangement, expectation)
def test_executable_file_must_be_executable(self): executable_file_name = 'existing-executable' act_phase_instructions = [ instr([executable_file_name]) ] arrangement = Arrangement( hds_contents=contents_in(RelHomeOptionType.REL_HOME_ACT, fs.DirContents([fs.empty_file(executable_file_name)])) ) expectation = Expectation( result_of_validate_pre_sds=svh_assertions.is_validation_error() ) check_execution(self, sut.Parser(), act_phase_instructions, arrangement, expectation)
def runTest(self): language_setup = SourceInterpreterSetup(_SourceFileManagerWithNonExistingInterpreter()) actor = sut.actor(language_setup) source = [instr(['print(1)'])] expected_file_name = language_setup.base_name_from_stem( sut.ActSourceFileNameGeneratorForSourceInterpreterSetup.FILE_NAME_STEM) exit_code_or_hard_error = check_execution( self, actor, source, Arrangement(), Expectation( result_of_execute=eh_assertions.is_hard_error, side_effects_on_files_after_execute=test_case_dir_contains_exactly(DirContents([ File(expected_file_name, lines_content(['print(1)'])) ])))) self.assertTrue(exit_code_or_hard_error.is_hard_error, 'Expecting a HARD ERROR')
def test_string_symbol_reference_in_executable_and_argument(self): symbol_for_executable = NameAndValue('executable_symbol_name', 'the-executable') argument_symbol = NameAndValue('argument_symbol_name', 'string-constant') expected_output = lines_content([argument_symbol.value]) command_line = '{executable} {argument} '.format( executable=symbol_reference_syntax_for_name(symbol_for_executable.name), argument=symbol_reference_syntax_for_name(argument_symbol.name), ) arrangement = Arrangement( hds_contents=contents_in(RelHomeOptionType.REL_HOME_ACT, fs.DirContents([ fs.python_executable_file( symbol_for_executable.value, PYTHON_PROGRAM_THAT_PRINTS_COMMAND_LINE_ARGUMENTS_ON_SEPARATE_LINES) ])), symbol_table=SymbolTable({ symbol_for_executable.name: su.string_constant_container(symbol_for_executable.value), argument_symbol.name: su.string_constant_container(argument_symbol.value), }) ) expectation = Expectation( result_of_execute=eh_assertions.is_exit_code(0), sub_process_result_from_execute=pr.stdout(asrt.Equals(expected_output, 'CLI arguments, one per line')), symbol_usages=equals_symbol_references([ SymbolReference(symbol_for_executable.name, path_or_string_reference_restrictions(PATH_RELATIVITY_VARIANTS_FOR_FILE_TO_RUN)), SymbolReference(argument_symbol.name, is_any_data_type()), ]), ) check_execution(self, sut.Parser(), [instr([command_line])], arrangement, expectation)
def test_symbol_reference_in_arguments(self): list_symbol = NameAndValue('list_symbol_name', ['first element', 'second element']) string_constant = 'string-constant' expected_output = lines_content(['string-constant'] + list_symbol.value) executable = 'the-executable' command_line = '{executable} {string_constant} {list_symbol}'.format( executable=executable, string_constant=string_constant, list_symbol=symbol_reference_syntax_for_name(list_symbol.name), ) arrangement = Arrangement( hds_contents=contents_in(RelHomeOptionType.REL_HOME_ACT, fs.DirContents([ fs.python_executable_file( executable, PYTHON_PROGRAM_THAT_PRINTS_COMMAND_LINE_ARGUMENTS_ON_SEPARATE_LINES) ])), symbol_table=SymbolTable({ list_symbol.name: su.list_value_constant_container(lv.list_value_of_string_constants(list_symbol.value)), }) ) expectation = Expectation( result_of_execute=eh_assertions.is_exit_code(0), sub_process_result_from_execute=pr.stdout(asrt.Equals(expected_output, 'CLI arguments, one per line')), symbol_usages=equals_symbol_references( [SymbolReference(list_symbol.name, is_any_data_type())] ) ) check_execution(self, sut.Parser(), [instr([command_line])], arrangement, expectation)
def runTest(self): executor_constructor = sut.Parser() act_phase_instructions = [ instr(['system-under-test first-argument "quoted argument"']) ] arrangement = Arrangement( hds_contents=contents_in(RelHomeOptionType.REL_HOME_ACT, fs.DirContents([ fs.python_executable_file( 'system-under-test', PYTHON_PROGRAM_THAT_PRINTS_COMMAND_LINE_ARGUMENTS_ON_SEPARATE_LINES) ]))) expected_output = lines_content(['first-argument', 'quoted argument']) expectation = Expectation(result_of_execute=eh_assertions.is_exit_code(0), sub_process_result_from_execute=pr.stdout(asrt.Equals(expected_output, 'CLI arguments, one per line'))) check_execution(self, executor_constructor, act_phase_instructions, arrangement, expectation)
def runTest(self): cases = [ ( 'no act phase contents', [] ), ( 'act phase contents with a line that has symbol reference syntax', [instr([symbol_reference_syntax_for_name('symbol_name')])] ), ] executor_parser = sut.Parser() for case_name, act_phase_instructions in cases: with self.subTest(case_name=case_name): arrangement = Arrangement() expectation = Expectation(symbol_usages=asrt.is_empty_sequence) check_execution(self, executor_parser, act_phase_instructions, arrangement, expectation)
def test_symbol_value_with_space_SHOULD_be_given_as_single_argument_to_program(self): symbol = NameAndValue('symbol_name', 'symbol value with space') expected_output = lines_content([symbol.value]) executable_file_name = 'the-executable_file_name' command_line = '{executable_file_name} {symbol}'.format( executable_file_name=executable_file_name, symbol=symbol_reference_syntax_for_name(symbol.name), ) arrangement = Arrangement( hds_contents=contents_in(RelHomeOptionType.REL_HOME_ACT, fs.DirContents([ fs.python_executable_file( executable_file_name, PYTHON_PROGRAM_THAT_PRINTS_COMMAND_LINE_ARGUMENTS_ON_SEPARATE_LINES) ])), symbol_table=SymbolTable({ symbol.name: su.string_constant_container(symbol.value), }) ) expectation = Expectation( result_of_execute=eh_assertions.is_exit_code(0), sub_process_result_from_execute=pr.stdout(asrt.Equals(expected_output, 'CLI arguments, one per line')), symbol_usages=equals_symbol_references( [SymbolReference(symbol.name, is_any_data_type())] ) ) check_execution(self, sut.Parser(), [instr([command_line])], arrangement, expectation)
def test_possibility_to_have_sds_file_references_in_argument(self): file_name_of_referenced_file = 'file-name.txt' symbol = NameAndValue('symbol_name', file_refs.of_rel_option(RelOptionType.REL_TMP, file_refs.constant_path_part(file_name_of_referenced_file))) executable = 'the-executable' command_line = '{executable} {symbol}'.format( executable=executable, symbol=symbol_reference_syntax_for_name(symbol.name), ) arrangement = Arrangement( hds_contents=contents_in(RelHomeOptionType.REL_HOME_ACT, fs.DirContents([ fs.python_executable_file( executable, PYTHON_PROGRAM_THAT_PRINTS_COMMAND_LINE_ARGUMENTS_ON_SEPARATE_LINES) ])), symbol_table=SymbolTable({ symbol.name: su.file_ref_constant_container(symbol.value), }) ) expectation = Expectation( result_of_execute=eh_assertions.is_exit_code(0), sub_process_result_from_execute=pr.stdout(str_asrt.contains(file_name_of_referenced_file)), symbol_usages=equals_symbol_references( [SymbolReference(symbol.name, is_any_data_type())] ) ) check_execution(self, sut.Parser(), [instr([command_line])], arrangement, expectation)
def runTest(self): existing_file = abs_path_to_interpreter_quoted_for_exactly() act_phase_instructions = [instr([existing_file, existing_file])] with self.assertRaises(ParseException): self._do_parse(act_phase_instructions)
def runTest(self): act_phase_instructions = [instr([''])] with self.assertRaises(ParseException): self._do_parse(act_phase_instructions)
def program_that_sleeps_at_least(self, number_of_seconds: int) -> TestCaseSourceSetup: program_lines = py_program.program_that_sleeps_at_least_and_then_exists_with_zero_exit_status(number_of_seconds) yield TestCaseSourceSetup( act_phase_instructions=[instr(['sut.py'])], home_act_dir_contents=DirContents([File('sut.py', lines_content(program_lines))]))
def _instructions_for(statements: list) -> list: return list(map(lambda stmt: instr([stmt]), statements))
def test_parse_fails_when_command_is_only_space(self): act_phase_instructions = [instr([shell_command_source_line_for(' '), ])] with self.assertRaises(ParseException): self._do_parse(act_phase_instructions)
def _instructions_for_file_in_home_act_dir(hds: HomeDirectoryStructure, statements: list) -> list: sut_path = hds.act_dir / 'sut.py' with sut_path.open('w') as f: f.write(lines_content(statements)) return [instr(['sut.py'])]
def _instructions_for_executing_py_file(src_path: pathlib.Path) -> list: cmd = py_exe.command_line_for_interpreting(src_path) return [instr([cmd])]
def runTest(self): act_phase_instructions = [instr(['name-of-non-existing-file'])] actual = self._do_parse_and_validate_pre_sds(act_phase_instructions) self.assertIs(svh.SuccessOrValidationErrorOrHardErrorEnum.VALIDATION_ERROR, actual.status, 'Validation result')