Skip to content

Commit

Permalink
Merge pull request #504 from bkeepers/autorestore-stubbed
Browse files Browse the repository at this point in the history
Fix: "can't modify frozen Hash" when stubbing ENV
  • Loading branch information
bkeepers committed May 6, 2024
2 parents e9c1907 + 6a12390 commit ea5de88
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/dotenv/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ def any?
private

def snapshot
ENV.to_h.freeze
# `dup` should not be required here, but some people use `stub_const` to replace ENV with
# a `Hash`. This ensures that we get a frozen copy of that instead of freezing the original.
# https://github.com/bkeepers/dotenv/issues/482
ENV.to_h.dup.freeze
end
end
end
9 changes: 9 additions & 0 deletions spec/dotenv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@
Dotenv.instance_variable_set(:@diff, nil)
expect { Dotenv.restore }.not_to raise_error
end

it "can save and restore stubbed ENV" do
stub_const("ENV", ENV.to_h.merge("STUBBED" => "1"))
Dotenv.save
ENV["MODIFIED"] = "1"
Dotenv.restore
expect(ENV["STUBBED"]).to eq("1")
expect(ENV["MODIFED"]).to be(nil)
end
end

describe "modify" do
Expand Down

0 comments on commit ea5de88

Please sign in to comment.