Skip to content

Commit

Permalink
Simplify Windows command string
Browse files Browse the repository at this point in the history
Process.spawn takes care of the escaping for us.
  • Loading branch information
mvz committed Jan 21, 2023
1 parent 1fdf2e0 commit ba9b147
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 33 deletions.
19 changes: 1 addition & 18 deletions lib/aruba/platforms/windows_command_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module Aruba
module Platforms
# This is a command which should be run
#
# This adds `cmd.exec` in front of commmand
#
# @private
class WindowsCommandString
def initialize(command, *arguments)
Expand All @@ -15,22 +13,7 @@ def initialize(command, *arguments)

# Convert to array
def to_a
[cmd_path, "/c", [escaped_command, *escaped_arguments].join(" ")]
end

private

def escaped_arguments
@arguments.map { |arg| arg.gsub(/"/, '"""') }
.map { |arg| / /.match?(arg) ? "\"#{arg}\"" : arg }
end

def escaped_command
@command.gsub(/ /, '""" """')
end

def cmd_path
Aruba.platform.which("cmd.exe")
[@command, *@arguments]
end
end
end
Expand Down
11 changes: 3 additions & 8 deletions spec/aruba/platforms/windows_command_string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@

RSpec.describe Aruba::Platforms::WindowsCommandString do
let(:command_string) { described_class.new(base_command, *arguments) }
let(:cmd_path) { 'C:\Some Path\cmd.exe' }
let(:arguments) { [] }

before do
allow(Aruba.platform).to receive(:which).with("cmd.exe").and_return(cmd_path)
end

describe "#to_a" do
context "with a command with a path" do
let(:base_command) { "C:\\Foo\\Bar" }

it { expect(command_string.to_a).to eq [cmd_path, "/c", base_command] }
it { expect(command_string.to_a).to eq [base_command] }
end

context "with a command with a path containing spaces" do
let(:base_command) { "C:\\Foo Bar\\Baz" }

it { expect(command_string.to_a).to eq [cmd_path, "/c", 'C:\Foo""" """Bar\Baz'] }
it { expect(command_string.to_a).to eq [base_command] }
end

context "with a command and arguments" do
Expand All @@ -28,7 +23,7 @@

it {
expect(command_string.to_a)
.to eq [cmd_path, "/c", "#{base_command} -w \"baz quux\""]
.to eq [base_command, *arguments]
}
end
end
Expand Down
13 changes: 6 additions & 7 deletions spec/aruba/processes/spawn_process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@
before do
allow(Aruba.platform).to receive(:command_string)
.and_return Aruba::Platforms::WindowsCommandString
allow(Aruba.platform).to receive(:which).with("cmd.exe").and_return(cmd_path)
allow(Aruba.platform)
.to receive(:which).with(command, anything).and_return(command_path)
allow(Aruba::Processes::ProcessRunner).to receive(:new).and_return(child)
Expand All @@ -289,30 +288,30 @@
end

context "with a command without a space in the path" do
it "passes the command and shell paths as single strings to ProcessRunner.new" do
it "passes the command as-is" do
process.start
expect(Aruba::Processes::ProcessRunner).to have_received(:new)
.with([cmd_path, "/c", command_path])
.with([command_path])
end
end

context "with a command with a space in the path" do
let(:command_path) { 'D:\Bar Baz\foo' }

it "escapes the spaces using excessive double quotes" do
it "passes the command as-is" do
process.start
expect(Aruba::Processes::ProcessRunner).to have_received(:new)
.with([cmd_path, "/c", 'D:\Bar""" """Baz\foo'])
.with([command_path])
end
end

context "with a command with arguments" do
let(:command_line) { "foo -x 'bar \"baz\"'" }

it "passes the command and arguments as one string with escaped quotes" do
it "passes the command and arguments individually" do
process.start
expect(Aruba::Processes::ProcessRunner).to have_received(:new)
.with([cmd_path, "/c", "#{command_path} -x \"bar \"\"\"baz\"\"\"\""])
.with([command_path, "-x", "bar \"baz\""])
end
end
end
Expand Down

0 comments on commit ba9b147

Please sign in to comment.