forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl_integration_test.py
118 lines (104 loc) · 3.93 KB
/
repl_integration_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from __future__ import annotations
from textwrap import dedent
import pytest
from pants.backend.codegen.protobuf.target_types import ProtobufSourceTarget
from pants.backend.python.goals import repl as python_repl
from pants.backend.python.subsystems.ipython import rules as ipython_subsystem_rules
from pants.backend.python.subsystems.setup import PythonSetup
from pants.backend.python.target_types import (
PythonRequirementTarget,
PythonSourcesGeneratorTarget,
PythonSourceTarget,
)
from pants.backend.python.util_rules import local_dists, pex_from_targets
from pants.backend.python.util_rules.pex import PexProcess
from pants.backend.python.util_rules.pex_from_targets import NoCompatibleResolveException
from pants.core.goals.repl import Repl
from pants.core.goals.repl import rules as repl_rules
from pants.engine.process import Process
from pants.testutil.python_interpreter_selection import all_major_minor_python_versions
from pants.testutil.rule_runner import (
GoalRuleResult,
QueryRule,
RuleRunner,
engine_error,
mock_console,
)
@pytest.fixture
def rule_runner() -> RuleRunner:
rule_runner = RuleRunner(
rules=[
*repl_rules(),
*ipython_subsystem_rules(),
*python_repl.rules(),
*pex_from_targets.rules(),
*local_dists.rules(),
QueryRule(Process, (PexProcess,)),
],
target_types=[
PythonSourcesGeneratorTarget,
ProtobufSourceTarget,
PythonSourceTarget,
PythonRequirementTarget,
],
)
rule_runner.write_files(
{
"src/python/foo.proto": 'syntax = "proto3";message Foo {}',
"src/python/lib.py": "from foo import Foo\nclass SomeClass:\n pass\n",
"src/python/BUILD": (
"protobuf_source(name='proto', source='foo.proto')\n"
"python_sources(dependencies=[':proto'])"
),
}
)
return rule_runner
def run_repl(
rule_runner: RuleRunner, args: list[str], *, global_args: list[str] | None = None
) -> GoalRuleResult:
# TODO(#9108): Expand `mock_console` to allow for providing input for the repl to verify
# that, e.g., the generated protobuf code is available. Right now this test prepares for
# that by including generated code, but cannot actually verify it.
with mock_console(rule_runner.options_bootstrapper):
return rule_runner.run_goal_rule(
Repl,
global_args=global_args or (),
args=args,
env_inherit={"PATH", "PYENV_ROOT", "HOME"},
)
def test_default_repl(rule_runner: RuleRunner) -> None:
assert run_repl(rule_runner, ["src/python/lib.py"]).exit_code == 0
@pytest.mark.platform_specific_behavior
@pytest.mark.parametrize(
"major_minor_interpreter",
all_major_minor_python_versions(PythonSetup.default_interpreter_constraints),
)
def test_ipython(rule_runner: RuleRunner, major_minor_interpreter: str) -> None:
assert (
run_repl(
rule_runner,
["src/python/lib.py"],
global_args=[
"--repl-shell=ipython",
f"--python-interpreter-constraints=['=={major_minor_interpreter}.*']",
],
).exit_code
== 0
)
def test_eagerly_validate_roots_have_common_resolve(rule_runner: RuleRunner) -> None:
rule_runner.write_files(
{
"BUILD": dedent(
"""\
python_requirement(name='t1', requirements=[], resolve='a')
python_source(name='t2', source='f.py', resolve='b')
"""
)
}
)
with engine_error(NoCompatibleResolveException, contains="./pants peek"):
run_repl(
rule_runner, ["//:t1", "//:t2"], global_args=["--python-resolves={'a': '', 'b': ''}"]
)