def run_typecheck_rule( *, request_types: List[Type[TypecheckRequest]], targets: List[Target], include_sources: bool = True, ) -> Tuple[int, str]: console = MockConsole(use_colors=False) union_membership = UnionMembership({TypecheckRequest: request_types}) result: Typecheck = run_rule_with_mocks( typecheck, rule_args=[console, Targets(targets), union_membership], mock_gets=[ MockGet( output_type=TypecheckResults, 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 console.stdout.getvalue() return result.exit_code, console.stderr.getvalue()
def run_goal( targets: List[MockTarget], *, show_documented: bool = False, show_provides: bool = False, provides_columns: Optional[str] = None, ) -> Tuple[str, str]: console = MockConsole(use_colors=False) run_rule_with_mocks( list_targets, rule_args=[ Addresses(tgt.address for tgt in targets), create_goal_subsystem( ListSubsystem, sep="\\n", output_file=None, documented=show_documented, provides=show_provides, provides_columns=provides_columns or "address,artifact_id", ), console, ], mock_gets=[ MockGet( product_type=UnexpandedTargets, subject_type=Addresses, mock=lambda _: UnexpandedTargets(targets), ) ], ) return cast(str, console.stdout.getvalue()), cast(str, console.stderr.getvalue())
def test_failed_run(self) -> None: console = MockConsole(use_colors=False) program_text = b'#!/usr/bin/python\nraise RuntimeError("foo")' res = self.single_target_run(console=console, program_text=program_text, address_spec="some/addr") assert res.exit_code == 1
def run_goal( targets: Sequence[Target], *, target_type: Optional[List[str]] = None, address_regex: Optional[List[str]] = None, tag_regex: Optional[List[str]] = None, ) -> str: console = MockConsole(use_colors=False) run_rule_with_mocks( filter_targets, rule_args=[ Targets(targets), create_goal_subsystem( FilterSubsystem, sep="\\n", output_file=None, target_type=target_type or [], address_regex=address_regex or [], tag_regex=tag_regex or [], # Deprecated. type=[], target=[], regex=[], ancestor=[], ), console, RegisteredTargetTypes.create({type(tgt) for tgt in targets}), ], ) assert not console.stderr.getvalue() return cast(str, console.stdout.getvalue())
def test_line_oriented_goal() -> None: class OutputtingGoalOptions(LineOriented, GoalSubsystem): name = "dummy" class OutputtingGoal(Goal): subsystem_cls = OutputtingGoalOptions @goal_rule def output_rule(console: Console, options: OutputtingGoalOptions) -> OutputtingGoal: with options.output(console) as write_stdout: write_stdout("output...") with options.line_oriented(console) as print_stdout: print_stdout("line oriented") return OutputtingGoal(0) console = MockConsole() result: OutputtingGoal = run_rule_with_mocks( output_rule, rule_args=[ console, create_goal_subsystem(OutputtingGoalOptions, sep="\\n", output_file=None), ], ) assert result.exit_code == 0 assert console.stdout.getvalue() == "output...line oriented\n"
def test_failed_run(rule_runner: RuleRunner) -> None: console = MockConsole(use_colors=False) program_text = b'#!/usr/bin/python\nraise RuntimeError("foo")' res = single_target_run(rule_runner, Address("some/addr"), console, program_text=program_text) assert res.exit_code == 1
def test_normal_run(self) -> None: console = MockConsole(use_colors=False) program_text = b'#!/usr/bin/python\nprint("hello")' res = self.single_target_run( console=console, program_text=program_text, address_spec="some/addr", ) assert res.exit_code == 0
def test_normal_run(rule_runner: RuleRunner) -> None: console = MockConsole(use_colors=False) program_text = b'#!/usr/bin/python\nprint("hello")' res = single_target_run( rule_runner, Address("some/addr"), console, program_text=program_text, ) assert res.exit_code == 0
def run_fmt_rule( rule_runner: RuleRunner, *, language_target_collection_types: List[Type[LanguageFmtTargets]], targets: List[Target], result_digest: Digest, per_file_caching: bool, include_sources: bool = True, ) -> str: console = MockConsole(use_colors=False) union_membership = UnionMembership( {LanguageFmtTargets: language_target_collection_types}) result: Fmt = run_rule_with_mocks( fmt, rule_args=[ console, Targets(targets), create_goal_subsystem(FmtSubsystem, per_file_caching=per_file_caching, per_target_caching=False), Workspace(rule_runner.scheduler), union_membership, ], mock_gets=[ MockGet( output_type=LanguageFmtResults, input_type=LanguageFmtTargets, mock=lambda language_targets_collection: language_targets_collection.language_fmt_results(result_digest ), ), MockGet( output_type=TargetsWithSources, input_type=TargetsWithSourcesRequest, mock=lambda tgts: TargetsWithSources(tgts if include_sources else ()), ), MockGet( output_type=Digest, input_type=MergeDigests, mock=lambda _: result_digest, ), ], union_membership=union_membership, ) assert result.exit_code == 0 assert not console.stdout.getvalue() return cast(str, console.stderr.getvalue())
def run_goal( *, union_membership: Optional[UnionMembership] = None, details_target: Optional[str] = None ) -> str: console = MockConsole(use_colors=False) run_rule_with_mocks( list_target_types, rule_args=[ RegisteredTargetTypes.create([FortranBinary, FortranLibrary, FortranTests]), union_membership or UnionMembership({}), create_goal_subsystem( TargetTypesSubsystem, sep="\\n", output_file=None, details=details_target ), console, PantsBin(name="./BNF"), ], ) return cast(str, console.stdout.getvalue())
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_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( 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_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 test_tailor_rule(rule_runner: RuleRunner) -> None: console = MockConsole(use_colors=False) workspace = Workspace(rule_runner.scheduler) union_membership = UnionMembership({PutativeTargetsRequest: [MockPutativeTargetsRequest]}) run_rule_with_mocks( tailor.tailor, rule_args=[ create_goal_subsystem(TailorSubsystem, build_file_indent=" "), console, workspace, union_membership, ], mock_gets=[ MockGet( output_type=PutativeTargets, input_type=PutativeTargetsRequest, mock=lambda req: PutativeTargets( [ PutativeTarget.for_target_type( FortranTests, "src/fortran/foo", "tests", ["bar1_test.f90"] ), PutativeTarget.for_target_type( FortranLibrary, "src/fortran/baz", "baz", ["qux1.f90"] ), PutativeTarget.for_target_type( FortranLibrary, "src/fortran/conflict", "conflict", ["conflict1.f90", "conflict2.f90"], ), ] ), ), MockGet( output_type=UniquelyNamedPutativeTargets, input_type=PutativeTargets, mock=lambda pts: UniquelyNamedPutativeTargets( PutativeTargets( [pt.rename("conflict0") if pt.name == "conflict" else pt for pt in pts] ) ), ), MockGet( output_type=DisjointSourcePutativeTarget, input_type=PutativeTarget, # This test exists to test the console output, which isn't affected by # whether the sources of a putative target were modified due to conflict, # so we don't bother to inject such modifications. The BUILD file content # generation, which is so affected, is tested separately above. mock=lambda pt: DisjointSourcePutativeTarget(pt), ), MockGet( output_type=EditedBuildFiles, input_type=EditBuildFilesRequest, mock=lambda _: EditedBuildFiles( # We test that the created digest contains what we expect above, and we # don't need to test here that writing digests to the Workspace works. # So the empty digest is sufficient. digest=EMPTY_DIGEST, created_paths=("src/fortran/baz/BUILD",), updated_paths=( "src/fortran/foo/BUILD", "src/fortran/conflict/BUILD", ), ), ), ], union_membership=union_membership, ) stdout_str = console.stdout.getvalue() assert ( "Created src/fortran/baz/BUILD:\n - Added fortran_library target src/fortran/baz" in stdout_str ) assert ( "Updated src/fortran/foo/BUILD:\n - Added fortran_tests target src/fortran/foo:tests" in stdout_str ) assert ( "Updated src/fortran/conflict/BUILD:\n - Added fortran_library target " "src/fortran/conflict:conflict0" ) in stdout_str
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()