def from_program(program: ArgumentElements, contents_matcher: List[WithToString], transformation: Optional[WithToString] = None) -> ArgumentElements: following_lines = [list(contents_matcher)] if transformation is not None: following_lines.insert(0, [ab.option( string_transformer.WITH_TRANSFORMED_CONTENTS_OPTION_NAME), transformation ]) return program \ .prepend_to_first_line([ab.option(OUTPUT_FROM_PROGRAM_OPTION_NAME)]) \ .followed_by_lines(following_lines)
def interpret_py_source_line( python_source: WithToString) -> ArgumentElementsRenderer: return ab.sequence([ ab.option(syntax_elements.PYTHON_EXECUTABLE_OPTION_NAME), python_program_execution.PY_ARG_FOR_EXECUTING_SOURCE_ON_COMMAND_LINE, syntax_elements.REMAINING_PART_OF_CURRENT_LINE_AS_LITERAL_MARKER, python_source ])
def program_followed_by_transformation( program_wo_transformation: ArgumentElementsRenderer, transformation: ArgumentElementsRenderer) -> ArgumentElementsRenderer: return primitive_ab.SeparatedByNewLine( program_wo_transformation, primitive_ab.SequenceOfArguments([ ab.option(WITH_TRANSFORMED_CONTENTS_OPTION_NAME), transformation, ]))
def program( program_arg: WithToString, transformation: Optional[WithToString] = None, ) -> ArgumentElements: extra = [] if transformation: extra.append([ ab.option(string_transformer.WITH_TRANSFORMED_CONTENTS_OPTION_NAME, transformation) ]) return ArgumentElements([program_arg], extra)
def test_symbol_references(self): file_to_interpret = fs.File( 'python-logic_symbol_utils.py', python_program_that_exits_with_code_given_as_first_cl_arg) file_to_interpret_symbol = StringConstantSymbolContext( 'file_to_interpret_symbol', file_to_interpret.file_name) python_interpreter_symbol = StringConstantSymbolContext( 'python_interpreter_symbol', sys.executable) exit_code_symbol = StringIntConstantSymbolContext( 'exit_code_symbol', 72) argument = ' {python_interpreter} {interpret_option} {file_to_interpret} "{exit_code}"'.format( python_interpreter=python_interpreter_symbol.name__sym_ref_syntax, interpret_option=args.option( syntax_elements.EXISTING_FILE_OPTION_NAME).as_str, file_to_interpret=file_to_interpret_symbol.name__sym_ref_syntax, exit_code=exit_code_symbol.name__sym_ref_syntax, ) following_line = 'following line' source = remaining_source(argument, [following_line]) arrangement = Arrangement.phase_agnostic( tcds=TcdsArrangement(tcds_contents=TcdsPopulatorForRelOptionType( path_relativities.ALL_REL_OPTIONS_ARG_CONFIG.options. default_option, fs.DirContents([file_to_interpret])), ), symbols=SymbolContext.symbol_table_of_contexts([ python_interpreter_symbol, file_to_interpret_symbol, exit_code_symbol, ]), ) expectation = Expectation.phase_agnostic( source=assert_source(current_line_number=asrt.equals(2), column_index=asrt.equals(0)), symbol_usages=asrt.matches_sequence([ python_interpreter_symbol.usage_assertion__path_or_string( syntax_elements.EXE_FILE_REL_OPTION_ARG_CONF.options. accepted_relativity_variants), file_to_interpret_symbol.usage_assertion__path_or_string( path_relativities.ALL_REL_OPTIONS_ARG_CONFIG.options. accepted_relativity_variants), exit_code_symbol.usage_assertion__w_str_rendering, ]), main_result=result_assertions.equals(exit_code_symbol.int_value, ''), ) parser = sut.embryo_parser('instruction-name') embryo_check.check(self, parser, source, arrangement, expectation)
def test_invalid_executable(self): argument = '/not/an/executable/program {} {} {}'.format(args.option(syntax_elements.EXISTING_FILE_OPTION_NAME), 'exit-with-value-on-command-line.py', 0) self._check_single_line_arguments_with_source_variants( argument, tcds_test.Arrangement( hds_contents_before=hds_case_dir_contents(DirContents([ File('exit-with-value-on-command-line.py', py_pgm_that_exits_with_1st_value_on_command_line(''))]))), tcds_test.Expectation( acton_raises_hard_error=True, ))
def test_validate_should_fail_when_file_to_interpret_does_not_exist(self): for relativity_option_conf in RELATIVITY_OPTIONS: argument = '"{python_interpreter}" {interpret_option} {relativity_option} non-existing-file.py'.format( python_interpreter=sys.executable, interpret_option=args.option( syntax_elements.EXISTING_FILE_OPTION_NAME).as_str, relativity_option=relativity_option_conf.option_argument, ) expectation = _expect_validation_error_and_symbol_usages_of( relativity_option_conf) arrangement = Arrangement.phase_agnostic( symbols=relativity_option_conf.symbols.in_arrangement(), ) with self.subTest(msg='option=' + relativity_option_conf.test_case_description): EXECUTION_CHECKER.check__w_source_variants( self, argument, arrangement, expectation)
def test_success_when_referenced_files_does_exist(self): symbol_name_for_executable_file = 'EXECUTABLE_FILE_SYMBOL_NAME' symbol_name_for_source_file = 'SOURCE_FILE_SYMBOL_NAME' source_file = File.empty('source-file.src') for roc_executable_file in relativity_options_of_symbol( symbol_name_for_executable_file): for roc_source_file in relativity_options_of_symbol( symbol_name_for_source_file): argument = '{relativity_option_executable} {executable_file} {interpret_option}' \ ' {relativity_option_source_file} {source_file}'.format( relativity_option_executable=roc_executable_file.option_argument, relativity_option_source_file=roc_source_file.option_argument, executable_file=EXECUTABLE_FILE_THAT_EXITS_WITH_CODE_0.file_name, interpret_option=args.option(syntax_elements.EXISTING_FILE_OPTION_NAME).as_str, source_file=source_file.file_name, ) expectation = MultiSourceExpectation.phase_agnostic( symbol_usages=asrt.matches_sequence( roc_executable_file.symbols. usage_expectation_assertions() + roc_source_file.symbols.usage_expectation_assertions() ), ) arrangement = Arrangement.phase_agnostic( tcds=TcdsArrangement(tcds_contents=multiple([ roc_executable_file. populator_for_relativity_option_root( fs.DirContents( [EXECUTABLE_FILE_THAT_EXITS_WITH_CODE_0])), roc_source_file.populator_for_relativity_option_root( fs.DirContents([source_file])), ]), ), symbols=SymbolContext.symbol_table_of_contexts( roc_executable_file.symbols.contexts_for_arrangement() + roc_source_file.symbols.contexts_for_arrangement()), ) test_name = 'exe-file-option={}, source-file-option={}'.format( roc_executable_file.test_case_description, roc_source_file.test_case_description, ) with self.subTest(msg=test_name): EXECUTION_CHECKER.check__w_source_variants( self, argument, arrangement, expectation)
def existing_path(path: PathArgument) -> ArgumentElementsRenderer: return ab.sequence__r([ ab.option(syntax_elements.EXISTING_PATH_OPTION_NAME), path, ])
def interpret_py_source_file( py_file: WithToString) -> ArgumentElementsRenderer: return ab.sequence([ ab.option(syntax_elements.PYTHON_EXECUTABLE_OPTION_NAME), ab.option(syntax_elements.EXISTING_FILE_OPTION_NAME), py_file ])
def py_interpreter_command_line() -> ArgumentElementsRenderer: return ab.option(syntax_elements.PYTHON_EXECUTABLE_OPTION_NAME)