async def lint(wrapped_target: IsortTarget, isort_setup: IsortSetup) -> LintResult: if isort_setup.skip: return LintResult.noop() args = IsortArgs.create(wrapped_target=wrapped_target, isort_setup=isort_setup, check_only=True) request = await Get[ExecuteProcessRequest](IsortArgs, args) result = await Get[FallibleExecuteProcessResult](ExecuteProcessRequest, request) return LintResult.from_fallible_execute_process_result(result)
async def lint(wrapped_target: BlackTarget, black_setup: BlackSetup) -> LintResult: if black_setup.skip: return LintResult.noop() args = BlackArgs.create(black_setup=black_setup, wrapped_target=wrapped_target, check_only=True) request = await Get[ExecuteProcessRequest](BlackArgs, args) result = await Get[FallibleExecuteProcessResult](ExecuteProcessRequest, request) return LintResult.from_fallible_execute_process_result(result)
async def lint(formatter: IsortFormatter, isort: Isort) -> LintResult: if isort.options.skip: return LintResult.noop() setup = await Get[Setup](SetupRequest(formatter, check_only=True)) result = await Get[FallibleExecuteProcessResult](ExecuteProcessRequest, setup.process_request) return LintResult.from_fallible_execute_process_result(result)
def mock_linters(adaptor: PythonTargetAdaptor) -> LintResults: name = adaptor.name if name == "bad": return LintResults([ LintResult(exit_code=0, stdout=f"Linter 1 passed for `{name}`", stderr=""), LintResult(exit_code=127, stdout=f"Linter 2 failed for `{name}`", stderr=""), ]) return LintResults([ LintResult(exit_code=0, stdout=f"Linter 1 passed for `{name}`", stderr=""), LintResult(exit_code=0, stdout=f"Linter 2 passed for `{name}`", stderr=""), ])
async def lint( wrapped_target: Flake8Target, flake8: Flake8, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> LintResult: if flake8.options.skip: return LintResult.noop() target = wrapped_target.target # NB: Flake8 output depends upon which Python interpreter version it's run with. We ensure that # each target runs with its own interpreter constraints. See # http://flake8.pycqa.org/en/latest/user/invocation.html. interpreter_constraints = PexInterpreterConstraints.create_from_adaptors( adaptors=[target] if isinstance(target, PythonTargetAdaptor) else [], python_setup=python_setup ) config_path: Optional[str] = flake8.options.config config_snapshot = await Get[Snapshot]( PathGlobs(include=tuple([config_path] if config_path else [])) ) requirements_pex = await Get[Pex]( CreatePex( output_filename="flake8.pex", requirements=PexRequirements(requirements=tuple(flake8.get_requirement_specs())), interpreter_constraints=interpreter_constraints, entry_point=flake8.get_entry_point(), ) ) merged_input_files = await Get[Digest]( DirectoriesToMerge( directories=( target.sources.snapshot.directory_digest, requirements_pex.directory_digest, config_snapshot.directory_digest, ) ), ) request = requirements_pex.create_execute_request( python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, pex_path=f'./flake8.pex', pex_args=generate_args(wrapped_target, flake8), input_files=merged_input_files, description=f'Run Flake8 for {target.address.reference()}', ) result = await Get[FallibleExecuteProcessResult](ExecuteProcessRequest, request) return LintResult.from_fallible_execute_process_result(result)
async def lint( wrapped_target: BanditTarget, bandit: Bandit, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> LintResult: if bandit.options.skip: return LintResult.noop() target = wrapped_target.target # NB: Bandit output depends upon which Python interpreter version it's run with. We ensure that # each target runs with its own interpreter constraints. See # https://github.com/PyCQA/bandit#under-which-version-of-python-should-i-install-bandit. interpreter_constraints = PexInterpreterConstraints.create_from_adaptors( adaptors=[target] if isinstance(target, PythonTargetAdaptor) else [], python_setup=python_setup) config_path: Optional[str] = bandit.options.config config_snapshot = await Get[Snapshot](PathGlobs( globs=tuple([config_path] if config_path else []), glob_match_error_behavior=GlobMatchErrorBehavior.error, description_of_origin="the option `--bandit-config`", )) requirements_pex = await Get[Pex](CreatePex( output_filename="bandit.pex", requirements=PexRequirements( requirements=tuple(bandit.get_requirement_specs())), interpreter_constraints=interpreter_constraints, entry_point=bandit.get_entry_point(), )) merged_input_files = await Get[Digest](DirectoriesToMerge(directories=( target.sources.snapshot.directory_digest, requirements_pex.directory_digest, config_snapshot.directory_digest, )), ) request = requirements_pex.create_execute_request( python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, pex_path=f'./bandit.pex', pex_args=generate_args(wrapped_target, bandit), input_files=merged_input_files, description=f'Run Bandit for {target.address.reference()}', ) result = await Get[FallibleExecuteProcessResult](ExecuteProcessRequest, request) return LintResult.from_fallible_execute_process_result(result)
def test_non_union_member_noops() -> None: result, console = run_lint_rule( targets=[make_target(adaptor_type=JvmAppAdaptor)], mock_linter=lambda target: LintResult(exit_code=1, stdout="", stderr=""), ) assert result.exit_code == 0 assert console.stdout.getvalue().strip() == ""
def lint_result(self) -> LintResult: addresses = [ adaptor_with_origin.adaptor.address for adaptor_with_origin in self.adaptors_with_origins ] return LintResult(self.exit_code(addresses), self.stdout(addresses), "")
def test_precise_file_args(self) -> None: target = self.make_target_with_origin( [self.good_source, self.bad_source], origin=FilesystemLiteralSpec(self.good_source.path)) lint_result, fmt_result = self.run_docformatter([target]) assert lint_result == LintResult.noop() assert fmt_result.digest == self.get_digest( [self.good_source, self.bad_source])
async def lint(wrapped_target: FormattablePythonTarget, isort_setup: IsortSetup) -> LintResult: args = IsortArgs.create(wrapped_target=wrapped_target, isort_setup=isort_setup, check_only=True) request = await Get[ExecuteProcessRequest](IsortArgs, args) result = await Get(FallibleExecuteProcessResult, ExecuteProcessRequest, request) return LintResult.from_fallible_execute_process_result(result)
def test_respects_passthrough_args(self) -> None: needs_config = FileContent( path="test/config.py", content= b'"""\nOne line docstring acting like it\'s multiline.\n"""\n', ) target = self.make_target_with_origin([needs_config]) lint_result, fmt_result = self.run_docformatter( [target], passthrough_args="--make-summary-multi-line") assert lint_result == LintResult.noop() assert fmt_result.digest == self.get_digest([needs_config])
async def lint( wrapped_target: FormattablePythonTarget, black_setup: BlackSetup, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> LintResult: request = black_setup.create_execute_request( wrapped_target=wrapped_target, python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, check_only=True ) result = await Get(FallibleExecuteProcessResult, ExecuteProcessRequest, request) return LintResult( exit_code=result.exit_code, stdout=result.stdout.decode(), stderr=result.stderr.decode(), )
def lint_with_black( wrapped_target: FormattablePythonTarget, black_input: BlackInput, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> LintResult: request = _generate_black_request(wrapped_target, black_input, python_setup, subprocess_encoding_environment, check_only=True) result = yield Get(FallibleExecuteProcessResult, ExecuteProcessRequest, request) yield LintResult( exit_code=result.exit_code, stdout=result.stdout.decode(), stderr=result.stderr.decode(), )
def run_lint_rule( *, targets: List[HydratedTarget], mock_linter: Optional[Callable[[PythonTargetAdaptor], LintResult]] = None, ) -> Tuple[Lint, str]: if mock_linter is None: mock_linter = lambda target_adaptor: LintResult( exit_code=1, stdout=f"Linted the target `{target_adaptor.name}`", stderr="" ) console = MockConsole(use_colors=False) result: Lint = run_rule( lint, rule_args=[ console, HydratedTargets(targets), UnionMembership(union_rules={TargetWithSources: [PythonTargetAdaptor]}) ], mock_gets=[ MockGet(product_type=LintResult, subject_type=PythonTargetAdaptor, mock=mock_linter), ], ) return result, console.stdout.getvalue()
async def lint( linter: Flake8Linter, flake8: Flake8, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> LintResult: if flake8.options.skip: return LintResult.noop() adaptors_with_origins = linter.adaptors_with_origins # NB: Flake8 output depends upon which Python interpreter version it's run with. We ensure that # each target runs with its own interpreter constraints. See # http://flake8.pycqa.org/en/latest/user/invocation.html. interpreter_constraints = PexInterpreterConstraints.create_from_adaptors( (adaptor_with_origin.adaptor for adaptor_with_origin in adaptors_with_origins), python_setup=python_setup, ) requirements_pex = await Get[Pex](CreatePex( output_filename="flake8.pex", requirements=PexRequirements(flake8.get_requirement_specs()), interpreter_constraints=interpreter_constraints, entry_point=flake8.get_entry_point(), )) config_path: Optional[str] = flake8.options.config config_snapshot = await Get[Snapshot](PathGlobs( globs=tuple([config_path] if config_path else []), glob_match_error_behavior=GlobMatchErrorBehavior.error, description_of_origin="the option `--flake8-config`", )) all_source_files = await Get[SourceFiles](AllSourceFilesRequest( adaptor_with_origin.adaptor for adaptor_with_origin in adaptors_with_origins)) specified_source_files = await Get[SourceFiles]( SpecifiedSourceFilesRequest(adaptors_with_origins)) merged_input_files = await Get[Digest](DirectoriesToMerge(directories=( all_source_files.snapshot.directory_digest, requirements_pex.directory_digest, config_snapshot.directory_digest, )), ) address_references = ", ".join( sorted(adaptor_with_origin.adaptor.address.reference() for adaptor_with_origin in adaptors_with_origins)) request = requirements_pex.create_execute_request( python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, pex_path=f"./flake8.pex", pex_args=generate_args(specified_source_files=specified_source_files, flake8=flake8), input_files=merged_input_files, description=f"Run Flake8 for {address_references}", ) result = await Get[FallibleExecuteProcessResult](ExecuteProcessRequest, request) return LintResult.from_fallible_execute_process_result(result)
async def lint( linter: PylintLinter, pylint: Pylint, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> LintResult: if pylint.options.skip: return LintResult.noop() adaptors_with_origins = linter.adaptors_with_origins # Pylint needs direct dependencies in the chroot to ensure that imports are valid. However, it # doesn't lint those direct dependencies nor does it care about transitive dependencies. hydrated_targets = [ HydratedTarget(adaptor_with_origin.adaptor) for adaptor_with_origin in adaptors_with_origins ] dependencies = await MultiGet( Get[HydratedTarget](Address, dependency) for dependency in itertools.chain.from_iterable( ht.adaptor.dependencies for ht in hydrated_targets)) chrooted_python_sources = await Get[ImportablePythonSources]( HydratedTargets([*hydrated_targets, *dependencies])) # NB: Pylint output depends upon which Python interpreter version it's run with. We ensure that # each target runs with its own interpreter constraints. See # http://pylint.pycqa.org/en/latest/faq.html#what-versions-of-python-is-pylint-supporting. interpreter_constraints = PexInterpreterConstraints.create_from_adaptors( (adaptor_with_origin.adaptor for adaptor_with_origin in adaptors_with_origins), python_setup=python_setup, ) requirements_pex = await Get[Pex](PexRequest( output_filename="pylint.pex", requirements=PexRequirements(pylint.get_requirement_specs()), interpreter_constraints=interpreter_constraints, entry_point=pylint.get_entry_point(), )) config_path: Optional[str] = pylint.options.config config_snapshot = await Get[Snapshot](PathGlobs( globs=tuple([config_path] if config_path else []), glob_match_error_behavior=GlobMatchErrorBehavior.error, description_of_origin="the option `--pylint-config`", )) merged_input_files = await Get[Digest](DirectoriesToMerge(directories=( requirements_pex.directory_digest, config_snapshot.directory_digest, chrooted_python_sources.snapshot.directory_digest, )), ) specified_source_files = await Get[SourceFiles]( LegacySpecifiedSourceFilesRequest(adaptors_with_origins, strip_source_roots=True)) address_references = ", ".join( sorted(adaptor_with_origin.adaptor.address.reference() for adaptor_with_origin in adaptors_with_origins)) request = requirements_pex.create_execute_request( python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, pex_path=f"./pylint.pex", pex_args=generate_args(specified_source_files=specified_source_files, pylint=pylint), input_files=merged_input_files, description=f"Run Pylint for {address_references}", ) result = await Get[FallibleExecuteProcessResult](ExecuteProcessRequest, request) return LintResult.from_fallible_execute_process_result(result)
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()
def test_passing_source(self) -> None: target = self.make_target_with_origin([self.good_source]) lint_result, fmt_result = self.run_docformatter([target]) assert lint_result == LintResult.noop() assert fmt_result.digest == self.get_digest([self.good_source])
def test_skip(self) -> None: result = self.run_bandit([self.bad_source], skip=True) assert result == LintResult.noop()
def mock_linter(adaptor: PythonTargetAdaptor) -> LintResult: if adaptor.name == "bad": return LintResult(exit_code=127, stdout="failure", stderr="") return LintResult(exit_code=0, stdout=f"Linted the target `{adaptor.name}`", stderr="")
def test_skip(self) -> None: target = self.make_target_with_origin([self.bad_source]) result = self.run_bandit([target], skip=True) assert result == LintResult.noop()
async def lint( linter: BanditLinter, bandit: Bandit, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> LintResult: if bandit.options.skip: return LintResult.noop() adaptors_with_origins = linter.adaptors_with_origins # NB: Bandit output depends upon which Python interpreter version it's run with. We ensure that # each target runs with its own interpreter constraints. See # https://github.com/PyCQA/bandit#under-which-version-of-python-should-i-install-bandit. interpreter_constraints = PexInterpreterConstraints.create_from_adaptors( (adaptor_with_origin.adaptor for adaptor_with_origin in adaptors_with_origins), python_setup=python_setup, ) requirements_pex = await Get[Pex](PexRequest( output_filename="bandit.pex", requirements=PexRequirements(bandit.get_requirement_specs()), interpreter_constraints=interpreter_constraints, entry_point=bandit.get_entry_point(), )) config_path: Optional[str] = bandit.options.config config_snapshot = await Get[Snapshot](PathGlobs( globs=tuple([config_path] if config_path else []), glob_match_error_behavior=GlobMatchErrorBehavior.error, description_of_origin="the option `--bandit-config`", )) all_source_files = await Get[SourceFiles](LegacyAllSourceFilesRequest( adaptor_with_origin.adaptor for adaptor_with_origin in adaptors_with_origins)) specified_source_files = await Get[SourceFiles]( LegacySpecifiedSourceFilesRequest(adaptors_with_origins)) merged_input_files = await Get[Digest](DirectoriesToMerge(directories=( all_source_files.snapshot.directory_digest, requirements_pex.directory_digest, config_snapshot.directory_digest, )), ) address_references = ", ".join( sorted(adaptor_with_origin.adaptor.address.reference() for adaptor_with_origin in adaptors_with_origins)) request = requirements_pex.create_execute_request( python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, pex_path=f"./bandit.pex", pex_args=generate_args(specified_source_files=specified_source_files, bandit=bandit), input_files=merged_input_files, description=f"Run Bandit for {address_references}", ) result = await Get[FallibleExecuteProcessResult](ExecuteProcessRequest, request) return LintResult.from_fallible_execute_process_result(result)
def mock_linters(_: PythonTargetAdaptor) -> LintResults: return LintResults([ LintResult(exit_code=0, stdout=f"Linter 1", stderr=""), LintResult(exit_code=1, stdout=f"Linter 2", stderr=""), ])