2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'fixtures/classes'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
|
|
|
describe :kernel_system, shared: true do
|
|
|
|
it "executes the specified command in a subprocess" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { @object.system("echo a") }.should output_to_fd("a\n")
|
2017-05-07 08:04:49 -04:00
|
|
|
|
|
|
|
$?.should be_an_instance_of Process::Status
|
2020-05-03 06:28:29 -04:00
|
|
|
$?.should.success?
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when the command exits with a zero exit status" do
|
|
|
|
@object.system(ruby_cmd('exit 0')).should == true
|
|
|
|
|
|
|
|
$?.should be_an_instance_of Process::Status
|
2020-05-03 06:28:29 -04:00
|
|
|
$?.should.success?
|
2017-05-07 08:04:49 -04:00
|
|
|
$?.exitstatus.should == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false when the command exits with a non-zero exit status" do
|
|
|
|
@object.system(ruby_cmd('exit 1')).should == false
|
|
|
|
|
|
|
|
$?.should be_an_instance_of Process::Status
|
2020-05-03 06:28:29 -04:00
|
|
|
$?.should_not.success?
|
2017-05-07 08:04:49 -04:00
|
|
|
$?.exitstatus.should == 1
|
|
|
|
end
|
|
|
|
|
2018-01-29 11:08:16 -05:00
|
|
|
ruby_version_is "2.6" do
|
|
|
|
it "raises RuntimeError when `exception: true` is given and the command exits with a non-zero exit status" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { @object.system(ruby_cmd('exit 1'), exception: true) }.should raise_error(RuntimeError)
|
2018-01-29 11:08:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises Errno::ENOENT when `exception: true` is given and the specified command does not exist" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { @object.system('feature_14386', exception: true) }.should raise_error(Errno::ENOENT)
|
2018-01-29 11:08:16 -05:00
|
|
|
end
|
2018-01-24 09:31:40 -05:00
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
it "returns nil when command execution fails" do
|
|
|
|
@object.system("sad").should be_nil
|
|
|
|
|
|
|
|
$?.should be_an_instance_of Process::Status
|
|
|
|
$?.pid.should be_kind_of(Integer)
|
2020-05-02 09:42:12 -04:00
|
|
|
$?.should_not.success?
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not write to stderr when command execution fails" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { @object.system("sad") }.should output_to_fd("", STDERR)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
platform_is_not :windows do
|
|
|
|
before :each do
|
|
|
|
@shell = ENV['SHELL']
|
|
|
|
end
|
|
|
|
|
2017-05-27 17:55:02 -04:00
|
|
|
after :each do
|
2017-05-07 08:04:49 -04:00
|
|
|
ENV['SHELL'] = @shell
|
|
|
|
end
|
|
|
|
|
|
|
|
it "executes with `sh` if the command contains shell characters" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { @object.system("echo $0") }.should output_to_fd("sh\n")
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores SHELL env var and always uses `sh`" do
|
2017-05-27 17:55:02 -04:00
|
|
|
ENV['SHELL'] = "/bin/fakeshell"
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { @object.system("echo $0") }.should output_to_fd("sh\n")
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before :each do
|
|
|
|
ENV['TEST_SH_EXPANSION'] = 'foo'
|
|
|
|
@shell_var = '$TEST_SH_EXPANSION'
|
|
|
|
platform_is :windows do
|
|
|
|
@shell_var = '%TEST_SH_EXPANSION%'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after :each do
|
|
|
|
ENV.delete('TEST_SH_EXPANSION')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "expands shell variables when given a single string argument" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { @object.system("echo #{@shell_var}") }.should output_to_fd("foo\n")
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
platform_is_not :windows do
|
|
|
|
it "does not expand shell variables when given multiples arguments" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { @object.system("echo", @shell_var) }.should output_to_fd("#{@shell_var}\n")
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
platform_is :windows do
|
|
|
|
it "does expand shell variables when given multiples arguments" do
|
|
|
|
# See https://bugs.ruby-lang.org/issues/12231
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { @object.system("echo", @shell_var) }.should output_to_fd("foo\n")
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
platform_is :windows do
|
|
|
|
it "runs commands starting with any number of @ using shell" do
|
|
|
|
`#{ruby_cmd("p system 'does_not_exist'")} 2>NUL`.chomp.should == "nil"
|
|
|
|
@object.system('@does_not_exist 2>NUL').should == false
|
|
|
|
@object.system("@@@#{ruby_cmd('exit 0')}").should == true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Kernel#system" do
|
|
|
|
it "is a private method" do
|
|
|
|
Kernel.should have_private_instance_method(:system)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like :kernel_system, :system, KernelSpecs::Method.new
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Kernel.system" do
|
|
|
|
it_behaves_like :kernel_system, :system, Kernel
|
|
|
|
end
|