def _failing_cases() -> Sequence[ContentsCase]:
    contents_symbol = StringSourceSymbolContextOfPrimitiveConstant(
        'CONTENTS_SYMBOL',
        'added contents',
    )
    regular_file = fs.File.empty('regular-file')
    return [
        ContentsCase(
            'create',
            regular_file.name,
            fs_abs_stx.FileContentsExplicitAbsStx(
                ModificationType.CREATE,
                contents_symbol.abstract_syntax
            ),
            symbols=[contents_symbol],
            pre_existing_files=[regular_file],
        ),
        ContentsCase(
            'append',
            regular_file.name,
            fs_abs_stx.FileContentsExplicitAbsStx(
                ModificationType.APPEND,
                contents_symbol.abstract_syntax
            ),
            symbols=[contents_symbol],
            pre_existing_files=[],
        ),
    ]
示例#2
0
    def _file_contents_cases(self) -> InvalidDestinationFileTestCasesData:
        arbitrary_transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            string_transformers.to_uppercase(),
        )

        symbols = arbitrary_transformer.symbol_table

        relativity_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE)

        src_file = relativity_conf.path_abs_stx_of_name('src-file.txt')

        contents_abs_stx_builder = string_source_abs_stx.TransformableAbsStxBuilder(
            string_source_abs_stx.StringSourceOfFileAbsStx(src_file))

        src_file_in_hds_contents = relativity_conf.populator_for_relativity_option_root(
            DirContents([File.empty(src_file.name)]))

        file_contents_cases = [
            NameAndValue(
                'contents of existing file / without transformation',
                fs_abs_stx.FileContentsExplicitAbsStx(
                    fs_abs_stx.ModificationType.CREATE,
                    contents_abs_stx_builder.without_transformation())),
            NameAndValue(
                'contents of existing file / with transformation',
                fs_abs_stx.FileContentsExplicitAbsStx(
                    fs_abs_stx.ModificationType.CREATE,
                    contents_abs_stx_builder.with_transformation(
                        arbitrary_transformer.abstract_syntax))),
        ]

        return InvalidDestinationFileTestCasesData(file_contents_cases,
                                                   symbols,
                                                   src_file_in_hds_contents)
 def runTest(self):
     # ARRANGE #
     dst_path = DefaultRelPathAbsStx('existing-file')
     for phase_is_after_act in [False, True]:
         for modification_type in ModificationType:
             checker = integration_check.checker(phase_is_after_act)
             for validation_case in validation_cases.failing_validation_cases():
                 instruction_syntax = InstructionAbsStx(
                     dst_path,
                     fs_abs_stx.FileContentsExplicitAbsStx(modification_type,
                                                           validation_case.value.syntax),
                 )
                 # ACT & ASSERT #
                 checker.check__abs_stx__std_layouts_and_source_variants(
                     self,
                     instruction_syntax,
                     embryo_arr_exp.Arrangement.phase_agnostic(
                         symbols=validation_case.value.symbol_context.symbol_table,
                     ),
                     embryo_arr_exp.MultiSourceExpectation.phase_agnostic(
                         validation=validation_case.value.assertion,
                         symbol_usages=validation_case.value.symbol_context.usages_assertion,
                     ),
                     sub_test_identifiers={
                         'modification_type': modification_type,
                         'phase_is_after_act': phase_is_after_act,
                         'validation': validation_case.name
                     },
                 )
示例#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
                    })
def _mk_explicit_contents(program: ProgramAbsStx) -> fs_abs_stx.FileContentsExplicitAbsStx:
    return fs_abs_stx.FileContentsExplicitAbsStx(
        fs_abs_stx.ModificationType.CREATE,
        string_source_abs_stx.StringSourceOfProgramAbsStx(
            ProcOutputFile.STDOUT,
            program,
        )
    )
示例#6
0
def with_explicit_contents_(destination: PathAbsStx,
                            modification: ModificationType,
                            contents: StringSourceAbsStx,
                            ) -> InstructionAbsStx:
    return InstructionAbsStx(destination,
                             fs_abs_stx.FileContentsExplicitAbsStx(
                                 modification,
                                 contents
                             ))
    def _file_contents_cases(
            self) -> common_test_cases.InvalidDestinationFileTestCasesData:
        file_contents_cases = [
            NameAndValue(
                'contents of here doc',
                fs_abs_stx.FileContentsExplicitAbsStx(
                    fs_abs_stx.ModificationType.CREATE,
                    string_source_abs_stx.StringSourceOfStringAbsStx(
                        rich_str_abs_stx.HereDocAbsStx('contents\n')))),
            NameAndValue(
                'contents of string',
                fs_abs_stx.FileContentsExplicitAbsStx(
                    fs_abs_stx.ModificationType.CREATE,
                    string_source_abs_stx.StringSourceOfStringAbsStx.of_str(
                        'contents'))),
        ]

        return common_test_cases.InvalidDestinationFileTestCasesData(
            file_contents_cases, empty_symbol_table())
 def test_fail_when_contents_is_missing(self):
     # ARRANGE #
     missing_contents = fs_abs_stx.FileContentsExplicitAbsStx(
         fs_abs_stx.ModificationType.CREATE,
         string_source_abs_stx.CustomStringSourceAbsStx.missing_value())
     instruction_syntax = instr_abs_stx.InstructionAbsStx(
         path_abs_stx.DefaultRelPathAbsStx('file-name'),
         missing_contents,
     )
     # ACT & ASSERT #
     check_invalid_syntax__abs_stx(self, instruction_syntax)
