• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15require 'spec_helper'
16require 'open3'
17require 'tmpdir'
18
19describe '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
51end
52
53def with_protos(file_paths)
54  fail 'CONFIG env variable unexpectedly unset' unless ENV['CONFIG']
55  bins_sub_dir = ENV['CONFIG']
56
57  pb_dir = File.dirname(__FILE__)
58  bins_dir = File.join('..', '..', '..', '..', '..', 'bins', bins_sub_dir)
59
60  plugin = File.join(bins_dir, 'grpc_ruby_plugin')
61  protoc = File.join(bins_dir, 'protobuf', 'protoc')
62
63  # Generate the service from the proto
64  Dir.mktmpdir(nil, File.dirname(__FILE__)) do |tmp_dir|
65    gen_file = system(protoc,
66                      '-I.',
67                      *file_paths,
68                      "--grpc_out=#{tmp_dir}", # generate the service
69                      "--ruby_out=#{tmp_dir}", # generate the definitions
70                      "--plugin=protoc-gen-grpc=#{plugin}",
71                      chdir: pb_dir,
72                      out: File::NULL)
73
74    expect(gen_file).to be_truthy
75    begin
76      $LOAD_PATH.push(tmp_dir)
77      yield
78    ensure
79      $LOAD_PATH.delete(tmp_dir)
80    end
81  end
82end
83