Skip to content

Commit 0782c78

Browse files
authored
Make schema artifact validation opt-in (#1415)
## Why The protobuf test helper compiles every generated schema, even during local unit tests. That added about 2.9 seconds to the protobuf suite on this Mac. GraphQL tests already make extra schema validation opt-in, so protobuf tests should follow the same pattern. ## What Use `VALIDATE_SCHEMA_ARTIFACTS` for both GraphQL and protobuf schema validation. Leave extra validation off by default locally and enable it in the existing CI job. ## How Rename `VALIDATE_GRAPHQL_SCHEMAS` and update its references. Check the new flag once when each test helper loads. When enabled, prepend a module that validates every generated protobuf schema for the rest of the process. Always render the schema and assign field numbers. Explicit encode/decode integration tests still run `protoc` without the flag. Protobuf validation failures include the compiler diagnostics and explain how to reproduce locally with `VALIDATE_SCHEMA_ARTIFACTS=1`. The flag keeps the existing presence-based behavior: any set value enables validation when the helper loads. Later changes to the environment do not affect validation. The old flag no longer enables it. ## Risk Low. This changes test support and CI configuration, not runtime ingestion. Local commands that use the old flag must switch to `VALIDATE_SCHEMA_ARTIFACTS`. ## Testing Before this change, benchmarked `bundle exec rspec elasticgraph-proto_ingestion/spec --seed 4247` four times per mode, alternating run order. A temporary probe skipped only automatic descriptor compilation; explicit encode/decode calls remained enabled. Median command runtime was 9.50 seconds with compilation and 6.62 seconds without it. The probe recorded 63 automatic compiler calls, taking about 2.59 seconds per run. These results are from an Apple Silicon Mac using the bundled x86 compiler under Rosetta. Manually checked the protobuf helper in fresh Ruby processes with the flag absent and with `VALIDATE_SCHEMA_ARTIFACTS=0`. After loading the helper, changed the flag to unset and then to `1`; validation stayed fixed at its initial setting in both cases. Also passed an invalid protobuf schema through the enabled helper and verified that the failure included compiler diagnostics and `VALIDATE_SCHEMA_ARTIFACTS=1`. Empty schemas still skip compilation. ## Bigger picture Follow-up to #1405 and Myron's suggestion to share one validation flag across generated schema artifacts.
1 parent 316c2da commit 0782c78

5 files changed

Lines changed: 39 additions & 11 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Custom gems can be added via `Gemfile-custom` (see `Gemfile-custom.example`), th
216216
- Use `expect_to_return_non_nil_values_from_all_attributes` to test wrapper classes (like `WarehouseLambda`, `GraphQL`, `Indexer`, etc.). This automatically exercises every zero-argument method and verifies all dependencies are built successfully.
217217
- Use the `:capture_logs` RSpec tag instead of logger test doubles for verifying log output. Access logs with `logged_jsons_of_type(message_type)`.
218218
- Use `build_*` helper methods from `spec/support/builds_*.rb` to construct test objects. These helpers provide sensible defaults while allowing selective overrides for testing specific scenarios.
219-
- Only tag a spec with `:dont_validate_graphql_schema` when it's actually required for the test to pass under `VALIDATE_GRAPHQL_SCHEMAS=1` (the tag skips that validation). First try to fix the test's schema so it produces a valid GraphQL schema while still exercising what the test is meant to exercise; reach for the tag only when that isn't possible. Verify by running the spec with `VALIDATE_GRAPHQL_SCHEMAS=1`.
219+
- Only tag a spec with `:dont_validate_graphql_schema` when it's actually required for the test to pass under `VALIDATE_SCHEMA_ARTIFACTS=1` (the tag skips that validation). First try to fix the test's schema so it produces a valid GraphQL schema while still exercising what the test is meant to exercise; reach for the tag only when that isn't possible. Verify by running the spec with `VALIDATE_SCHEMA_ARTIFACTS=1`.
220220

221221
## Important Patterns
222222

‎elasticgraph-json_ingestion/spec/unit/elastic_graph/json_ingestion/schema_definition/indexing/wrappers_spec.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module SchemaDefinition
4343
})
4444
end
4545

46-
# `:dont_validate_graphql_schema` matters here: with `VALIDATE_GRAPHQL_SCHEMAS=1` (as on CI), the
46+
# `:dont_validate_graphql_schema` matters here: with `VALIDATE_SCHEMA_ARTIFACTS=1` (as on CI), the
4747
# eager SDL validation would raise this error during GraphQL schema generation, before the JSON
4848
# schema generation path exercises the wrapped `FieldReference#resolve` nil return that this
4949
# example exists to cover.

‎elasticgraph-proto_ingestion/spec/support/proto_schema_support.rb‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def define_proto_schema(**options, &block)
2828
define_proto_schema_results(**options, &block).proto_schema
2929
end
3030

