def test_filter_field_sets(rule_runner: RuleRunner) -> None: @dataclass(frozen=True) class MockFieldSet(FieldSet): sources: Sources # Another field to demo that we will preserve the whole FieldSet data structure. tags: Tags rule_runner.create_file("f1.txt") valid_addr = Address("", target_name="valid") valid_field_set = MockFieldSet(valid_addr, Sources(["f1.txt"], address=valid_addr), Tags(None, address=valid_addr)) empty_addr = Address("", target_name="empty") empty_field_set = MockFieldSet(empty_addr, Sources(None, address=empty_addr), Tags(None, address=empty_addr)) result = rule_runner.request_product( FieldSetsWithSources, [ FieldSetsWithSourcesRequest([valid_field_set, empty_field_set]), create_options_bootstrapper(), ], ) assert tuple(result) == (valid_field_set, )
async def lint( console: Console, targets_with_origins: TargetsWithOrigins, options: LintOptions, union_membership: UnionMembership, ) -> Lint: lint_request_types = union_membership[LintRequest] lint_requests: Iterable[StyleRequest] = tuple( lint_request_type( lint_request_type.field_set_type.create(target_with_origin) for target_with_origin in targets_with_origins if lint_request_type.field_set_type.is_valid(target_with_origin.target) ) for lint_request_type in union_membership[LintRequest] ) field_sets_with_sources: Iterable[FieldSetsWithSources] = await MultiGet( Get[FieldSetsWithSources](FieldSetsWithSourcesRequest(lint_request.field_sets)) for lint_request in lint_requests ) valid_lint_requests: Iterable[StyleRequest] = tuple( lint_request_cls(lint_request) for lint_request_cls, lint_request in zip(lint_request_types, field_sets_with_sources) if lint_request ) if options.values.per_target_caching: results = await MultiGet( Get[LintResults](LintRequest, lint_request.__class__([field_set])) for lint_request in valid_lint_requests for field_set in lint_request.field_sets ) else: results = await MultiGet( Get[LintResults](LintRequest, lint_request) for lint_request in valid_lint_requests ) if not results: return Lint(exit_code=0) exit_code = 0 sorted_results = sorted(itertools.chain.from_iterable(results), key=lambda res: res.linter_name) for result in sorted_results: console.print_stderr( f"{console.green('✓')} {result.linter_name} succeeded." if result.exit_code == 0 else f"{console.red('𐄂')} {result.linter_name} failed." ) if result.stdout: console.print_stderr(result.stdout) if result.stderr: console.print_stderr(result.stderr) if result != sorted_results[-1]: console.print_stderr("") if result.exit_code != 0: exit_code = result.exit_code return Lint(exit_code)
async def check( console: Console, workspace: Workspace, targets: Targets, dist_dir: DistDir, union_membership: UnionMembership, ) -> Check: typecheck_request_types = cast("Iterable[type[StyleRequest]]", union_membership[CheckRequest]) requests = tuple( typecheck_request_type( typecheck_request_type.field_set_type.create(target) for target in targets if typecheck_request_type.field_set_type.is_applicable(target)) for typecheck_request_type in typecheck_request_types) field_sets_with_sources = await MultiGet( Get(FieldSetsWithSources, FieldSetsWithSourcesRequest(request.field_sets)) for request in requests) valid_requests = tuple( request_cls(request) for request_cls, request in zip( typecheck_request_types, field_sets_with_sources) if request) all_results = await MultiGet( Get(CheckResults, CheckRequest, request) for request in valid_requests) def get_tool_name(res: CheckResults) -> str: return res.checker_name write_reports( all_results, workspace, dist_dir, goal_name=CheckSubsystem.name, get_tool_name=get_tool_name, ) exit_code = 0 if all_results: console.print_stderr("") for results in sorted(all_results, key=lambda results: results.checker_name): if results.skipped: sigil = console.sigil_skipped() status = "skipped" elif results.exit_code == 0: sigil = console.sigil_succeeded() status = "succeeded" else: sigil = console.sigil_failed() status = "failed" exit_code = results.exit_code console.print_stderr(f"{sigil} {results.checker_name} {status}.") return Check(exit_code)
async def lint( console: Console, workspace: Workspace, targets: Targets, lint_subsystem: LintSubsystem, union_membership: UnionMembership, ) -> Lint: request_types = union_membership[LintRequest] requests: Iterable[StyleRequest] = tuple( request_type( request_type.field_set_type.create(target) for target in targets if request_type.field_set_type.is_valid(target)) for request_type in request_types) field_sets_with_sources: Iterable[FieldSetsWithSources] = await MultiGet( Get(FieldSetsWithSources, FieldSetsWithSourcesRequest(request.field_sets)) for request in requests) valid_requests: Iterable[StyleRequest] = tuple( request_cls(request) for request_cls, request in zip(request_types, field_sets_with_sources) if request) if lint_subsystem.per_target_caching: results = await MultiGet( Get(LintResults, LintRequest, request.__class__([field_set])) for request in valid_requests for field_set in request.field_sets) else: results = await MultiGet( Get(LintResults, LintRequest, lint_request) for lint_request in valid_requests) sorted_results = sorted(itertools.chain.from_iterable(results), key=lambda res: res.linter_name) if not sorted_results: return Lint(exit_code=0) exit_code = 0 for result in sorted_results: console.print_stderr( f"{console.green('✓')} {result.linter_name} succeeded." if result. exit_code == 0 else f"{console.red('𐄂')} {result.linter_name} failed.") if result.stdout: console.print_stderr(result.stdout) if result.stderr: console.print_stderr(result.stderr) if result != sorted_results[-1]: console.print_stderr("") result.materialize(console, workspace) if result.exit_code != 0: exit_code = result.exit_code return Lint(exit_code)
async def typecheck( console: Console, targets: Targets, union_membership: UnionMembership ) -> Typecheck: typecheck_request_types = union_membership[TypecheckRequest] requests: Iterable[StyleRequest] = tuple( lint_request_type( lint_request_type.field_set_type.create(target) for target in targets if lint_request_type.field_set_type.is_applicable(target) ) for lint_request_type in typecheck_request_types ) field_sets_with_sources: Iterable[FieldSetsWithSources] = await MultiGet( Get(FieldSetsWithSources, FieldSetsWithSourcesRequest(request.field_sets)) for request in requests ) valid_requests: Iterable[StyleRequest] = tuple( request_cls(request) for request_cls, request in zip(typecheck_request_types, field_sets_with_sources) if request ) all_results = await MultiGet( Get(TypecheckResults, TypecheckRequest, request) for request in valid_requests ) exit_code = 0 if all_results: console.print_stderr("") for results in sorted(all_results, key=lambda results: results.typechecker_name): if results.skipped: sigil = console.yellow("-") status = "skipped" elif results.exit_code == 0: sigil = console.green("✓") status = "succeeded" else: sigil = console.red("𐄂") status = "failed" exit_code = results.exit_code console.print_stderr(f"{sigil} {results.typechecker_name} {status}.") return Typecheck(exit_code)
def test_filter_field_sets(self) -> None: @dataclass(frozen=True) class MockFieldSet(FieldSet): sources: Sources # Another field to demo that we will preserve the whole FieldSet data structure. tags: Tags self.create_file("f1.txt") valid_addr = Address.parse(":valid") valid_field_set = MockFieldSet( valid_addr, Sources(["f1.txt"], address=valid_addr), Tags(None, address=valid_addr) ) empty_addr = Address.parse(":empty") empty_field_set = MockFieldSet( empty_addr, Sources(None, address=empty_addr), Tags(None, address=empty_addr) ) result = self.request_single_product( FieldSetsWithSources, FieldSetsWithSourcesRequest([valid_field_set, empty_field_set]), ) assert tuple(result) == (valid_field_set,)
async def run_tests( console: Console, test_subsystem: TestSubsystem, interactive_runner: InteractiveRunner, workspace: Workspace, union_membership: UnionMembership, dist_dir: DistDir, ) -> Test: if test_subsystem.debug: targets_to_valid_field_sets = await Get( TargetRootsToFieldSets, TargetRootsToFieldSetsRequest( TestFieldSet, goal_description="`test --debug`", no_applicable_targets_behavior=NoApplicableTargetsBehavior.error, ), ) debug_requests = await MultiGet( Get(TestDebugRequest, TestFieldSet, field_set) for field_set in targets_to_valid_field_sets.field_sets ) exit_code = 0 for debug_request in debug_requests: if debug_request.process is None: continue debug_result = interactive_runner.run(debug_request.process) if debug_result.exit_code != 0: exit_code = debug_result.exit_code return Test(exit_code) targets_to_valid_field_sets = await Get( TargetRootsToFieldSets, TargetRootsToFieldSetsRequest( TestFieldSet, goal_description=f"the `{test_subsystem.name}` goal", no_applicable_targets_behavior=NoApplicableTargetsBehavior.warn, ), ) field_sets_with_sources = await Get( FieldSetsWithSources, FieldSetsWithSourcesRequest(targets_to_valid_field_sets.field_sets) ) results = await MultiGet( Get(EnrichedTestResult, TestFieldSet, field_set) for field_set in field_sets_with_sources ) # Print summary. exit_code = 0 if results: console.print_stderr("") for result in sorted(results): if result.skipped: continue if result.exit_code == 0: sigil = console.green("✓") status = "succeeded" else: sigil = console.red("𐄂") status = "failed" exit_code = cast(int, result.exit_code) console.print_stderr(f"{sigil} {result.address} {status}.") if result.extra_output and result.extra_output.files: workspace.write_digest( result.extra_output.digest, path_prefix=str(dist_dir.relpath / "test" / result.address.path_safe_spec), ) merged_xml_results = await Get( Digest, MergeDigests(result.xml_results.digest for result in results if result.xml_results), ) workspace.write_digest(merged_xml_results) if test_subsystem.use_coverage: # NB: We must pre-sort the data for itertools.groupby() to work properly, using the same # key function for both. However, you can't sort by `types`, so we call `str()` on it. all_coverage_data = sorted( (result.coverage_data for result in results if result.coverage_data is not None), key=lambda cov_data: str(type(cov_data)), ) coverage_types_to_collection_types: Dict[ Type[CoverageData], Type[CoverageDataCollection] ] = { collection_cls.element_type: collection_cls for collection_cls in union_membership.get(CoverageDataCollection) } coverage_collections: List[CoverageDataCollection] = [] for data_cls, data in itertools.groupby(all_coverage_data, lambda data: type(data)): collection_cls = coverage_types_to_collection_types[data_cls] coverage_collections.append(collection_cls(data)) # We can create multiple reports for each coverage data (console, xml and html) coverage_reports_collections = await MultiGet( Get(CoverageReports, CoverageDataCollection, coverage_collection) for coverage_collection in coverage_collections ) coverage_report_files: List[PurePath] = [] for coverage_reports in coverage_reports_collections: report_files = coverage_reports.materialize(console, workspace) coverage_report_files.extend(report_files) if coverage_report_files and test_subsystem.open_coverage: open_files = await Get( OpenFiles, OpenFilesRequest(coverage_report_files, error_if_open_not_found=False) ) for process in open_files.processes: interactive_runner.run(process) return Test(exit_code)
async def lint( console: Console, workspace: Workspace, targets: Targets, lint_subsystem: LintSubsystem, union_membership: UnionMembership, ) -> Lint: request_types = union_membership[LintRequest] requests: Iterable[StyleRequest] = tuple( request_type( request_type.field_set_type.create(target) for target in targets if request_type.field_set_type.is_applicable(target)) for request_type in request_types) field_sets_with_sources: Iterable[FieldSetsWithSources] = await MultiGet( Get(FieldSetsWithSources, FieldSetsWithSourcesRequest(request.field_sets)) for request in requests) valid_requests: Iterable[StyleRequest] = tuple( request_cls(request) for request_cls, request in zip(request_types, field_sets_with_sources) if request) if lint_subsystem.per_file_caching: all_per_file_results = await MultiGet( Get(LintResults, LintRequest, request.__class__([field_set])) for request in valid_requests for field_set in request.field_sets) def key_fn(results: LintResults): return results.linter_name # NB: We must pre-sort the data for itertools.groupby() to work properly. sorted_all_per_files_results = sorted(all_per_file_results, key=key_fn) # We consolidate all results for each linter into a single `LintResults`. all_results = tuple( LintResults( itertools.chain.from_iterable( per_file_results.results for per_file_results in all_linter_results), linter_name=linter_name, ) for linter_name, all_linter_results in itertools.groupby( sorted_all_per_files_results, key=key_fn)) else: all_results = await MultiGet( Get(LintResults, LintRequest, lint_request) for lint_request in valid_requests) all_results = tuple( sorted(all_results, key=lambda results: results.linter_name)) reports = list( itertools.chain.from_iterable(results.reports for results in all_results)) if reports: # TODO(#10532): Tolerate when a linter has multiple reports. linters_with_multiple_reports = [ results.linter_name for results in all_results if len(results.reports) > 1 ] if linters_with_multiple_reports: if lint_subsystem.per_file_caching: suggestion = "Try running without `--lint-per-file-caching` set." else: suggestion = ( "The linters likely partitioned the input targets, such as grouping by Python " "interpreter compatibility. Try running on fewer targets or unset " "`--lint-reports-dir`.") raise InvalidLinterReportsError( "Multiple reports would have been written for these linters: " f"{linters_with_multiple_reports}. The option `--lint-reports-dir` only works if " f"each linter has a single result. {suggestion}") merged_reports = await Get( Digest, MergeDigests(report.digest for report in reports)) workspace.write_digest(merged_reports) logger.info( f"Wrote lint result files to {lint_subsystem.reports_dir}.") exit_code = 0 if all_results: console.print_stderr("") for results in all_results: if results.skipped: sigil = console.yellow("-") status = "skipped" elif results.exit_code == 0: sigil = console.green("✓") status = "succeeded" else: sigil = console.red("𐄂") status = "failed" exit_code = results.exit_code console.print_stderr(f"{sigil} {results.linter_name} {status}.") return Lint(exit_code)
async def run_tests( console: Console, test_subsystem: TestSubsystem, interactive_runner: InteractiveRunner, workspace: Workspace, union_membership: UnionMembership, ) -> Test: if test_subsystem.debug: targets_to_valid_field_sets = await Get( TargetRootsToFieldSets, TargetRootsToFieldSetsRequest( TestFieldSet, goal_description="`test --debug`", error_if_no_applicable_targets=True ), ) debug_requests = await MultiGet( Get(TestDebugRequest, TestFieldSet, field_set) for field_set in targets_to_valid_field_sets.field_sets ) exit_code = 0 for debug_request in debug_requests: if debug_request.process is None: continue debug_result = interactive_runner.run(debug_request.process) if debug_result.exit_code != 0: exit_code = debug_result.exit_code return Test(exit_code) targets_to_valid_field_sets = await Get( TargetRootsToFieldSets, TargetRootsToFieldSetsRequest( TestFieldSet, goal_description=f"the `{test_subsystem.name}` goal", error_if_no_applicable_targets=False, ), ) field_sets_with_sources = await Get( FieldSetsWithSources, FieldSetsWithSourcesRequest(targets_to_valid_field_sets.field_sets) ) results = await MultiGet( Get(EnrichedTestResult, TestFieldSet, field_set) for field_set in field_sets_with_sources ) # Print summary. exit_code = 0 if results: console.print_stderr("") for result in sorted(results): if result.skipped: continue if result.exit_code == 0: sigil = console.green("✓") status = "succeeded" else: sigil = console.red("𐄂") status = "failed" exit_code = cast(int, result.exit_code) console.print_stderr(f"{sigil} {result.address} {status}.") merged_xml_results = await Get( Digest, MergeDigests(result.xml_results.digest for result in results if result.xml_results), ) workspace.write_digest(merged_xml_results) if test_subsystem.use_coverage: all_coverage_data: Iterable[CoverageData] = [ result.coverage_data for result in results if result.coverage_data is not None ] coverage_types_to_collection_types: Dict[ Type[CoverageData], Type[CoverageDataCollection] ] = { collection_cls.element_type: collection_cls for collection_cls in union_membership.get(CoverageDataCollection) } coverage_collections: List[CoverageDataCollection] = [] for data_cls, data in itertools.groupby(all_coverage_data, lambda data: type(data)): collection_cls = coverage_types_to_collection_types[data_cls] coverage_collections.append(collection_cls(data)) # We can create multiple reports for each coverage data (console, xml and html) coverage_reports_collections = await MultiGet( Get(CoverageReports, CoverageDataCollection, coverage_collection) for coverage_collection in coverage_collections ) coverage_report_files: List[PurePath] = [] for coverage_reports in coverage_reports_collections: report_files = coverage_reports.materialize(console, workspace) coverage_report_files.extend(report_files) if coverage_report_files and test_subsystem.open_coverage: open_files = await Get( OpenFiles, OpenFilesRequest(coverage_report_files, error_if_open_not_found=False) ) for process in open_files.processes: interactive_runner.run(process) return Test(exit_code)
async def run_tests( console: Console, options: TestOptions, interactive_runner: InteractiveRunner, workspace: Workspace, union_membership: UnionMembership, ) -> Test: if options.values.debug: targets_to_valid_field_sets = await Get[TargetsToValidFieldSets]( TargetsToValidFieldSetsRequest( TestFieldSet, goal_description="`test --debug`", error_if_no_valid_targets=True, expect_single_field_set=True, ) ) field_set = targets_to_valid_field_sets.field_sets[0] request = await Get[TestDebugRequest](TestFieldSet, field_set) debug_result = interactive_runner.run_local_interactive_process(request.ipr) return Test(debug_result.process_exit_code) targets_to_valid_field_sets = await Get[TargetsToValidFieldSets]( TargetsToValidFieldSetsRequest( TestFieldSet, goal_description=f"the `{options.name}` goal", error_if_no_valid_targets=False, ) ) field_sets_with_sources = await Get[FieldSetsWithSources]( FieldSetsWithSourcesRequest(targets_to_valid_field_sets.field_sets) ) results = await MultiGet( Get[AddressAndTestResult](WrappedTestFieldSet(field_set)) for field_set in field_sets_with_sources ) exit_code = PANTS_SUCCEEDED_EXIT_CODE for result in results: if result.test_result.status == Status.FAILURE: exit_code = PANTS_FAILED_EXIT_CODE has_output = result.test_result.stdout or result.test_result.stderr if has_output: status = ( console.green("✓") if result.test_result.status == Status.SUCCESS else console.red("𐄂") ) console.print_stderr(f"{status} {result.address}") if result.test_result.stdout: console.print_stderr(result.test_result.stdout) if result.test_result.stderr: console.print_stderr(result.test_result.stderr) if has_output and result != results[-1]: console.print_stderr("") # Print summary if len(results) > 1: console.print_stderr("") for result in results: console.print_stderr( f"{result.address.reference():80}.....{result.test_result.status.value:>10}" ) for result in results: xml_results = result.test_result.xml_results if not xml_results: continue workspace.materialize_directory(DirectoryToMaterialize(xml_results)) if options.values.use_coverage: all_coverage_data: Iterable[CoverageData] = [ result.test_result.coverage_data for result in results if result.test_result.coverage_data is not None ] coverage_types_to_collection_types: Dict[ Type[CoverageData], Type[CoverageDataCollection] ] = { collection_cls.element_type: collection_cls for collection_cls in union_membership.get(CoverageDataCollection) } coverage_collections: List[CoverageDataCollection] = [] for data_cls, data in itertools.groupby(all_coverage_data, lambda data: type(data)): collection_cls = coverage_types_to_collection_types[data_cls] coverage_collections.append(collection_cls(data)) coverage_reports = await MultiGet( Get[CoverageReport](CoverageDataCollection, coverage_collection) for coverage_collection in coverage_collections ) coverage_report_files = [] for report in coverage_reports: report_file = report.materialize(console, workspace) if report_file is not None: coverage_report_files.append(report_file) if coverage_report_files and options.values.open_coverage: desktop.ui_open(console, interactive_runner, coverage_report_files) return Test(exit_code)
async def lint( console: Console, targets_with_origins: TargetsWithOrigins, options: LintOptions, union_membership: UnionMembership, ) -> Lint: field_set_collection_types: Iterable[ Type[LinterFieldSets]] = union_membership.union_rules[LinterFieldSets] field_set_collections: Iterable[LinterFieldSets] = tuple( field_set_collection_type( field_set_collection_type.field_set_type.create(target_with_origin) for target_with_origin in targets_with_origins if field_set_collection_type.field_set_type.is_valid( target_with_origin.target)) for field_set_collection_type in field_set_collection_types) field_set_collections_with_sources: Iterable[ FieldSetsWithSources] = await MultiGet( Get[FieldSetsWithSources](FieldSetsWithSourcesRequest( field_set_collection)) for field_set_collection in field_set_collections) # NB: We must convert back the generic FieldSetsWithSources objects back into their # corresponding LinterFieldSets, e.g. back to IsortFieldSets, in order for the union rule to # work. valid_field_set_collections: Iterable[LinterFieldSets] = tuple( field_set_collection_cls(field_set_collection) for field_set_collection_cls, field_set_collection in zip( field_set_collection_types, field_set_collections_with_sources) if field_set_collection) if options.values.per_target_caching: results = await MultiGet( Get[LintResult](LinterFieldSets, field_set_collection.__class__([field_set])) for field_set_collection in valid_field_set_collections for field_set in field_set_collection) else: results = await MultiGet( Get[LintResult](LinterFieldSets, field_set_collection) for field_set_collection in valid_field_set_collections) if not results: return Lint(exit_code=0) exit_code = 0 sorted_results = sorted(results, key=lambda res: res.linter_name) for result in sorted_results: console.print_stderr( f"{console.green('✓')} {result.linter_name} succeeded." if result. exit_code == 0 else f"{console.red('𐄂')} {result.linter_name} failed.") if result.stdout: console.print_stderr(result.stdout) if result.stderr: console.print_stderr(result.stderr) if result != sorted_results[-1]: console.print_stderr("") if result.exit_code != 0: exit_code = result.exit_code return Lint(exit_code)
async def run_tests( console: Console, test_subsystem: TestSubsystem, interactive_runner: InteractiveRunner, workspace: Workspace, union_membership: UnionMembership, ) -> Test: if test_subsystem.debug: targets_to_valid_field_sets = await Get( TargetsToValidFieldSets, TargetsToValidFieldSetsRequest( TestFieldSet, goal_description="`test --debug`", error_if_no_valid_targets=True, expect_single_field_set=True, ), ) field_set = targets_to_valid_field_sets.field_sets[0] request = await Get(TestDebugRequest, TestFieldSet, field_set) debug_result = interactive_runner.run(request.process) return Test(debug_result.exit_code) targets_to_valid_field_sets = await Get( TargetsToValidFieldSets, TargetsToValidFieldSetsRequest( TestFieldSet, goal_description=f"the `{test_subsystem.name}` goal", error_if_no_valid_targets=False, ), ) field_sets_with_sources = await Get( FieldSetsWithSources, FieldSetsWithSourcesRequest(targets_to_valid_field_sets.field_sets)) results = await MultiGet( Get(AddressAndTestResult, WrappedTestFieldSet(field_set)) for field_set in field_sets_with_sources) # Print details. for result in results: if test_subsystem.options.output == ShowOutput.NONE or ( test_subsystem.options.output == ShowOutput.FAILED and result.test_result.status == Status.SUCCESS): continue has_output = result.test_result.stdout or result.test_result.stderr if has_output: status = (console.green("✓") if result.test_result.status == Status.SUCCESS else console.red("𐄂")) console.print_stderr(f"{status} {result.address}") if result.test_result.stdout: console.print_stderr(result.test_result.stdout) if result.test_result.stderr: console.print_stderr(result.test_result.stderr) if has_output and result != results[-1]: console.print_stderr("") # Print summary console.print_stderr("") for result in results: color = console.green if result.test_result.status == Status.SUCCESS else console.red # The right-align logic sees the color control codes as characters, so we have # to account for that. In f-strings the alignment field widths must be literals, # so we have to indirect via a call to .format(). right_align = 19 if console.use_colors else 10 format_str = f"{{addr:80}}.....{{result:>{right_align}}}" console.print_stderr( format_str.format(addr=result.address.spec, result=color(result.test_result.status.value))) merged_xml_results = await Get( Digest, MergeDigests(result.test_result.xml_results for result in results if result.test_result.xml_results), ) workspace.write_digest(merged_xml_results) if test_subsystem.use_coverage: all_coverage_data: Iterable[CoverageData] = [ result.test_result.coverage_data for result in results if result.test_result.coverage_data is not None ] coverage_types_to_collection_types: Dict[ Type[CoverageData], Type[CoverageDataCollection]] = { collection_cls.element_type: collection_cls for collection_cls in union_membership.get( CoverageDataCollection) } coverage_collections: List[CoverageDataCollection] = [] for data_cls, data in itertools.groupby(all_coverage_data, lambda data: type(data)): collection_cls = coverage_types_to_collection_types[data_cls] coverage_collections.append(collection_cls(data)) # We can create multiple reports for each coverage data (console, xml and html) coverage_reports_collections = await MultiGet( Get(CoverageReports, CoverageDataCollection, coverage_collection) for coverage_collection in coverage_collections) coverage_report_files: List[PurePath] = [] for coverage_reports in coverage_reports_collections: report_files = coverage_reports.materialize(console, workspace) coverage_report_files.extend(report_files) if coverage_report_files and test_subsystem.open_coverage: desktop.ui_open(console, interactive_runner, coverage_report_files) exit_code = (PANTS_FAILED_EXIT_CODE if any( res.test_result.status == Status.FAILURE for res in results) else PANTS_SUCCEEDED_EXIT_CODE) return Test(exit_code)
async def lint( console: Console, workspace: Workspace, targets: Targets, lint_subsystem: LintSubsystem, union_membership: UnionMembership, dist_dir: DistDir, ) -> Lint: request_types = cast("Iterable[type[StyleRequest]]", union_membership[LintRequest]) requests = tuple( request_type( request_type.field_set_type.create(target) for target in targets if request_type.field_set_type.is_applicable(target) ) for request_type in request_types ) field_sets_with_sources = await MultiGet( Get(FieldSetsWithSources, FieldSetsWithSourcesRequest(request.field_sets)) for request in requests ) valid_requests = tuple( request_cls(request) for request_cls, request in zip(request_types, field_sets_with_sources) if request ) if lint_subsystem.per_file_caching: all_per_file_results = await MultiGet( Get(LintResults, LintRequest, request.__class__([field_set])) for request in valid_requests for field_set in request.field_sets ) def key_fn(results: LintResults): return results.linter_name # NB: We must pre-sort the data for itertools.groupby() to work properly. sorted_all_per_files_results = sorted(all_per_file_results, key=key_fn) # We consolidate all results for each linter into a single `LintResults`. all_results = tuple( LintResults( itertools.chain.from_iterable( per_file_results.results for per_file_results in all_linter_results ), linter_name=linter_name, ) for linter_name, all_linter_results in itertools.groupby( sorted_all_per_files_results, key=key_fn ) ) else: all_results = await MultiGet( Get(LintResults, LintRequest, lint_request) for lint_request in valid_requests ) all_results = tuple(sorted(all_results, key=lambda results: results.linter_name)) def get_tool_name(res: LintResults) -> str: return res.linter_name write_reports( all_results, workspace, dist_dir, goal_name=LintSubsystem.name, get_tool_name=get_tool_name, ) exit_code = 0 if all_results: console.print_stderr("") for results in all_results: if results.skipped: sigil = console.sigil_skipped() status = "skipped" elif results.exit_code == 0: sigil = console.sigil_succeeded() status = "succeeded" else: sigil = console.sigil_failed() status = "failed" exit_code = results.exit_code console.print_stderr(f"{sigil} {results.linter_name} {status}.") return Lint(exit_code)