Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate code snippets by default #1044

Merged
merged 9 commits into from
Oct 29, 2021
Merged
15 changes: 13 additions & 2 deletions gapic/utils/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Options:
warehouse_package_name: str = ''
retry: Optional[Dict[str, Any]] = None
sample_configs: Tuple[str, ...] = dataclasses.field(default=())
autogen_snippets: bool = False
autogen_snippets: bool = True
templates: Tuple[str, ...] = dataclasses.field(default=('DEFAULT',))
lazy_import: bool = False
old_naming: bool = False
Expand Down Expand Up @@ -132,6 +132,17 @@ def tweak_path(p):
# Build the options instance.
sample_paths = opts.pop('samples', [])

# autogen-snippets is True by default, so make sure users can disable
# by passing `autogen-snippets=false`
autogen_snippets = opts.pop(
"autogen-snippets", ["True"])[0] in ("True", "true", "T", "t", "TRUE")

# NOTE: Snippets are not currently correct for the alternative (Ads) templates
# so always disable snippetgen in that case
# https://github.com/googleapis/gapic-generator-python/issues/1052
if opts.get("old-naming"):
autogen_snippets = False

answer = Options(
name=opts.pop('name', ['']).pop(),
namespace=tuple(opts.pop('namespace', [])),
Expand All @@ -143,7 +154,7 @@ def tweak_path(p):
for s in sample_paths
for cfg_path in samplegen_utils.generate_all_sample_fpaths(s)
),
autogen_snippets=bool(opts.pop("autogen-snippets", False)),
autogen_snippets=autogen_snippets,
templates=tuple(path.expanduser(i) for i in templates),
lazy_import=bool(opts.pop('lazy-import', False)),
old_naming=bool(opts.pop('old-naming', False)),
Expand Down
16 changes: 12 additions & 4 deletions tests/unit/generator/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ def test_get_response_enumerates_proto():


def test_get_response_divides_subpackages():
g = make_generator()
# NOTE: autogen-snippets is intentionally disabled for this test
# The API schema below is incomplete and will result in errors when the
# snippetgen logic tries to parse it.
g = make_generator("autogen-snippets=false")
api_schema = api.API.build(
[
descriptor_pb2.FileDescriptorProto(
Expand Down Expand Up @@ -277,7 +280,7 @@ def test_get_response_divides_subpackages():
""".strip()
)
cgr = g.get_response(api_schema=api_schema,
opts=Options.build(""))
opts=Options.build("autogen-snippets=false"))
assert len(cgr.file) == 6
assert {i.name for i in cgr.file} == {
"foo/types/top.py",
Expand Down Expand Up @@ -683,7 +686,12 @@ def test_dont_generate_in_code_samples(mock_gmtime, mock_generate_sample, fs):
),
)

generator = make_generator(f"samples={config_fpath}")
# NOTE: autogen-snippets is intentionally disabled for this test
# The API schema below is incomplete and will result in errors when the
# snippetgen logic attempts to parse it.
generator = make_generator(
f"samples={config_fpath},autogen-snippets=False")
print(generator)
generator._env.loader = jinja2.DictLoader({"sample.py.j2": ""})
api_schema = make_api(
make_proto(
Expand Down Expand Up @@ -743,7 +751,7 @@ def test_dont_generate_in_code_samples(mock_gmtime, mock_generate_sample, fs):
expected.supported_features |= CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL

actual = generator.get_response(
api_schema=api_schema, opts=Options.build("")
api_schema=api_schema, opts=Options.build("autogen-snippets=False")
)
assert actual == expected

Expand Down