-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
rules_integration_test.py
204 lines (179 loc) · 6.7 KB
/
rules_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from textwrap import dedent
from typing import List
import pytest
from pants.backend.codegen.protobuf.python import additional_fields
from pants.backend.codegen.protobuf.python.rules import GeneratePythonFromProtobufRequest
from pants.backend.codegen.protobuf.python.rules import rules as protobuf_rules
from pants.backend.codegen.protobuf.target_types import ProtobufLibrary, ProtobufSources
from pants.core.util_rules import determine_source_files, strip_source_roots
from pants.engine.addresses import Address
from pants.engine.internals.scheduler import ExecutionError
from pants.engine.rules import RootRule
from pants.engine.target import (
GeneratedSources,
HydratedSources,
HydrateSourcesRequest,
WrappedTarget,
)
from pants.source.source_root import NoSourceRootError
from pants.testutil.engine.util import Params
from pants.testutil.external_tool_test_base import ExternalToolTestBase
from pants.testutil.option.util import create_options_bootstrapper
class ProtobufPythonIntegrationTest(ExternalToolTestBase):
@classmethod
def target_types(cls):
return [ProtobufLibrary]
@classmethod
def rules(cls):
return (
*super().rules(),
*protobuf_rules(),
*additional_fields.rules(),
*determine_source_files.rules(),
*strip_source_roots.rules(),
RootRule(GeneratePythonFromProtobufRequest),
)
def assert_files_generated(
self, spec: str, *, expected_files: List[str], source_roots: List[str]
) -> None:
tgt = self.request_single_product(WrappedTarget, Address.parse(spec)).target
protocol_sources = self.request_single_product(
HydratedSources,
Params(HydrateSourcesRequest(tgt[ProtobufSources]), create_options_bootstrapper()),
)
generated_sources = self.request_single_product(
GeneratedSources,
Params(
GeneratePythonFromProtobufRequest(protocol_sources.snapshot, tgt),
create_options_bootstrapper(
args=[
"--backend-packages=pants.backend.codegen.protobuf.python",
f"--source-root-patterns={repr(source_roots)}",
]
),
),
)
assert set(generated_sources.snapshot.files) == set(expected_files)
def test_generates_python(self) -> None:
# This tests a few things:
# * We generate the correct file names.
# * Protobuf files can import other protobuf files, and those can import others
# (transitive dependencies). We'll only generate the requested target, though.
# * We can handle multiple source roots, which need to be preserved in the final output.
self.create_file(
"src/protobuf/dir1/f.proto",
dedent(
"""\
syntax = "proto2";
package dir1;
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
}
"""
),
)
self.create_file(
"src/protobuf/dir1/f2.proto",
dedent(
"""\
syntax = "proto2";
package dir1;
"""
),
)
self.add_to_build_file("src/protobuf/dir1", "protobuf_library()")
self.create_file(
"src/protobuf/dir2/f.proto",
dedent(
"""\
syntax = "proto2";
package dir2;
import "dir1/f.proto";
"""
),
)
self.add_to_build_file(
"src/protobuf/dir2",
"protobuf_library(dependencies=['src/protobuf/dir1'], python_source_root='src/python')",
)
# Test another source root.
self.create_file(
"tests/protobuf/test_protos/f.proto",
dedent(
"""\
syntax = "proto2";
package test_protos;
import "dir2/f.proto";
"""
),
)
self.add_to_build_file(
"tests/protobuf/test_protos", "protobuf_library(dependencies=['src/protobuf/dir2'])"
)
source_roots = ["src/python", "/src/protobuf", "/tests/protobuf"]
self.assert_files_generated(
"src/protobuf/dir1",
source_roots=source_roots,
expected_files=["src/protobuf/dir1/f_pb2.py", "src/protobuf/dir1/f2_pb2.py"],
)
self.assert_files_generated(
"src/protobuf/dir2",
source_roots=source_roots,
expected_files=["src/python/dir2/f_pb2.py"],
)
self.assert_files_generated(
"tests/protobuf/test_protos",
source_roots=source_roots,
expected_files=["tests/protobuf/test_protos/f_pb2.py"],
)
def test_top_level_proto_root(self) -> None:
self.create_file(
"protos/f.proto",
dedent(
"""\
syntax = "proto2";
package protos;
"""
),
)
self.add_to_build_file("protos", "protobuf_library()")
self.assert_files_generated(
"protos", source_roots=["/"], expected_files=["protos/f_pb2.py"]
)
def test_top_level_python_source_root(self) -> None:
self.create_file(
"src/proto/protos/f.proto",
dedent(
"""\
syntax = "proto2";
package protos;
"""
),
)
self.add_to_build_file("src/proto/protos", "protobuf_library(python_source_root='.')")
self.assert_files_generated(
"src/proto/protos", source_roots=["/", "src/proto"], expected_files=["protos/f_pb2.py"]
)
def test_bad_python_source_root(self) -> None:
self.create_file(
"src/protobuf/dir1/f.proto",
dedent(
"""\
syntax = "proto2";
package dir1;
"""
),
)
self.add_to_build_file(
"src/protobuf/dir1", "protobuf_library(python_source_root='notasourceroot')"
)
with pytest.raises(ExecutionError) as exc:
self.assert_files_generated(
"src/protobuf/dir1", source_roots=["src/protobuf"], expected_files=[]
)
assert len(exc.value.wrapped_exceptions) == 1
assert isinstance(exc.value.wrapped_exceptions[0], NoSourceRootError)