Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions .generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def handle_generate(
api_path = api.get("path")
if api_path:
_generate_api(
api_path, library_id, source, output, version, is_mono_repo
api_path, library_id, source, output, version, is_mono_repo, input
)
_copy_files_needed_for_post_processing(output, input, library_id, is_mono_repo)
_generate_repo_metadata_file(
Expand Down Expand Up @@ -843,6 +843,7 @@ def _generate_api(
output: str,
gapic_version: str,
is_mono_repo: bool,
input: str,
):
"""
Handles the generation and staging process for a single API path.
Expand All @@ -855,6 +856,8 @@ def _generate_api(
gapic_version(str): The desired version number for the GAPIC client library
in a format which follows PEP-440.
is_mono_repo(bool): True if the current repository is a mono-repo.
input(str): The path to the directory in the container
which contains additional generator input.
"""
py_gapic_config = _read_bazel_build_py_rule(api_path, source)
is_proto_only_library = len(py_gapic_config) == 0
Expand All @@ -873,13 +876,18 @@ def _generate_api(
_run_protoc_command(command, source)

# 3. Determine staging location
staging_child_directory = _get_staging_child_directory(
api_path, is_proto_only_library
)
staging_dir = os.path.join(output, "owl-bot-staging")
if is_mono_repo:
staging_dir = os.path.join(staging_dir, library_id)
staging_dir = os.path.join(staging_dir, staging_child_directory)
# If it's a non-mono repo with a custom owlbot.py, stage directly to output.
# Otherwise, use the owl-bot-staging directory.
if not is_mono_repo and Path(f"{input}/owlbot.py").exists():
staging_dir = output
else:
staging_child_directory = _get_staging_child_directory(
api_path, is_proto_only_library
)
staging_dir = os.path.join(output, "owl-bot-staging")
if is_mono_repo:
staging_dir = os.path.join(staging_dir, library_id)
staging_dir = os.path.join(staging_dir, staging_child_directory)

# 4. Stage the generated code
if is_proto_only_library:
Expand Down
26 changes: 21 additions & 5 deletions .generator/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,16 +617,21 @@ def test_run_protoc_command_failure(mocker):
_run_protoc_command(command, source)


@pytest.mark.parametrize("is_mono_repo", [False, True])
def test_generate_api_success_py_gapic(mocker, caplog, is_mono_repo):
@pytest.mark.parametrize(
"is_mono_repo,owlbot_py_exists",
[(False, False), (True, False), (False, True)],
)
def test_generate_api_success_py_gapic(mocker, caplog, is_mono_repo, owlbot_py_exists):
caplog.set_level(logging.INFO)

API_PATH = "google/cloud/language/v1"
LIBRARY_ID = "google-cloud-language"
SOURCE = "source"
OUTPUT = "output"
INPUT = "input"
gapic_version = "1.2.99"

mocker.patch("pathlib.Path.exists", return_value=owlbot_py_exists)
mock_read_bazel_build_py_rule = mocker.patch(
"cli._read_bazel_build_py_rule",
return_value={
Expand All @@ -638,11 +643,19 @@ def test_generate_api_success_py_gapic(mocker, caplog, is_mono_repo):
mock_run_protoc_command = mocker.patch("cli._run_protoc_command")
mock_shutil_copytree = mocker.patch("shutil.copytree")

_generate_api(API_PATH, LIBRARY_ID, SOURCE, OUTPUT, gapic_version, is_mono_repo)
_generate_api(
API_PATH, LIBRARY_ID, SOURCE, OUTPUT, gapic_version, is_mono_repo, INPUT
)

mock_read_bazel_build_py_rule.assert_called_once()
mock_run_protoc_command.assert_called_once()
mock_shutil_copytree.assert_called_once()
if not is_mono_repo and owlbot_py_exists:
# In this case, the staging directory is the output directory itself.
mock_shutil_copytree.assert_called_once_with(
unittest.mock.ANY, OUTPUT, dirs_exist_ok=True
)
else:
mock_shutil_copytree.assert_called_once()


@pytest.mark.parametrize("is_mono_repo", [False, True])
Expand All @@ -653,6 +666,7 @@ def test_generate_api_success_py_proto(mocker, caplog, is_mono_repo):
LIBRARY_ID = "google-cloud-language"
SOURCE = "source"
OUTPUT = "output"
INPUT = "input"
gapic_version = "1.2.99"

mock_read_bazel_build_py_rule = mocker.patch(
Expand All @@ -661,7 +675,9 @@ def test_generate_api_success_py_proto(mocker, caplog, is_mono_repo):
mock_run_protoc_command = mocker.patch("cli._run_protoc_command")
mock_shutil_copytree = mocker.patch("shutil.copytree")

_generate_api(API_PATH, LIBRARY_ID, SOURCE, OUTPUT, gapic_version, is_mono_repo)
_generate_api(
API_PATH, LIBRARY_ID, SOURCE, OUTPUT, gapic_version, is_mono_repo, INPUT
)

mock_read_bazel_build_py_rule.assert_called_once()
mock_run_protoc_command.assert_called_once()
Expand Down
Loading