From f3e6538ba0267f5ff52580805719800ef66a85e6 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Tue, 13 Dec 2022 18:44:32 +0900 Subject: [PATCH 1/3] Prefer to use URI.open and File.open instead of Kernel.open --- lib/thor/actions.rb | 2 +- lib/thor/runner.rb | 5 +++-- spec/actions_spec.rb | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/thor/actions.rb b/lib/thor/actions.rb index 7e81e5c0..67d07720 100644 --- a/lib/thor/actions.rb +++ b/lib/thor/actions.rb @@ -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) diff --git a/lib/thor/runner.rb b/lib/thor/runner.rb index 441ab7df..24accfde 100644 --- a/lib/thor/runner.rb +++ b/lib/thor/runner.rb @@ -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}'" diff --git a/spec/actions_spec.rb b/spec/actions_spec.rb index 14df41fd..ee050deb 100644 --- a/spec/actions_spec.rb +++ b/spec/actions_spec.rb @@ -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 @@ -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 From e7713ad30300dbc587dcd93738891f68c69237f1 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Tue, 13 Dec 2022 18:50:26 +0900 Subject: [PATCH 2/3] Workaround for Ruby 2.1-2.4 --- lib/thor/actions.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/thor/actions.rb b/lib/thor/actions.rb index 67d07720..e4af5df9 100644 --- a/lib/thor/actions.rb +++ b/lib/thor/actions.rb @@ -223,7 +223,8 @@ 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 File.open(path, &:read) end From 16db5639f18021fd70c050107bbca5df5fbc3f65 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Thu, 15 Dec 2022 13:57:24 +0900 Subject: [PATCH 3/3] Also use File.open --- lib/thor/actions/file_manipulation.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/thor/actions/file_manipulation.rb b/lib/thor/actions/file_manipulation.rb index c20e5d19..d7e6cb9f 100644 --- a/lib/thor/actions/file_manipulation.rb +++ b/lib/thor/actions/file_manipulation.rb @@ -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?