async def fmt(formatter: IsortFormatter, isort: Isort) -> FmtResult: if isort.options.skip: return FmtResult.noop() setup = await Get[Setup](SetupRequest(formatter, check_only=False)) result = await Get[ExecuteProcessResult](ExecuteProcessRequest, setup.process_request) return FmtResult.from_execute_process_result(result)
async def fmt(wrapped_target: BlackTarget, black_setup: BlackSetup) -> FmtResult: if black_setup.skip: return FmtResult.noop() args = BlackArgs.create(black_setup=black_setup, wrapped_target=wrapped_target, check_only=False) request = await Get[ExecuteProcessRequest](BlackArgs, args) result = await Get[ExecuteProcessResult](ExecuteProcessRequest, request) return FmtResult.from_execute_process_result(result)
async def fmt(wrapped_target: IsortTarget, isort_setup: IsortSetup) -> FmtResult: if isort_setup.skip: return FmtResult.noop() args = IsortArgs.create(wrapped_target=wrapped_target, isort_setup=isort_setup, check_only=False) request = await Get[ExecuteProcessRequest](IsortArgs, args) result = await Get[ExecuteProcessResult](ExecuteProcessRequest, request) return FmtResult.from_execute_process_result(result)
async def black_fmt(formatter: BlackFormatter, black: Black) -> FmtResult: if black.options.skip: return FmtResult.noop() setup = await Get[Setup](SetupRequest(formatter, check_only=False)) result = await Get[ExecuteProcessResult](ExecuteProcessRequest, setup.process_request) return FmtResult.from_execute_process_result(result)
def run_fmt_rule(self, *, targets: List[HydratedTarget]) -> Tuple[Fmt, str]: result_digest = self.request_single_product( Digest, InputFilesContent([ FileContent(path=str(self.formatted_file), content=self.formatted_content.encode()) ])) console = MockConsole(use_colors=False) result: Fmt = run_rule( fmt, rule_args=[ console, HydratedTargets(targets), Workspace(self.scheduler), UnionMembership( union_rules={FormatTarget: [PythonTargetAdaptor]}) ], mock_gets=[ MockGet(product_type=AggregatedFmtResults, subject_type=PythonTargetAdaptor, mock=lambda adaptor: AggregatedFmtResults( (FmtResult(digest=result_digest, stdout=f"Formatted `{adaptor.name}`", stderr=""), ), combined_digest=result_digest)), MockGet(product_type=Digest, subject_type=DirectoriesToMerge, mock=lambda _: result_digest), ], ) return result, console.stdout.getvalue()
def test_non_union_member_noops() -> None: result, console = run_fmt_rule( targets=[make_target(adaptor_type=JvmAppAdaptor)], mock_formatter=lambda adaptor: FmtResult( digest=EMPTY_DIRECTORY_DIGEST, stdout=f"Formatted target `{adaptor.name}`", stderr="" ), ) assert result.exit_code == 0 assert console.stdout.getvalue().strip() == ""
def language_fmt_results(self) -> LanguageFmtResults: addresses = [ adaptor_with_origin.adaptor.address for adaptor_with_origin in self.adaptors_with_origins ] # NB: Due to mocking `await Get[Digest](DirectoriesToMerge), the digest we use here does # not matter. digest = EMPTY_DIRECTORY_DIGEST return LanguageFmtResults( (FmtResult(digest=digest, stdout=self.stdout(addresses), stderr=""), ), combined_digest=digest, )
async def fmt( wrapped_target: FormattablePythonTarget, black_setup: BlackSetup, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> FmtResult: request = black_setup.create_execute_request( wrapped_target=wrapped_target, python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, check_only=False ) result = await Get(ExecuteProcessResult, ExecuteProcessRequest, request) return FmtResult( digest=result.output_directory_digest, stdout=result.stdout.decode(), stderr=result.stderr.decode(), )
def fmt_with_black( wrapped_target: FormattablePythonTarget, black_input: BlackInput, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> FmtResult: request = _generate_black_request(wrapped_target, black_input, python_setup, subprocess_encoding_environment, check_only=False) result = yield Get(ExecuteProcessResult, ExecuteProcessRequest, request) yield FmtResult( digest=result.output_directory_digest, stdout=result.stdout.decode(), stderr=result.stderr.decode(), )
async def format_python_target( python_formatters: PythonFormatters, union_membership: UnionMembership) -> LanguageFmtResults: adaptors_with_origins = python_formatters.adaptors_with_origins original_sources = await Get[SourceFiles](AllSourceFilesRequest( adaptor_with_origin.adaptor for adaptor_with_origin in adaptors_with_origins)) prior_formatter_result = original_sources.snapshot results: List[FmtResult] = [] formatters: Iterable[ Type[PythonFormatter]] = union_membership.union_rules[PythonFormatter] for formatter in formatters: result = await Get[FmtResult]( PythonFormatter, formatter(adaptors_with_origins, prior_formatter_result=prior_formatter_result), ) if result != FmtResult.noop(): results.append(result) prior_formatter_result = await Get[Snapshot](Digest, result.digest) return LanguageFmtResults( tuple(results), combined_digest=prior_formatter_result.directory_digest)
def run_black( wrapped_target: FormattablePythonTarget, black: Black, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> FmtResult: config_path = black.get_options().config config_snapshot = yield Get(Snapshot, PathGlobs(include=(config_path, ))) resolved_requirements_pex = yield Get( Pex, CreatePex( output_filename="black.pex", requirements=PexRequirements( requirements=tuple(black.get_requirement_specs())), interpreter_constraints=PexInterpreterContraints( constraint_set=frozenset( black.default_interpreter_constraints)), entry_point=black.get_entry_point(), )) target = wrapped_target.target sources_digest = target.sources.snapshot.directory_digest all_input_digests = [ sources_digest, resolved_requirements_pex.directory_digest, config_snapshot.directory_digest, ] merged_input_files = yield Get( Digest, DirectoriesToMerge(directories=tuple(all_input_digests)), ) # The exclude option from Black only works on recursive invocations, # so call black with the directories in which the files are present # and passing the full file names with the include option dirs: Set[str] = set() for filename in target.sources.snapshot.files: dirs.add(f"{Path(filename).parent}") pex_args = tuple(sorted(dirs)) if config_path: pex_args += ("--config", config_path) if target.sources.snapshot.files: pex_args += ("--include", "|".join( re.escape(f) for f in target.sources.snapshot.files)) request = resolved_requirements_pex.create_execute_request( python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, pex_path="./black.pex", pex_args=pex_args, input_files=merged_input_files, output_files=target.sources.snapshot.files, description=f'Run Black for {target.address.reference()}', ) result = yield Get(ExecuteProcessResult, ExecuteProcessRequest, request) yield FmtResult( digest=result.output_directory_digest, stdout=result.stdout.decode(), stderr=result.stderr.decode(), )
def test_skip(self) -> None: lint_result, fmt_result = self.run_black([self.bad_source], skip=True) assert lint_result == LintResult.noop() assert fmt_result == FmtResult.noop()
def test_skip(self) -> None: target = self.make_target_with_origin([self.bad_source]) lint_result, fmt_result = self.run_isort([target], skip=True) assert lint_result == LintResult.noop() assert fmt_result == FmtResult.noop()