Skip to content

Commit c1af051

Browse files
authored
feat: enable snippetgen for default templates (googleapis#1171)
Enable snippetgen for the default (non-Ads) templates. This reverts commit 8bdb709 (which was a revert of googleapis#1044 and googleapis#1055). I've checked that the changes are OK (don't break generation for any APIs) by creating a [tag](https://github.com/googleapis/gapic-generator-python/commits/v0.62.0b1) and running the [presubmit](https://critique.corp.google.com/cl/424921742).
1 parent 8c191a5 commit c1af051

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

gapic/utils/options.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Options:
3838
warehouse_package_name: str = ''
3939
retry: Optional[Dict[str, Any]] = None
4040
sample_configs: Tuple[str, ...] = dataclasses.field(default=())
41-
autogen_snippets: bool = False
41+
autogen_snippets: bool = True
4242
templates: Tuple[str, ...] = dataclasses.field(default=('DEFAULT',))
4343
lazy_import: bool = False
4444
old_naming: bool = False
@@ -146,6 +146,18 @@ def tweak_path(p):
146146
# Build the options instance.
147147
sample_paths = opts.pop('samples', [])
148148

149+
# autogen-snippets is True by default, so make sure users can disable
150+
# by passing `autogen-snippets=false`
151+
autogen_snippets = opts.pop(
152+
"autogen-snippets", ["True"])[0] in ("True", "true", "T", "t", "TRUE")
153+
154+
# NOTE: Snippets are not currently correct for the alternative (Ads) templates
155+
# so always disable snippetgen in that case
156+
# https://github.com/googleapis/gapic-generator-python/issues/1052
157+
old_naming = bool(opts.pop('old-naming', False))
158+
if old_naming:
159+
autogen_snippets = False
160+
149161
answer = Options(
150162
name=opts.pop('name', ['']).pop(),
151163
namespace=tuple(opts.pop('namespace', [])),
@@ -157,10 +169,10 @@ def tweak_path(p):
157169
for s in sample_paths
158170
for cfg_path in samplegen_utils.generate_all_sample_fpaths(s)
159171
),
160-
autogen_snippets=bool(opts.pop("autogen-snippets", False)),
172+
autogen_snippets=autogen_snippets,
161173
templates=tuple(path.expanduser(i) for i in templates),
162174
lazy_import=bool(opts.pop('lazy-import', False)),
163-
old_naming=bool(opts.pop('old-naming', False)),
175+
old_naming=old_naming,
164176
add_iam_methods=bool(opts.pop('add-iam-methods', False)),
165177
metadata=bool(opts.pop('metadata', False)),
166178
# transport should include desired transports delimited by '+', e.g. transport='grpc+rest'

tests/unit/generator/test_generator.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,10 @@ def test_get_response_enumerates_proto():
255255

256256

257257
def test_get_response_divides_subpackages():
258-
g = make_generator()
258+
# NOTE: autogen-snippets is intentionally disabled for this test
259+
# The API schema below is incomplete and will result in errors when the
260+
# snippetgen logic tries to parse it.
261+
g = make_generator("autogen-snippets=false")
259262
api_schema = api.API.build(
260263
[
261264
descriptor_pb2.FileDescriptorProto(
@@ -290,7 +293,7 @@ def test_get_response_divides_subpackages():
290293
""".strip()
291294
)
292295
cgr = g.get_response(api_schema=api_schema,
293-
opts=Options.build(""))
296+
opts=Options.build("autogen-snippets=false"))
294297
assert len(cgr.file) == 6
295298
assert {i.name for i in cgr.file} == {
296299
"foo/types/top.py",
@@ -466,7 +469,7 @@ def test_samplegen_config_to_output_files(mock_gmtime, fs):
466469

467470
with mock.patch("gapic.samplegen.samplegen.generate_sample", side_effect=mock_generate_sample):
468471
actual_response = g.get_response(
469-
api_schema, opts=Options.build(""))
472+
api_schema, opts=Options.build("autogen-snippets=False"))
470473

471474
expected_snippet_index_json = {
472475
"snippets": [
@@ -606,7 +609,7 @@ def test_samplegen_id_disambiguation(mock_gmtime, fs):
606609
)
607610
with mock.patch("gapic.samplegen.samplegen.generate_sample", side_effect=mock_generate_sample):
608611
actual_response = g.get_response(api_schema,
609-
opts=Options.build(""))
612+
opts=Options.build("autogen-snippets=False"))
610613

611614
expected_snippet_metadata_json = {
612615
"snippets": [

tests/unit/generator/test_options.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,7 @@ def test_options_service_yaml_config(fs):
171171

172172

173173
def test_options_bool_flags():
174-
# All these options are default False.
175-
# If new options violate this assumption,
176-
# this test may need to be tweaked.
174+
# Most options are default False.
177175
# New options should follow the dash-case/snake_case convention.
178176
opt_str_to_attr_name = {
179177
name: re.sub(r"-", "_", name)
@@ -191,3 +189,22 @@ def test_options_bool_flags():
191189

192190
options = Options.build(opt)
193191
assert getattr(options, attr)
192+
193+
# Check autogen-snippets separately, as it is default True
194+
options = Options.build("")
195+
assert options.autogen_snippets
196+
197+
options = Options.build("autogen-snippets=False")
198+
assert not options.autogen_snippets
199+
200+
201+
def test_options_autogen_snippets_false_for_old_naming():
202+
# NOTE: Snippets are not currently correct for the alternative (Ads) templates
203+
# so always disable snippetgen in that case
204+
# https://github.com/googleapis/gapic-generator-python/issues/1052
205+
options = Options.build("old-naming")
206+
assert not options.autogen_snippets
207+
208+
# Even if autogen-snippets is set to True, do not enable snippetgen
209+
options = Options.build("old-naming,autogen-snippets=True")
210+
assert not options.autogen_snippets

0 commit comments

Comments
 (0)