예제 #1
0
def test_setup_lockfile_interpreter_constraints() -> None:
    rule_runner = RuleRunner(
        rules=[
            *subsystem_rules(),
            *skip_field.rules(),
            QueryRule(PythonLockfileRequest, [BlackLockfileSentinel]),
        ],
        target_types=[PythonSourcesGeneratorTarget, GenericTarget],
    )

    global_constraint = "==3.9.*"
    rule_runner.set_options(
        ["--black-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})
        lockfile_request = rule_runner.request(PythonLockfileRequest,
                                               [BlackLockfileSentinel()])
        assert lockfile_request.interpreter_constraints == InterpreterConstraints(
            expected)

    # If all code is Py38+, use those constraints. Otherwise, use subsystem constraints.
    assert_ics("python_sources()", [global_constraint])
    assert_ics("python_sources(interpreter_constraints=['==3.10.*'])",
               ["==3.10.*"])
    assert_ics(
        "python_sources(interpreter_constraints=['==3.8.*', '==3.10.*'])",
        ["==3.8.*", "==3.10.*"])

    assert_ics(
        "python_sources(interpreter_constraints=['==3.6.*'])",
        Black.default_interpreter_constraints,
    )
    assert_ics(
        dedent("""\
            python_sources(name='t1', interpreter_constraints=['==3.6.*'])
            python_sources(name='t2', interpreter_constraints=['==3.8.*'])
            """),
        Black.default_interpreter_constraints,
    )
    assert_ics(
        dedent("""\
            python_sources(name='t1', interpreter_constraints=['==3.6.*', '>=3.8'])
            python_sources(name='t2', interpreter_constraints=['==3.8.*'])
            """),
        Black.default_interpreter_constraints,
    )

    # Ignore targets that are skipped.
    assert_ics(
        dedent("""\
            python_sources(name='a', interpreter_constraints=['==3.6.*'], skip_black=True)
            python_sources(name='b', interpreter_constraints=['==3.8.*'])
            """),
        ["==3.8.*"],
    )
예제 #2
0
def rules():
    return (*black_rules.rules(), *python_fmt.rules(), *skip_field.rules(),
            *subsystem.rules())