示例#1
0
 def get_stripped_files_for_snapshot(
     paths: List[str],
     *,
     use_representative_path: bool = True,
     args: Optional[List[str]] = None,
 ) -> StrippedResponseData:
     input_snapshot = self.make_snapshot_of_empty_files(paths)
     request = StripSnapshotRequest(input_snapshot,
                                    representative_path=paths[0] if
                                    use_representative_path else None)
     return self.get_stripped_files(request, args=args)
示例#2
0
 def get_stripped_files_for_snapshot(
     paths: List[str],
     *,
     use_representative_path: bool = True,
     args: Optional[List[str]] = None,
 ) -> List[str]:
     input_snapshot = self.make_snapshot({fp: "" for fp in paths})
     request = StripSnapshotRequest(input_snapshot,
                                    representative_path=paths[0] if
                                    use_representative_path else None)
     return self.get_stripped_files(request, args=args)
示例#3
0
 def get_stripped_file_mapping(
         paths: List[str],
         args: Optional[List[str]] = None) -> Dict[str, str]:
     args = args or []
     args.append("--source-root-patterns=src/python")
     args.append("--source-root-patterns=src/java")
     request = StripSnapshotRequest(
         self.make_snapshot_of_empty_files(paths))
     result = self.request_single_product(
         SourceRootStrippedSources,
         Params(request, create_options_bootstrapper(args=args)),
     )
     return dict(result.get_file_to_stripped_file_mapping())
示例#4
0
async def list_backends(
    backend_options: BackendsOptions,
    global_options: GlobalOptions,
    console: Console,
) -> Backends:
    discovered_register_pys = await Get[Snapshot](PathGlobs(
        ["**/*/register.py"]))
    register_pys_content = await Get[FilesContent](
        Digest, discovered_register_pys.digest)

    source_root_stripped_sources = await Get[SourceRootStrippedSources](
        StripSnapshotRequest(snapshot=discovered_register_pys))
    file_to_stripped_file_mapping = source_root_stripped_sources.get_file_to_stripped_file_mapping(
    )
    stripped_paths = []
    for fc in register_pys_content:
        stripped_path = file_to_stripped_file_mapping.get(fc.path)
        if stripped_path is None:
            # This should never happen, but it's worth checking proactively.
            raise NoSourceRootError(fc.path)
        stripped_paths.append(stripped_path)

    backend_infos = tuple(
        BackendInfo.create(fc, stripped_path, global_options)
        for fc, stripped_path in zip(register_pys_content, stripped_paths))
    v1_backends = []
    v2_backends = []
    for backend in backend_infos:
        if backend.is_v1:
            v1_backends.append(backend)
        if backend.is_v2:
            v2_backends.append(backend)

    with backend_options.line_oriented(console) as print_stdout:
        if global_options.options.v1:
            print_stdout(
                format_section(v1_backends,
                               console,
                               version_number=1,
                               option_name="backend_packages"))
        if global_options.options.v2:
            print_stdout(
                format_section(v2_backends,
                               console,
                               version_number=2,
                               option_name="backend_packages2"))
    return Backends(exit_code=0)
