def run_typecheck_rule( *, request_types: List[Type[TypecheckRequest]], targets: List[TargetWithOrigin], include_sources: bool = True, ) -> Tuple[int, str]: console = MockConsole(use_colors=False) union_membership = UnionMembership({TypecheckRequest: request_types}) result: Typecheck = run_rule( typecheck, rule_args=[console, TargetsWithOrigins(targets), union_membership], mock_gets=[ MockGet( product_type=TypecheckResults, subject_type=TypecheckRequest, mock=lambda field_set_collection: field_set_collection.typecheck_results, ), MockGet( product_type=FieldSetsWithSources, subject_type=FieldSetsWithSourcesRequest, mock=lambda field_sets: FieldSetsWithSources( field_sets if include_sources else () ), ), ], union_membership=union_membership, ) assert not console.stdout.getvalue() return result.exit_code, console.stderr.getvalue()
def run_typecheck_rule( *, request_types: List[Type[TypecheckRequest]], targets: List[Target], include_sources: bool = True, ) -> Tuple[int, str]: union_membership = UnionMembership({TypecheckRequest: request_types}) with mock_console(create_options_bootstrapper()) as (console, stdio_reader): result: Typecheck = run_rule_with_mocks( typecheck, rule_args=[console, Targets(targets), union_membership], mock_gets=[ MockGet( output_type=EnrichedTypecheckResults, input_type=TypecheckRequest, mock=lambda field_set_collection: field_set_collection. typecheck_results, ), MockGet( output_type=FieldSetsWithSources, input_type=FieldSetsWithSourcesRequest, mock=lambda field_sets: FieldSetsWithSources( field_sets if include_sources else ()), ), ], union_membership=union_membership, ) assert not stdio_reader.get_stdout() return result.exit_code, stdio_reader.get_stderr()
def run_lint_rule( *, lint_request_types: List[Type[LintRequest]], targets: List[TargetWithOrigin], per_target_caching: bool, include_sources: bool = True, ) -> Tuple[int, str]: console = MockConsole(use_colors=False) union_membership = UnionMembership({LintRequest: lint_request_types}) result: Lint = run_rule( lint, rule_args=[ console, TargetsWithOrigins(targets), create_goal_subsystem(LintOptions, per_target_caching=per_target_caching), union_membership, ], mock_gets=[ MockGet( product_type=LintResults, subject_type=LintRequest, mock=lambda field_set_collection: field_set_collection.lint_result, ), MockGet( product_type=FieldSetsWithSources, subject_type=FieldSetsWithSourcesRequest, mock=lambda field_sets: FieldSetsWithSources( field_sets if include_sources else () ), ), ], union_membership=union_membership, ) assert not console.stdout.getvalue() return result.exit_code, console.stderr.getvalue()
def run_lint_rule( rule_runner: RuleRunner, *, lint_request_types: List[Type[LintRequest]], targets: List[Target], per_file_caching: bool, include_sources: bool = True, ) -> Tuple[int, str]: with mock_console(rule_runner.options_bootstrapper) as (console, stdio_reader): workspace = Workspace(rule_runner.scheduler) union_membership = UnionMembership({LintRequest: lint_request_types}) result: Lint = run_rule_with_mocks( lint, rule_args=[ console, workspace, Targets(targets), create_goal_subsystem(LintSubsystem, per_file_caching=per_file_caching, per_target_caching=False), union_membership, ], mock_gets=[ MockGet( output_type=EnrichedLintResults, input_type=LintRequest, mock=lambda field_set_collection: field_set_collection. lint_results, ), MockGet( output_type=FieldSetsWithSources, input_type=FieldSetsWithSourcesRequest, mock=lambda field_sets: FieldSetsWithSources( field_sets if include_sources else ()), ), MockGet(output_type=Digest, input_type=MergeDigests, mock=lambda _: EMPTY_DIGEST), ], union_membership=union_membership, ) assert not stdio_reader.get_stdout() return result.exit_code, stdio_reader.get_stderr()
def run_lint_rule( self, *, lint_request_types: List[Type[LintRequest]], targets: List[Target], per_file_caching: bool, include_sources: bool = True, ) -> Tuple[int, str]: console = MockConsole(use_colors=False) workspace = Workspace(self.scheduler) union_membership = UnionMembership({LintRequest: lint_request_types}) result: Lint = run_rule( lint, rule_args=[ console, workspace, Targets(targets), create_goal_subsystem( LintSubsystem, per_file_caching=per_file_caching, per_target_caching=False ), union_membership, ], mock_gets=[ MockGet( product_type=LintResults, subject_type=LintRequest, mock=lambda field_set_collection: field_set_collection.lint_results, ), MockGet( product_type=FieldSetsWithSources, subject_type=FieldSetsWithSourcesRequest, mock=lambda field_sets: FieldSetsWithSources( field_sets if include_sources else () ), ), MockGet( product_type=Digest, subject_type=MergeDigests, mock=lambda _: EMPTY_DIGEST ), ], union_membership=union_membership, ) assert not console.stdout.getvalue() return result.exit_code, console.stderr.getvalue()
def run_typecheck_rule( *, request_types: List[Type[CheckRequest]], targets: List[Target], include_sources: bool = True, ) -> Tuple[int, str]: union_membership = UnionMembership({CheckRequest: request_types}) with mock_console(create_options_bootstrapper()) as (console, stdio_reader): rule_runner = RuleRunner() result: Check = run_rule_with_mocks( check, rule_args=[ console, Workspace(rule_runner.scheduler), Targets(targets), DistDir(relpath=Path("dist")), union_membership, ], mock_gets=[ MockGet( output_type=CheckResults, input_type=CheckRequest, mock=lambda field_set_collection: field_set_collection. check_results, ), MockGet( output_type=FieldSetsWithSources, input_type=FieldSetsWithSourcesRequest, mock=lambda field_sets: FieldSetsWithSources( field_sets if include_sources else ()), ), ], union_membership=union_membership, ) assert not stdio_reader.get_stdout() return result.exit_code, stdio_reader.get_stderr()
def run_test_rule( rule_runner: RuleRunner, *, field_set: Type[TestFieldSet], targets: List[Target], debug: bool = False, use_coverage: bool = False, output: ShowOutput = ShowOutput.ALL, include_sources: bool = True, valid_targets: bool = True, ) -> Tuple[int, str]: console = MockConsole(use_colors=False) test_subsystem = create_goal_subsystem( TestSubsystem, debug=debug, use_coverage=use_coverage, output=output, extra_env_vars=[], ) interactive_runner = InteractiveRunner(rule_runner.scheduler) workspace = Workspace(rule_runner.scheduler) union_membership = UnionMembership({ TestFieldSet: [field_set], CoverageDataCollection: [MockCoverageDataCollection] }) def mock_find_valid_field_sets( _: TargetRootsToFieldSetsRequest, ) -> TargetRootsToFieldSets: if not valid_targets: return TargetRootsToFieldSets({}) return TargetRootsToFieldSets( {tgt: [field_set.create(tgt)] for tgt in targets}) def mock_debug_request(_: TestFieldSet) -> TestDebugRequest: digest = rule_runner.request(Digest, [ CreateDigest((FileContent(path="program.py", content=b"def test(): pass"), )) ]) process = InteractiveProcess(["/usr/bin/python", "program.py"], input_digest=digest) return TestDebugRequest(process) def mock_coverage_report_generation( coverage_data_collection: MockCoverageDataCollection, ) -> CoverageReports: addresses = ", ".join(coverage_data.address.spec for coverage_data in coverage_data_collection) console_report = ConsoleCoverageReport(f"Ran coverage on {addresses}") return CoverageReports(reports=(console_report, )) result: Test = run_rule_with_mocks( run_tests, rule_args=[ console, test_subsystem, interactive_runner, workspace, union_membership, ], mock_gets=[ MockGet( output_type=TargetRootsToFieldSets, input_type=TargetRootsToFieldSetsRequest, mock=mock_find_valid_field_sets, ), MockGet( output_type=EnrichedTestResult, input_type=TestFieldSet, mock=lambda fs: fs.test_result, ), MockGet( output_type=TestDebugRequest, input_type=TestFieldSet, mock=mock_debug_request, ), MockGet( output_type=FieldSetsWithSources, input_type=FieldSetsWithSourcesRequest, mock=lambda field_sets: FieldSetsWithSources( field_sets if include_sources else ()), ), # Merge XML results. MockGet( output_type=Digest, input_type=MergeDigests, mock=lambda _: EMPTY_DIGEST, ), MockGet( output_type=CoverageReports, input_type=CoverageDataCollection, mock=mock_coverage_report_generation, ), MockGet( output_type=OpenFiles, input_type=OpenFilesRequest, mock=lambda _: OpenFiles(()), ), ], union_membership=union_membership, ) assert not console.stdout.getvalue() return result.exit_code, console.stderr.getvalue()
def run_test_rule( self, *, field_set: Type[TestFieldSet], targets: List[TargetWithOrigin], debug: bool = False, use_coverage: bool = False, include_sources: bool = True, valid_targets: bool = True, ) -> Tuple[int, str]: console = MockConsole(use_colors=False) options = create_goal_subsystem(TestOptions, debug=debug, use_coverage=use_coverage) interactive_runner = InteractiveRunner(self.scheduler) workspace = Workspace(self.scheduler) union_membership = UnionMembership({ TestFieldSet: [field_set], CoverageDataCollection: [MockCoverageDataCollection] }) def mock_find_valid_field_sets( _: TargetsToValidFieldSetsRequest, ) -> TargetsToValidFieldSets: if not valid_targets: return TargetsToValidFieldSets({}) return TargetsToValidFieldSets({ tgt_with_origin: [field_set.create(tgt_with_origin)] for tgt_with_origin in targets }) def mock_coordinator_of_tests( wrapped_field_set: WrappedTestFieldSet, ) -> AddressAndTestResult: field_set = cast(MockTestFieldSet, wrapped_field_set.field_set) return AddressAndTestResult(address=field_set.address, test_result=field_set.test_result) def mock_coverage_report_generation( coverage_data_collection: MockCoverageDataCollection, ) -> CoverageReports: addresses = ", ".join( coverage_data.address.spec for coverage_data in coverage_data_collection) console_report = ConsoleCoverageReport( f"Ran coverage on {addresses}") return CoverageReports(reports=(console_report, )) result: Test = run_rule( run_tests, rule_args=[ console, options, interactive_runner, workspace, union_membership ], mock_gets=[ MockGet( product_type=TargetsToValidFieldSets, subject_type=TargetsToValidFieldSetsRequest, mock=mock_find_valid_field_sets, ), MockGet( product_type=AddressAndTestResult, subject_type=WrappedTestFieldSet, mock=lambda wrapped_config: mock_coordinator_of_tests( wrapped_config), ), MockGet( product_type=TestDebugRequest, subject_type=TestFieldSet, mock=lambda _: TestDebugRequest( self.make_interactive_process()), ), MockGet( product_type=FieldSetsWithSources, subject_type=FieldSetsWithSourcesRequest, mock=lambda field_sets: FieldSetsWithSources( field_sets if include_sources else ()), ), MockGet( product_type=CoverageReports, subject_type=CoverageDataCollection, mock=mock_coverage_report_generation, ), ], union_membership=union_membership, ) assert not console.stdout.getvalue() return result.exit_code, console.stderr.getvalue()
def run_test_rule( self, *, field_set: Type[TestFieldSet], targets: List[TargetWithOrigin], debug: bool = False, use_coverage: bool = False, output: ShowOutput = ShowOutput.ALL, include_sources: bool = True, valid_targets: bool = True, ) -> Tuple[int, str]: console = MockConsole(use_colors=False) test_subsystem = create_goal_subsystem( TestSubsystem, debug=debug, use_coverage=use_coverage, output=output, ) interactive_runner = InteractiveRunner(self.scheduler) workspace = Workspace(self.scheduler) union_membership = UnionMembership({ TestFieldSet: [field_set], CoverageDataCollection: [MockCoverageDataCollection] }) def mock_find_valid_field_sets( _: TargetsToValidFieldSetsRequest, ) -> TargetsToValidFieldSets: if not valid_targets: return TargetsToValidFieldSets({}) return TargetsToValidFieldSets({ tgt_with_origin: [field_set.create(tgt_with_origin.target)] for tgt_with_origin in targets }) def mock_coverage_report_generation( coverage_data_collection: MockCoverageDataCollection, ) -> CoverageReports: addresses = ", ".join( coverage_data.address.spec for coverage_data in coverage_data_collection) console_report = ConsoleCoverageReport( f"Ran coverage on {addresses}") return CoverageReports(reports=(console_report, )) result: Test = run_rule_with_mocks( run_tests, rule_args=[ console, test_subsystem, interactive_runner, workspace, union_membership ], mock_gets=[ MockGet( product_type=TargetsToValidFieldSets, subject_type=TargetsToValidFieldSetsRequest, mock=mock_find_valid_field_sets, ), MockGet( product_type=EnrichedTestResult, subject_type=TestFieldSet, mock=lambda fs: fs.test_result, ), MockGet( product_type=TestDebugRequest, subject_type=TestFieldSet, mock=lambda _: TestDebugRequest( self.make_interactive_process()), ), MockGet( product_type=FieldSetsWithSources, subject_type=FieldSetsWithSourcesRequest, mock=lambda field_sets: FieldSetsWithSources( field_sets if include_sources else ()), ), # Merge XML results. MockGet( product_type=Digest, subject_type=MergeDigests, mock=lambda _: EMPTY_DIGEST, ), MockGet( product_type=CoverageReports, subject_type=CoverageDataCollection, mock=mock_coverage_report_generation, ), MockGet( product_type=OpenFiles, subject_type=OpenFilesRequest, mock=lambda _: OpenFiles(()), ), ], union_membership=union_membership, ) assert not console.stdout.getvalue() return result.exit_code, console.stderr.getvalue()