Skip to content

Commit

Permalink
Merge pull request #180 from denzelem/ed/add-enconding-option-176
Browse files Browse the repository at this point in the history
Add encoding options to Resolver::Static#read and Resolver::Dynamic#read
  • Loading branch information
rafaelfranca authored Sep 4, 2024
2 parents 6c924e2 + 89768db commit e7b4c17
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/propshaft/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def initialize(path, logical_path:, load_path:)
@path, @logical_path, @load_path = path, Pathname.new(logical_path), load_path
end

def content
File.binread(path)
def content(encoding: "ASCII-8BIT")
File.read(path, encoding: encoding)
end

def content_type
Expand Down
4 changes: 2 additions & 2 deletions lib/propshaft/resolver/dynamic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def resolve(logical_path)
end
end

def read(logical_path)
def read(logical_path, options = {})
if asset = load_path.find(logical_path)
asset.content
asset.content(**options)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/propshaft/resolver/static.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def resolve(logical_path)
end
end

def read(logical_path)
def read(logical_path, encoding: "ASCII-8BIT")
if asset_path = parsed_manifest[logical_path]
manifest_path.dirname.join(asset_path).read
File.read(manifest_path.dirname.join(asset_path), encoding: encoding)
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/propshaft/asset_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@

class Propshaft::AssetTest < ActiveSupport::TestCase
test "content" do
assert_equal "ASCII-8BIT", find_asset("one.txt").content.encoding.to_s
assert_equal "One from first path", find_asset("one.txt").content
end

test "content with encoding" do
assert_equal "UTF-8", find_asset("one.txt").content(encoding: "UTF-8").encoding.to_s
assert_equal "One from first path", find_asset("one.txt").content(encoding: "UTF-8")
end

test "content type" do
assert_equal "text/plain", find_asset("one.txt").content_type.to_s
assert_equal "text/javascript", find_asset("again.js").content_type.to_s
Expand Down
6 changes: 6 additions & 0 deletions test/propshaft/resolver/dynamic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ class Propshaft::Resolver::DynamicTest < ActiveSupport::TestCase
end

test "reading static asset" do
assert_equal "ASCII-8BIT", @resolver.read("one.txt").encoding.to_s
assert_equal "One from first path", @resolver.read("one.txt")
end

test "reading static asset with encoding option" do
assert_equal "UTF-8", @resolver.read("one.txt", encoding: "UTF-8").encoding.to_s
assert_equal "One from first path", @resolver.read("one.txt", encoding: "UTF-8")
end

test "resolving missing asset returns nil" do
assert_nil @resolver.resolve("nowhere.txt")
end
Expand Down
6 changes: 6 additions & 0 deletions test/propshaft/resolver/static_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ class Propshaft::Resolver::StaticTest < ActiveSupport::TestCase
end

test "reading static asset" do
assert_equal "ASCII-8BIT", @resolver.read("one.txt").encoding.to_s
assert_equal "One from first path", @resolver.read("one.txt")
end

test "reading static asset with encoding option" do
assert_equal "UTF-8", @resolver.read("one.txt", encoding: "UTF-8").encoding.to_s
assert_equal "One from first path", @resolver.read("one.txt", encoding: "UTF-8")
end

test "resolving missing asset returns nil" do
assert_nil @resolver.resolve("nowhere.txt")
end
Expand Down

0 comments on commit e7b4c17

Please sign in to comment.