• 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    fail 'CONFIG env variable unexpectedly unset' unless ENV['CONFIG']
22    bins_sub_dir = ENV['CONFIG']
23
24    src_dir = File.join(File.dirname(__FILE__), '..', '..', '..', '..')
25    pb_dir = File.join(src_dir, 'proto')
26    bins_dir = File.join(src_dir, '..', 'bins', bins_sub_dir)
27
28    plugin = File.join(bins_dir, 'grpc_ruby_plugin')
29    protoc = File.join(bins_dir, 'protobuf', 'protoc')
30
31    # Generate the service from the proto
32    Dir.mktmpdir(nil, File.dirname(__FILE__)) do |tmp_dir|
33      gen_file = system(protoc,
34                        '-I.',
35                        'grpc/testing/package_options.proto',
36                        "--grpc_out=#{tmp_dir}", # generate the service
37                        "--ruby_out=#{tmp_dir}", # generate the definitions
38                        "--plugin=protoc-gen-grpc=#{plugin}",
39                        chdir: pb_dir,
40                        out: File::NULL)
41
42      expect(gen_file).to be_truthy
43      begin
44        $LOAD_PATH.push(tmp_dir)
45        expect { Grpc::Testing::Package::Options::TestService::Service }.to raise_error(NameError)
46        expect(require('grpc/testing/package_options_services_pb')).to be_truthy
47        expect { Grpc::Testing::Package::Options::TestService::Service }.to_not raise_error
48      ensure
49        $LOAD_PATH.delete(tmp_dir)
50      end
51    end
52  end
53end
54