@@ -38,6 +38,25 @@ module SchemaDefinition
3838 )
3939 end
4040
41+ it "does not invoke Buf or dump a proto artifact when no indexed types are defined" do
42+ expect ( BufBreakingChangeDetector ) . not_to receive ( :new )
43+ write_proto_schema ( table_defs : <<~EOS )
44+ s.object_type "Point" do |t|
45+ t.field "x", "Float"
46+ end
47+
48+ s.on_root_query_type do |t|
49+ t.field "point", "Point" do |f|
50+ f.resolve_with :object_without_lookahead
51+ end
52+ end
53+ EOS
54+
55+ run_rake_with_proto ( "schema_artifacts:dump" )
56+
57+ expect ( read_artifact ( PROTO_SCHEMA_FILE ) ) . to be_nil
58+ end
59+
4160 it "idempotently dumps proto artifacts" do
4261 write_proto_schema ( table_defs : <<~EOS )
4362 s.object_type "Product" do |t|
@@ -55,6 +74,8 @@ module SchemaDefinition
5574 end
5675
5776 it "persists proto field-number mappings and reuses them on the next dump" do
77+ stub_buf_breaking_changes ( nil )
78+
5879 write_proto_schema ( table_defs : <<~EOS )
5980 s.object_type "Product" do |t|
6081 t.field "id", "ID"
@@ -96,6 +117,88 @@ module SchemaDefinition
96117
97118 expect ( read_artifact ( PROTO_SCHEMA_FILE ) ) . to include ( "string id = 1;" , "string name = 2;" )
98119 end
120+
121+ it "dumps a compatible change that Buf reports as safe" do
122+ write_proto_schema ( table_defs : <<~EOS )
123+ s.object_type "Product" do |t|
124+ t.field "id", "ID"
125+ t.index "products"
126+ end
127+ EOS
128+ run_rake_with_proto ( "schema_artifacts:dump" )
129+
130+ detector = stub_buf_breaking_changes ( nil )
131+ write_proto_schema ( table_defs : <<~EOS )
132+ s.object_type "Product" do |t|
133+ t.field "id", "ID"
134+ t.field "name", "String"
135+ t.index "products"
136+ end
137+ EOS
138+
139+ expect {
140+ run_rake_with_proto ( "schema_artifacts:dump" )
141+ } . to change { read_artifact ( PROTO_SCHEMA_FILE ) }
142+
143+ expect ( detector ) . to have_received ( :breaking_changes ) . with (
144+ current_schema : a_string_including ( "string name = 2;" ) ,
145+ against_schema : a_string_excluding ( "string name = 2;" )
146+ )
147+ end
148+
149+ it "refuses to dump a breaking change reported by Buf" do
150+ write_proto_schema ( table_defs : <<~EOS )
151+ s.object_type "Product" do |t|
152+ t.field "id", "ID"
153+ t.index "products"
154+ end
155+ EOS
156+ run_rake_with_proto ( "schema_artifacts:dump" )
157+
158+ original_proto = read_artifact ( PROTO_SCHEMA_FILE )
159+ stub_buf_breaking_changes ( "schema.proto: Field changed type from string to int32." )
160+ write_proto_schema ( table_defs : <<~EOS )
161+ s.object_type "Product" do |t|
162+ t.field "id", "Int"
163+ t.index "products"
164+ end
165+ EOS
166+
167+ expect {
168+ run_rake_with_proto ( "schema_artifacts:dump" )
169+ } . to abort_with a_string_including (
170+ "Buf detected a breaking change" ,
171+ "Field changed type from string to int32" ,
172+ "Protobuf offers no way to version your way out of this" ,
173+ "reserve the"
174+ )
175+ expect ( read_artifact ( PROTO_SCHEMA_FILE ) ) . to eq ( original_proto )
176+ end
177+
178+ it "surfaces Buf failures without a stack trace" do
179+ write_proto_schema ( table_defs : <<~EOS )
180+ s.object_type "Product" do |t|
181+ t.field "id", "ID"
182+ t.index "products"
183+ end
184+ EOS
185+ run_rake_with_proto ( "schema_artifacts:dump" )
186+
187+ detector = instance_double ( BufBreakingChangeDetector )
188+ allow ( detector ) . to receive ( :breaking_changes ) . and_raise ( Errors ::SchemaError , "Buf could not compile an import." )
189+ allow ( BufBreakingChangeDetector ) . to receive ( :new ) . and_return ( detector )
190+ write_proto_schema ( table_defs : <<~EOS )
191+ s.object_type "Product" do |t|
192+ t.field "id", "ID"
193+ t.field "name", "String"
194+ t.index "products"
195+ end
196+ EOS
197+
198+ expect {
199+ run_rake_with_proto ( "schema_artifacts:dump" )
200+ } . to abort_with ( "Buf could not compile an import." )
201+ end
99202 end
100203
101204 describe "schema_artifacts:check" do
@@ -129,6 +232,12 @@ def write_proto_schema(table_defs:)
129232 EOS
130233 end
131234
235+ def stub_buf_breaking_changes ( changes )
236+ detector = instance_double ( BufBreakingChangeDetector , breaking_changes : changes )
237+ allow ( BufBreakingChangeDetector ) . to receive ( :new ) . and_return ( detector )
238+ detector
239+ end
240+
132241 def run_rake_with_proto ( *args )
133242 run_rake ( *args ) do |output |
134243 ElasticGraph ::SchemaDefinition ::RakeTasks . new (
0 commit comments