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

Prefer to use URI.open and File.open instead of Kernel.open #802

Merged
merged 3 commits into from
Feb 23, 2023
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: 3 additions & 2 deletions lib/thor/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,10 @@ def apply(path, config = {})

contents = if is_uri
require "open-uri"
URI.open(path, "Accept" => "application/x-thor-template", &:read)
# for ruby 2.1-2.4
URI.send(:open, path, "Accept" => "application/x-thor-template", &:read)
else
open(path, &:read)
File.open(path, &:read)
end

instance_eval(contents, path)
Expand Down
2 changes: 1 addition & 1 deletion lib/thor/actions/file_manipulation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def get(source, *args, &block)
URI.send(:open, source) { |input| input.binmode.read }
else
source = File.expand_path(find_in_source_paths(source.to_s))
open(source) { |input| input.binmode.read }
File.open(source) { |input| input.binmode.read }
end

destination ||= if block_given?
Expand Down
5 changes: 3 additions & 2 deletions lib/thor/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ def install(name) # rubocop:disable Metrics/MethodLength
if File.directory?(File.expand_path(name))
base = File.join(name, "main.thor")
package = :directory
contents = open(base, &:read)
contents = File.open(base, &:read)
else
base = name
package = :file
contents = open(name, &:read)
require "open-uri"
contents = URI.send(:open, name, &:read) # for ruby 2.1-2.4
end
rescue Errno::ENOENT
raise Error, "Error opening file '#{name}'"
Expand Down
4 changes: 2 additions & 2 deletions spec/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def file
allow(@template).to receive(:read).and_return(@template)

@file = "/"
allow(runner).to receive(:open).and_return(@template)
allow(File).to receive(:open).and_return(@template)
end

it "accepts a URL as the path" do
Expand All @@ -255,7 +255,7 @@ def file

it "accepts a local file path with spaces" do
@file = File.expand_path("fixtures/path with spaces", File.dirname(__FILE__))
expect(runner).to receive(:open).with(@file).and_return(@template)
expect(File).to receive(:open).with(@file).and_return(@template)
action(:apply, @file)
end

Expand Down