def test_directory(self): dir_to_check = 'dir-to-check' cases = [ NEA('match', True, DirContents([Dir.empty(dir_to_check)]), ), NEA('match: symlink to directory', True, DirContents([Dir.empty('the dir'), sym_link(dir_to_check, 'the dir')]), ), NEA('not match: actual is regular', False, DirContents([File.empty(dir_to_check)]), ), NEA('not match: actual is broken symlink', False, DirContents([File.empty('the file.txt'), sym_link(dir_to_check, 'name-of-non-existing-file')]), ), ] for case in cases: with self.subTest(case_name=case.name): self._check(file_type_to_check_for=FileType.DIRECTORY, expected_result=case.expected, base_name_of_file_to_check=dir_to_check, dir_contents=case.actual)
def test_file_exists(self): # ARRANGE # file_name = 'existing-file' instruction_argument_constructor = ArgumentsConstructorWithFileMatcher(file_name) cases_with_existing_file_of_different_types = [ NameAndValue( 'dir', DirContents([Dir.empty(file_name)])), NameAndValue( 'regular file', DirContents([File.empty(file_name)])), NameAndValue( 'sym-link', DirContents( [Dir.empty('directory'), Link(file_name, 'directory')]) ), NameAndValue( 'broken sym-link', DirContents( [Link(file_name, 'non-existing-target-file')]) ), ] self.checker.check_multiple_cases_with_rel_opt_variants_and_expectation_type_variants( cases_with_existing_file_of_different_types, instruction_argument_constructor, main_result_for_positive_expectation=PassOrFail.PASS, )
def test_copy_file__destination_is_existing_directory(self): # ARRANGE # src = 'src-file' dst = 'dst-dir' file_to_install = File(src, 'contents') home_dir_contents = [file_to_install] act_dir_contents = [Dir.empty(dst)] act_dir_contents_after = [Dir(dst, [file_to_install])] for parser_case in _EXECUTION_CHECKERS: with self.subTest(parser=parser_case.name): # ACT & ASSERT # parser_case.value.check__w_source_variants( self, args.copy( defs.DEFAULT_SRC_REL_OPT.path_argument_of_rel_name(src), defs.DEFAULT_DST_REL_OPT.path_argument_of_rel_name(dst) ).as_str, Arrangement.phase_agnostic( tcds=TcdsArrangement( pre_population_action=MAKE_SUB_DIR_OF_SDS_CURRENT_DIRECTORY, hds_contents=defs.DEFAULT_SRC_REL_OPT.populator_for_relativity_option_root__hds( DirContents(home_dir_contents) ), sds_contents=defs.DEFAULT_DST_REL_OPT.populator_for_relativity_option_root__sds( DirContents(act_dir_contents) ), ), ), MultiSourceExpectation.phase_agnostic( main_side_effects_on_sds=sds_contents_check.cwd_contains_exactly( DirContents(act_dir_contents_after)) ), )
def test_copy_directory(self): src_path_arg = defs.DEFAULT_SRC_REL_OPT.path_argument_of_rel_name('existing-dir') files_to_install = DirContents([Dir(src_path_arg.name, [File('a', 'a'), Dir('d', []), Dir('d2', [File('f', 'f')]) ])]) for execution_checker in _EXECUTION_CHECKERS: with self.subTest(parser=execution_checker.name): execution_checker.value.check( self, args.copy(src_path_arg).as_remaining_source, Arrangement.phase_agnostic( tcds=TcdsArrangement( pre_population_action=MAKE_SUB_DIR_OF_SDS_CURRENT_DIRECTORY, hds_contents=defs.DEFAULT_SRC_REL_OPT.populator_for_relativity_option_root__hds( files_to_install), ), ), Expectation.phase_agnostic_2( main_side_effects_on_sds=sds_contents_check.cwd_contains_exactly( files_to_install) ) )
def test_relative_tmp__with_argument(self): self._check_argument( format_rel_options('{rel_tmp} sub1/sub2'), sds_test.Arrangement(sds_contents_before=contents_in( RelSdsOptionType.REL_TMP, DirContents([Dir('sub1', [Dir.empty('sub2')])]))), sds_test.Expectation( expected_action_result=is_success(), post_action_check=CwdIs( lambda sds: sds.user_tmp_dir / 'sub1' / 'sub2')))
def runTest(self): first_dir = 'first_dir' second_dir = 'second_dir' self._check( syntax_for_assignment_of('{}/{}'.format(first_dir, second_dir)), Arrangement(root_dir_contents=DirContents( [Dir(first_dir, [Dir.empty(second_dir)])])), Expectation(path_rel_root_2_conf=self.conf_prop_equals( lambda path_rel_root_dir: path_rel_root_dir / first_dir / second_dir)))
def runTest(self): self.conf.run_single_line_test_with_source_variants_and_source_check( self, 'first-component/second-component', self.conf.arrangement( sds_contents_before_main=sds_populator.contents_in( RelSdsOptionType.REL_ACT, DirContents([ Dir('first-component', [Dir.empty('second-component')]) ]))), self.conf.expect_successful_execution_with_side_effect( AssertCwdIsSubDirOf( RelOptionType.REL_ACT, pathlib.Path('first-component') / 'second-component')))
def test_one_dir_and_one_regular(self): self.instruction_checker.check_expectation_type_variants( self.exists__type_dir__arguments, model.model_with_source_path_as_sub_dir_of_rel_root(self.name_of_checked_dir), main_result_for_positive_expectation=PassOrFail.PASS, root_dir_of_dir_contents=AN_ACCEPTED_SDS_REL_OPT_CONFIG, contents_of_relativity_option_root=DirContents([ Dir(self.name_of_checked_dir, [ File.empty('regular.txt'), Dir.empty('dir'), ]), ]), )
def test_relative_argument_should_change_dir_relative_to_cwd__from_tmp_dir( self): self._check_argument( 'sub1/sub2', sds_test.Arrangement( sds_contents_before=contents_in( RelSdsOptionType.REL_TMP, DirContents([Dir('sub1', [Dir.empty('sub2')])])), pre_action_action=ChangeDirTo(lambda sds: sds.user_tmp_dir)), sds_test.Expectation( expected_action_result=is_success(), post_action_check=CwdIs( lambda sds: sds.user_tmp_dir / 'sub1' / 'sub2')))
def runTest(self): # ARRANGE # actual_file_name = 'actual' invalid_file_cases = [ NameAndValue( 'file does not exist', [], ), NameAndValue( 'file is a dir', [Dir.empty(actual_file_name)], ), NameAndValue( 'sym-link to dir', [ Dir.empty('a-dir'), fs.sym_link(actual_file_name, 'a-dir') ], ), NameAndValue( 'broken sym-link', [fs.sym_link(actual_file_name, 'non-existing-target')], ), ] for invalid_file_case in invalid_file_cases: for rel_conf in RELATIVITY_OPTION_CONFIGURATIONS_FOR_ACTUAL_FILE: with self.subTest(invalid_cause=invalid_file_case, path_variant=rel_conf.name): CHECKER.check__abs_stx__source_variants( self, InstructionArgumentsAbsStx( rel_conf.path_abs_stx_of_name(actual_file_name), EmptyAbsStx(), ), ArrangementPostAct2( symbols=rel_conf.symbols.in_arrangement(), tcds=TcdsArrangementPostAct( tcds_contents=rel_conf.populator_for_relativity_option_root( DirContents(invalid_file_case.value)), post_population_action=MK_SUB_DIR_OF_ACT_AND_MAKE_IT_CURRENT_DIRECTORY, ) ), MultiSourceExpectation( symbol_usages=rel_conf.symbols.usages_expectation(), execution=ExecutionExpectation.validation_corresponding_to_dsp__post_sds_as_hard_error( rel_conf.directory_structure_partition, ) ), )
def file_structure(self, root_path: pathlib.Path) -> DirContents: return DirContents([ File( 'main.suite', lines_content( ['[cases]', 'sub-dir-1/?.case', 'sub-dir-2/*.case'])), Dir('sub-dir-1', [ File.empty('b.case'), File.empty('a.case'), ]), Dir('sub-dir-2', [ File.empty('22.case'), File.empty('11.case'), ]), ])
def test_access_of_sub_dir_for_failure_message_rendering(self): # ARRANGE # root_1__file = File.empty('f-1') root_2__dir = Dir('d-2', []) arguments = args.matches_full( fc_args.FilesCondition([fc_args.FileCondition(root_1__file.name)]) ) checked_dir = DirArgumentHelper(RelOptionType.REL_TMP, 'checked-dir') # ACT & ASSERT # integration_check.CHECKER__PARSE_FULL.check__w_source_variants( self, arguments.as_arguments, input_=model_constructor__recursive(checked_dir.path_sdv), arrangement=Arrangement( tcds=checked_dir.tcds_arrangement_dir_with_contents([ root_1__file, root_2__dir, ]) ), expectation=Expectation( parse=ParseExpectation( symbol_references=asrt.is_empty_sequence, ), execution=PRIM_AND_EXE_EXPECTATION__NON_MATCH.execution, primitive=PRIM_AND_EXE_EXPECTATION__NON_MATCH.primitive, ), )
def test_relative_path_to_root_file_in_sub_dir_of_cwd(self): # ARRANGE # root_file_sub_dir_path = Path('sub-dir') root_file_in_sub_dir = file_with_lines('root-file-base-name.src', [ phase_names.CONFIGURATION.syntax, INSTR_THAT_ASSERTS_SOURCE_INFO_MATCHES_LOCATION, ]) cwd_dir_contents = DirContents([ Dir(str(root_file_sub_dir_path), [ root_file_in_sub_dir, ]) ]) file_arg_to_parser_rel_cwd = root_file_sub_dir_path / root_file_in_sub_dir.name with tmp_dir_as_cwd(cwd_dir_contents) as abs_cwd_dir_path: instruction_parser_file_loc_assertion = matches_file_location_info( abs_path_of_dir_containing_first_file_path=asrt.equals(abs_cwd_dir_path), file_path_rel_referrer=asrt.equals(file_arg_to_parser_rel_cwd), file_inclusion_chain=asrt.is_empty_sequence, ) # ACT & ASSERT # fail_if_test_case_does_not_pass( self, file_arg_to_parser_rel_cwd, test_case_definition_with_config_phase_assertion_instruction(self, instruction_parser_file_loc_assertion))
def runTest(self): # ARRANGE # files_matcher_name = 'the_files_matcher' checked_dir_location = RelSdsOptionType.REL_TMP checked_dir = Dir.empty('checked-dir') # ACT & ASSERT # integration_check.CHECKER__PARSE_FULL.check_multi__w_source_variants( self, arguments=args.DirContents( fsm_args.SymbolReference(files_matcher_name)).as_arguments, input_=integration_check.file_in_sds(checked_dir_location, checked_dir.name), symbol_references=asrt.matches_singleton_sequence( is_reference_to_files_matcher(files_matcher_name)), execution=[ NExArr( 'checked dir is empty', PrimAndExeExpectation.of_exe( main_result=asrt_matching_result.matches_value( matcher_result)), arrangement_w_tcds( non_hds_contents=sds_populator.contents_in( checked_dir_location, DirContents([checked_dir])), symbols=FilesMatcherSymbolContext. of_primitive_constant(files_matcher_name, matcher_result).symbol_table, ), ) for matcher_result in [False, True] ], )
def test_file_is_directory_with_files_but_none_that_matches_name_pattern( self): name_of_directory = 'name-of-directory' pattern_that_matches_exactly_one_file = 'a*' dir_with_two_files = Dir(name_of_directory, [ File.empty('a file'), File.empty('b file'), ]) contents_of_relativity_option_root = DirContents([dir_with_two_files]) instruction_argument_constructor = argument_constructor_for_num_files_check( int_condition(comparators.EQ, 1), name_option_pattern=pattern_that_matches_exactly_one_file) self.checker.check_parsing_with_different_source_variants( instruction_argument_constructor, model.model_with_source_path_as_sub_dir_of_rel_root( name_of_directory), default_relativity=RelOptionType.REL_CWD, non_default_relativity=RelOptionType.REL_TMP, main_result_for_positive_expectation=PassOrFail.PASS, contents_of_relativity_option_root= contents_of_relativity_option_root, )
def test_destination_already_exists_in_destination_directory(self): src = 'src-file-name' dst = 'dst-dir-name' home_dir_contents = DirContents([(File.empty(src))]) cwd_dir_contents = DirContents([Dir(dst, [File.empty(src)])]) for parser_case in _EXECUTION_CHECKERS: with self.subTest(parser=parser_case.name): # ACT & ASSERT # parser_case.value.check( self, args.copy( defs.DEFAULT_SRC_REL_OPT.path_argument_of_rel_name(src), defs.DEFAULT_DST_REL_OPT.path_argument_of_rel_name(dst) ).as_remaining_source, Arrangement.phase_agnostic( tcds=TcdsArrangement( pre_population_action=MAKE_SUB_DIR_OF_SDS_CURRENT_DIRECTORY, hds_contents=defs.DEFAULT_SRC_REL_OPT.populator_for_relativity_option_root__hds( home_dir_contents), sds_contents=defs.DEFAULT_DST_REL_OPT.populator_for_relativity_option_root__sds( cwd_dir_contents ), ) ), Expectation.phase_agnostic( main_result=asrt_text_doc.is_any_text(), ), )
def test_succeed(self): # ARRANGE # existing_dir = Dir.empty('a-dir') cwd_contents = DirContents([existing_dir]) cases = [ NameAndValue( 'existing dir', existing_dir.name_as_path, ), NameAndValue( 'non-existing dir', existing_dir.name_as_path / 'non-existing', ), NameAndValue( 'non-existing dir / multiple non-existing path components', existing_dir.name_as_path / 'non-existing-1' / 'non-existing-2', ), ] with tmp_dir.tmp_dir_as_cwd(cwd_contents): for case in cases: with self.subTest(case.name): path = case.value # ACT # sut.ensure_directory_exists(path) # ASSERT # self.assertTrue(path.is_dir())
def file_structure(self, root_path: pathlib.Path) -> DirContents: return DirContents([ File('main.suite', lines_content(['[suites]', '?'])), File.empty('1'), Dir('2', []), File.empty('3'), ])
def dir_with_symlink_to_existing_dir() -> pathlib.Path: with tmp_dir( DirContents([ Dir.empty('existing-dir'), sym_link('existing-symlink', 'existing-dir') ])) as dir_path: yield dir_path / 'existing-symlink'
def runTest(self): # ARRANGE # helper = IntegrationCheckHelper() actual_contents = [ Dir('P1-matches', [ File.empty('file-in-pruned-dir'), ]) ] arguments = conjunction([ Parentheses( fsm_args.Prune( fm_args.SymbolReference(NAME_STARTS_WITH__P1.name), fsm_args.NumFiles(int_condition(comparators.EQ, 1)))), fsm_args.NumFiles(int_condition(comparators.EQ, 2)), ]) # ACT & ASSERT # integration_check.CHECKER__PARSE_FULL.check__w_source_variants( self, arguments=arguments.as_arguments, input_=helper.model_constructor_for_checked_dir__recursive(), arrangement=Arrangement( symbols=NAME_STARTS_WITH__P1.symbol_table, tcds=helper.dir_arg.tcds_arrangement_dir_with_contents( actual_contents)), expectation=Expectation( ParseExpectation(symbol_references=asrt.matches_sequence([ NAME_STARTS_WITH__P1.reference_assertion, ])), EXECUTION_IS_PASS, ))
def test_symlink(self): link_target = 'link-target-file' file_to_check = 'file-to-check' cases = [ NEA('match: symlink to regular', True, DirContents([File.empty(link_target), sym_link(file_to_check, link_target)]), ), NEA('match: symlink to directory', True, DirContents([Dir.empty(link_target), sym_link(file_to_check, link_target)]), ), NEA('match: broken symlink', True, DirContents([sym_link(file_to_check, 'non-existing-target-file')]), ), NEA('not match: actual is regular', False, DirContents([File.empty(file_to_check)]), ), ] for case in cases: with self.subTest(case_name=case.name): self._check(file_type_to_check_for=FileType.SYMLINK, expected_result=case.expected, base_name_of_file_to_check=file_to_check, dir_contents=case.actual)
def test_single_file_in_existing_sub_dir(self): sub_dir_name = 'sub-dir' expected_file = File.empty('file-name.txt') dst_file_name = '/'.join([sub_dir_name, expected_file.name]) for phase_is_after_act in [False, True]: checker = integration_check.checker(phase_is_after_act) for rel_opt_conf in ALLOWED_DST_FILE_RELATIVITIES: instruction_syntax = abs_stx.without_contents( rel_opt_conf.path_abs_stx_of_name(dst_file_name)) with self.subTest( relativity_option_string=rel_opt_conf.option_argument): 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, non_hds_contents=rel_opt_conf. populator_for_relativity_option_root__non_hds( fs.DirContents([Dir.empty(sub_dir_name)])), ), ), 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( [fs.Dir(sub_dir_name, [expected_file])])), ))
def runTest(self): # ARRANGE # checked_dir = Dir.empty('the-checked-dir') fsm_1 = FilesMatcherSymbolContextOfPrimitiveConstant('fsm_1', True) fsm_2 = FilesMatcherSymbolContextOfPrimitiveConstant('fsm_2', False) symbols = [fsm_1, fsm_2] rel_conf = rel_opt_conf.conf_rel_any(RelOptionType.REL_ACT) arguments = args.recursive( rel_conf.path_argument_of_rel_name(checked_dir.name), fsm_args.disjunction([fsm_1.argument, fsm_2.argument]), ) is_pass = fsm_1.result_value or fsm_2.result_value # ACT # & ASSERT # INSTRUCTION_CHECKER.check_2( self, arguments.as_remaining_source, ArrangementPostAct2( symbols=SymbolContext.symbol_table_of_contexts(symbols), tcds=TcdsArrangementPostAct( tcds_contents=rel_conf. populator_for_relativity_option_root( DirContents([checked_dir])))), Expectation2( ParseExpectation( source=asrt_source.source_is_at_end, symbol_usages=SymbolContext.usages_assertion_of_contexts( symbols), ), ExecutionExpectation( main_result=asrt_pfh.is_non_hard_error(is_pass), ), ))
def test_single_definition_in_default_suite_file(self): symbol_name = 'STRING_SYMBOL' suite_with_single_def = File( DEFAULT_SUITE_FILE, lines_content([ phase_names.SETUP.syntax, sym_def.define_string(symbol_name, 'value'), ])) dir_arg = Dir('a-dir', [suite_with_single_def]) test_with_files_in_tmp_dir.check( self, command_line_arguments=symbol_args.arguments__suite([dir_arg.name ]), arrangement=Arrangement( cwd_contents=DirContents([ dir_arg, ]), main_program_config=sym_def.main_program_config(), ), expectation=asrt_proc_result.sub_process_result( exitcode=asrt.equals(exit_codes.EXIT_OK), stdout=asrt.equals( output.list_of([ output.SymbolSummary(symbol_name, ValueType.STRING, num_refs=0), ])), ))
def test_relative_path_to_root_file_in_sub_dir_of_cwd(self): # ARRANGE # root_file_sub_dir_path = Path('sub-dir') root_file_base_name = 'root-file-base-name.src' root_file_in_sub_dir = file_with_lines(root_file_base_name, [ phase_names.CONFIGURATION.syntax, INSTR_THAT_ASSERTS_HDS_DIR_MATCHES_PATH, ]) cwd_dir_contents = DirContents( [Dir(str(root_file_sub_dir_path), [ root_file_in_sub_dir, ])]) file_arg_to_parser_rel_cwd = root_file_sub_dir_path / root_file_base_name with tmp_dir_as_cwd(cwd_dir_contents) as abs_cwd_dir_path: hds_dir_assertion = asrt.equals(abs_cwd_dir_path / root_file_sub_dir_path) # ACT & ASSERT # fail_if_test_case_does_not_pass( self, file_arg_to_parser_rel_cwd, test_case_definition_with_config_phase_assertion_instruction( self, hds_dir_assertion))
def test_copy_file__non_existing_destination(self): dst_file_name = 'dst-file_name-file.txt' sub_dir_name = 'src-sub-dir' source_file_contents = 'contents' src_file = File('src-file_name-file.txt', source_file_contents) home_dir_contents_cases = [ (src_file.file_name, DirContents([src_file])), (str(PurePosixPath(sub_dir_name) / src_file.file_name), DirContents([Dir(sub_dir_name, [src_file]) ])), ] expected_destination_dir_contents = DirContents([File(dst_file_name, src_file.contents)]) for src_rel_option in source_relativity_options__hds(): for dst_rel_option in destination_relativity_options(): for src_argument, home_dir_contents in home_dir_contents_cases: self._sub_test__copy_file( src_rel_option=src_rel_option, dst_rel_option=dst_rel_option, src_file_name=src_argument, dst_file_name=dst_file_name, hds_contents=src_rel_option.populator_for_relativity_option_root__hds(home_dir_contents), sds_populator_before_main=sds_populator.empty(), expected_destination_dir_contents=expected_destination_dir_contents, )
def tcds_arrangement_for_contents_of_checked_dir( self, contents: List[FileSystemElement], ) -> TcdsArrangement: return TcdsArrangement( tcds_contents=tcds_populators.TcdsPopulatorForRelOptionType( self.model_file.location, DirContents([Dir(self.model_file.name, contents)])), )
def file_structure(self, root_path: pathlib.Path) -> DirContents: return DirContents([ File('main.suite', lines_content(['[suites]', '**/_*.suite'])), Dir('sub-dir-2', [ File.empty('_y.suite'), File.empty('_x.suite'), ]), Dir('sub-dir-1', [ File.empty('_b.suite'), File.empty('_a.suite'), Dir('sub-dir-1-1', [ File.empty('_1-1.suite'), ]) ]), File.empty('_1.suite'), File.empty('_2.suite'), ])
def test_hard_error_when_there_is_a_single_file_that_is_not_a_regular_file(self): name_of_checked_dir = 'checked-dir' relativity_root_conf = rel_opt_conf.default_conf_rel_any(RelOptionType.REL_CWD) the_model = model.model_with_source_path_as_sub_dir_of_rel_root(self.name_of_checked_dir)(relativity_root_conf) non_regular_files = [ Dir.empty('a-directory'), sym_link('sym-link', 'non-existing-target') ] for file_contents_assertion in self.file_content_assertion_variants: for quantifier in Quantifier: arguments_constructor = args.complete_arguments_constructor( FileQuantificationAssertionVariant( quantifier, file_contents_arg2(file_contents_assertion))) for expectation_type in ExpectationType: arguments = arguments_constructor.apply( expectation_type_config__non_is_success(expectation_type)) for non_regular_file in non_regular_files: with self.subTest( quantifier=quantifier.name, expectation_type=expectation_type.name, arguments=arguments, non_regular_file=non_regular_file.name): integration_check.CHECKER__PARSE_FULL.check( self, remaining_source(arguments), the_model, arrangement= arrangement_w_tcds( tcds_contents=relativity_root_conf.populator_for_relativity_option_root( DirContents([ Dir(name_of_checked_dir, [ non_regular_file, ]), ]) ) ), expectation= Expectation( execution=ExecutionExpectation( is_hard_error=asrt.anything_goes() ), ) )
def file_structure_to_read(self, root_path: pathlib.Path) -> DirContents: return DirContents([ Dir('main-sub-dir', [ File( 'main.suite', lines_content([ section_names.SUITES.syntax, 'suite-sub-dir/sub.suite', ])), Dir('suite-sub-dir', [ File('sub.suite', lines_content([ '*.case', ])), File.empty('sub.case') ]), ]) ])