Skip to content

Commit

Permalink
Adjust specs for default skip-empty behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
darronschall committed May 28, 2024
1 parent 328e071 commit a711ee4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 86 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
111 changes: 25 additions & 86 deletions spec/twirp/protoc_plugin/process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
end
end

context "when passing an invalid value for the skip-empty flag" do
context "when passing the `skip-empty` flag" do
# Generate code gen request fixture:
# `./spec/support/create_fixture -b -p skip-empty=true -f service_code_gen_request_invalid_skip_empty_value_param_pb.bin ./spec/fixtures/service.proto`
let(:request_pb) { fixture("service_code_gen_request_invalid_skip_empty_value_param_pb.bin").read }
# `./spec/support/create_fixture -b -p skip-empty -f service_code_gen_request_skip_empty_flag_pb.bin ./spec/fixtures/service.proto`
let(:request_pb) { fixture("service_code_gen_request_skip_empty_flag_pb.bin").read }

it "raises an argument error" do
expect {
Twirp::ProtocPlugin.process(request_pb)
}.to raise_error(ArgumentError, "Unexpected value passed to skip-empty flag: true")
it "outputs a deprecation warning to stderr" do
allow($stderr).to receive(:<<)

Twirp::ProtocPlugin.process(request_pb)

expect($stderr).to have_received(:<<).with("WARNING: The `skip-empty` flag is deprecated and will be removed next release; it is now the default behavior.")
end
end

Expand Down Expand Up @@ -137,76 +139,13 @@ class HelloWorldClient < ::Twirp::Client
end

context "when using a complex example with imported types and multiple services" do
# Generate code gen request fixture:
# `./spec/support/create_fixture -b -f api_code_gen_request_pb.bin -o ./spec/fixtures/complex_example -I ./spec/fixtures/complex_example api.proto common/rpc/status.proto common/type/color.proto common/type/time_of_day.proto`
let(:api_code_gen_request_pb) { fixture("complex_example/api_code_gen_request_pb.bin").read }

context "when omitting the `skip-empty` option", :aggregate_failures do
it "generates expected output" do
response_pb = Twirp::ProtocPlugin.process(api_code_gen_request_pb)
response = Google::Protobuf::Compiler::CodeGeneratorResponse.decode(response_pb)

expect(response.supported_features).to eq(Google::Protobuf::Compiler::CodeGeneratorResponse::Feature::FEATURE_PROTO3_OPTIONAL)
expect(response.file.size).to eq(4)

first_file = response.file[0]
expect(first_file.name).to eq("common/rpc/status_twirp.rb")
expect(first_file.content).to be_empty_scaffolding("common/rpc/status.proto", "status_pb", %w[Common RPC])

second_file = response.file[1]
expect(second_file.name).to eq("common/type/color_twirp.rb")
expect(second_file.content).to be_empty_scaffolding("common/type/color.proto", "color_pb", %w[Common Type])

third_file = response.file[2]
expect(third_file.name).to eq("common/type/time_of_day_twirp.rb")
expect(third_file.content).to be_empty_scaffolding("common/type/time_of_day.proto", "time_of_day_pb", %w[Common Type])

fourth_file = response.file[3]
expect(fourth_file.name).to eq("api_twirp.rb")
expect(fourth_file.content).to eq <<~EOF
# frozen_string_literal: true
# Generated by the protoc-gen-twirp_ruby gem v#{Twirp::ProtocPlugin::VERSION}. DO NOT EDIT!
# source: api.proto
require "twirp"
require_relative "api_pb"
module API
class GreetService < ::Twirp::Service
package "api"
service "GreetService"
rpc :SayHello, HelloRequest, HelloResponse, ruby_method: :say_hello
rpc :SayGoodbye, GoodbyeRequest, GoodbyeResponse, ruby_method: :say_goodbye
rpc :ChangeColor, ::Common::Type::Color, ChangeColorWrapper::Response, ruby_method: :change_color
end
class GreetClient < ::Twirp::Client
client_for GreetService
end
class StatusService < ::Twirp::Service
package "api"
service "StatusService"
rpc :GetStatus, ::Google::Protobuf::Empty, ::Common::RPC::Status, ruby_method: :get_status
rpc :GetTimeOfDay, TimeOfDayRequest, ::Common::Type::TimeOfDay, ruby_method: :get_time_of_day
end
class StatusClient < ::Twirp::Client
client_for StatusService
end
end
EOF
end
end

context "when specifying the `skip-empty` option" do
context "when run without any options" do
# Generate code gen request fixture:
# `./spec/support/create_fixture -b -f api_code_gen_request_skip_empty_pb.bin -p skip-empty -o ./spec/fixtures/complex_example -I ./spec/fixtures/complex_example api.proto common/rpc/status.proto common/type/color.proto common/type/time_of_day.proto`
let(:api_code_gen_request_skip_empty_pb) { fixture("complex_example/api_code_gen_request_skip_empty_pb.bin").read }
# `./spec/support/create_fixture -b -f api_code_gen_request_pb.bin -o ./spec/fixtures/complex_example -I ./spec/fixtures/complex_example api.proto common/rpc/status.proto common/type/color.proto common/type/time_of_day.proto`
let(:api_code_gen_request_pb) { fixture("complex_example/api_code_gen_request_pb.bin").read }

