Skip to content

Commit

Permalink
Merge pull request #225 from bkeepers/bom
Browse files Browse the repository at this point in the history
Gracefully handle files with UTF-8 BOM
  • Loading branch information
bkeepers committed Jan 14, 2016
2 parents 6e34733 + 4d32245 commit c44af19
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/dotenv/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def load
end

def read
File.read(@filename)
File.open(@filename, "rb:bom|utf-8") { |file| file.read }
end

def apply
Expand Down
13 changes: 13 additions & 0 deletions spec/dotenv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@
end
end

describe "Unicode" do
subject { fixture_path("bom.env") }

it "loads a file with a Unicode BOM" do
expect(Dotenv.load(subject)).to eql({"BOM" => "UTF-8"})
end

it "fixture file has UTF-8 BOM" do
contents = File.open(subject, "rb") { |f| f.read }.force_encoding("UTF-8")
expect(contents).to start_with("\xEF\xBB\xBF")
end
end

def expand(path)
File.expand_path path
end
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/bom.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BOM=UTF-8

0 comments on commit c44af19

Please sign in to comment.