fix(rspec): scope everything the gem defines under RSpec::Trunk - #1211
Merged
Merged
Conversation
The gem is loaded into other people's test suites but defined things in the global namespace: top-level classes from the native extension (Status, TestReport, CIInfo, ...), a global env_parse function, eleven top-level methods, $globals, and color methods patched onto String. These collide with app code (a Status model fails with "superclass mismatch") and with gems like colorize. Everything now lives under RSpec::Trunk. The per-run state is a single RSpec::Trunk.current_run, created only when Trunk is enabled. The Example patch is a prepended module calling super instead of an alias chain. Metadata keys the gem sets are trunk_-prefixed, with the old unprefixed names still written and deprecated. The documented setup (the gem, require "trunk_spec_helper", env vars) is unchanged. Whether Trunk is enabled is now decided once, when the helper loads, rather than rechecked on every failure. test/namespace_spec.rb loads the gem in a fresh process and fails if anything is defined outside RSpec::Trunk; RuboCop now enforces Style/GlobalVars with no exceptions and Style/TopLevelMethodDefinition. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
😎 Merged successfully - details. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1211 +/- ##
==========================================
+ Coverage 83.74% 83.96% +0.21%
==========================================
Files 74 74
Lines 17745 17745
==========================================
+ Hits 14860 14899 +39
+ Misses 2885 2846 -39 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…refactor check_quarantine and add_test_case carry over the trunk-ignore comments from the methods they replace (handle_quarantine_check and add_test_case), which also suppressed Metrics/CyclomaticComplexity. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
acatxnamedvirtue
approved these changes
Sep 28, 2026
|
Contributor
|
nice work fixing those "naughty" things |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
rspec_trunk_flaky_testsruns inside other people's test suites, but it defined a lot in the global namespace:Status,TestReport,IsQuarantinedResult,CIInfo,CIPlatform,BranchClass, plus a globalenv_parsefunction.trunk_spec_helper.rb: eleven top-level methods (escape,trunk_disabled,strip_ansi_codes, ...), which become private methods on every object; top-level constants (ANSI_ESCAPE_PATTERN,PlainColorizer,TrunkAnalyticsListener); three$globals; andcolorize/red/green/yellowpatched ontoString.These break real apps:
Statusclass, e.g. a Rails model, fails withsuperclass mismatch for class Status, whichever of the two loads first.Stringpatch breaks thecolorizegem regardless of load order:"hi".colorize(:red)becomes"\e[redmhi\e[0m".escapesilently replaces the gem's. The gem usesescapeto build the file and classname it sends to Trunk, which feed the quarantine lookup.Changes
RSpec::Trunk. The native classes are defined under it (magnus::wrap(class = "RSpec::Trunk::..."), andruby_initincontextandtest_reportnow takes the namespace), andenv_parsebecameRSpec::Trunk.env_parse.RSpec::Trunk::Colorsreplaces theStringpatch.RSpec::Trunk.current_run, holding the report and the warn-once flags. It's only created when Trunk is enabled; before, aTestReportwas built at require time even when Trunk was off.RSpec::Core::Exampleis extended withprependplussuperinstead of an alias chain, and only when Trunk is enabled. It adds one public method,trunk_id.trunk_-prefixed (:trunk_quarantined_exception,:trunk_attempt_number,:trunk_is_description_generated). The old unprefixed keys are still written and marked deprecated inRSpec::Trunk::DEPRECATED_METADATA_KEYS.Compatibility
The documented setup is unchanged:
gem "rspec_trunk_flaky_tests",require "trunk_spec_helper", and the env vars listed in the docs. I checked this against https://docs.trunk.io/flaky-tests/get-started/frameworks/rspec. Anyone who followed the docs upgrades without edits.Two things do change:
TestReport,TrunkAnalyticsListener,env_parse,$test_report,"x".redand so on directly must use theRSpec::Trunknames. For a 0.x gem this suggests a minor bump (0.16.0), with the renames listed in the release notes. No compatibility shims.DISABLE_RSPEC_TRUNK_FLAKY_TESTS,TRUNK_ORG_URL_SLUGandTRUNK_API_TOKENare read whentrunk_spec_helperloads. Before, they were also rechecked on every failure, so env vars changed after the require no longer have any effect. The docs already say to set them before running.Keeping it this way
test/namespace_spec.rbloads the gem, with Trunk enabled, into a fresh Ruby process and compares everything defined before and after. It fails if anything besidesRSpec::Trunkappears. It covers the native extension and patches to core classes, which RuboCop can't see..rubocop.ymlno longer exempts$test_reportfromStyle/GlobalVars, and turns onStyle/TopLevelMethodDefinition.Testing
bundle exec rake test: 23 examples, 0 failures. This includes the newnamespace_spec.rbandmetadata_spec.rb.cargo fmt --checkis clean. RuboCop reports nothing new; the only offense isset_exception, which the existingtrunk-ignorealready covers.🤖 Generated with Claude Code