Add test coverage for auto-attestation skipping on non-rubygems.org hosts and JRuby#9326
Add test coverage for auto-attestation skipping on non-rubygems.org hosts and JRuby#9326hsbt merged 3 commits intopush-with-attestationfrom
Conversation
Co-authored-by: hsbt <12301+hsbt@users.noreply.github.com>
Co-authored-by: hsbt <12301+hsbt@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds missing test coverage for the push command’s auto-attestation branching logic, specifically the cases where attestation should be skipped on JRuby and when pushing to non-rubygems.org hosts.
Changes:
- Add a test asserting
attest!is skipped when the gem’sallowed_push_hostcauses pushes to a non-rubygems.orghost. - Add a test asserting
attest!is skipped whenRUBY_ENGINEisjruby.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @cmd.options[:args] = [@path] | ||
|
|
||
| attest_called = false | ||
| @cmd.stub(:attest!, proc { attest_called = true }) do |
There was a problem hiding this comment.
The attest! stub returns true, so if the code path regresses and attest! is invoked, the test will likely fail with an unrelated Gem.read_binary(true) type error before reaching the refute attest_called assertion. Consider making the stub explicitly fail (e.g., raising/flunking) to produce a clearer failure reason tied to the behavior being tested.
| @cmd.stub(:attest!, proc { attest_called = true }) do | |
| @cmd.stub(:attest!, proc { | |
| attest_called = true | |
| flunk "attest! should not be called for non-rubygems.org hosts" | |
| }) do |
| engine = RUBY_ENGINE | ||
| Object.send :remove_const, :RUBY_ENGINE | ||
| Object.const_set :RUBY_ENGINE, "jruby" | ||
|
|
||
| begin |
There was a problem hiding this comment.
RUBY_ENGINE is modified outside the begin/ensure region, so if an unexpected exception occurs during the constant swap, the original value may not be restored and could leak into other tests. Consider moving the constant swap inside the begin/ensure (or using the existing helper methods in test/rubygems/helper.rb that manage Ruby version/engine constants) to guarantee cleanup.
| engine = RUBY_ENGINE | |
| Object.send :remove_const, :RUBY_ENGINE | |
| Object.const_set :RUBY_ENGINE, "jruby" | |
| begin | |
| begin | |
| engine = RUBY_ENGINE | |
| Object.send :remove_const, :RUBY_ENGINE | |
| Object.const_set :RUBY_ENGINE, "jruby" |
| @cmd.stub(:attest!, proc { attest_called = true }) do | ||
| @cmd.execute | ||
| end |
There was a problem hiding this comment.
Same issue as above: the attest! stub returns true, so if attest! is unexpectedly called this test will likely error out during request construction rather than failing with a clear assertion message. Prefer a stub that explicitly fails if invoked so the failure points directly at the regression.
What was the end-user or developer problem that led to this PR?
The auto-attestation feature in the push command has branching logic to skip attestation on JRuby and non-rubygems.org hosts, but test coverage only verified the happy path and error fallback. Missing tests for the conditional skipping paths.
What is your fix for the problem, implemented in this PR?
Added two test cases to verify auto-attestation is correctly skipped:
test_execute_attestation_skipped_on_non_rubygems_host: Verifiesattest!is not called when pushing to a private gem server. Usesallowed_push_hostmetadata to simulate a non-rubygems.org host, stubsattest!with a flag, and asserts rawapplication/octet-streampush succeeds.test_execute_attestation_skipped_on_jruby: Verifiesattest!is not called on JRuby. Temporarily modifiesRUBY_ENGINEconstant to simulate JRuby environment, stubsattest!with a flag, and asserts raw push succeeds with proper cleanup in ensure block.Both tests follow the existing pattern of using boolean flags with method stubs to track invocations, ensuring the branching logic in
send_push_requestworks correctly.Make sure the following tasks are checked
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.