Пример #1
0
    def runTest(self):
        sub_dir = fs.Dir.empty('sub-dir')
        top_dir = fs.Dir('top-dir', [sub_dir])

        for rel_conf in RELATIVITY_OPTIONS:
            dst_path = rel_conf.path_abs_stx_of_name__c(
                [top_dir.name, sub_dir.name])
            instruction_syntax = NewDirArguments.implicitly_empty(dst_path)
            with self.subTest(relativity=rel_conf.name):
                # ACT & ASSERT #
                _CHECKER.check__abs_stx(
                    self,
                    instruction_syntax,
                    embryo_arr_exp.Arrangement.phase_agnostic(
                        symbols=rel_conf.symbols.in_arrangement(),
                        tcds=TcdsArrangement(
                            pre_population_action=
                            SETUP_CWD_TO_NON_TCDS_DIR_ACTION)),
                    embryo_arr_exp.Expectation.phase_agnostic_3(
                        symbol_usages=rel_conf.symbols.usages_expectation(),
                        main_result=is_success(),
                        main_side_effects_on_files=TcdsExpectation(
                            sds=rel_conf.assert_root_dir_contains_exactly__p(
                                [top_dir]))),
                )
Пример #2
0
 def runTest(self):
     # ARRANGE #
     non_existing_leaf = fs.Dir.empty('leaf-dir')
     existing_top_dir__name = 'top-dir'
     for rel_conf in RELATIVITY_OPTIONS:
         dst_path = rel_conf.path_abs_stx_of_name__c(
             [existing_top_dir__name, non_existing_leaf.name])
         instruction_syntax = NewDirArguments.implicitly_empty(dst_path)
         # ACT & ASSERT #
         with self.subTest(relativity=rel_conf.name):
             _CHECKER.check__abs_stx(
                 self,
                 instruction_syntax,
                 embryo_arr_exp.Arrangement.phase_agnostic(
                     symbols=rel_conf.symbols.in_arrangement(),
                     tcds=TcdsArrangement(
                         pre_population_action=
                         SETUP_CWD_TO_NON_TCDS_DIR_ACTION,
                         tcds_contents=rel_conf.
                         populator_for_relativity_option_root__s(
                             [fs.Dir.empty(existing_top_dir__name)]),
                     )),
                 embryo_arr_exp.Expectation.phase_agnostic_3(
                     symbol_usages=rel_conf.symbols.usages_expectation(),
                     main_result=is_success(),
                     main_side_effects_on_files=TcdsExpectation(
                         sds=rel_conf.assert_root_dir_contains_exactly__p([
                             fs.Dir(existing_top_dir__name,
                                    [non_existing_leaf])
                         ]))),
             )
Пример #3
0
 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])])),
                     ))
Пример #4
0
    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
                    })
Пример #5
0
    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])])),
            ))
Пример #6
0
def target_locations() -> Sequence[TargetLocationCase]:
    sub_dir_name = 'sub-dir'
    return [
        TargetLocationCase(
            'direct (no sub dir)',
            functional.identity,
            functional.identity,
        ),
        TargetLocationCase(
            'in sub dir',
            lambda fn: str(PurePosixPath(sub_dir_name, fn)),
            lambda fse: fs.Dir(sub_dir_name, [fse]),
        ),
    ]
Пример #7
0
def rel_file_name_cases__create(
    created_leaf_file_name: str,
    expected_created_file: FileSystemElement,
) -> Sequence[RelFileNameCase]:
    sub_dir_name = 'sub-dir'
    sub_sub_dir_name = 'sub-sub-dir'

    return [
        RelFileNameCase(
            'direct (no sub dirs)',
            PurePosixPath(created_leaf_file_name),
            [],
            [expected_created_file],
        ),
        RelFileNameCase(
            'in sub dir (existing)',
            PurePosixPath(sub_dir_name, created_leaf_file_name),
            [fs.Dir.empty(sub_dir_name)],
            [fs.Dir(sub_dir_name, [expected_created_file])],
        ),
        RelFileNameCase(
            'in sub dir (non-existing)',
            PurePosixPath(sub_dir_name, created_leaf_file_name),
            [],
            [fs.Dir(sub_dir_name, [expected_created_file])],
        ),
        RelFileNameCase(
            'in sub sub dir (non-existing)',
            PurePosixPath(sub_dir_name, sub_sub_dir_name,
                          created_leaf_file_name),
            [],
            [
                fs.Dir(sub_dir_name,
                       [fs.Dir(sub_sub_dir_name, [expected_created_file])])
            ],
        ),
    ]
