Exemplo n.º 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
Exemplo n.º 2
0
def assert_rule_match(rule_runner: RuleRunner, glob: str,
                      paths: Tuple[str, ...], *, should_match: bool) -> None:
    # Confirm in-memory behavior.
    matched_filespec = matches_filespec({"includes": [glob]}, paths=paths)
    if should_match:
        assert matched_filespec == paths
    else:
        assert not matched_filespec

    # Confirm on-disk behavior.
    for expected_match in paths:
        if expected_match.endswith("/"):
            rule_runner.create_dir(expected_match)
        else:
            rule_runner.create_file(expected_match)
    snapshot = rule_runner.request_product(Snapshot, [PathGlobs([glob])])
    if should_match:
        assert sorted(paths) == sorted(snapshot.files)
    else:
        assert not snapshot.files
def test_fingerprint_dir(rule_runner: RuleRunner) -> None:
    d1 = rule_runner.create_dir("a")
    d2 = rule_runner.create_dir("b")
    d3 = rule_runner.create_dir("c")

    rule_runner.write_files({
        "a/bar/bar.config": "blah blah blah",
        "a/foo/foo.config": "meow meow meow",
        "b/foo/foo.config": "meow meow meow",
        "b/bar/bar.config": "blah blah blah",
        "c/bar/bar.config": "blah meow blah",
    })

    dp1 = OptionsFingerprinter().fingerprint(dir_option, [d1])
    dp2 = OptionsFingerprinter().fingerprint(dir_option, [d1, d2])
    dp3 = OptionsFingerprinter().fingerprint(dir_option, [d2, d1])
    dp4 = OptionsFingerprinter().fingerprint(dir_option, [d3])

    assert dp1 == dp1
    assert dp2 == dp2
    assert dp1 != dp3
    assert dp1 != dp4
    assert dp2 != dp3
Exemplo n.º 4
0
def test_fingerprint_dir(rule_runner: RuleRunner) -> None:
    d1 = rule_runner.create_dir("a")
    d2 = rule_runner.create_dir("b")
    d3 = rule_runner.create_dir("c")

    for f, c in [
        ("a/bar/bar.config", "blah blah blah"),
        ("a/foo/foo.config", "meow meow meow"),
        ("b/foo/foo.config", "meow meow meow"),
        ("b/bar/bar.config", "blah blah blah"),
        ("c/bar/bar.config", "blah meow blah"),
    ]:
        rule_runner.create_file(f, contents=c)

    dp1 = OptionsFingerprinter().fingerprint(dir_option, [d1])
    dp2 = OptionsFingerprinter().fingerprint(dir_option, [d1, d2])
    dp3 = OptionsFingerprinter().fingerprint(dir_option, [d2, d1])
    dp4 = OptionsFingerprinter().fingerprint(dir_option, [d3])

    assert dp1 == dp1
    assert dp2 == dp2
    assert dp1 != dp3
    assert dp1 != dp4
    assert dp2 != dp3
Exemplo n.º 5
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
Exemplo n.º 6
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
Exemplo n.º 7
0
def test_buildroot_is_source_root(rule_runner: RuleRunner) -> None:
    rule_runner.create_dir("code")
    assert_roots(rule_runner, ["/"], expected=["."])
Exemplo n.º 8
0
def test_multiple_source_roots(rule_runner: RuleRunner) -> None:
    rule_runner.create_dir("fakerootA")
    rule_runner.create_dir("fakerootB")
    assert_roots(rule_runner, ["fakerootA", "fakerootB"])
Exemplo n.º 9
0
def test_single_source_root(rule_runner: RuleRunner) -> None:
    rule_runner.create_dir("fakeroot")
    assert_roots(rule_runner, ["fakeroot"])