Skip to content

Commit e73d12d

Browse files
authored
fix: samplegen always produces sample dicts with "response" (#914)
- fix samplegen logic to always set a `sample[response]` - enable `autogen-snippets` for the golden integration tests that are run through Bazel - install some extra stub packages for mypy that are needed with the new release
1 parent ff67883 commit e73d12d

File tree

72 files changed

+2826
-17
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+2826
-17
lines changed

packages/gapic-generator/BUILD.bazel

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ config_setting(
3535
py_runtime(
3636
name = "pyenv3_runtime",
3737
interpreter = ":pyenv3wrapper.sh",
38-
python_version="PY3",
38+
python_version = "PY3",
3939
)
4040

4141
py_runtime_pair(
@@ -52,7 +52,10 @@ toolchain(
5252
py_binary(
5353
name = "gapic_plugin",
5454
srcs = glob(["gapic/**/*.py"]),
55-
data = [":pandoc_binary"] + glob(["gapic/**/*.j2", "gapic/**/.*.j2"]),
55+
data = [":pandoc_binary"] + glob([
56+
"gapic/**/*.j2",
57+
"gapic/**/.*.j2",
58+
]),
5659
main = "gapic/cli/generate_with_pandoc.py",
5760
python_version = "PY3",
5861
visibility = ["//visibility:public"],

packages/gapic-generator/DEVELOPMENT.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,25 @@ Execute unit tests by running one of the sessions prefixed with `unit-`.
3333

3434
- Run a single integration test for one API. This generates Python source code
3535
with the microgenerator and compares them to the golden files in
36-
`test/integration/goldens/asset`.
36+
`tests/integration/goldens/asset`.
3737

3838
```sh
39-
bazel test //test/integration:asset
39+
bazel test //tests/integration:asset
4040
```
4141

42-
- Update goldens files. This overwrites the golden files in
43-
`test/integration/goldens/asset`.
42+
- Run integration tests for all APIs.
4443

4544
```sh
46-
bazel run //test/integration:asset_update
47-
```
45+
bazel test //tests/integration:all
46+
```
47+
48+
- Update all goldens files. This overwrites the golden files in
49+
`tests/integration/goldens/`.
50+
51+
```sh
52+
bazel run //tests/integration:asset_update
53+
bazel run //tests/integration:credentials_update
54+
bazel run //tests/integration:logging_update
55+
bazel run //tests/integration:redis_update
56+
```
57+

packages/gapic-generator/gapic/samplegen/samplegen.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ def preprocess_sample(sample, api_schema: api.API, rpc: wrappers.Method):
301301
# Add reasonable defaults depending on the type of the sample
302302
if not rpc.void:
303303
sample.setdefault("response", [{"print": ["%s", "$resp"]}])
304+
else:
305+
sample.setdefault("response", [])
304306

305307
@utils.cached_property
306308
def flattenable_fields(self) -> FrozenSet[str]:
@@ -1000,6 +1002,7 @@ def generate_sample(sample, api_schema, sample_template: jinja2.Template) -> str
10001002
sample["request"] = v.validate_and_transform_request(
10011003
calling_form, sample["request"]
10021004
)
1005+
10031006
v.validate_response(sample["response"])
10041007

10051008
return sample_template.render(

packages/gapic-generator/gapic/templates/examples/feature_fragments.j2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,12 @@ client.{{ sample.rpc|snake_case }}({{ render_request_params_unary(sample.request
220220
{% if calling_form == calling_form_enum.Request %}
221221
response = {{ method_invocation_text|trim }}
222222

223+
{% if response_statements %}
223224
# Handle response
224225
{% for statement in response_statements %}
225226
{{ dispatch_statement(statement)|trim }}
226227
{% endfor %}
228+
{% endif %}
227229
{% elif calling_form == calling_form_enum.RequestPagedAll %}
228230
page_result = {{ method_invocation_text|trim }}
229231
for response in page_result:

packages/gapic-generator/gapic/templates/noxfile.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def cover(session):
5959
@nox.session(python=['3.6', '3.7'])
6060
def mypy(session):
6161
"""Run the type checker."""
62-
session.install('mypy')
62+
session.install('mypy', 'types-pkg_resources')
6363
session.install('.')
6464
session.run(
6565
'mypy',

packages/gapic-generator/noxfile.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def showcase_mypy(
273273
"""Perform typecheck analysis on the generated Showcase library."""
274274

275275
# Install pytest and gapic-generator-python
276-
session.install("mypy")
276+
session.install("mypy", "types-pkg-resources")
277277

278278
with showcase_library(session, templates=templates, other_opts=other_opts) as lib:
279279
session.chdir(lib)
@@ -340,6 +340,11 @@ def docs(session):
340340
def mypy(session):
341341
"""Perform typecheck analysis."""
342342

343-
session.install("mypy")
343+
session.install(
344+
"mypy",
345+
"types-protobuf",
346+
"types-PyYAML",
347+
"types-dataclasses"
348+
)
344349
session.install(".")
345350
session.run("mypy", "gapic")

packages/gapic-generator/tests/integration/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,19 @@ py_gapic_library(
5151
name = "asset_py_gapic",
5252
srcs = ["@com_google_googleapis//google/cloud/asset/v1:asset_proto"],
5353
grpc_service_config = "cloudasset_grpc_service_config.json",
54+
opt_args = [
55+
"autogen-snippets",
56+
],
5457
)
5558

5659
# Credentials.
5760
py_gapic_library(
5861
name = "credentials_py_gapic",
5962
srcs = ["@com_google_googleapis//google/iam/credentials/v1:credentials_proto"],
6063
grpc_service_config = "iamcredentials_grpc_service_config.json",
64+
opt_args = [
65+
"autogen-snippets",
66+
],
6167
)
6268

6369
# Logging.
@@ -68,11 +74,15 @@ py_gapic_library(
6874
opt_args = [
6975
"python-gapic-namespace=google.cloud",
7076
"python-gapic-name=logging",
77+
"autogen-snippets",
7178
],
7279
)
7380

7481
py_gapic_library(
7582
name = "redis_py_gapic",
7683
srcs = ["@com_google_googleapis//google/cloud/redis/v1:redis_proto"],
7784
grpc_service_config = "redis_grpc_service_config.json",
85+
opt_args = [
86+
"autogen-snippets",
87+
],
7888
)

packages/gapic-generator/tests/integration/goldens/asset/noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def cover(session):
7070
@nox.session(python=['3.6', '3.7'])
7171
def mypy(session):
7272
"""Run the type checker."""
73-
session.install('mypy')
73+
session.install('mypy', 'types-pkg_resources')
7474
session.install('.')
7575
session.run(
7676
'mypy',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2020 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# Generated code. DO NOT EDIT!
17+
#
18+
# Snippet for AnalyzeIamPolicy
19+
# NOTE: This snippet has been automatically generated for illustrative purposes only.
20+
# It may require modifications to work in your environment.
21+
22+
# To install the latest published package dependency, execute the following:
23+
# python3 -m pip install google-cloud-asset
24+
25+
26+
# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_grpc]
27+
from google.cloud import asset_v1
28+
29+
30+
def sample_analyze_iam_policy():
31+
"""Snippet for analyze_iam_policy"""
32+
33+
# Create a client
34+
client = asset_v1.AssetServiceClient()
35+
36+
# Initialize request argument(s)
37+
request = asset_v1.AnalyzeIamPolicyRequest(
38+
)
39+
40+
# Make the request
41+
response = client.analyze_iam_policy(request=request)
42+
43+
# Handle response
44+
print("{}".format(response))
45+
46+
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_grpc]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2020 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# Generated code. DO NOT EDIT!
17+
#
18+
# Snippet for AnalyzeIamPolicyLongrunning
19+
# NOTE: This snippet has been automatically generated for illustrative purposes only.
20+
# It may require modifications to work in your environment.
21+
22+
# To install the latest published package dependency, execute the following:
23+
# python3 -m pip install google-cloud-asset
24+
25+
26+
# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_grpc]
27+
from google.cloud import asset_v1
28+
29+
30+
def sample_analyze_iam_policy_longrunning():
31+
"""Snippet for analyze_iam_policy_longrunning"""
32+
33+
# Create a client
34+
client = asset_v1.AssetServiceClient()
35+
36+
# Initialize request argument(s)
37+
request = asset_v1.AnalyzeIamPolicyLongrunningRequest(
38+
)
39+
40+
# Make the request
41+
operation = client.analyze_iam_policy_longrunning(request=request)
42+
43+
print("Waiting for operation to complete...")
44+
45+
response = operation.result()
46+
print("{}".format(response))
47+
48+
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_grpc]

0 commit comments

Comments
 (0)