Skip to content

Commit

Permalink
Merge pull request #802 from hsbt/suppress-warning-codeql
Browse files Browse the repository at this point in the history
Prefer to use URI.open and File.open instead of Kernel.open
  • Loading branch information
rafaelfranca authored Feb 23, 2023
2 parents 0e40ffa + 16db563 commit 2d69011
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
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

0 comments on commit 2d69011

Please sign in to comment.