pants icon indicating copy to clipboard operation
pants copied to clipboard

Add `[flake8].extra_files` to allow configuring plugins like Bandit

Open ShantanuKumar opened this issue 2 years ago • 2 comments

Adds a new option for passing config files used byflake8 plugin.

Fixes https://github.com/pantsbuild/pants/issues/15225

[ci skip-rust] [ci skip-build-wheels]

ShantanuKumar avatar Aug 10 '22 18:08 ShantanuKumar

I am trying to add a test but I think I am missing some understanding of how options etc are being passed in test

This is how the modified test_3rdparty_plugin looks like.

def test_3rdparty_plugin(rule_runner: RuleRunner) -> None:
    rule_runner.write_files(
        {"f.py": "'constant' and 'constant2'\n", "BUILD": "python_sources(name='t')"}
    )
    tgt = rule_runner.get_target(Address("", target_name="t", relative_file_path="f.py"))
    result = run_flake8(
        rule_runner,
        [tgt],
        extra_args=[
            "--flake8-extra-requirements=flake8-pantsbuild>=2.0,<3",
            "--flake8-lockfile=<none>",
        ],
    )
    assert len(result) == 1
    assert result[0].exit_code == 1
    assert "f.py:1:1: PB11" in result[0].stdout

    # Test extra_files option
    rule_runner.write_files(
        {
            "f.py": "assert None == 1\n",
            ".bandit": 'skips: ["B101"]\n',
            "BUILD": "python_sources(name='t')",
        }
    )
    tgt = rule_runner.get_target(Address("", target_name="t", relative_file_path="f.py"))
    result = run_flake8(
        rule_runner,
        [tgt],
        extra_args=[
            "--flake8-extra-requirements=flake8-bandit==3.0.0",
            "--flake8-lockfile=<none>",
            "--flake8-extra-files=[.bandit]",
        ],
    )
    assert len(result) == 1
    assert result[0].exit_code == 0

ShantanuKumar avatar Aug 10 '22 19:08 ShantanuKumar

By the way how are these options propagated to the reference? Is this automated?

ShantanuKumar avatar Aug 10 '22 19:08 ShantanuKumar

am trying to add a test but I think I am missing some understanding of how options etc are being passed in test

Hm what's the issue? Btw, I think it's fine to remove the flake8-pantsbuild part and only do the Bandit plugin. That is sufficient to prove that 3rd-party plugins can work.

By the way how are these options propagated to the reference?

A maintainer will run the generate_docs.py script to automatically update based on the help message you wrote :)

Eric-Arellano avatar Aug 10 '22 23:08 Eric-Arellano

So it's just not this new test that I am trying to add which is failing. I think something else is wrong

./pants --changed-since=HEAD test
08:29:13.05 [ERROR] Completed: Run Pytest - src/python/pants/backend/python/lint/flake8/rules_integration_test.py:tests failed (exit code 2).
============================= test session starts ==============================
collected 0 items / 1 error

==================================== ERRORS ====================================
_ ERROR collecting src/python/pants/backend/python/lint/flake8/rules_integration_test.py _
ImportError while importing test module '/private/var/folders/9w/9_4n35r57d707zkrk86rvh9w0000gn/T/pants-sandbox-EgsQUA/src/python/pants/backend/python/lint/flake8/rules_integration_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/Users/developer/.pyenv/versions/3.7.9/lib/python3.7/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
src/python/pants/backend/python/lint/flake8/rules_integration_test.py:10: in <module>
    from pants.backend.python import target_types_rules
src/python/pants/backend/python/target_types_rules.py:19: in <module>
    from pants.backend.python.dependency_inference.module_mapper import (
src/python/pants/backend/python/dependency_inference/module_mapper.py:21: in <module>
    from pants.backend.python.subsystems.setup import PythonSetup
src/python/pants/backend/python/subsystems/setup.py:19: in <module>
    from pants.option.subsystem import Subsystem
src/python/pants/option/subsystem.py:12: in <module>
    from pants.engine.internals.selectors import AwaitableConstraints, Get
src/python/pants/engine/internals/selectors.py:24: in <module>
    from pants.engine.internals.native_engine import (
E   ImportError: dlopen(/private/var/folders/9w/9_4n35r57d707zkrk86rvh9w0000gn/T/pants-sandbox-EgsQUA/src/python/pants/engine/internals/native_engine.so, 0x0002): symbol not found in flat namespace (_PyCMethod_New)
- generated xml file: /private/var/folders/9w/9_4n35r57d707zkrk86rvh9w0000gn/T/pants-sandbox-EgsQUA/src.python.pants.backend.python.lint.flake8.rules_integration_test.py.tests.xml -
=========================== short test summary info ============================
ERROR src/python/pants/backend/python/lint/flake8/rules_integration_test.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.52s ===============================

ShantanuKumar avatar Aug 11 '22 06:08 ShantanuKumar

Yeah, that's not related to options. That's that the native engine is not loading for you during test runs. Would you be open posting about this in #development in Slack? It will probably be easier to help you debug there.

(In the meantime, feel free to push the code to this PR and we can have CI see that the test works. Not ideal, but to unblock you.)

Eric-Arellano avatar Aug 11 '22 14:08 Eric-Arellano