Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix command spawning when spaces occur in the path #520

Merged
merged 7 commits into from
Dec 17, 2017
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
2 changes: 1 addition & 1 deletion lib/aruba/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

require 'aruba/platform'
require 'aruba/api/core'
require 'aruba/api/command'
require 'aruba/api/commands'

require 'aruba/api/environment'
require 'aruba/api/filesystem'
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/aruba/cucumber/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
shell ||= Aruba.platform.default_shell

Aruba::ScriptFile.new(interpreter: shell, content: commands, path: full_path).call
step "I run `#{full_path}`"
run_command_and_stop(Shellwords.escape(full_path), fail_on_error: false)
end

When(/^I run `([^`]*)` interactively$/)do |cmd|
Expand Down
2 changes: 1 addition & 1 deletion lib/aruba/platforms/unix_command_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(cmd)

# Convert to array
def to_a
Shellwords.split __getobj__
[__getobj__]
end
end
end
Expand Down
13 changes: 7 additions & 6 deletions lib/aruba/platforms/windows_command_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ module Platforms
#
# @private
class WindowsCommandString < SimpleDelegator
def initialize(cmd)
__setobj__ format('%s /c "%s"', Aruba.platform.which('cmd.exe'), cmd)
end

# Convert to array
def to_a
Shellwords.split( __getobj__.gsub('\\', 'ARUBA_TMP_PATHSEPARATOR') )
.map { |w| w.gsub('ARUBA_TMP_PATHSEPARATOR', '\\') }
[cmd_path, '/c', __getobj__]
end

private

def cmd_path
Aruba.platform.which('cmd.exe')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/aruba/processes/spawn_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def start

@started = true

@process = ChildProcess.build(*[command_string.to_a, arguments].flatten)
@process = ChildProcess.build(*command_string.to_a, *arguments)
@stdout_file = Tempfile.new('aruba-stdout-')
@stderr_file = Tempfile.new('aruba-stderr-')

Expand Down
17 changes: 17 additions & 0 deletions spec/aruba/platforms/unix_command_string_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

RSpec.describe Aruba::Platforms::UnixCommandString do
let(:command_string) { described_class.new(base_command) }

describe '#to_a' do
context 'with a command with a path' do
let(:base_command) { '/foo/bar' }
it { expect(command_string.to_a).to eq [base_command] }
end

context 'with a command with a path containing spaces' do
let(:base_command) { '/foo bar/baz' }
it { expect(command_string.to_a).to eq [base_command] }
end
end
end
22 changes: 22 additions & 0 deletions spec/aruba/platforms/windows_command_string_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'

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

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] }
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', base_command] }
end
end
end
36 changes: 36 additions & 0 deletions spec/aruba/processes/spawn_process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,41 @@
expect { process.start }.to raise_error(Aruba::LaunchError, "It tried to start foo. Foobar!")
end
end

context 'with a command with a space in the path on unix' do
let(:child) { instance_double(ChildProcess::AbstractProcess).as_null_object }
let(:io) { instance_double(ChildProcess::AbstractIO).as_null_object }
let(:command) { 'foo' }
let(:command_path) { '/path with space/foo' }

before do
allow(Aruba.platform).to receive(:command_string).and_return Aruba::Platforms::UnixCommandString
allow(Aruba.platform).to receive(:which).with(command, anything).and_return(command_path)
allow(ChildProcess).to receive(:build).with(command_path).and_return(child)
allow(child).to receive(:io).and_return io
allow(child).to receive(:environment).and_return({})
end

it { expect { process.start }.to_not raise_error }
end

context 'with a command with a space in the path on windows' do
let(:child) { instance_double(ChildProcess::AbstractProcess).as_null_object }
let(:io) { instance_double(ChildProcess::AbstractIO).as_null_object }
let(:command) { 'foo' }
let(:cmd_path) { 'C:\Some Path\cmd.exe' }
let(:command_path) { 'D:\Bar Baz\foo' }

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(ChildProcess).to receive(:build).with(cmd_path, '/c', command_path).and_return(child)
allow(child).to receive(:io).and_return io
allow(child).to receive(:environment).and_return({})
end

it { expect { process.start }.to_not raise_error }
end
end
end