Skip to content

Commit

Permalink
test: fix broken unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
busunkim96 committed Oct 29, 2021
1 parent 168b4c4 commit 2bf9822
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 9 additions & 1 deletion gapic/utils/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from os import path
from typing import Any, DefaultDict, Dict, FrozenSet, List, Optional, Tuple

import distutils.util
import dataclasses
import json
import os
Expand Down Expand Up @@ -132,6 +133,13 @@ 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`
if opts.get("autogen-snippets"):
autogen_snippets = bool(distutils.util.strtobool(opts.pop("autogen-snippets")[0]))
else:
autogen_snippets = True

answer = Options(
name=opts.pop('name', ['']).pop(),
namespace=tuple(opts.pop('namespace', [])),
Expand All @@ -143,7 +151,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", True)),
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
15 changes: 11 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,11 @@ 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 +750,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

0 comments on commit 2bf9822

Please sign in to comment.