Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: "can't modify frozen Hash" when stubbing ENV #504

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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