diff --git a/artman/config/converter.py b/artman/config/converter.py index db89f89e..d8d81232 100644 --- a/artman/config/converter.py +++ b/artman/config/converter.py @@ -37,6 +37,7 @@ def convert_to_legacy_config_dict(artifact_config, root_dir, output_dir): common['service_yaml'] = artifact_config.service_yaml common['gapic_yaml'] = artifact_config.gapic_yaml common['proto_package'] = artifact_config.proto_package + common['gapic_samples'] = artifact_config.gapic_samples src_proto_paths = artifact_config_dict.get('src_proto_paths', []) common['src_proto_path'], excluded_proto_path = _calculate_proto_paths(src_proto_paths) if excluded_proto_path: diff --git a/artman/config/loader.py b/artman/config/loader.py index dc17ffb9..1ff178ba 100644 --- a/artman/config/loader.py +++ b/artman/config/loader.py @@ -174,6 +174,10 @@ def _normalize_artifact_config(artifact_config, artman_config_path): artifact_config.gapic_yaml = _normalize_path( artifact_config.gapic_yaml, artman_config_path, 'gapic_yaml') + if artifact_config.gapic_samples: + artifact_config.gapic_samples = _normalize_path( + artifact_config.gapic_samples, artman_config_path, 'gapic_samples') + normalized_src_proto_paths = [] for src_proto_path in artifact_config.src_proto_paths: if src_proto_path.startswith('-'): diff --git a/artman/config/proto/config.proto b/artman/config/proto/config.proto index de5e08b6..80b09eda 100644 --- a/artman/config/proto/config.proto +++ b/artman/config/proto/config.proto @@ -183,6 +183,12 @@ message Artifact { // If a gRPC client is to be generated, either a gapic_yaml or a proto_package must be given. string proto_package = 18; + // The path to sample config yaml(s). It can be an absolute path or a path + // relative to the artman config yaml. It can also be either a directory or + // a file. When it is a directory, artman will consume all the sample config + // yamls recursively in this directory. + string gapic_samples = 19; + reserved 11; reserved "import_proto_path"; diff --git a/artman/config/proto/config_pb2.py b/artman/config/proto/config_pb2.py index 708530a0..6ffec4d1 100644 --- a/artman/config/proto/config_pb2.py +++ b/artman/config/proto/config_pb2.py @@ -20,7 +20,7 @@ package='googleapis.artman', syntax='proto3', serialized_options=None, - serialized_pb=_b('\n\x0c\x63onfig.proto\x12\x11googleapis.artman\"e\n\x06\x43onfig\x12+\n\x06\x63ommon\x18\x01 \x01(\x0b\x32\x1b.googleapis.artman.Artifact\x12.\n\tartifacts\x18\x02 \x03(\x0b\x32\x1b.googleapis.artman.Artifact\"\xf8\x0b\n\x08\x41rtifact\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x10\n\x08\x61pi_name\x18\x02 \x01(\t\x12\x13\n\x0b\x61pi_version\x18\x03 \x01(\t\x12\x19\n\x11organization_name\x18\x04 \x01(\t\x12?\n\rrelease_level\x18\x05 \x01(\x0e\x32(.googleapis.artman.Artifact.ReleaseLevel\x12\x17\n\x0fsrc_proto_paths\x18\x06 \x03(\t\x12?\n\nproto_deps\x18\x07 \x03(\x0b\x32+.googleapis.artman.Artifact.ProtoDependency\x12\x44\n\x0ftest_proto_deps\x18\x08 \x03(\x0b\x32+.googleapis.artman.Artifact.ProtoDependency\x12\x14\n\x0cservice_yaml\x18\t \x01(\t\x12\x12\n\ngapic_yaml\x18\n \x01(\t\x12\x15\n\rproto_package\x18\x12 \x01(\t\x12.\n\x04type\x18\x0c \x01(\x0e\x32 .googleapis.artman.Artifact.Type\x12\x36\n\x08language\x18\r \x01(\x0e\x32$.googleapis.artman.Artifact.Language\x12\x43\n\x0fpackage_version\x18\x0e \x01(\x0b\x32*.googleapis.artman.Artifact.PackageVersion\x12\x42\n\x0fpublish_targets\x18\x0f \x03(\x0b\x32).googleapis.artman.Artifact.PublishTarget\x12\x15\n\rdiscovery_doc\x18\x10 \x01(\t\x12\x32\n\x06\x61spect\x18\x11 \x01(\x0e\x32\".googleapis.artman.Artifact.Aspect\x1a\x33\n\x0fProtoDependency\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x12\n\nproto_path\x18\x02 \x01(\t\x1a]\n\x0ePackageVersion\x12\x0f\n\x07version\x18\x01 \x01(\t\x12\x1c\n\x14grpc_dep_lower_bound\x18\x02 \x01(\t\x12\x1c\n\x14grpc_dep_upper_bound\x18\x03 \x01(\t\x1a\xb7\x02\n\rPublishTarget\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x10\n\x08location\x18\x02 \x01(\t\x12<\n\x04type\x18\x03 \x01(\x0e\x32..googleapis.artman.Artifact.PublishTarget.Type\x12V\n\x12\x64irectory_mappings\x18\x04 \x03(\x0b\x32:.googleapis.artman.Artifact.PublishTarget.DirectoryMapping\x1a;\n\x10\x44irectoryMapping\x12\x0b\n\x03src\x18\x01 \x01(\t\x12\x0c\n\x04\x64\x65st\x18\x02 \x01(\t\x12\x0c\n\x04name\x18\x03 \x01(\t\"3\n\x04Type\x12\x1f\n\x1bPUBLISHING_TYPE_UNSPECIFIED\x10\x00\x12\n\n\x06GITHUB\x10\x01\"J\n\x0cReleaseLevel\x12\x1d\n\x19RELEASE_LEVEL_UNSPECIFIED\x10\x00\x12\x06\n\x02GA\x10\x01\x12\x08\n\x04\x42\x45TA\x10\x02\x12\t\n\x05\x41LPHA\x10\x03\"r\n\x04Type\x12\t\n\x05GAPIC\x10\x00\x12\x10\n\x0cGAPIC_CONFIG\x10\x01\x12\x08\n\x04GRPC\x10\x02\x12\x0c\n\x08PROTOBUF\x10\x03\x12\x0e\n\nDISCOGAPIC\x10\x04\x12\x15\n\x11\x44ISCOGAPIC_CONFIG\x10\x05\x12\x0e\n\nGAPIC_ONLY\x10\x63\"m\n\x08Language\x12\x18\n\x14LANGUAGE_UNSPECIFIED\x10\x00\x12\x08\n\x04JAVA\x10\x01\x12\n\n\x06PYTHON\x10\x02\x12\n\n\x06NODEJS\x10\x03\x12\x06\n\x02GO\x10\x04\x12\x07\n\x03PHP\x10\x05\x12\n\n\x06\x43SHARP\x10\x06\x12\x08\n\x04RUBY\x10\x07\"(\n\x06\x41spect\x12\x07\n\x03\x41LL\x10\x00\x12\x08\n\x04\x43ODE\x10\x01\x12\x0b\n\x07PACKAGE\x10\x02J\x04\x08\x0b\x10\x0cR\x11import_proto_pathb\x06proto3') + serialized_pb=_b('\n\x0c\x63onfig.proto\x12\x11googleapis.artman\"e\n\x06\x43onfig\x12+\n\x06\x63ommon\x18\x01 \x01(\x0b\x32\x1b.googleapis.artman.Artifact\x12.\n\tartifacts\x18\x02 \x03(\x0b\x32\x1b.googleapis.artman.Artifact\"\x8f\x0c\n\x08\x41rtifact\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x10\n\x08\x61pi_name\x18\x02 \x01(\t\x12\x13\n\x0b\x61pi_version\x18\x03 \x01(\t\x12\x19\n\x11organization_name\x18\x04 \x01(\t\x12?\n\rrelease_level\x18\x05 \x01(\x0e\x32(.googleapis.artman.Artifact.ReleaseLevel\x12\x17\n\x0fsrc_proto_paths\x18\x06 \x03(\t\x12?\n\nproto_deps\x18\x07 \x03(\x0b\x32+.googleapis.artman.Artifact.ProtoDependency\x12\x44\n\x0ftest_proto_deps\x18\x08 \x03(\x0b\x32+.googleapis.artman.Artifact.ProtoDependency\x12\x14\n\x0cservice_yaml\x18\t \x01(\t\x12\x12\n\ngapic_yaml\x18\n \x01(\t\x12\x15\n\rproto_package\x18\x12 \x01(\t\x12\x15\n\rgapic_samples\x18\x13 \x01(\t\x12.\n\x04type\x18\x0c \x01(\x0e\x32 .googleapis.artman.Artifact.Type\x12\x36\n\x08language\x18\r \x01(\x0e\x32$.googleapis.artman.Artifact.Language\x12\x43\n\x0fpackage_version\x18\x0e \x01(\x0b\x32*.googleapis.artman.Artifact.PackageVersion\x12\x42\n\x0fpublish_targets\x18\x0f \x03(\x0b\x32).googleapis.artman.Artifact.PublishTarget\x12\x15\n\rdiscovery_doc\x18\x10 \x01(\t\x12\x32\n\x06\x61spect\x18\x11 \x01(\x0e\x32\".googleapis.artman.Artifact.Aspect\x1a\x33\n\x0fProtoDependency\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x12\n\nproto_path\x18\x02 \x01(\t\x1a]\n\x0ePackageVersion\x12\x0f\n\x07version\x18\x01 \x01(\t\x12\x1c\n\x14grpc_dep_lower_bound\x18\x02 \x01(\t\x12\x1c\n\x14grpc_dep_upper_bound\x18\x03 \x01(\t\x1a\xb7\x02\n\rPublishTarget\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x10\n\x08location\x18\x02 \x01(\t\x12<\n\x04type\x18\x03 \x01(\x0e\x32..googleapis.artman.Artifact.PublishTarget.Type\x12V\n\x12\x64irectory_mappings\x18\x04 \x03(\x0b\x32:.googleapis.artman.Artifact.PublishTarget.DirectoryMapping\x1a;\n\x10\x44irectoryMapping\x12\x0b\n\x03src\x18\x01 \x01(\t\x12\x0c\n\x04\x64\x65st\x18\x02 \x01(\t\x12\x0c\n\x04name\x18\x03 \x01(\t\"3\n\x04Type\x12\x1f\n\x1bPUBLISHING_TYPE_UNSPECIFIED\x10\x00\x12\n\n\x06GITHUB\x10\x01\"J\n\x0cReleaseLevel\x12\x1d\n\x19RELEASE_LEVEL_UNSPECIFIED\x10\x00\x12\x06\n\x02GA\x10\x01\x12\x08\n\x04\x42\x45TA\x10\x02\x12\t\n\x05\x41LPHA\x10\x03\"r\n\x04Type\x12\t\n\x05GAPIC\x10\x00\x12\x10\n\x0cGAPIC_CONFIG\x10\x01\x12\x08\n\x04GRPC\x10\x02\x12\x0c\n\x08PROTOBUF\x10\x03\x12\x0e\n\nDISCOGAPIC\x10\x04\x12\x15\n\x11\x44ISCOGAPIC_CONFIG\x10\x05\x12\x0e\n\nGAPIC_ONLY\x10\x63\"m\n\x08Language\x12\x18\n\x14LANGUAGE_UNSPECIFIED\x10\x00\x12\x08\n\x04JAVA\x10\x01\x12\n\n\x06PYTHON\x10\x02\x12\n\n\x06NODEJS\x10\x03\x12\x06\n\x02GO\x10\x04\x12\x07\n\x03PHP\x10\x05\x12\n\n\x06\x43SHARP\x10\x06\x12\x08\n\x04RUBY\x10\x07\"(\n\x06\x41spect\x12\x07\n\x03\x41LL\x10\x00\x12\x08\n\x04\x43ODE\x10\x01\x12\x0b\n\x07PACKAGE\x10\x02J\x04\x08\x0b\x10\x0cR\x11import_proto_pathb\x06proto3') ) @@ -42,8 +42,8 @@ ], containing_type=None, serialized_options=None, - serialized_start=1246, - serialized_end=1297, + serialized_start=1269, + serialized_end=1320, ) _sym_db.RegisterEnumDescriptor(_ARTIFACT_PUBLISHTARGET_TYPE) @@ -72,8 +72,8 @@ ], containing_type=None, serialized_options=None, - serialized_start=1299, - serialized_end=1373, + serialized_start=1322, + serialized_end=1396, ) _sym_db.RegisterEnumDescriptor(_ARTIFACT_RELEASELEVEL) @@ -114,8 +114,8 @@ ], containing_type=None, serialized_options=None, - serialized_start=1375, - serialized_end=1489, + serialized_start=1398, + serialized_end=1512, ) _sym_db.RegisterEnumDescriptor(_ARTIFACT_TYPE) @@ -160,8 +160,8 @@ ], containing_type=None, serialized_options=None, - serialized_start=1491, - serialized_end=1600, + serialized_start=1514, + serialized_end=1623, ) _sym_db.RegisterEnumDescriptor(_ARTIFACT_LANGUAGE) @@ -186,8 +186,8 @@ ], containing_type=None, serialized_options=None, - serialized_start=1602, - serialized_end=1642, + serialized_start=1625, + serialized_end=1665, ) _sym_db.RegisterEnumDescriptor(_ARTIFACT_ASPECT) @@ -263,8 +263,8 @@ extension_ranges=[], oneofs=[ ], - serialized_start=837, - serialized_end=888, + serialized_start=860, + serialized_end=911, ) _ARTIFACT_PACKAGEVERSION = _descriptor.Descriptor( @@ -307,8 +307,8 @@ extension_ranges=[], oneofs=[ ], - serialized_start=890, - serialized_end=983, + serialized_start=913, + serialized_end=1006, ) _ARTIFACT_PUBLISHTARGET_DIRECTORYMAPPING = _descriptor.Descriptor( @@ -351,8 +351,8 @@ extension_ranges=[], oneofs=[ ], - serialized_start=1185, - serialized_end=1244, + serialized_start=1208, + serialized_end=1267, ) _ARTIFACT_PUBLISHTARGET = _descriptor.Descriptor( @@ -403,8 +403,8 @@ extension_ranges=[], oneofs=[ ], - serialized_start=986, - serialized_end=1297, + serialized_start=1009, + serialized_end=1320, ) _ARTIFACT = _descriptor.Descriptor( @@ -492,42 +492,49 @@ is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( - name='type', full_name='googleapis.artman.Artifact.type', index=11, + name='gapic_samples', full_name='googleapis.artman.Artifact.gapic_samples', index=11, + number=19, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=_b("").decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='type', full_name='googleapis.artman.Artifact.type', index=12, number=12, type=14, cpp_type=8, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( - name='language', full_name='googleapis.artman.Artifact.language', index=12, + name='language', full_name='googleapis.artman.Artifact.language', index=13, number=13, type=14, cpp_type=8, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( - name='package_version', full_name='googleapis.artman.Artifact.package_version', index=13, + name='package_version', full_name='googleapis.artman.Artifact.package_version', index=14, number=14, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( - name='publish_targets', full_name='googleapis.artman.Artifact.publish_targets', index=14, + name='publish_targets', full_name='googleapis.artman.Artifact.publish_targets', index=15, number=15, type=11, cpp_type=10, label=3, has_default_value=False, default_value=[], message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( - name='discovery_doc', full_name='googleapis.artman.Artifact.discovery_doc', index=15, + name='discovery_doc', full_name='googleapis.artman.Artifact.discovery_doc', index=16, number=16, type=9, cpp_type=9, label=1, has_default_value=False, default_value=_b("").decode('utf-8'), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( - name='aspect', full_name='googleapis.artman.Artifact.aspect', index=16, + name='aspect', full_name='googleapis.artman.Artifact.aspect', index=17, number=17, type=14, cpp_type=8, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, @@ -550,7 +557,7 @@ oneofs=[ ], serialized_start=139, - serialized_end=1667, + serialized_end=1690, ) _CONFIG.fields_by_name['common'].message_type = _ARTIFACT diff --git a/artman/tasks/gapic_tasks.py b/artman/tasks/gapic_tasks.py index 45f00feb..ef9f3b61 100644 --- a/artman/tasks/gapic_tasks.py +++ b/artman/tasks/gapic_tasks.py @@ -103,7 +103,7 @@ class GapicCodeGenTask(task_base.TaskBase): def execute(self, language, toolkit_path, descriptor_set, service_yaml, gapic_yaml, package_metadata_yaml, proto_package, gapic_code_dir, api_name, api_version, organization_name, - aspect, generator_args): + aspect, gapic_samples, generator_args): existing = glob.glob('%s/*' % gapic_code_dir) if existing: self.exec_command(['rm', '-r'] + existing) @@ -112,6 +112,13 @@ def execute(self, language, toolkit_path, descriptor_set, service_yaml, gapic_args.append('--package=' + proto_package) if gapic_yaml: gapic_args.append('--gapic_yaml=' + os.path.abspath(gapic_yaml)) + if gapic_samples: + sample_yamls = self._get_sample_yamls(gapic_samples) + if sample_yamls: + gapic_args.append('--sample_yamls') + for sample_yaml in sample_yamls: + gapic_args.append(sample_yaml) + args = [ '--descriptor_set=' + os.path.abspath(descriptor_set), '--package_yaml2=' + os.path.abspath(package_metadata_yaml), @@ -140,6 +147,19 @@ def execute(self, language, toolkit_path, descriptor_set, service_yaml, return gapic_code_dir + def _get_sample_yamls(self, path): + sample_path = os.path.abspath(path) + if os.path.isfile(sample_path): + return [sample_path] + elif os.path.isdir(sample_path): + sample_paths = [] + for root, dirnames, filenames in os.walk(sample_path): + for filename in filenames: + if filename.endswith('.yaml'): + sample_paths.append(os.path.join(root, filename)) + return sample_paths + else: + raise ValueError('GapicCodeGenTask: `--samples` directory or file does not exist.') class DiscoGapicCodeGenTask(task_base.TaskBase): """Generates GAPIC wrappers from a Discovery document""" diff --git a/test/config/testdata/artman_pubsub.yaml b/test/config/testdata/artman_pubsub.yaml index 588fa241..67315d25 100644 --- a/test/config/testdata/artman_pubsub.yaml +++ b/test/config/testdata/artman_pubsub.yaml @@ -14,6 +14,7 @@ common: proto_path: google/iam/v1 service_yaml: /tmp/input/google/pubsub/pubsub.yaml gapic_yaml: /tmp/input/google/pubsub/v1/pubsub_gapic.yaml + gapic_samples: /path/to/the/samples artifacts: - name: java_gapic release_level: BETA diff --git a/test/config/testdata/expected_library_config.yaml b/test/config/testdata/expected_library_config.yaml index 1aacc59f..1702ffe7 100644 --- a/test/config/testdata/expected_library_config.yaml +++ b/test/config/testdata/expected_library_config.yaml @@ -14,6 +14,7 @@ common: - name: google-iam-v1 proto_path: google/iam/v1 proto_package: google.example.library.v1 + gapic_samples: '' service_yaml: /tmp/input/google/example/library/library.yaml src_proto_path: - /tmp/input/google/example/library/v1 diff --git a/test/config/testdata/expected_pubsub_java_legacy_config.yaml b/test/config/testdata/expected_pubsub_java_legacy_config.yaml index 8d5561c8..cb1ced7e 100644 --- a/test/config/testdata/expected_pubsub_java_legacy_config.yaml +++ b/test/config/testdata/expected_pubsub_java_legacy_config.yaml @@ -12,6 +12,7 @@ common: - name: google-iam-v1 proto_path: google/iam/v1 proto_package: '' + gapic_samples: /path/to/the/samples service_yaml: /tmp/input/google/pubsub/pubsub.yaml src_proto_path: - /tmp/input/google/pubsub/v1 diff --git a/test/config/testdata/expected_pubsub_java_proto_legacy_config.yaml b/test/config/testdata/expected_pubsub_java_proto_legacy_config.yaml index 2485afa6..3401acbd 100644 --- a/test/config/testdata/expected_pubsub_java_proto_legacy_config.yaml +++ b/test/config/testdata/expected_pubsub_java_proto_legacy_config.yaml @@ -14,6 +14,7 @@ common: - name: google-iam-v1 proto_path: google/iam/v1 proto_package: '' + gapic_samples: /path/to/the/samples test_proto_deps: - name: google-iam-v1 proto_path: google/iam/v1 diff --git a/test/config/testdata/expected_pubsub_python_legacy_config.yaml b/test/config/testdata/expected_pubsub_python_legacy_config.yaml index ecebe5aa..d8887553 100644 --- a/test/config/testdata/expected_pubsub_python_legacy_config.yaml +++ b/test/config/testdata/expected_pubsub_python_legacy_config.yaml @@ -12,6 +12,7 @@ common: - name: google-iam-v1 proto_path: google/iam/v1 proto_package: '' + gapic_samples: /path/to/the/samples service_yaml: /tmp/input/google/pubsub/pubsub.yaml src_proto_path: - /tmp/input/google/pubsub/v1 diff --git a/test/tasks/data/test_descriptor/descriptor_set_updated_py_docs b/test/tasks/data/test_descriptor/descriptor_set_updated_py_docs new file mode 100644 index 00000000..3eba13e1 Binary files /dev/null and b/test/tasks/data/test_descriptor/descriptor_set_updated_py_docs differ diff --git a/test/tasks/test_gapic.py b/test/tasks/test_gapic.py index 05959776..317f5523 100644 --- a/test/tasks/test_gapic.py +++ b/test/tasks/test_gapic.py @@ -158,6 +158,7 @@ def test_execute(self, exec_command, check_output): service_yaml='/path/to/service.yaml', toolkit_path='/path/to/toolkit', aspect='ALL', + gapic_samples='', generator_args='--extra_args', proto_package='' ) @@ -178,6 +179,109 @@ def test_execute(self, exec_command, check_output): ] assert_calls_equal(check_output.mock_calls, expected_cmds2) +class GapicCodeGenTaskSamplesTests(unittest.TestCase): + def mock_isdir(*args): + return args[0] == '/path/to/samples' + + def mock_isfile(*args): + return args[0] == '/path/to/samples/fakedir/fakesample.yaml' + + def mock_walk(*args): + return [ + ('/path', ('to'), []), + ('/path/to', ('samples'), []), + ('/path/to/samples', ('fakedir', 'another_fakedir'), []), + ('/path/to/samples/fakedir', (), ['fakesample.yaml']), + ('/path/to/samples/another_fakedir', (), ['another_fake_sample.yaml']) + ] + + @mock.patch.object(gapic_tasks.GapicCodeGenTask, 'exec_command') + @mock.patch.object(subprocess, 'check_output') + @mock.patch.object(os, 'walk', side_effect=mock_walk) + @mock.patch.object(os.path, 'isdir', side_effect=mock_isdir) + @mock.patch.object(os.path, 'isfile', side_effect=mock_isfile) + def test_execute(self, isfile, isdir, listdir, check_output, exec_command): + task = gapic_tasks.GapicCodeGenTask() + task.execute( + api_name='pubsub', + api_version='v1', + descriptor_set='/path/to/desc', + gapic_yaml='/path/to/pubsub.yaml', + gapic_code_dir='/path/to/output', + language='python', + organization_name='google-cloud', + package_metadata_yaml='/path/to/pmy.yaml', + service_yaml='/path/to/service.yaml', + toolkit_path='/path/to/toolkit', + aspect='ALL', + gapic_samples='/path/to/samples', + generator_args='--extra_args', + proto_package='' + ) + expected_cmds = [ + ' '.join(['java -cp', + '/path/to/toolkit/build/libs/gapic-generator-latest-fatjar.jar', + 'com.google.api.codegen.GeneratorMain LEGACY_GAPIC_AND_PACKAGE', + '--descriptor_set=/path/to/desc --package_yaml2=/path/to/pmy.yaml', + '--output=/path/to/output --language=python', + '--service_yaml=/path/to/service.yaml', + '--gapic_yaml=/path/to/pubsub.yaml', + '--sample_yamls', + '/path/to/samples/fakedir/fakesample.yaml', + '/path/to/samples/another_fakedir/another_fake_sample.yaml', + '--extra_args' + ]) + ] + assert_calls_equal(exec_command.mock_calls, expected_cmds) + + expected_cmds2 = [ + '/path/to/toolkit/gradlew -p /path/to/toolkit fatJar -Pclargs=', + ] + assert_calls_equal(check_output.mock_calls, expected_cmds2) + + @mock.patch.object(gapic_tasks.GapicCodeGenTask, 'exec_command') + @mock.patch.object(subprocess, 'check_output') + @mock.patch.object(os, 'walk', side_effect=mock_walk) + @mock.patch.object(os.path, 'isdir', side_effect=mock_isdir) + @mock.patch.object(os.path, 'isfile', side_effect=mock_isfile) + def test_execute2(self, isfile, isdir, listdir, check_output, exec_command): + task = gapic_tasks.GapicCodeGenTask() + task.execute( + api_name='pubsub', + api_version='v1', + descriptor_set='/path/to/desc', + gapic_yaml='/path/to/pubsub.yaml', + gapic_code_dir='/path/to/output', + language='python', + organization_name='google-cloud', + package_metadata_yaml='/path/to/pmy.yaml', + service_yaml='/path/to/service.yaml', + toolkit_path='/path/to/toolkit', + aspect='ALL', + gapic_samples='/path/to/samples/fakedir/fakesample.yaml', + generator_args='--extra_args', + proto_package='' + ) + expected_cmds = [ + ' '.join(['java -cp', + '/path/to/toolkit/build/libs/gapic-generator-latest-fatjar.jar', + 'com.google.api.codegen.GeneratorMain LEGACY_GAPIC_AND_PACKAGE', + '--descriptor_set=/path/to/desc --package_yaml2=/path/to/pmy.yaml', + '--output=/path/to/output --language=python', + '--service_yaml=/path/to/service.yaml', + '--gapic_yaml=/path/to/pubsub.yaml', + '--sample_yamls', + '/path/to/samples/fakedir/fakesample.yaml', + '--extra_args' + ]) + ] + assert_calls_equal(exec_command.mock_calls, expected_cmds) + + expected_cmds2 = [ + '/path/to/toolkit/gradlew -p /path/to/toolkit fatJar -Pclargs=', + ] + assert_calls_equal(check_output.mock_calls, expected_cmds2) + class DiscoGapicCodeGenTaskTests(unittest.TestCase): @mock.patch.object(subprocess, 'check_output')