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

Add encoding options to Resolver::Static#read and Resolver::Dynamic#read #180

Merged
merged 1 commit into from
Sep 4, 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
4 changes: 2 additions & 2 deletions lib/propshaft/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def initialize(path, logical_path:, version: nil)
@path, @logical_path, @version = path, Pathname.new(logical_path), version
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)
Comment on lines +15 to +17
Copy link
Collaborator

@brenogazzola brenogazzola Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the parameter here named options instead of encoding like in assets.rb and static.rb?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used an option hash to avoid duplicating the default value to this top-level method.

Alternative 1:

def read(logical_path, encoding: 'ASCII-8BIT')
  # ...
  asset.content(encoding: encoding)
  # ...
end

Alternative 2:

def read(logical_path, encoding: nil)
  # ...
  options = {}
  options[:encoding] = encoding if encoding.present?
  asset.content(**options)
  # ...
end

@brenogazzola Wdyt?

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