def rule_runner() -> RuleRunner: return RuleRunner( rules=[ *subsystem_rules(), *skip_field.rules(), *python_sources.rules(), *target_types_rules.rules(), QueryRule(Flake8FirstPartyPlugins, []), QueryRule(GeneratePythonLockfile, [Flake8LockfileSentinel]), ], target_types=[PythonSourcesGeneratorTarget, GenericTarget, PythonRequirementTarget], )
def rules(): return (*flake8_rules.rules(), *skip_field.rules(), *subsystem.rules())
def test_setup_lockfile_interpreter_constraints() -> None: rule_runner = RuleRunner( rules=[ *subsystem_rules(), *skip_field.rules(), *target_types_rules.rules(), QueryRule(PythonLockfileRequest, [Flake8LockfileSentinel]), ], target_types=[PythonSourcesGeneratorTarget, GenericTarget], ) global_constraint = "==3.9.*" rule_runner.set_options( ["--flake8-lockfile=lockfile.txt"], env={ "PANTS_PYTHON_INTERPRETER_CONSTRAINTS": f"['{global_constraint}']" }, ) def assert_ics(build_file: str, expected: list[str]) -> None: rule_runner.write_files({ "project/BUILD": build_file, "project/f.py": "" }) lockfile_request = rule_runner.request(PythonLockfileRequest, [Flake8LockfileSentinel()]) assert lockfile_request.interpreter_constraints == InterpreterConstraints( expected) assert_ics("python_sources()", [global_constraint]) assert_ics("python_sources(interpreter_constraints=['==2.7.*'])", ["==2.7.*"]) assert_ics( "python_sources(interpreter_constraints=['==2.7.*', '==3.5.*'])", ["==2.7.*", "==3.5.*"]) # If no Python targets in repo, fall back to global [python] constraints. assert_ics("target()", [global_constraint]) # Ignore targets that are skipped. assert_ics( dedent("""\ python_sources(name='a', interpreter_constraints=['==2.7.*']) python_sources(name='b', interpreter_constraints=['==3.5.*'], skip_flake8=True) """), ["==2.7.*"], ) # If there are multiple distinct ICs in the repo, we OR them. This is because Flake8 will # group into each distinct IC. assert_ics( dedent("""\ python_sources(name='a', interpreter_constraints=['==2.7.*']) python_sources(name='b', interpreter_constraints=['==3.5.*']) """), ["==2.7.*", "==3.5.*"], ) assert_ics( dedent("""\ python_sources(name='a', interpreter_constraints=['==2.7.*', '==3.5.*']) python_sources(name='b', interpreter_constraints=['>=3.5']) """), ["==2.7.*", "==3.5.*", ">=3.5"], ) assert_ics( dedent("""\ python_sources(name='a') python_sources(name='b', interpreter_constraints=['==2.7.*']) python_sources(name='c', interpreter_constraints=['>=3.6']) """), ["==2.7.*", global_constraint, ">=3.6"], )