Skip to content
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
18 changes: 13 additions & 5 deletions lib/thor/shell/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,11 @@ def file_collision_help(block_given) #:nodoc:
end

def show_diff(destination, content) #:nodoc:
diff_cmd = ENV["THOR_DIFF"] || ENV["RAILS_DIFF"] || "diff -u"

require "tempfile"
Tempfile.open(File.basename(destination), File.dirname(destination), binmode: true) do |temp|
temp.write content
temp.rewind
system %(#{diff_cmd} "#{destination}" "#{temp.path}")
system(*diff_tool, destination, temp.path)
end
end

Expand Down Expand Up @@ -372,12 +370,22 @@ def merge(destination, content) #:nodoc:
Tempfile.open([File.basename(destination), File.extname(destination)], File.dirname(destination)) do |temp|
temp.write content
temp.rewind
system(merge_tool, temp.path, destination)
system(*merge_tool, temp.path, destination)
end
end

def merge_tool #:nodoc:
@merge_tool ||= ENV["THOR_MERGE"] || "git difftool --no-index"
@merge_tool ||= begin
require "shellwords"
Shellwords.split(ENV["THOR_MERGE"] || "git difftool --no-index")
end
end

def diff_tool #:nodoc:
@diff_cmd ||= begin
require "shellwords"
Shellwords.split(ENV["THOR_DIFF"] || ENV["RAILS_DIFF"] || "diff -u")
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/actions/create_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def silence!
it "executes the block given to show file content" do
create_file("doc/config.rb")
expect(Thor::LineEditor).to receive(:readline).and_return("d", "n")
expect(@base.shell).to receive(:system).with(/diff -u/)
expect(@base.shell).to receive(:system).with("diff", "-u", /doc\/config\.rb/, /doc\/config\.rb/)
invoke!
end

Expand Down
2 changes: 1 addition & 1 deletion spec/actions/file_manipulation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def file
File.write(destination, "blabla")

expect(Thor::LineEditor).to receive(:readline).and_return("d", "y")
expect(runner.shell).to receive(:system).with(/diff -u/)
expect(runner.shell).to receive(:system).with("diff", "-u", /encoding_with_utf8.thor/, /encoding_with_utf8.thor/)
action :copy_file, "encoding_with_utf8.thor"

exists_and_identical?("encoding_with_utf8.thor", "encoding_with_utf8.thor")
Expand Down
15 changes: 14 additions & 1 deletion spec/shell/basic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def #456 Lanç...
it "invokes the diff command" do
expect(Thor::LineEditor).to receive(:readline).and_return("d")
expect(Thor::LineEditor).to receive(:readline).and_return("n")
expect(shell).to receive(:system).with(/diff -u/)
expect(shell).to receive(:system).with("diff", "-u", "foo", /foo/)
capture(:stdout) { shell.file_collision("foo") {} }
end

Expand All @@ -564,6 +564,19 @@ def #456 Lanç...
capture(:stdout) { shell.file_collision("foo") {} }
end

it "invokes the merge tool with arguments when THOR_MERGE contains them" do
allow(ENV).to receive(:[]).with("THOR_MERGE").and_return("nvim -d")
expect(Thor::LineEditor).to receive(:readline).and_return("m")
expect(shell).to receive(:system).with("nvim", "-d", /foo/, "foo")
capture(:stdout) { shell.file_collision("foo") {} }
end

it "invokes the merge tool with arguments when there is no THOR_MERGE" do
expect(Thor::LineEditor).to receive(:readline).and_return("m")
expect(shell).to receive(:system).with("git", "difftool", "--no-index", /foo/, "foo")
capture(:stdout) { shell.file_collision("foo") {} }
end

it "show warning if user chooses merge but merge tool is not specified" do
allow(shell).to receive(:merge_tool).and_return("")
expect(Thor::LineEditor).to receive(:readline).and_return("m")
Expand Down