示例#9
0
    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
                    })
示例#10
0
 def test_regular_file(self):
     valid_file_name = StringLiteralAbsStx('valid_file_name')
     for validation_case in str_src_validation_cases.failing_validation_cases():
         symbol_context = validation_case.value.symbol_context
         for contents_case in ContentsCase.cases():
             invalid_file_spec = abs_stx.regular_file_spec(
                 valid_file_name,
                 abs_stx.FileContentsExplicitAbsStx(contents_case.modification_type,
                                                    symbol_context.abstract_syntax),
             )
             # ACT & ASSERT #
             self._check_case(
                 invalid_file_spec,
                 contents_case,
                 validation_case,
             )
示例#11
0
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],
    )
示例#12
0
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],
    )
示例#13
0
    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,
        )
示例#14
0
        for file_type in defs.FileType
        for contents_variant in _CONTENTS_CASES[file_type]
    ]


_VALID_STRING_SOURCE = StringSourceOfStringAbsStx.of_plain(StringLiteralAbsStx.empty_string())
_VALID_FILES_SOURCE = abs_stx.LiteralFilesSourceAbsStx(())

_CONTENTS_CASES: Mapping[FileType, Sequence[NameAndValue[ContentsAbsStx]]] = {
    defs.FileType.REGULAR: (
            [
                NameAndValue('empty', abs_stx.FileContentsEmptyAbsStx()),
            ] +
            [
                NameAndValue('explicit/{}'.format(mod),
                             abs_stx.FileContentsExplicitAbsStx(mod, _VALID_STRING_SOURCE))
                for mod in defs.ModificationType
            ]),
    defs.FileType.DIR: (
            [
                NameAndValue('empty', abs_stx.DirContentsEmptyAbsStx()),
            ] +
            [
                NameAndValue('explicit/{}'.format(mod),
                             abs_stx.DirContentsExplicitAbsStx(mod, _VALID_FILES_SOURCE))
                for mod in defs.ModificationType
            ])
}

if __name__ == '__main__':
    unittest.TextTestRunner().run(suite())
示例#15
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)])
            ]),
        ),
    ]
示例#16
0
    def runTest(self):
        fn_f_1 = _fn_symbol('file__name__1', 'file-1.txt')
        fn_f_2 = _fn_symbol('file__name__2', 'file-2.txt')
        fn_f_3 = _fn_symbol('file__name__3', fn_f_2.str_value)
        fn_d_1 = _fn_symbol('dir__name__1', 'dir-1')
        fn_d_2 = _fn_symbol('dir__name__2', 'dir-2')
        fn_d_3 = _fn_symbol('dir__name__3', fn_d_2.str_value)

        contents_f_2 = StringSourceSymbolContextOfPrimitiveConstant('file_contents_2', 'contents of file 2')
        contents_f_3 = StringSourceSymbolContextOfPrimitiveConstant('file_contents_3', 'contents of file 3')

        contents_d_2 = FilesSourceSymbolContext.of_primitive('dir_contents_2',
                                                             primitives.FilesSourceThatDoesNothingTestImpl())
        contents_d_3 = FilesSourceSymbolContext.of_primitive('dir_contents_3',
                                                             primitives.FilesSourceThatDoesNothingTestImpl())

        expected_created_files = [
            fs.File.empty(fn_f_1.str_value),
            fs.File(fn_f_2.str_value, contents_f_2.contents_str + contents_f_3.contents_str),
            fs.Dir.empty(fn_d_1.str_value),
            fs.Dir.empty(fn_d_2.str_value),
        ]

        all_symbols = [
            fn_f_1,
            fn_f_2, contents_f_2,
            fn_f_3, contents_f_3,
            fn_d_1,
            fn_d_2, contents_d_2,
            fn_d_3, contents_d_3,
        ]
        syntax = LiteralFilesSourceAbsStx([
            _spec_regular(fn_f_1,
                          abs_stx.FileContentsEmptyAbsStx()),
            _spec_regular(fn_f_2,
                          abs_stx.FileContentsExplicitAbsStx(
                              ModificationType.CREATE,
                              contents_f_2.abstract_syntax,
                          )),
            _spec_regular(fn_f_3,
                          abs_stx.FileContentsExplicitAbsStx(
                              ModificationType.APPEND,
                              contents_f_3.abstract_syntax,
                          )),
            _spec_dir(fn_d_1,
                      abs_stx.DirContentsEmptyAbsStx()),
            _spec_dir(fn_d_2,
                      abs_stx.DirContentsExplicitAbsStx(
                          ModificationType.CREATE,
                          contents_d_2.abstract_syntax,
                      )),
            _spec_dir(fn_d_3,
                      abs_stx.DirContentsExplicitAbsStx(
                          ModificationType.APPEND,
                          contents_d_3.abstract_syntax,
                      )),
        ])
        # ACT & ASSERT #
        integration_check.CHECKER.check__abs_stx__layout__std_source_variants(
            self,
            syntax,
            (),
            arrangement_w_tcds(
                symbols=SymbolContext.symbol_table_of_contexts(all_symbols)
            ),
            MultiSourceExpectation(
                symbol_references=SymbolContext.references_assertion_of_contexts(all_symbols),
                execution=ExecutionExpectation(
                    main_result=asrt_fs.dir_contains_exactly_2(
                        expected_created_files
                    )
                )
            )
        )