| 1 |
# Copyright 2018 gRPC authors. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
require 'spec_helper' |
| 16 |
require 'open3' |
| 17 |
require 'tmpdir' |
| 18 |
|
| 19 |
describe 'Code Generation Options' do |
| 20 |
it 'should generate and respect package options' do |
| 21 |
with_protos(%w[grpc/testing/package_options.proto]) do |
| 22 |
expect { Grpc::Testing::Package::Options::TestService::Service }.to raise_error(NameError) |
| 23 |
expect(require('grpc/testing/package_options_services_pb')).to be_truthy |
| 24 |
expect { Grpc::Testing::Package::Options::TestService::Service }.to_not raise_error |
| 25 |
expect { Grpc::Testing::TestService::Service }.to raise_error(NameError) |
| 26 |
end |
| 27 |
end |
| 28 |
|
| 29 |
it 'should generate and respect Ruby style package options' do |
| 30 |
with_protos(['grpc/testing/package_options_ruby_style.proto', |
| 31 |
'grpc/testing/package_options_import.proto', |
| 32 |
'grpc/testing/package_options_import2.proto']) do |
| 33 |
expect { RPC::Test::New::Package::Options::AnotherTestService::Service }.to raise_error(NameError) |
| 34 |
expect(require('grpc/testing/package_options_ruby_style_services_pb')).to be_truthy |
| 35 |
expect { RPC::Test::New::Package::Options::AnotherTestService::Service }.to_not raise_error |
| 36 |
expect { Grpc::Testing::AnotherTestService::Service }.to raise_error(NameError) |
| 37 |
|
| 38 |
services = RPC::Test::New::Package::Options::AnotherTestService::Service.rpc_descs |
| 39 |
expect(services[:GetTest].input).to eq(RPC::Test::New::Package::Options::AnotherTestRequest) |
| 40 |
expect(services[:GetTest].output).to eq(RPC::Test::New::Package::Options::AnotherTestResponse) |
| 41 |
expect(services[:OtherTest].input).to eq(A::Other::Thing) |
| 42 |
expect(services[:OtherTest].output).to eq(A::Other::Thing) |
| 43 |
expect(services[:PackageTest].input).to eq(A::Other::Thing) |
| 44 |
expect(services[:PackageTest].output).to eq(B::Other::Foo::Bar) |
| 45 |
expect(services[:FooTest].input).to eq(RPC::Test::New::Package::Options::Foo) |
| 46 |
expect(services[:FooTest].output).to eq(RPC::Test::New::Package::Options::Foo) |
| 47 |
expect(services[:NestedMessageTest].input).to eq(RPC::Test::New::Package::Options::Foo) |
| 48 |
expect(services[:NestedMessageTest].output).to eq(RPC::Test::New::Package::Options::Bar::Baz) |
| 49 |
end |
| 50 |
end |
| 51 |
|
| 52 |
it 'should generate when package and service has same name' do |
| 53 |
with_protos(['grpc/testing/same_package_service_name.proto']) do |
| 54 |
expect { SameName::SameName::Service }.to raise_error(NameError) |
| 55 |
expect(require('grpc/testing/same_package_service_name_services_pb')).to be_truthy |
| 56 |
expect { SameName::SameName::Service }.to_not raise_error |
| 57 |
expect { SameName::Request }.to_not raise_error |
| 58 |
expect { SameName::Status }.to_not raise_error |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
it 'should generate when ruby_package and service has same name' do |
| 63 |
with_protos(['grpc/testing/same_ruby_package_service_name.proto']) do |
| 64 |
expect { SameName2::SameName2::Service }.to raise_error(NameError) |
| 65 |
expect(require('grpc/testing/same_ruby_package_service_name_services_pb')).to be_truthy |
| 66 |
expect { SameName2::SameName2::Service }.to_not raise_error |
| 67 |
expect { SameName2::Request }.to_not raise_error |
| 68 |
expect { SameName2::Status }.to_not raise_error |
| 69 |
end |
| 70 |
end |
| 71 |
end |
| 72 |
|
| 73 |
def with_protos(file_paths) |
| 74 |
pb_dir = File.dirname(__FILE__) |
| 75 |
bins_dir = File.join('..', '..', '..', '..', '..', 'cmake', 'build') |
| 76 |
plugin = File.join(bins_dir, 'grpc_ruby_plugin') |
| 77 |
protoc = File.join(bins_dir, 'third_party', 'protobuf', 'protoc') |
| 78 |
|
| 79 |
# Generate the service from the proto |
| 80 |
Dir.mktmpdir(nil, File.dirname(__FILE__)) do |tmp_dir| |
| 81 |
gen_file = system(protoc, |
| 82 |
'-I.', |
| 83 |
*file_paths, |
| 84 |
"--grpc_out=#{tmp_dir}", # generate the service |
| 85 |
"--ruby_out=#{tmp_dir}", # generate the definitions |
| 86 |
"--plugin=protoc-gen-grpc=#{plugin}", |
| 87 |
chdir: pb_dir, |
| 88 |
out: File::NULL) |
| 89 |
|
| 90 |
expect(gen_file).to be_truthy |
| 91 |
begin |
| 92 |
$LOAD_PATH.push(tmp_dir) |
| 93 |
yield |
| 94 |
ensure |
| 95 |
$LOAD_PATH.delete(tmp_dir) |
| 96 |
end |
| 97 |
end |
| 98 |
end |