Пример #8
0
 def test_invalid_path(self):
     # ARRANGE #
     existing_file = fs.File.empty('existing-file')
     existing_dir_with_file = fs.Dir('existing-dir', [existing_file])
     path_cases: Sequence[NInpArr[
         FileNameComponents, FileSystemElement]] = [
             NInpArr(
                 'file (direct)',
                 [existing_file.name],
                 existing_file,
             ),
             NInpArr(
                 'file in dir',
                 [existing_dir_with_file.name, existing_file.name],
                 existing_dir_with_file,
             ),
             NInpArr(
                 'middle component is regular file',
                 [existing_file.name, 'non-existing'],
                 existing_file,
             ),
         ]
     for path_case in path_cases:
         for rel_conf in RELATIVITY_OPTIONS:
             syntax_of_existing_file = NewDirArguments.implicitly_empty(
                 rel_conf.path_abs_stx_of_name__c(path_case.input))
             with self.subTest(relativity=rel_conf.name,
                               path=path_case.name):
                 # ACT & ASSERT #
                 _CHECKER.check__abs_stx__std_layouts_and_source_variants(
                     self,
                     syntax_of_existing_file,
                     embryo_arr_exp.Arrangement.phase_agnostic(
                         symbols=rel_conf.symbols.in_arrangement(),
                         tcds=TcdsArrangement(
                             pre_population_action=
                             SETUP_CWD_TO_NON_TCDS_DIR_ACTION,
                             tcds_contents=rel_conf.
                             populator_for_relativity_option_root__s(
                                 [path_case.arrangement]))),
                     embryo_arr_exp.MultiSourceExpectation.phase_agnostic_2(
                         symbol_usages=rel_conf.symbols.usages_expectation(
                         ),
                         main_result=is_failure(),
                     ),
                     sub_test_identifiers={
                         'relativity': rel_conf.name,
                         'path': path_case.name
                     })
Пример #9
0
    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)
Пример #10
0
    def runTest(self):
        # ARRANGE #
        an_empty_dir = fs.Dir.empty('an-empty-dir')
        top_dir_with_sub_dir = fs.Dir('top-dir', [an_empty_dir])

        path_cases: Sequence[NInpArr[FileNameComponents,
                                     FileSystemElement]] = [
                                         NInpArr(
                                             'direct',
                                             [an_empty_dir.name],
                                             an_empty_dir,
                                         ),
                                         NInpArr(
                                             'dir in dir',
                                             [
                                                 top_dir_with_sub_dir.name,
                                                 an_empty_dir.name
                                             ],
                                             top_dir_with_sub_dir,
                                         ),
                                     ]
        for path_case in path_cases:
            for rel_conf in RELATIVITY_OPTIONS:
                dst_path = rel_conf.path_abs_stx_of_name__c(path_case.input)
                instruction_syntax = NewDirArguments.implicitly_empty(dst_path)
                # ACT & ASSERT #
                with self.subTest(relativity=rel_conf.name,
                                  path=path_case.name):
                    _CHECKER.check__abs_stx(
                        self,
                        instruction_syntax,
                        embryo_arr_exp.Arrangement.phase_agnostic(
                            symbols=rel_conf.symbols.in_arrangement(),
                            tcds=TcdsArrangement(
                                pre_population_action=
                                SETUP_CWD_TO_NON_TCDS_DIR_ACTION,
                                tcds_contents=rel_conf.
                                populator_for_relativity_option_root__s(
                                    [path_case.arrangement]),
                            )),
                        embryo_arr_exp.Expectation.phase_agnostic_3(
                            symbol_usages=rel_conf.symbols.usages_expectation(
                            ),
                            main_result=is_failure(),
                        ),
                    )