31-
# Defines a schema, verifies its proto compiles, and returns its `Results`.
32-
# Pass the results of a previous
31+
# Defines a schema and returns its `Results`. When `VALIDATE_SCHEMA_ARTIFACTS` is set
32+
# when this file loads, also verifies that its proto compiles. Pass the results of a previous
3333
# `define_proto_schema_results` call as `prior_results` to seed the new schema with the
3434
# field-number mappings the previous one generated, standing in for the
3535
# `proto_field_numbers.yaml` file that `schema_artifacts:dump` would have written between
@@ -49,8 +49,7 @@ def define_proto_schema_results(prior_results = nil, proto_field_number_mappings
4949
block.call(schema)
5050
end
5151

52-
proto = results.proto_schema
53-
run_protoc(proto, "--descriptor_set_out=#{::File::NULL}", "") unless proto.empty?
52+
results.proto_schema
5453
results
5554
end
5655

@@ -96,6 +95,9 @@ def run_protoc(proto_schema, operation, input)
9695
binmode: true,
9796
chdir: directory
9897
)
98+
# simplecov:disable -- custom failure messages are only used during opt-in schema validation.
99+
errors = yield(errors) if block_given?
100+
# simplecov:enable
99101
expect(status.success?).to be(true), errors
100102
output
101103
end
@@ -132,6 +134,32 @@ def proto_type_def_from(proto, type)
132134
end
133135
end
134136

137+
# simplecov:disable -- only enabled for opt-in schema validation.
138+
if ENV["VALIDATE_SCHEMA_ARTIFACTS"]
139+
module ValidateProtoSchemas
140+
def define_proto_schema_results(...)
141+
super.tap do |results|
142+
proto = results.proto_schema
143+
next if proto.empty?
144+
145+
run_protoc(proto, "--descriptor_set_out=#{::File::NULL}", "") do |errors|
146+
<<~EOS
147+
This test generated a protobuf schema that can't be compiled by protoc. The error[^1] is shown below.
148+
Note that the extra protobuf compilation validation is not applied by default when you run tests locally.
149+
The extra validation runs on CI (where we are OK with the slow down that produces), and you can opt into
150+
it by passing `VALIDATE_SCHEMA_ARTIFACTS=1` when running your tests.
151+
152+
[^1]: #{errors}
153+
EOS
154+
end
155+
end
156+
end
157+
158+
SchemaSupport.prepend(self)
159+
end
160+
end
161+
# simplecov:enable
162+
135163
RSpec.configure do |config|
136164
config.include SchemaSupport, :proto_schema
137165
end

‎script/ci_parts/run_specs_with_vcr‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ source "script/ci_parts/setup_env" "test" $1 $2
1010
unset NO_VCR
1111
unset RUBYOPT
1212

13-
# In addition, we want to run the extra GraphQL schema validation when running the whole schema together.
14-
VALIDATE_GRAPHQL_SCHEMAS=1 script/run_specs
13+
# In addition, validate generated GraphQL and protobuf schemas when running the whole suite together.
14+
VALIDATE_SCHEMA_ARTIFACTS=1 script/run_specs

‎spec_support/lib/elastic_graph/spec_support/validate_graphql_schemas.rb‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
# Here we define a method that uses the GraphQL gem to enforce the validity of our generated
1515
# schemas. However, parsing all our test schemas makes our tests 2-3 times slower. We don't
1616
# want to slow down every local test run to add this validation, so it's something that you can
17-
# opt in to via the `VALIDATE_GRAPHQL_SCHEMAS` env var. We also pass this env var from our CI
17+
# opt in to via the `VALIDATE_SCHEMA_ARTIFACTS` env var. We also pass this env var from our CI
1818
# build where it's ok if the test suite is slower.
1919
#
2020
# simplecov:disable -- only one of the two branches gets run on any test run.
21-
return unless ENV["VALIDATE_GRAPHQL_SCHEMAS"]
21+
return unless ENV["VALIDATE_SCHEMA_ARTIFACTS"]
2222

2323
require "elastic_graph/schema_definition/test_support"
2424
require "graphql"
@@ -48,7 +48,7 @@ def self.validate_graphql_schema!(results)
4848
This test generated SDL that can't be parsed by the GraphQL gem. The error[^1] is shown below.
4949
Note that the extra GraphQL gem parsing validation is not applied by default when you run tests locally.
5050
The extra validation runs on CI (where we are OK with the slow down that produces), and you can opt into
51-
it by passing `VALIDATE_GRAPHQL_SCHEMAS=1` when running your tests.
51+
it by passing `VALIDATE_SCHEMA_ARTIFACTS=1` when running your tests.
5252
5353
[^1]: #{e.message}
5454
EOS

0 commit comments

Comments
 (0)