def runTest(self): # ARRANGE # dst_file_name = 'dst-file_name-file.txt' src_file = fs.File('src-file_name-file.txt', 'contents') home_dir_contents = fs.DirContents([src_file]) expected_destination_dir_contents = fs.DirContents( [fs.File(dst_file_name, src_file.contents)]) src_rel_option = defs.ARBITRARY_SRC_REL_OPT dst_rel_option = defs.ARBITRARY_DST_REL_OPT # ACT & ASSERT # self.conf.run_test( self, args.copy(src_rel_option.path_argument_of_rel_name(src_file.name), dst_rel_option.path_argument_of_rel_name( dst_file_name)).as_remaining_source, self.conf.arrangement(hds_contents=src_rel_option. populator_for_relativity_option_root__hds( home_dir_contents), ), self.conf.expect_success( main_side_effects_on_sds=sds_contents_check. non_hds_dir_contains_exactly( dst_rel_option.root_dir__non_hds, expected_destination_dir_contents), ), )
def test_dir(self): # ARRANGE # to_append_file = fs.File('file-to-append', 'contents of file to append') pre_existing_file = fs.File('pre-existing', 'contents of pre existing file') append_single_file__contents_syntax = abs_stx.DirContentsExplicitAbsStx( ModificationType.APPEND, abs_stx.LiteralFilesSourceAbsStx([ abs_stx.regular_file_spec( file_name_arg(to_append_file.name), abs_stx.FileContentsExplicitAbsStx( ModificationType.CREATE, str_src_abs_stx.StringSourceOfStringAbsStx.of_str( to_append_file.contents, QuoteType.HARD))) ])) target_dir_name = 'destination-dir' contents_cases: Sequence[AppendDirContentsCase] = [ AppendDirContentsCase( 'empty', [], [to_append_file], ), AppendDirContentsCase( 'non-empty', [pre_existing_file], [pre_existing_file, to_append_file], ), ] for target_location_case in target_locations(): syntax = abs_stx.LiteralFilesSourceAbsStx([ abs_stx.dir_spec( file_name_arg( target_location_case.target_rel_path(target_dir_name)), append_single_file__contents_syntax, ) ]) for contents_case in contents_cases: # ACT & ASSERT # integration_check.CHECKER.check__abs_stx__layout__std_source_variants( self, syntax, [ target_location_case.file_for_leaf( contents_case.original(target_dir_name)) ], arrangement_w_tcds(), MultiSourceExpectation(execution=ExecutionExpectation( main_result=asrt_fs.dir_contains_exactly_2([ target_location_case.file_for_leaf( fs.Dir( target_dir_name, contents_case. expected_contents_after_modification)) ]))), sub_test_identifiers={ 'contents': contents_case.name, 'target_location': target_location_case.name })
def runTest(self): # ARRANGE # src_file = fs.File('source-file.txt', 'contents of source file') src_rel_opt_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE) expected_file = fs.File('a-file-name.txt', src_file.contents.upper()) dst_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_ACT) to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'TRANSFORMER_SYMBOL', to_uppercase(), ) symbols = to_upper_transformer.symbol_table transformed_file_syntax = string_source_abs_stx.TransformedStringSourceAbsStx( string_source_abs_stx.StringSourceOfFileAbsStx( src_rel_opt_conf.path_abs_stx_of_name(src_file.name)), to_upper_transformer.abstract_syntax, ) instruction_syntax = instr_abs_stx.create_w_explicit_contents( dst_rel_opt_conf.path_abs_stx_of_name(expected_file.name), transformed_file_syntax, ) expected_non_hds_contents = dst_rel_opt_conf.assert_root_dir_contains_exactly( fs.DirContents([expected_file])) instruction_checker = self.conf.instruction_checker parser = self.conf.parser() for source_case in equivalent_source_variants__with_source_check__consume_last_line__abs_stx( instruction_syntax): with self.subTest(source_case.name): source = source_case.value.source # ACT # instruction = parser.parse(ARBITRARY_FS_LOCATION_INFO, source) # ASSERT # source_case.value.expectation.apply_with_message( self, source, 'source-after-parse') # ACT & ASSERT # instruction_checker.check( self, instruction, self.conf.arrangement( pre_contents_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR, tcds_contents=src_rel_opt_conf. populator_for_relativity_option_root( DirContents([src_file])), symbols=symbols, ), self.conf.expect_success( symbol_usages=asrt.matches_sequence([ to_upper_transformer.reference_assertion, ]), main_side_effects_on_sds=expected_non_hds_contents, ))
def runTest(self): to_upper_transformer = NameAndValue( 'TRANSFORMER_SYMBOL', StringTransformerSdvConstantTestImpl(to_uppercase())) src_file = fs.File('src-file.txt', 'contents of source file') src_file_symbol = NameAndValue('SRC_FILE_SYMBOL', src_file.name) expected_dst_file = fs.File('dst-file-name.txt', src_file.contents.upper()) dst_file_symbol = NameAndValue('DST_FILE_SYMBOL', expected_dst_file.name) transformed_file_syntax = string_source_abs_stx.TransformedStringSourceAbsStx( string_source_abs_stx.StringSourceOfFileAbsStx( PathSymbolReferenceAbsStx(src_file_symbol.name)), str_trans_abs_stx.StringTransformerSymbolReferenceAbsStx( to_upper_transformer.name)) instruction_syntax = instr_abs_stx.create_w_explicit_contents( PathSymbolReferenceAbsStx(dst_file_symbol.name), transformed_file_syntax, ) # ACT # for layout_case in tokens_layout.STANDARD_LAYOUT_SPECS: with self.subTest(layout_case.name): instruction = self.conf.parse_checker.parse__abs_stx( self, instruction_syntax, layout_case.value) assert isinstance( instruction, TestCaseInstructionWithSymbols) # Sanity check # ASSERT # expected_symbol_usages = [ asrt_sym_ref.matches_reference_2( dst_file_symbol.name, asrt_w_str_rend_rest.equals__w_str_rendering( path_or_string_reference_restrictions( new_file.REL_OPT_ARG_CONF.options. accepted_relativity_variants))), asrt_sym_ref.matches_reference_2( src_file_symbol.name, asrt_w_str_rend_rest.equals__w_str_rendering( path_or_string_reference_restrictions( src_rel_opt_arg_conf_for_phase( self.conf.phase_is_after_act()).options. accepted_relativity_variants))), is_reference_to_string_transformer__usage( to_upper_transformer.name), ] expected_symbol_references = asrt.matches_sequence( expected_symbol_usages) expected_symbol_references.apply_without_message( self, instruction.symbol_usages())
def test__with_transformer(self): # ARRANGE # src_file = fs.File('source-file.txt', 'contents of source file') expected_file = fs.File('a-file-name.txt', src_file.contents.upper()) to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'TRANSFORMER_SYMBOL', string_transformers.to_uppercase(), ) src_rel_opt_conf = ALLOWED_SRC_FILE_RELATIVITIES__BEFORE_ACT[0] dst_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_ACT) expected_non_hds_contents = self._expected_non_hds_contents( dst_rel_opt_conf, expected_file, src_rel_opt_conf, src_file, ) transformed_file_contents_abs_stx = string_source_abs_stx.TransformedStringSourceAbsStx( string_source_abs_stx.StringSourceOfFileAbsStx( src_rel_opt_conf.path_abs_stx_of_name(src_file.name)), to_upper_transformer.abstract_syntax, ) instruction_syntax = instr_abs_stx.create_w_explicit_contents( dst_rel_opt_conf.path_abs_stx_of_name(expected_file.file_name), transformed_file_contents_abs_stx, ) symbols = to_upper_transformer.symbol_table for phase_is_after_act in [False, True]: checker = integration_check.checker(phase_is_after_act) with self.subTest(phase_is_after_act=phase_is_after_act): # ACT & ASSERT # checker.check__abs_stx__std_layouts_and_source_variants( self, instruction_syntax, Arrangement.phase_agnostic( tcds=TcdsArrangement( pre_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, tcds_contents=src_rel_opt_conf. populator_for_relativity_option_root( DirContents([src_file])), ), symbols=symbols, ), MultiSourceExpectation.phase_agnostic( main_result=IS_SUCCESS, main_side_effects_on_sds=expected_non_hds_contents, symbol_usages=to_upper_transformer.usages_assertion, ))
class TestMultiplePopulators(unittest.TestCase): first_file = fs.File('first-file-name.txt', 'contents of first file') second_file = fs.File('second-file-name.txt', 'contents of second file') def test_two_populators_populate_hds(self): # ARRANGE # expected_dir_contents = fs.DirContents([self.first_file, self.second_file]) first_populator = sut.hds_case_dir_contents(fs.DirContents([self.first_file])) second_populator = sut.hds_case_dir_contents(fs.DirContents([self.second_file])) populator = sut.multiple([first_populator, second_populator]) expectation = f_asrt.dir_contains_exactly(expected_dir_contents) with home_directory_structure() as hds: # ACT # populator.populate_hds(hds) # ASSERT # expectation.apply_with_message(self, hds.case_dir, 'contents of HDS/case dir') def test_two_populators_populate_tcds(self): # ARRANGE # expected_dir_contents = fs.DirContents([self.first_file, self.second_file]) first_populator = sut.hds_case_dir_contents(fs.DirContents([self.first_file])) second_populator = sut.hds_case_dir_contents(fs.DirContents([self.second_file])) populator = sut.multiple([first_populator, second_populator]) expectation = f_asrt.dir_contains_exactly(expected_dir_contents) sds = fake_sds() with home_directory_structure() as hds: tcds = TestCaseDs(hds, sds) # ACT # populator.populate_tcds(tcds) # ASSERT # expectation.apply_with_message(self, hds.case_dir, 'contents of HDS/case dir') def test_no_populators_populate_hds(self): # ARRANGE # expected_dir_contents = fs.DirContents([]) populator = sut.multiple([]) expectation = f_asrt.dir_contains_exactly(expected_dir_contents) with home_directory_structure() as hds: # ACT # populator.populate_hds(hds) # ASSERT # expectation.apply_with_message(self, hds.case_dir, 'contents of HDS/case dir')
def test_contents_from_here_doc(self): # ARRANGE # string_value = rich_str_abs_stx.HereDocAbsStx( 'single line in here doc\n') expected_file = fs.File('a-file-name.txt', string_value.value) rel_opt_conf = ARBITRARY_ALLOWED_DST_FILE_RELATIVITY dst_path = rel_opt_conf.path_abs_stx_of_name(expected_file.name) instruction_syntax = instr_abs_stx.create_w_explicit_contents( dst_path, string_source_abs_stx.StringSourceOfStringAbsStx(string_value), ) for phase_is_after_act in [False, True]: checker = integration_check.checker(phase_is_after_act) with self.subTest(phase_is_after_act=phase_is_after_act): # ACT & ASSERT # checker.check__abs_stx__std_layouts_and_source_variants( self, instruction_syntax, Arrangement.phase_agnostic(tcds=TcdsArrangement( pre_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, ), ), MultiSourceExpectation.phase_agnostic( main_result=IS_SUCCESS, side_effects_on_hds=f_asrt.dir_is_empty(), symbol_usages=asrt.is_empty_sequence, main_side_effects_on_sds=non_hds_dir_contains_exactly( rel_opt_conf.root_dir__non_hds, fs.DirContents([expected_file])), ))
def test_symbol_reference_in_dst_file_argument(self): dst_path_symbol = ConstantSuffixPathDdvSymbolContext( 'dst_path_symbol', RelOptionType.REL_ACT, 'dst-file.txt', ACCEPTED_DST_RELATIVITY_VARIANTS) string_value = rich_str_abs_stx.HereDocAbsStx( 'single line in here doc\n') instruction_syntax = instr_abs_stx.create_w_explicit_contents( dst_path_symbol.abstract_syntax, string_source_abs_stx.StringSourceOfStringAbsStx(string_value), ) expected_file = fs.File(dst_path_symbol.path_suffix, string_value.value) integration_check.CHECKER__BEFORE_ACT.check__abs_stx( self, instruction_syntax, Arrangement.phase_agnostic( tcds=TcdsArrangement( pre_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, ), symbols=dst_path_symbol.symbol_table, ), Expectation.phase_agnostic( main_result=IS_SUCCESS, symbol_usages=asrt.matches_singleton_sequence( dst_path_symbol.reference_assertion), main_side_effects_on_sds=dir_contains_exactly( dst_path_symbol.rel_option_type, fs.DirContents([expected_file])), ))
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
def test_without_transformer(self): # ARRANGE # src_file = fs.File('source-file.txt', 'contents of source file') src_rel_opt_conf = conf_rel_any(RelOptionType.REL_TMP) checker = integration_check.checker(parse_check.rel_opts_conf_of_single(src_rel_opt_conf.relativity)) file_contents_abs_stx = string_source_abs_stx.StringSourceOfFileAbsStx( src_rel_opt_conf.path_abs_stx_of_name(src_file.name) ) # ACT & ASSERT # checker.check__abs_stx__layouts__std_source_variants__wo_input( self, OptionallyOnNewLine(file_contents_abs_stx), arrangement_w_tcds( tcds_contents=src_rel_opt_conf.populator_for_relativity_option_root( DirContents([src_file])) ), MultiSourceExpectation.of_prim__const( asrt_string_source.pre_post_freeze__matches_str__const( src_file.contents, may_depend_on_external_resources=True, ), symbol_references=asrt.is_empty_sequence, ) )
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 __init__(self, contents: str): super().__init__(_dir_file_space_that_must_not_be_used_getter) self.contents = contents file_with_contents = fs.File('model-file.txt', self.contents) relativity = RelOptionType.REL_TMP self._ddv = path_ddvs.simple_of_rel_option(relativity, file_with_contents.name) self._tcds_populator = tcds_populators.TcdsPopulatorForRelOptionType( relativity, fs.DirContents([file_with_contents]))
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 _successful__append() -> ContentsCase: contents_symbol = StringSourceSymbolContextOfPrimitiveConstant( 'CONTENTS_SYMBOL', '<added contents>', ) original_dst_file = fs.File('regular-file', 'original contents') modified_dst_file = fs.File(original_dst_file.name, original_dst_file.contents + contents_symbol.contents_str) return ContentsCase( 'append', original_dst_file.name, fs_abs_stx.FileContentsExplicitAbsStx( ModificationType.APPEND, contents_symbol.abstract_syntax, ), symbols=[contents_symbol], pre_existing_files=[original_dst_file], expected_files=[modified_dst_file], )
def test_all_relativities__without_transformer(self): # ARRANGE # src_file = fs.File('source-file.txt', 'contents of source file') expected_file = fs.File('a-file-name.txt', src_file.contents) for phase_is_after_act in [False, True]: checker = integration_check.checker(phase_is_after_act) for dst_rel_opt_conf in ALLOWED_DST_FILE_RELATIVITIES: for src_rel_opt_conf in accepted_src_file_relativities( phase_is_after_act): file_contents_abs_stx = string_source_abs_stx.StringSourceOfFileAbsStx( src_rel_opt_conf.path_abs_stx_of_name(src_file.name)) instruction_syntax = instr_abs_stx.create_w_explicit_contents( dst_rel_opt_conf.path_abs_stx_of_name( expected_file.file_name), file_contents_abs_stx, ) expected_non_hds_contents = self._expected_non_hds_contents( dst_rel_opt_conf, expected_file, src_rel_opt_conf, src_file) with self.subTest(phase_is_after_act=phase_is_after_act, relativity_dst=dst_rel_opt_conf. option_argument.as_str, relativity_src=src_rel_opt_conf. option_argument.as_str): # ACT & ASSERT # checker.check__abs_stx__std_layouts_and_source_variants( self, instruction_syntax, Arrangement.phase_agnostic(tcds=TcdsArrangement( pre_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, tcds_contents=src_rel_opt_conf. populator_for_relativity_option_root( DirContents([src_file]))), ), MultiSourceExpectation.phase_agnostic( main_result=IS_SUCCESS, symbol_usages=asrt.is_empty_sequence, main_side_effects_on_sds= expected_non_hds_contents, ))
def runTest(self): text_printed_by_program = 'single line of output' expected_file_contents = text_printed_by_program.upper() expected_file = fs.File('dst-file.txt', expected_file_contents) to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'TO_UPPER_CASE', to_uppercase(), ) symbols = to_upper_transformer.symbol_table dst_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_TMP) program_syntax = string_source_abs_stx.StringSourceOfProgramAbsStx( ProcOutputFile.STDOUT, program_abs_stx.FullProgramAbsStx( program_abs_stx.ProgramOfPythonInterpreterAbsStx. of_execute_python_src_string( py_programs.single_line_pgm_that_prints_to( ProcOutputFile.STDOUT, text_printed_by_program)), transformation=to_upper_transformer.abstract_syntax, )) instruction_syntax = instr_abs_stx.create_w_explicit_contents( dst_rel_opt_conf.path_abs_stx_of_name(expected_file.name), program_syntax, ) instruction_checker = self.conf.instruction_checker parser = self.conf.parser() for source_case in equivalent_source_variants__with_source_check__consume_last_line__abs_stx( instruction_syntax): with self.subTest(source_case.name): source = source_case.value.source # ACT # instruction = parser.parse(ARBITRARY_FS_LOCATION_INFO, source) # ASSERT # source_case.value.expectation.apply_with_message( self, source, 'source-after-parse') # ACT & ASSERT # instruction_checker.check( self, instruction, self.conf.arrangement( pre_contents_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR, symbols=symbols), self.conf.expect_success( symbol_usages=asrt.matches_sequence([ to_upper_transformer.reference_assertion, ]), main_side_effects_on_sds=non_hds_dir_contains_exactly( dst_rel_opt_conf.root_dir__non_hds, fs.DirContents([expected_file])), ))
def test_that_populator_is_applied(self): # ARRANGE # expected_dir_contents = fs.DirContents([fs.File('file-name.txt', 'file contents')]) populator = hds_populators.hds_case_dir_contents(expected_dir_contents) expectation = f_asrt.dir_contains_exactly(expected_dir_contents) # ACT # with home_directory_structure(contents=populator) as hds: # ASSERT # expectation.apply_with_message(self, hds.case_dir, 'contents of home/case dir')
def _to_upper_command(empty_tmp_dir: Path, exit_code: int) -> Command: py_program = fs.File( 'to-upper.py', _to_upper_stdin_py_program_w_exit_code(exit_code)) fs.DirContents([py_program]).write_to(empty_tmp_dir) return Command( CommandDriverForExecutableFile( described_path.new_primitive(Path(sys.executable)) ), [str(empty_tmp_dir / py_program.name)] )
def test_populate_hds(self): # ARRANGE # expected_dir_contents = fs.DirContents([fs.File('file-name.txt', 'file contents')]) populator = sut.hds_case_dir_contents(expected_dir_contents) expectation = f_asrt.dir_contains_exactly(expected_dir_contents) with home_directory_structure() as hds: # ACT # populator.populate_hds(hds) # ASSERT # expectation.apply_with_message(self, hds.case_dir, 'contents of HDS/case dir')
def runTest(self): # ARRANGE # dst_file_name = 'dst-file_name-file.txt' src_file = fs.File('src-file_name-file.txt', 'contents') home_dir_contents = fs.DirContents([src_file]) expected_destination_dir_contents = fs.DirContents( [fs.File(dst_file_name, src_file.contents)]) src_rel_option = rel_opt_conf.symbol_conf_rel_hds( defs.ARBITRARY_LEGAL_RELATIVITY__SRC__HDS, 'SRC_SYMBOL', defs.path_relativity_variants__src(self.conf.phase_is_after_act()), ) dst_rel_option = rel_opt_conf.symbol_conf_rel_non_hds( defs.ARBITRARY_LEGAL_RELATIVITY__DST__NON_HDS, 'DST_SYMBOL', defs.PATH_RELATIVITY_VARIANTS__DST, ) # ACT & ASSERT # self.conf.run_test( self, args.copy(src_rel_option.path_argument_of_rel_name(src_file.name), dst_rel_option.path_argument_of_rel_name( dst_file_name)).as_remaining_source, self.conf.arrangement( hds_contents=src_rel_option. populator_for_relativity_option_root__hds(home_dir_contents), symbols=SymbolContext.symbol_table_of_contexts( src_rel_option.symbols.contexts_for_arrangement() + dst_rel_option.symbols.contexts_for_arrangement())), self.conf.expect_success( symbol_usages=asrt.matches_sequence( src_rel_option.symbols.usage_expectation_assertions() + dst_rel_option.symbols.usage_expectation_assertions()), main_side_effects_on_sds=sds_contents_check. non_hds_dir_contains_exactly( dst_rel_option.root_dir__non_hds, expected_destination_dir_contents), ), )
def test_regular(self): # ARRANGE # to_append__text = '<appended text>' non_empty_original__text = '<original text>' append_contents_syntax = abs_stx.FileContentsExplicitAbsStx( ModificationType.APPEND, str_src_abs_stx.StringSourceOfStringAbsStx.of_str_hard( to_append__text)) name_of_modified_file = 'destination.txt' contents_cases: Sequence[AppendRegularContentsCase] = [ AppendRegularContentsCase( 'empty', '', to_append__text, ), AppendRegularContentsCase( 'non-empty', non_empty_original__text, non_empty_original__text + to_append__text, ), ] for target_location_case in target_locations(): syntax = abs_stx.LiteralFilesSourceAbsStx([ abs_stx.regular_file_spec( file_name_arg( target_location_case.target_rel_path( name_of_modified_file)), append_contents_syntax, ) ]) for contents_case in contents_cases: # ACT & ASSERT # integration_check.CHECKER.check__abs_stx__layout__std_source_variants( self, syntax, [ target_location_case.file_for_leaf( contents_case.original(name_of_modified_file)) ], arrangement_w_tcds(), MultiSourceExpectation(execution=ExecutionExpectation( main_result=asrt_fs.dir_contains_exactly_2([ target_location_case.file_for_leaf( fs.File( name_of_modified_file, contents_case. expected_contents_after_modification)) ]))), sub_test_identifiers={ 'contents': contents_case.name, 'target_location': target_location_case.name })
def runTest(self): # ARRANGE # to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'TRANSFORMER_SYMBOL', string_transformers.to_uppercase(), ) src_file = fs.File('src-file.txt', 'contents of source file') src_file_rel_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE) expected_contents = src_file.contents.upper() path_relativity_variants = PathRelativityVariants({src_file_rel_conf.relativity}, True) checker = integration_check.checker(RelOptionsConfiguration(path_relativity_variants, src_file_rel_conf.relativity)) src_file_symbol = ConstantSuffixPathDdvSymbolContext( 'SRC_FILE_SYMBOL', src_file_rel_conf.relativity, src_file.name, path_relativity_variants, ) string_source_syntax = string_source_abs_stx.TransformedStringSourceAbsStx( string_source_abs_stx.StringSourceOfFileAbsStx(src_file_symbol.abstract_syntax), to_upper_transformer.abstract_syntax, ) symbol_table = SymbolContext.symbol_table_of_contexts([ src_file_symbol, to_upper_transformer, ]) # ACT & ASSERT # checker.check__abs_stx__layouts__std_source_variants__wo_input( self, OptionallyOnNewLine(string_source_syntax), arrangement_w_tcds( symbols=symbol_table, hds_contents=src_file_rel_conf.populator_for_relativity_option_root__hds( DirContents([src_file])) ), MultiSourceExpectation.of_prim__const( asrt_string_source.pre_post_freeze__matches_str__const( expected_contents, may_depend_on_external_resources=True, ), symbol_references=asrt.matches_sequence([ src_file_symbol.reference_assertion__path_or_string, to_upper_transformer.reference_assertion, ]), ), )
def _test_symbol_reference_in_dst_file_and_contents( self, symbol_ref_syntax_2_contents_arguments: Callable[ [str], StringSourceAbsStx], symbol_value_2_expected_contents: Callable[[str], str]): sub_dir_symbol = ConstantSuffixPathDdvSymbolContext( 'sub_dir_symbol', RelOptionType.REL_ACT, 'sub-dir', ACCEPTED_DST_RELATIVITY_VARIANTS, ) contents_symbol = StringConstantSymbolContext( 'contents_symbol_name', 'contents symbol value', default_restrictions=asrt_rest.is__w_str_rendering(), ) expected_file_contents = symbol_value_2_expected_contents( contents_symbol.str_value) expected_file = fs.File('a-file-name.txt', expected_file_contents) symbols = [sub_dir_symbol, contents_symbol] expected_symbol_references = SymbolContext.references_assertion_of_contexts( symbols) symbol_table = SymbolContext.symbol_table_of_contexts(symbols) contents_arguments = symbol_ref_syntax_2_contents_arguments( symbol_reference_syntax_for_name(contents_symbol.name)) instruction_syntax = instr_abs_stx.create_w_explicit_contents( path_abs_stx.PathStringAbsStx.of_plain_components( [sub_dir_symbol.name__sym_ref_syntax, expected_file.name]), contents_arguments, ) integration_check.CHECKER__AFTER_ACT.check__abs_stx( self, instruction_syntax, Arrangement.phase_agnostic( tcds=TcdsArrangement( pre_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, ), symbols=symbol_table, ), Expectation.phase_agnostic( main_result=IS_SUCCESS, symbol_usages=expected_symbol_references, main_side_effects_on_sds=dir_contains_exactly( sub_dir_symbol.rel_option_type, fs.DirContents( [fs.Dir(sub_dir_symbol.path_suffix, [expected_file])])), ))
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_populate_tcds(self): # ARRANGE # expected_dir_contents = fs.DirContents([fs.File('a-file-name.txt', 'the file contents')]) populator = sut.contents_in(RelHdsOptionType.REL_HDS_CASE, expected_dir_contents) expectation = f_asrt.dir_contains_exactly(expected_dir_contents) sds = fake_sds() with home_directory_structure() as hds: tcds = TestCaseDs(hds, sds) # ACT # populator.populate_tcds(tcds) # ASSERT # expectation.apply_with_message(self, hds.case_dir, 'contents of HDS/case dir')
def runTest(self): sub_dir_of_home = 'sub-dir' dir_symbol = ConstantSuffixPathDdvSymbolContext('dir_symbol_name', RelOptionType.REL_HDS_ACT, sub_dir_of_home, PATH_RELATIVITY_VARIANTS_FOR_FILE_TO_RUN) source_file_name_symbol = StringConstantSymbolContext('source_file_name_symbol_name', 'the-source-file.py') argument = 'argument_string' expected_output = lines_content([argument]) command_line = '{dir}/{file_name} {argument} '.format( dir=dir_symbol.name__sym_ref_syntax, file_name=source_file_name_symbol.name__sym_ref_syntax, argument=argument, ) executable_file = fs.File( source_file_name_symbol.str_value, PYTHON_PROGRAM_THAT_PRINTS_COMMAND_LINE_ARGUMENTS_ON_SEPARATE_LINES) arrangement = arrangement_w_tcds( hds_contents=contents_in(RelHdsOptionType.REL_HDS_ACT, fs.DirContents([ fs.Dir(sub_dir_of_home, [executable_file]) ])), symbol_table=SymbolContext.symbol_table_of_contexts([ dir_symbol, source_file_name_symbol, ]) ) expectation = Expectation( symbol_usages=asrt.matches_sequence([ dir_symbol.reference_assertion__path_or_string, source_file_name_symbol.reference_assertion__path_component, ]), 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')) ), ) self._check(command_line, arrangement, expectation)
def _instructions_for_executing_py_source( self, py_src: List[str]) -> ContextManager[TestCaseSourceSetup]: py_file = fs.File('the-program.py', lines_content(py_src)) symbol = 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_ACT, py_file.name, ))) program_line = args.symbol_ref_command_line(symbol.name).as_str yield TestCaseSourceSetup( act_phase_instructions=[instr([program_line])], home_act_dir_contents=DirContents([py_file]), symbols=symbol.symbol_table, symbol_usages=symbol.usages_assertion)
def _successful__create() -> ContentsCase: contents_symbol = StringSourceSymbolContextOfPrimitiveConstant( 'CONTENTS_SYMBOL', 'contents', ) created_file = fs.File('regular-file', contents_symbol.contents_str) return ContentsCase( 'create', created_file.name, fs_abs_stx.FileContentsExplicitAbsStx( ModificationType.CREATE, contents_symbol.abstract_syntax, ), symbols=[contents_symbol], pre_existing_files=[], expected_files=[created_file], )
def test_reference(self): # ARRANGE # defined_name = symbol_syntax.A_VALID_SYMBOL_NAME create_file = fs.File('created-file.txt', 'contents of created file') referenced_symbol = FilesSourceSymbolContext.of_primitive( 'referenced_name', file_list.Primitive([ file_list.FileSpecification( create_file.name, RegularFileMaker( ModificationType.CREATE, string_sources.of_string(create_file.contents), None, ), ), ]), ) instruction_syntax = _syntax_of( referenced_symbol.abstract_syntax, defined_name, ) arrangement = Arrangement.phase_agnostic() # EXPECTATION # expectation = _expect_definition_of( defined_name, referenced_symbol.symbol_table, (), referenced_symbol.references_assertion, asrt_fs.dir_contains_exactly_2([create_file]) ) # ACT & ASSERT # INSTRUCTION_CHECKER.check__abs_stx__std_layouts_and_source_variants( self, instruction_syntax, arrangement, expectation, )
def test_literal(self): # ARRANGE # defined_name = symbol_syntax.A_VALID_SYMBOL_NAME create_file = fs.File('created-file.txt', 'contents of created file') literal_syntax = abs_stx.LiteralFilesSourceAbsStx([ abs_stx.regular_file_spec( StringLiteralAbsStx(create_file.name, QuoteType.HARD), abs_stx.FileContentsExplicitAbsStx( ModificationType.CREATE, StringSourceOfStringAbsStx.of_str(create_file.contents, QuoteType.HARD), ) ) ]) syntax = _syntax_of( literal_syntax, defined_name, ) arrangement = Arrangement.phase_agnostic() # EXPECTATION # expectation = _expect_definition_of( defined_name, SymbolTable.empty(), (), asrt.is_empty_sequence, asrt_fs.dir_contains_exactly_2([create_file]) ) # ACT & ASSERT # INSTRUCTION_CHECKER.check__abs_stx__std_layouts_and_source_variants( self, syntax, arrangement, expectation, )