Skip to content

Commit

Permalink
Ensure reset of deferred validation (#481)
Browse files Browse the repository at this point in the history
There is an edge case where the block in defer_validation could raises an exception and validation_deferred isn't reset properly. Perhaps contrived, but I thought it worth addressing.

Co-authored-by: Patrik Ragnarsson <patrik@starkast.net>
  • Loading branch information
dpep and dentarg committed Dec 15, 2022
1 parent d6f6288 commit 32bd534
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2392,7 +2392,8 @@ def defer_validation
yield
@validation_deferred = false
validate
return nil
ensure
@validation_deferred = false
end

protected
Expand Down
22 changes: 22 additions & 0 deletions spec/addressable/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6743,3 +6743,25 @@ def to_str
).to eq(main)
end
end

describe Addressable::URI, "when deferring validation" do
subject(:deferred) { uri.instance_variable_get(:@validation_deferred) }

let(:uri) { Addressable::URI.parse("http://example.com") }

it "defers validation within the block" do
uri.defer_validation do
expect(deferred).to be true
end
end

it "always resets deferral afterward" do
expect { uri.defer_validation { raise "boom" } }.to raise_error("boom")
expect(deferred).to be false
end

it "returns nil" do
res = uri.defer_validation {}
expect(res).to be nil
end
end

0 comments on commit 32bd534

Please sign in to comment.