Пример #11
0
    def test_multiple_symbol_references_in_executable(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)

        executable_file_name_symbol = StringConstantSymbolContext(
            'executable_file_name_symbol_name', 'the-executable-file')

        argument = 'argument_string'

        expected_output = lines_content([argument])

        command_line = '{dir}/{file_name}  {argument} '.format(
            dir=dir_symbol.name__sym_ref_syntax,
            file_name=executable_file_name_symbol.name__sym_ref_syntax,
            argument=argument,
        )

        executable_file = fs.python_executable_file(
            executable_file_name_symbol.str_value,
            PYTHON_PROGRAM_THAT_PRINTS_COMMAND_LINE_ARGUMENTS_ON_SEPARATE_LINES
        )

        arrangement = arrangement_w_tcds(
            hds_contents=relativity_configurations.ATC_FILE.
            populator_for_relativity_option_root__hds(
                fs.DirContents([fs.Dir(sub_dir_of_home, [executable_file])])),
            symbol_table=SymbolContext.symbol_table_of_contexts([
                dir_symbol,
                executable_file_name_symbol,
            ]))

        expectation = Expectation(
            symbol_usages=asrt.matches_sequence([
                dir_symbol.reference_assertion__path_or_string,
                executable_file_name_symbol.
                reference_assertion__string__w_all_indirect_refs_are_strings,
            ]),
            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)
Пример #12
0
    def apply(self,
              put: unittest.TestCase,
              message_builder: MessageBuilder,
              primitive: FilesSource,
              resolving_environment: FullResolvingEnvironment,
              input_: FileSystemElements) -> pathlib.Path:
        dir_arrangement = fs.DirContents([
            fs.Dir(self.NON_SDS_ROOT_FILE_NAME,
                   list(input_))
        ])

        existing_dir = resolving_environment.tcds.sds.root_dir

        dir_arrangement.write_to(existing_dir)

        population_dir = existing_dir / self.NON_SDS_ROOT_FILE_NAME

        primitive.populate(described_path.new_primitive(population_dir))

        return population_dir
Пример #13
0
def _successful__append() -> ContentsCase:
    created_file = fs.File.empty('created-regular-file')
    dst_dir = fs.Dir('destination-dir', [
        created_file,
    ])

    return ContentsCase(
        'create',
        dst_dir.name,
        fs_abs_stx.DirContentsExplicitAbsStx(
            ModificationType.APPEND,
            fs_abs_stx.LiteralFilesSourceAbsStx([
                fs_abs_stx.regular_file_spec__str_name(
                    created_file.name,
                    fs_abs_stx.FileContentsEmptyAbsStx(),
                )
            ])
        ),
        pre_existing_files=[fs.Dir.empty(dst_dir.name)],
        expected_files=[dst_dir]
    )
Пример #14
0
def _successful__create() -> ContentsCase:
    created_file = fs.File.empty('created-regular-file')
    created_dir = fs.Dir('created-dir', [
        created_file,
    ])

    return ContentsCase(
        'create',
        created_dir.name,
        fs_abs_stx.DirContentsExplicitAbsStx(
            ModificationType.CREATE,
            fs_abs_stx.LiteralFilesSourceAbsStx([
                fs_abs_stx.regular_file_spec__str_name(
                    created_file.name,
                    fs_abs_stx.FileContentsEmptyAbsStx(),
                )
            ])
        ),
        pre_existing_files=(),
        expected_files=[created_dir]
    )
