Skip to content

Commit d059530

Browse files
authored
chore: migrate java tests (#3227)
In this PR: - Migrate `test_language_java.py` and test resources from synthtool. - Remove synthtool installation step in CI. - Remove unused methods in `java.py`. - Refactor code to align with the current coding style.
1 parent 9105674 commit d059530

File tree

21 files changed

+993
-379
lines changed

21 files changed

+993
-379
lines changed

.github/workflows/verify_library_generation.yaml

+1-15
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,8 @@ jobs:
8787
set -ex
8888
pushd library_generation
8989
pip install -r requirements.txt
90+
pip install .
9091
popd
91-
- name: install synthtool
92-
shell: bash
93-
run: |
94-
set -ex
95-
mkdir -p /tmp/synthtool
96-
pushd /tmp/synthtool
97-
if [ ! -d "synthtool" ]; then
98-
git clone https://github.com/googleapis/synthtool.git
99-
fi
100-
pushd "synthtool"
101-
102-
git reset --hard origin/no-java-templates
103-
104-
python3 -m pip install -e .
105-
python3 -m pip install -r requirements.in
10692
- name: Run shell unit tests
10793
run: |
10894
set -x

library_generation/owlbot/synthtool/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
"""Synthtool synthesizes libraries from disparate sources."""
1616

17-
import sys
18-
1917
from synthtool.transforms import (
2018
move,
2119
replace,

library_generation/owlbot/synthtool/gcp/common.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,17 @@
1414

1515
import json
1616
import os
17-
import re
1817
import sys
19-
import shutil
2018
import yaml
2119
from pathlib import Path
2220
from typing import Dict, List, Optional
23-
import jinja2
2421
import logging
2522

2623
from synthtool import _tracked_paths
2724
from synthtool.sources import templates
2825

2926
logger = logging.getLogger()
3027
logger.setLevel(logging.DEBUG)
31-
32-
33-
DEFAULT_TEMPLATES_PATH = "synthtool/gcp/templates"
34-
LOCAL_TEMPLATES: Optional[str] = os.environ.get("SYNTHTOOL_TEMPLATES")
35-
3628
# Originally brought from gcp/partials.py.
3729
# These are the default locations to look up
3830
_DEFAULT_PARTIAL_FILES = [
@@ -44,11 +36,12 @@
4436

4537
class CommonTemplates:
4638
def __init__(self, template_path: Optional[Path] = None):
47-
if LOCAL_TEMPLATES is None:
39+
local_templates = os.environ.get("SYNTHTOOL_TEMPLATES")
40+
if local_templates is None:
4841
logger.error("env var SYNTHTOOL_TEMPLATES must be set")
4942
sys.exit(1)
50-
logger.debug(f"Using local templates at {LOCAL_TEMPLATES}")
51-
self._template_root = Path(LOCAL_TEMPLATES)
43+
logger.debug(f"Using local templates at {local_templates}")
44+
self._template_root = Path(local_templates)
5245
self._templates = templates.Templates(self._template_root)
5346
self.excludes = [] # type: List[str]
5447

library_generation/owlbot/synthtool/languages/java.py

-75
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import glob
1615
import os
1716
import xml.etree.ElementTree as ET
1817
import re
@@ -166,80 +165,6 @@ def version_from_maven_metadata(metadata: str) -> Optional[str]:
166165
return None
167166

168167

169-
def _common_generation(
170-
service: str,
171-
version: str,
172-
library: Path,
173-
package_pattern: str,
174-
suffix: str = "",
175-
destination_name: str = None,
176-
cloud_api: bool = True,
177-
diregapic: bool = False,
178-
preserve_gapic: bool = False,
179-
):
180-
"""Helper function to execution the common generation cleanup actions.
181-
182-
Fixes headers for protobuf classes and generated gRPC stub services. Copies
183-
code and samples to their final destinations by convention. Runs the code
184-
formatter on the generated code.
185-
186-
Args:
187-
service (str): Name of the service.
188-
version (str): Service API version.
189-
library (Path): Path to the temp directory with the generated library.
190-
package_pattern (str): Package name template for fixing file headers.
191-
suffix (str, optional): Suffix that the generated library folder. The
192-
artman output differs from bazel's output directory. Defaults to "".
193-
destination_name (str, optional): Override the service name for the
194-
destination of the output code. Defaults to the service name.
195-
preserve_gapic (bool, optional): Whether to preserve the gapic directory
196-
prefix. Default False.
197-
"""
198-
199-
if destination_name is None:
200-
destination_name = service
201-
202-
cloud_prefix = "cloud-" if cloud_api else ""
203-
package_name = package_pattern.format(service=service, version=version)
204-
proto_library_name = f"proto-google-{cloud_prefix}{service}-{version}"
205-
grpc_library_name = f"grpc-google-{cloud_prefix}{service}-{version}"
206-
gapic_library_name = f"gapic-google-{cloud_prefix}{service}-{version}"
207-
fix_proto_headers(library / f"{proto_library_name}{suffix}")
208-
fix_grpc_headers(library / f"{grpc_library_name}{suffix}", package_name)
209-
210-
if preserve_gapic:
211-
s.copy(
212-
[library / f"{gapic_library_name}{suffix}/src"],
213-
f"{gapic_library_name}/src",
214-
required=True,
215-
)
216-
else:
217-
s.copy(
218-
[library / f"{gapic_library_name}{suffix}/src"],
219-
f"google-{cloud_prefix}{destination_name}/src",
220-
required=True,
221-
)
222-
223-
s.copy(
224-
[library / f"{grpc_library_name}{suffix}/src"],
225-
f"{grpc_library_name}/src",
226-
# For REST-only clients, like java-compute, gRPC artifact does not exist
227-
required=(not diregapic),
228-
)
229-
s.copy(
230-
[library / f"{proto_library_name}{suffix}/src"],
231-
f"{proto_library_name}/src",
232-
required=True,
233-
)
234-
235-
if preserve_gapic:
236-
format_code(f"{gapic_library_name}/src")
237-
else:
238-
format_code(f"google-{cloud_prefix}{destination_name}/src")
239-
format_code(f"{grpc_library_name}/src")
240-
format_code(f"{proto_library_name}/src")
241-
242-
243168
def _merge_release_please(destination_text: str):
244169
handle_gh_release_key = "handleGHRelease"
245170
branches_key = "branches"

library_generation/owlbot/templates/java_library/.github/workflows/update_generation_config.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ on:
1818
schedule:
1919
- cron: '0 2 * * *'
2020
workflow_dispatch:
21-
21+
{% raw %}
2222
jobs:
2323
update-generation-config:
2424
runs-on: ubuntu-22.04
@@ -40,3 +40,4 @@ jobs:
4040
--repo ${{ github.repository }}
4141
env:
4242
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
43+
{% endraw %}

library_generation/requirements.in

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ jinja2==3.1.4
2626
# a list where typing extensions is pinned to >=4.0.1. This will produce an error saying "all requirements
2727
# must have their versions pinned with ==". The following line pins the dependency to a specific version via ==
2828
typing-extensions==4.0.1
29+
requests-mock # used in owlbot unit tests

0 commit comments

Comments
 (0)