package_option_spec.rb 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. require 'spec_helper'
  15. require 'open3'
  16. require 'tmpdir'
  17. describe 'Code Generation Options' do
  18. it 'should generate and respect package options' do
  19. with_protos(%w[grpc/testing/package_options.proto]) do
  20. expect { Grpc::Testing::Package::Options::TestService::Service }.to raise_error(NameError)
  21. expect(require('grpc/testing/package_options_services_pb')).to be_truthy
  22. expect { Grpc::Testing::Package::Options::TestService::Service }.to_not raise_error
  23. expect { Grpc::Testing::TestService::Service }.to raise_error(NameError)
  24. end
  25. end
  26. it 'should generate and respect Ruby style package options' do
  27. with_protos(%w[grpc/testing/package_options_ruby_style.proto grpc/testing/package_options_import.proto]) do
  28. expect { RPC::Test::New::Package::Options::AnotherTestService::Service }.to raise_error(NameError)
  29. expect(require('grpc/testing/package_options_ruby_style_services_pb')).to be_truthy
  30. expect { RPC::Test::New::Package::Options::AnotherTestService::Service }.to_not raise_error
  31. expect { Grpc::Testing::AnotherTestService::Service }.to raise_error(NameError)
  32. services = RPC::Test::New::Package::Options::AnotherTestService::Service.rpc_descs
  33. expect(services[:GetTest].input).to eq(RPC::Test::New::Package::Options::AnotherTestRequest)
  34. expect(services[:GetTest].output).to eq(RPC::Test::New::Package::Options::AnotherTestResponse)
  35. expect(services[:OtherTest].input).to eq(A::Other::Thing)
  36. expect(services[:OtherTest].output).to eq(A::Other::Thing)
  37. expect(services[:FooTest].input).to eq(RPC::Test::New::Package::Options::Foo)
  38. expect(services[:FooTest].output).to eq(RPC::Test::New::Package::Options::Foo)
  39. end
  40. end
  41. end
  42. def with_protos(file_paths)
  43. fail 'CONFIG env variable unexpectedly unset' unless ENV['CONFIG']
  44. bins_sub_dir = ENV['CONFIG']
  45. pb_dir = File.dirname(__FILE__)
  46. bins_dir = File.join('..', '..', '..', '..', '..', 'bins', bins_sub_dir)
  47. plugin = File.join(bins_dir, 'grpc_ruby_plugin')
  48. protoc = File.join(bins_dir, 'protobuf', 'protoc')
  49. # Generate the service from the proto
  50. Dir.mktmpdir(nil, File.dirname(__FILE__)) do |tmp_dir|
  51. gen_file = system(protoc,
  52. '-I.',
  53. *file_paths,
  54. "--grpc_out=#{tmp_dir}", # generate the service
  55. "--ruby_out=#{tmp_dir}", # generate the definitions
  56. "--plugin=protoc-gen-grpc=#{plugin}",
  57. chdir: pb_dir,
  58. out: File::NULL)
  59. expect(gen_file).to be_truthy
  60. begin
  61. $LOAD_PATH.push(tmp_dir)
  62. yield
  63. ensure
  64. $LOAD_PATH.delete(tmp_dir)
  65. end
  66. end
  67. end