Пример #15
0
    def runTest(self):
        # ARRANGE #
        pre_existing_files_cases: Sequence[NameAndValue[FileSystemElements]] = [
            NameAndValue(
                'no pre-existing files',
                [],
            ),
            NameAndValue(
                'regular file',
                [fs.File.empty('pre-exist-regular')],
            ),
            NameAndValue(
                'dir',
                [fs.Dir('pre-existing-dir', [fs.File.empty('file-in-pre-existing-dir')])],
            ),
        ]

        src_dir_contents_cases: Sequence[NameAndValue[FileSystemElements]] = [
            NameAndValue(
                'empty',
                [],
            ),
            NameAndValue(
                'single regular file',
                [fs.File.empty('regular.txt')],
            ),
            NameAndValue(
                'single dir',
                [fs.Dir.empty('a-dir')],
            ),
            NameAndValue(
                'multiple files',
                [fs.File.empty('regular.txt'),
                 fs.Dir.empty('a-dir')],
            ),
            NameAndValue(
                'dir with non-empty sub dirs',
                [fs.Dir('top-dir', [
                    fs.File.empty('file-in-top-dir'),
                    fs.Dir('dir-in-top-dir', [
                        fs.File('file-in-sub-dir.txt', 'file contents'),
                        fs.Dir.empty('dir-in-sub-dir'),
                    ]),
                ])],
            ),
        ]
        src_dir_relativity = relativity_options.conf_rel_any(RelOptionType.REL_HDS_CASE)
        src_dir_symbol = PathDdvSymbolContext.of_no_suffix(
            'SRC_PATH_SYMBOL',
            src_dir_relativity.relativity,
            accepted_relativities=RELATIVITY_VARIANTS__READ__BEFORE_ACT,
        )
        arguments_syntax = abs_stx.CopyOfDirContentsAbsStx(src_dir_symbol.abstract_syntax)
        for pre_existing_files_case in pre_existing_files_cases:
            for src_dir_contents_case in src_dir_contents_cases:
                with self.subTest(pre_existing=pre_existing_files_case.name,
                                  src_dir_contents=src_dir_contents_case.name):
                    # ACT & ASSERT #
                    integration_check.CHECKER.check__abs_stx(
                        self,
                        arguments_syntax,
                        pre_existing_files_case.value,
                        Arrangement(
                            symbols=src_dir_symbol.symbol_table,
                            tcds=TcdsArrangement(
                                tcds_contents=src_dir_relativity.populator_for_relativity_option_root__s(
                                    src_dir_contents_case.value
                                )
                            )
                        ),
                        Expectation(
                            ParseExpectation(
                                source=asrt_source.is_at_end_of_line(1),
                                symbol_references=src_dir_symbol.references_assertion,
                            ),
                            execution=ExecutionExpectation(
                                main_result=asrt_files.DirContainsExactly.of_elements(
                                    list(pre_existing_files_case.value) +
                                    list(src_dir_contents_case.value)
                                )
                            )
                        ),
                    )
Пример #16
0
def create_path_cases(
        created_leaf_file_name: str) -> Sequence[FileSpecAndExpectedCase]:
    explicit_file_contents = 'explicit file contents'
    name_of_created_file_in_dir = 'file-in-dir.txt'
    name_of_sub_dir = 'created-sub-dir'
    file_in_created_dir__file_spec = abs_stx.regular_file_spec(
        StringLiteralAbsStx(name_of_created_file_in_dir, QuoteType.HARD),
        abs_stx.FileContentsEmptyAbsStx(),
    )
    return [
        FileSpecAndExpectedCase(
            'regular / implicit empty',
            FileType.REGULAR,
            abs_stx.FileContentsEmptyAbsStx(),
            fs.File.empty(created_leaf_file_name),
        ),
        FileSpecAndExpectedCase(
            'regular / explicit contents',
            FileType.REGULAR,
            abs_stx.FileContentsExplicitAbsStx(
                ModificationType.CREATE,
                str_src_abs_stx.StringSourceOfStringAbsStx.of_str_hard(
                    explicit_file_contents)),
            fs.File(created_leaf_file_name, explicit_file_contents),
        ),
        FileSpecAndExpectedCase(
            'dir / implicit empty',
            FileType.DIR,
            abs_stx.DirContentsEmptyAbsStx(),
            fs.Dir.empty(created_leaf_file_name),
        ),
        FileSpecAndExpectedCase(
            'dir / explicit contents',
            FileType.DIR,
            abs_stx.DirContentsExplicitAbsStx(
                ModificationType.CREATE,
                abs_stx.LiteralFilesSourceAbsStx(
                    [file_in_created_dir__file_spec])),
            fs.Dir(created_leaf_file_name,
                   [fs.File.empty(name_of_created_file_in_dir)]),
        ),
        FileSpecAndExpectedCase(
            'dir / explicit contents / nested dirs',
            FileType.DIR,
            abs_stx.DirContentsExplicitAbsStx(
                ModificationType.CREATE,
                abs_stx.LiteralFilesSourceAbsStx([
                    abs_stx.dir_spec(
                        file_name_arg(name_of_sub_dir),
                        abs_stx.DirContentsExplicitAbsStx(
                            ModificationType.CREATE,
                            abs_stx.LiteralFilesSourceAbsStx([
                                file_in_created_dir__file_spec,
                            ])),
                    )
                ])),
            fs.Dir(created_leaf_file_name, [
                fs.Dir(name_of_sub_dir,
                       [fs.File.empty(name_of_created_file_in_dir)])
            ]),
        ),
    ]
Пример #17
0
 def original(self, file_name: str) -> FileSystemElement:
     return fs.Dir(file_name, self.contents_before_modification)