it "generates a single file containing two services and two clients" do
response_pb = Twirp::ProtocPlugin.process(api_code_gen_request_skip_empty_pb)
response_pb = Twirp::ProtocPlugin.process(api_code_gen_request_pb)
response = Google::Protobuf::Compiler::CodeGeneratorResponse.decode(response_pb)

expect(response.supported_features).to eq(Google::Protobuf::Compiler::CodeGeneratorResponse::Feature::FEATURE_PROTO3_OPTIONAL)
Expand Down Expand Up @@ -251,13 +190,13 @@ class StatusClient < ::Twirp::Client
end
end

context "when specifying the `skip-empty` and `generate=both` options" do
context "when specifying the `generate=both` option" do
# Generate code gen request fixture:
# `./spec/support/create_fixture -b -f api_code_gen_request_skip_empty_generate_both_pb.bin -p skip-empty,generate=both -o ./spec/fixtures/complex_example -I ./spec/fixtures/complex_example api.proto common/rpc/status.proto common/type/color.proto common/type/time_of_day.proto`
let(:api_code_gen_request_skip_empty_generate_both_pb) { fixture("complex_example/api_code_gen_request_skip_empty_generate_both_pb.bin").read }
# `./spec/support/create_fixture -b -f api_code_gen_request_generate_both_pb.bin -p generate=both -o ./spec/fixtures/complex_example -I ./spec/fixtures/complex_example api.proto common/rpc/status.proto common/type/color.proto common/type/time_of_day.proto`
let(:api_code_gen_request_generate_both_pb) { fixture("complex_example/api_code_gen_request_generate_both_pb.bin").read }

it "generates a single file containing two services and two clients" do
response_pb = Twirp::ProtocPlugin.process(api_code_gen_request_skip_empty_generate_both_pb)
response_pb = Twirp::ProtocPlugin.process(api_code_gen_request_generate_both_pb)
response = Google::Protobuf::Compiler::CodeGeneratorResponse.decode(response_pb)

expect(response.supported_features).to eq(Google::Protobuf::Compiler::CodeGeneratorResponse::Feature::FEATURE_PROTO3_OPTIONAL)
Expand Down Expand Up @@ -302,13 +241,13 @@ class StatusClient < ::Twirp::Client
end
end

context "when specifying the `skip-empty` and `generate=service` options" do
context "when specifying the `generate=service` option" do
# Generate code gen request fixture:
# `./spec/support/create_fixture -b -f api_code_gen_request_skip_empty_generate_service_pb.bin -p skip-empty,generate=service -o ./spec/fixtures/complex_example -I ./spec/fixtures/complex_example api.proto common/rpc/status.proto common/type/color.proto common/type/time_of_day.proto`
let(:api_code_gen_request_skip_empty_generate_service_pb) { fixture("complex_example/api_code_gen_request_skip_empty_generate_service_pb.bin").read }
# `./spec/support/create_fixture -b -f api_code_gen_request_generate_service_pb.bin -p generate=service -o ./spec/fixtures/complex_example -I ./spec/fixtures/complex_example api.proto common/rpc/status.proto common/type/color.proto common/type/time_of_day.proto`
let(:api_code_gen_request_generate_service_pb) { fixture("complex_example/api_code_gen_request_generate_service_pb.bin").read }

it "generates a single file with only two services" do
response_pb = Twirp::ProtocPlugin.process(api_code_gen_request_skip_empty_generate_service_pb)
response_pb = Twirp::ProtocPlugin.process(api_code_gen_request_generate_service_pb)
response = Google::Protobuf::Compiler::CodeGeneratorResponse.decode(response_pb)

expect(response.supported_features).to eq(Google::Protobuf::Compiler::CodeGeneratorResponse::Feature::FEATURE_PROTO3_OPTIONAL)
Expand Down Expand Up @@ -345,13 +284,13 @@ class StatusService < ::Twirp::Service
end
end

context "when specifying the `skip-empty` and `generate=client` options" do
context "when specifying the `generate=client` option" do
# Generate code gen request fixture:
# `./spec/support/create_fixture -b -f api_code_gen_request_skip_empty_generate_client_pb.bin -p skip-empty,generate=client -o ./spec/fixtures/complex_example -I ./spec/fixtures/complex_example api.proto common/rpc/status.proto common/type/color.proto common/type/time_of_day.proto`
let(:api_code_gen_request_skip_empty_generate_client_pb) { fixture("complex_example/api_code_gen_request_skip_empty_generate_client_pb.bin").read }
# `./spec/support/create_fixture -b -f api_code_gen_request_generate_client_pb.bin -p generate=client -o ./spec/fixtures/complex_example -I ./spec/fixtures/complex_example api.proto common/rpc/status.proto common/type/color.proto common/type/time_of_day.proto`
let(:api_code_gen_request_generate_client_pb) { fixture("complex_example/api_code_gen_request_generate_client_pb.bin").read }

it "generates a single file with only two clients without the `client_for` DSL" do
response_pb = Twirp::ProtocPlugin.process(api_code_gen_request_skip_empty_generate_client_pb)
response_pb = Twirp::ProtocPlugin.process(api_code_gen_request_generate_client_pb)
response = Google::Protobuf::Compiler::CodeGeneratorResponse.decode(response_pb)

expect(response.supported_features).to eq(Google::Protobuf::Compiler::CodeGeneratorResponse::Feature::FEATURE_PROTO3_OPTIONAL)
Expand Down

0 comments on commit a711ee4

Please sign in to comment.