Skip to content

Commit

Permalink
Prefer to use URI.open and File.open instead of Kernel.open
Browse files Browse the repository at this point in the history
  • Loading branch information
hsbt committed Feb 9, 2023
1 parent 0e40ffa commit f3e6538
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/thor/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def apply(path, config = {})
require "open-uri"
URI.open(path, "Accept" => "application/x-thor-template", &:read)
else
open(path, &:read)
File.open(path, &:read)
end

instance_eval(contents, path)
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

0 comments on commit f3e6538

Please sign in to comment.