示例#5
0
async def create_coverage_config(
        coverage_config_request: CoverageConfigRequest) -> CoverageConfig:
    sources = await Get[SourceFiles](AllSourceFilesRequest(
        (tgt.get(PythonSources) for tgt in coverage_config_request.targets
         if tgt.has_field(PythonSources))))

    source_root_stripped_sources = await Get[SourceRootStrippedSources](
        StripSnapshotRequest(snapshot=sources.snapshot))

    # Generate a map from source root stripped source to its source root. eg:
    #  {'pants/testutil/subsystem/util.py': 'src/python'}. This is so that coverage reports
    #  referencing /chroot/path/pants/testutil/subsystem/util.py can be mapped back to the actual
    #  sources they reference when generating coverage reports.
    # Note that this map doesn't contain injected __init__.py files, but we tell coverage
    # to ignore those using the -i flag (see below).
    stripped_files_to_source_roots = {}
    for source_root, files in source_root_stripped_sources.root_to_relfiles.items(
    ):
        for file in files:
            stripped_files_to_source_roots[file] = source_root

    default_config = dedent("""
        [run]
        branch = True
        timid = False
        relative_files = True
        """)

    config_parser = configparser.ConfigParser()
    config_parser.read_file(StringIO(default_config))
    config_parser.set("run", "plugins", COVERAGE_PLUGIN_MODULE_NAME)
    config_parser.add_section(COVERAGE_PLUGIN_MODULE_NAME)
    config_parser.set(
        COVERAGE_PLUGIN_MODULE_NAME,
        "source_to_target_base",
        json.dumps(stripped_files_to_source_roots),
    )
    config_parser.set(COVERAGE_PLUGIN_MODULE_NAME, "test_time",
                      json.dumps(coverage_config_request.is_test_time))

    config_io_stream = StringIO()
    config_parser.write(config_io_stream)
    digest = await Get[Digest](InputFilesContent([
        FileContent(".coveragerc",
                    content=config_io_stream.getvalue().encode())
    ]))
    return CoverageConfig(digest)
示例#6
0
    def test_strip_snapshot(self) -> None:
        def get_stripped_files_for_snapshot(
            paths: List[str],
            *,
            use_representative_path: bool = True,
            args: Optional[List[str]] = None,
        ) -> StrippedResponseData:
            input_snapshot = self.make_snapshot_of_empty_files(paths)
            request = StripSnapshotRequest(input_snapshot,
                                           representative_path=paths[0] if
                                           use_representative_path else None)
            return self.get_stripped_files(request, args=args)

        # Normal source roots
        assert get_stripped_files_for_snapshot(
            ["src/python/project/example.py"]) == (
                ["project/example.py"],
                {
                    "src/python": ("project/example.py", )
                },
            )
        assert get_stripped_files_for_snapshot(
            ["src/python/project/example.py"],
            use_representative_path=False) == (["project/example.py"], {
                "src/python": ("project/example.py", )
            })
        assert get_stripped_files_for_snapshot(
            ["src/java/com/project/example.java"]) == (
                ["com/project/example.java"],
                {
                    "src/java": ("com/project/example.java", )
                },
            )
        assert get_stripped_files_for_snapshot(
            ["tests/python/project_test/example.py"]) == (
                ["project_test/example.py"],
                {
                    "tests/python": ("project_test/example.py", )
                },
            )

        # Unrecognized source root
        unrecognized_source_root = "no-source-root/example.txt"
        with pytest.raises(ExecutionError) as exc:
            get_stripped_files_for_snapshot([unrecognized_source_root])
        assert "NoSourceRootError: No source root found for `no-source-root`." in str(
            exc.value)

        # Support for multiple source roots
        file_names = [
            "src/python/project/example.py",
            "src/java/com/project/example.java"
        ]
        with pytest.raises(ExecutionError) as exc:
            get_stripped_files_for_snapshot(file_names,
                                            use_representative_path=True)
        assert "Cannot strip prefix src/python" in str(exc.value)
        assert get_stripped_files_for_snapshot(
            file_names, use_representative_path=False) == (
                ["com/project/example.java", "project/example.py"],
                {
                    "src/python": ("project/example.py", ),
                    "src/java": ("com/project/example.java", )
                },
            )

        # Test a source root at the repo root. We have performance optimizations for this case
        # because there is nothing to strip.
        source_root_config = [f"--source-root-patterns={json.dumps(['/'])}"]
        assert get_stripped_files_for_snapshot(
            ["project/f1.py", "project/f2.py"],
            args=source_root_config,
            use_representative_path=True,
        ) == (["project/f1.py", "project/f2.py"], {
            ".": ("project/f1.py", "project/f2.py")
        })
        assert get_stripped_files_for_snapshot(
            ["dir1/f.py", "dir2/f.py"],
            args=source_root_config,
            use_representative_path=False) == (["dir1/f.py", "dir2/f.py"], {
                ".": ("dir1/f.py", "dir2/f.py")
            })

        # Gracefully handle an empty snapshot
        assert self.get_stripped_files(
            StripSnapshotRequest(EMPTY_SNAPSHOT)) == ([], {})