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)
class SelectorShouldBeApplied(MultipleExecutionCasesGenerator): min_depth = 1 max_depth = 1 num_regular_files_eq_1 = [ Dir('lvl0', [ File.empty('lvl1-included'), Dir('lvl1-not-included', [ File.empty('lvl2-not-included-file'), Dir.empty('lvl2-not-included-dir'), ]) ]) ] num_regular_files_eq_2 = [ Dir('lvl0', [ File.empty('lvl1-included-1'), File.empty('lvl1-included-2'), Dir('lvl1-not-included', [ File.empty('lvl2-not-included-file'), Dir.empty('lvl2-not-included-dir'), ]) ]) ] num_files_setup = NumFilesSetup(comparators.EQ, 2, [ NEA( 'not match', False, num_regular_files_eq_1, ), NEA( 'match', True, num_regular_files_eq_2, ), ]) def arguments(self) -> FileMatcherArg: return _as_arguments( RecWLimArguments( fms_args.Selection(fm_args.Type(FileType.REGULAR), self.num_files_setup.num_files_arg()), self.min_depth, self.max_depth, )) def expected_symbols(self) -> Sequence[Assertion[SymbolReference]]: return () def execution_cases( self) -> Sequence[NExArr[ExecutionResult, Arrangement]]: return [ NExArr( num_files_setup.name, FullExecutionResult(num_files_setup.expected), Arrangement(tcds=self._helper. tcds_arrangement_for_contents_of_checked_dir( num_files_setup.actual))) for num_files_setup in self.num_files_setup.cases ]
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 cases() -> Sequence[NameAndValue[List[FileSystemElement]]]: return [ NameAndValue( 'empty', [], ), NameAndValue( 'single regular file', [File.empty('an-empty-file')], ), NameAndValue( 'single empty dir', [Dir.empty('an-empty-dir')], ), NameAndValue('directory with contents - one level', [ File.empty('an-empty-file'), Dir('non-empty-dir', [ File.empty('file-in-dir'), Dir.empty('dir-in-dir'), ]), ]), NameAndValue('directory with contents - multiple levels', [ Dir('non-empty-dir-1', [File.empty('file-in-dir-1')]), Dir('non-empty-dir-2', [ File.empty('file-in-dir-2'), Dir('non-empty-dir-2-1', [ File.empty('file-in-dir-2-1'), Dir.empty('dir-in-dir-2-1'), ]) ]), ]), ]
def test_WHEN_multiple_selectors_THEN_selection_SHOULD_be_conjunction_of_selectors( self): # ARRANGE # prefix_to_include = 'A' file_type_to_include = FileType.DIRECTORY name = prefix_to_include + '-matching-base-name' file_name = prefix_to_include + '-matching-base-name--file' name1 = prefix_to_include + 'dir-in-dir-matching-base-name' name2 = prefix_to_include + '-matching-base-name--dir-in-dir' name3 = prefix_to_include + '-matching-base-name' name4 = prefix_to_include + '-matching-base-name--empty-dir' actual_cases = [ NameAndValue( 'empty', [], ), NameAndValue( 'single regular file that matches on base name', [File.empty(name)], ), NameAndValue( 'single dir that matches on base name', [Dir.empty(name3)], ), NameAndValue( 'single dir that not matches on base name', [Dir.empty('non-matching-base-name')], ), NameAndValue('directories with contents - one level', [ File.empty(file_name), Dir.empty(name4), Dir('non-matching-name-non-empty-dir', [ File.empty('file-in-dir'), Dir.empty(name1), ]), Dir(prefix_to_include + '-matching-base-name--non-empty-dir', [ File.empty('file-in-dir'), Dir.empty(name2), ]), ]), ] cases = test_data.strip_file_type_info_s( test_data.filter_on_file_type( file_type_to_include, test_data.filter_on_base_name_prefix(prefix_to_include, [ test_data.expected_is_direct_contents_of_actual( case.name, case.value) for case in actual_cases ])))
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 test_copy_file__destination_with_multiple_path_components(self): source_file = File('src-file_name-file.txt', 'contents') home_dir_contents = DirContents([source_file]) destination_dir_contents_cases = [ DestinationSetup( case_name='two components - non of them exists', path_argument_str='sub/leaf', dst_relativity_root_contents_arrangement=DirContents([]), expected_relativity_root_contents=DirContents([ Dir('sub', [ File('leaf', source_file.contents) ]) ]), ), DestinationSetup( case_name='two components - first exists as dir', path_argument_str='sub/leaf', dst_relativity_root_contents_arrangement=DirContents([Dir.empty('sub')]), expected_relativity_root_contents=DirContents([ Dir('sub', [ File('leaf', source_file.contents) ]) ]), ), DestinationSetup( case_name='two components - both exist as dirs', path_argument_str='sub/leaf', dst_relativity_root_contents_arrangement=DirContents([Dir('sub', [Dir.empty('leaf')])]), expected_relativity_root_contents=DirContents([ Dir('sub', [ Dir('leaf', [source_file]) ]) ]), ), ] src_rel_option = rel_opt_conf.default_conf_rel_hds(RelHdsOptionType.REL_HDS_CASE) for dst_rel_option in some_destination_relativity_options(): for destination_setup in destination_dir_contents_cases: self._sub_test__copy_file( src_rel_option=src_rel_option, dst_rel_option=dst_rel_option, src_file_name=source_file.file_name, dst_file_name=destination_setup.path_argument_str, hds_contents=src_rel_option.populator_for_relativity_option_root__hds(home_dir_contents), sds_populator_before_main=destination_setup.sds_populator_of_root_of( dst_rel_option, destination_setup.dst_relativity_root_contents_arrangement), expected_destination_dir_contents=destination_setup.expected_relativity_root_contents)
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 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 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_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 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 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 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): directory_name = 'existing-directory' self._check( syntax_for_assignment_of(directory_name), Arrangement( root_dir_contents=DirContents([Dir.empty(directory_name)])), Expectation(path_rel_root_2_conf=self.conf_prop_equals( lambda path_rel_root_dir: path_rel_root_dir / directory_name)))
def runTest(self): # ARRANGE # files_matcher_name = 'the_files_matcher' checked_dir_location = RelOptionType.REL_ACT checked_dir = Dir.empty('checked-dir') matcher_argument = SymbolReferenceArgument(files_matcher_name).as_str tcds = TcdsArrangementPostAct( TcdsPopulatorForRelOptionType(checked_dir_location, DirContents([checked_dir]))) source_cases = [ NameAndValue( 'All arguments on separate lines', Arguments(checked_dir.name, [ reserved_words.COLON, args.RECURSION_OPTION_STR, matcher_argument ]), ), NameAndValue( 'Empty lines between arguments', Arguments(checked_dir.name, [ '', reserved_words.COLON, '', args.RECURSION_OPTION_STR, '', matcher_argument, ]), ), ] execution_cases = [ NExArr( 'matcher gives ' + str(matcher_result), ExecutionExpectation( main_result=asrt_pfh.is_non_hard_error(matcher_result)), ArrangementPostAct2( tcds, symbols=FilesMatcherSymbolContext.of_primitive_constant( files_matcher_name, matcher_result).symbol_table)) for matcher_result in [False, True] ] # ACT & ASSERT # for source_case in source_cases: with self.subTest(source_case=source_case.name): INSTRUCTION_CHECKER.check_multi__with_source_variants( self, SourceArrangement.new_w_arbitrary_fs_location( source_case.value), symbol_usages=asrt.matches_singleton_sequence( is_reference_to_files_matcher__usage( files_matcher_name)), execution=execution_cases, )
def test_file_is_dir_that_do_not_contain_default_suite_file(self): # ARRANGE # a_dir = Dir.empty('dir') test_with_files_in_tmp_dir.check( self, symbol_args.arguments__suite([a_dir.name]), arrangement=Arrangement(cwd_contents=DirContents([a_dir])), expectation=asrt_proc_result.is_result_for_empty_stdout( exit_codes.EXIT_INVALID_USAGE))
def test_file_is_directory_that_is_empty(self): name_of_empty_directory = 'name-of-empty_directory' instruction_argument_constructor = argument_constructor_for_emptiness_check() contents_of_relativity_option_root = DirContents([Dir.empty(name_of_empty_directory)]) self.checker.check_rel_opt_variants_and_expectation_type_variants( instruction_argument_constructor, model_with_source_path_as_sub_dir_of_rel_root(name_of_empty_directory), PassOrFail.PASS, contents_of_relativity_option_root=contents_of_relativity_option_root)
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 arrangement(self) -> ArrangementPostAct: return ArrangementPostAct( tcds_contents=TcdsPopulatorForRelOptionType( self.checked_file_location, DirContents([Dir.empty(self.checked_file_name)])), symbols=FilesMatcherSymbolContext.of_primitive( self.name_of_referenced_symbol, matchers.MatcherThatReportsHardError( self.error_message)).symbol_table, )
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 arrangement(self) -> Arrangement: return arrangement_w_tcds( non_hds_contents=sds_populator.contents_in( self.checked_dir_location, DirContents([Dir.empty(self.checked_dir_name)])), symbols=FilesMatcherSymbolContext.of_primitive( self.files_matcher_name, matchers.MatcherThatReportsHardError( self.error_message)).symbol_table, )
def test_relative_argument_should_change_dir_relative_to_cwd__from_act_dir( self): self._check_argument( 'existing-dir', sds_test.Arrangement(sds_contents_before=contents_in( RelSdsOptionType.REL_ACT, DirContents([Dir.empty('existing-dir')]))), sds_test.Expectation( expected_action_result=is_success(), post_action_check=CwdIs( lambda sds: sds.act_dir / 'existing-dir')))
def runTest(self): path_argument_str = 'path-argument' self._check( syntax_for_assignment_of(path_argument_str), Arrangement( root_dir_contents=DirContents([Dir.empty(path_argument_str)])), Expectation( main_result=svh_assertions.is_success(), path_rel_root_2_conf=self.conf_prop_equals( lambda path_rel_root: path_rel_root / path_argument_str)))
def test_single_dot_argument_should_have_no_effect(self): self._check_argument( '.', sds_test.Arrangement(pre_action_action=ChangeDirTo( lambda sds: sds.act_dir / 'sub-dir'), sds_contents_before=contents_in( RelSdsOptionType.REL_ACT, DirContents([Dir.empty('sub-dir')]))), sds_test.Expectation( expected_action_result=is_success(), post_action_check=CwdIs(lambda sds: sds.act_dir / 'sub-dir')))
def runTest(self): source_file = 'source-file.src' command_line = source_file arrangement = arrangement_w_tcds( hds_contents=contents_in(RelHdsOptionType.REL_HDS_ACT, fs.DirContents([ Dir.empty(source_file)])) ) expectation = Expectation( validation=ValidationExpectationSvh.fails__pre_sds() ) self._check(command_line, arrangement, expectation)
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 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 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_file_is_directory_that_is_empty(self): empty_directory = Dir.empty('name-of-empty-dir') instruction_argument_constructor = argument_constructor_for_emptiness_check() contents_of_relativity_option_root = DirContents([empty_directory]) self.checker.check_parsing_with_different_source_variants( instruction_argument_constructor, model_with_source_path_as_sub_dir_of_rel_root(empty_directory.name), 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, )