Skip to content

Commit

Permalink
Address code review comments and add test
Browse files Browse the repository at this point in the history
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
  • Loading branch information
benjyw committed Aug 4, 2020
1 parent 03f41f3 commit a7c33b4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Expand Up @@ -11,13 +11,12 @@ class ProtobufPythonInterpreterCompatibility(PythonInterpreterCompatibility):


class PythonSourceRootField(StringField):
"""The source root to generate python sources under.
"""The source root to generate Python sources under.
If unspecified, the source root the protobuf_library is under will be used.
"""

alias = "python_source_root"
default = None


def rules():
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/codegen/protobuf/python/rules.py
Expand Up @@ -107,7 +107,7 @@ async def generate_python_from_protobuf(
)

# We must do some path manipulation on the output digest for it to look like normal sources,
# including adding back the original source root.
# including adding back a source root.
py_source_root = request.protocol_target.get(PythonSourceRootField).value
if py_source_root:
# Verify that the python source root specified by the target is in fact a source root.
Expand Down
Expand Up @@ -150,7 +150,7 @@ def test_generates_python(self) -> None:
expected_files=["tests/protobuf/test_protos/f_pb2.py"],
)

def test_top_level_source_root(self) -> None:
def test_top_level_proto_root(self) -> None:
self.create_file(
"protos/f.proto",
dedent(
Expand All @@ -166,6 +166,22 @@ def test_top_level_source_root(self) -> None:
"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",
Expand Down

0 comments on commit a7c33b4

Please sign in to comment.