예제 #1
0
def test_edit_build_files_without_header_text(rule_runner: RuleRunner) -> None:
    rule_runner.create_dir(
        "src/fortran/baz/BUILD")  # NB: A directory, not a file.
    req = EditBuildFilesRequest(
        PutativeTargets([
            PutativeTarget.for_target_type(FortranLibraryTarget,
                                           "src/fortran/baz", "baz",
                                           ["qux1.f90"]),
        ]), )
    edited_build_files = rule_runner.request(EditedBuildFiles, [req])

    assert edited_build_files.created_paths == (
        "src/fortran/baz/BUILD.pants", )

    contents = rule_runner.request(DigestContents, [edited_build_files.digest])
    expected = [
        FileContent(
            "src/fortran/baz/BUILD.pants",
            dedent("""\
               fortran_library()
               """).encode(),
        ),
    ]
    actual = list(contents)
    # We do these more laborious asserts instead of just comparing the lists so that
    # on a text mismatch we see the actual string diff on the decoded strings.
    assert len(expected) == len(actual)
    for efc, afc in zip(expected, actual):
        assert efc.path == afc.path
        assert efc.content.decode() == afc.content.decode()
        assert efc.is_executable == afc.is_executable
예제 #2
0
def test_build_file_lacks_leading_whitespace(rule_runner: RuleRunner,
                                             header: str | None) -> None:
    rule_runner.create_dir(
        "src/fortran/baz/BUILD")  # NB: A directory, not a file.
    req = EditBuildFilesRequest(
        PutativeTargets([
            PutativeTarget.for_target_type(FortranLibraryTarget,
                                           "src/fortran/baz", "baz",
                                           ["qux1.f90"]),
        ]), )
    if header:
        rule_runner.set_options([f"--tailor-build-file-header={header}"])
    edited_build_files = rule_runner.request(EditedBuildFiles, [req])

    assert edited_build_files.created_paths == (
        "src/fortran/baz/BUILD.pants", )

    contents = rule_runner.request(DigestContents, [edited_build_files.digest])
    actual = list(contents)
    # We do these more laborious asserts instead of just comparing the lists so that
    # on a text mismatch we see the actual string diff on the decoded strings.
    for afc in actual:
        content = afc.content.decode()
        assert content.lstrip() == content
예제 #3
0
def test_edit_build_files(rule_runner: RuleRunner, name: str) -> None:
    rule_runner.write_files(
        {f"src/fortran/foo/{name}": 'fortran_library(sources=["bar1.f90"])'})
    rule_runner.create_dir(
        f"src/fortran/baz/{name}")  # NB: A directory, not a file.
    req = EditBuildFilesRequest(
        PutativeTargets([
            PutativeTarget.for_target_type(
                FortranTests,
                "src/fortran/foo",
                "tests",
                ["bar1_test.f90"],
                kwargs={
                    "name": "tests",
                    "life_the_universe_and_everything": 42
                },
            ),
            PutativeTarget.for_target_type(
                FortranLibrary,
                "src/fortran/foo",
                "foo0",
                ["bar2.f90", "bar3.f90"],
                kwargs={
                    "name": "foo0",
                    "sources": ("bar2.f90", "bar3.f90")
                },
                comments=["# A comment spread", "# over multiple lines."],
            ),
            PutativeTarget.for_target_type(FortranLibrary, "src/fortran/baz",
                                           "baz", ["qux1.f90"]),
        ]),
        name=name,
        header="Copyright © 2021 FooCorp.",
        indent="    ",
    )
    edited_build_files = rule_runner.request(EditedBuildFiles, [req])

    assert edited_build_files.created_paths == (
        f"src/fortran/baz/{name}.pants", )
    assert edited_build_files.updated_paths == (f"src/fortran/foo/{name}", )

    contents = rule_runner.request(DigestContents, [edited_build_files.digest])
    expected = [
        FileContent(
            f"src/fortran/baz/{name}.pants",
            textwrap.dedent("""
                Copyright © 2021 FooCorp.

                fortran_library()
            """).lstrip().encode(),
        ),
        FileContent(
            f"src/fortran/foo/{name}",
            textwrap.dedent("""
            fortran_library(sources=["bar1.f90"])

            # A comment spread
            # over multiple lines.
            fortran_library(
                name="foo0",
                sources=[
                    "bar2.f90",
                    "bar3.f90",
                ],
            )

            fortran_tests(
                name="tests",
                life_the_universe_and_everything=42,
            )
            """).lstrip().encode(),
        ),
    ]
    actual = list(contents)
    # We do these more laborious asserts instead of just comparing the lists so that
    # on a text mismatch we see the actual string diff on the decoded strings.
    assert len(expected) == len(actual)
    for efc, afc in zip(expected, actual):
        assert efc.path == afc.path
        assert efc.content.decode() == afc.content.decode()
        assert efc.is_executable == afc.is_executable