Skip to content

Commit 9b3a58b

Browse files
alessandro54eregon
authored andcommitted
Fix Process.spawn to raise correct errors for non-executable and missing files
1 parent c09a9ce commit 9b3a58b

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

core/process/spawn_spec.rb

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,13 +694,53 @@ def child_pids(pid)
694694
end
695695

696696
it "raises an Errno::ENOENT if the command does not exist" do
697-
-> { Process.spawn "nonesuch" }.should raise_error(Errno::ENOENT)
697+
-> { Process.spawn "nonesuch" }.should raise_error(Errno::ENOENT, /No such file or directory.*nonesuch/)
698+
end
699+
700+
it "sets $? to exit status 127 when the command does not exist" do
701+
Process.spawn("nonesuch") rescue nil
702+
$?.exitstatus.should == 127
703+
end
704+
705+
it "raises an Errno::ENOENT if the file does not exist" do
706+
-> { Process.spawn "./nonesuch" }.should raise_error(Errno::ENOENT, /No such file or directory.*nonesuch/)
707+
end
708+
709+
it "sets $? to exit status 127 when the file does not exist" do
710+
Process.spawn("./nonesuch") rescue nil
711+
$?.exitstatus.should == 127
712+
end
713+
714+
platform_is_not :windows do
715+
it "raises an Errno::EACCES when the path is a directory" do
716+
-> { Process.spawn "./" }.should raise_error(Errno::EACCES, /Permission denied/)
717+
end
698718
end
699719

700720
unless File.executable?(__FILE__) # Some FS (e.g. vboxfs) locate all files executable
701721
platform_is_not :windows do
702722
it "raises an Errno::EACCES when the file does not have execute permissions" do
703-
-> { Process.spawn __FILE__ }.should raise_error(Errno::EACCES)
723+
-> { Process.spawn __FILE__ }.should raise_error(Errno::EACCES, /Permission denied/)
724+
end
725+
726+
it "sets $? to exit status 127 when the file does not have execute permissions" do
727+
Process.spawn(__FILE__) rescue nil
728+
$?.exitstatus.should == 127
729+
end
730+
731+
it "raises an Errno::ENOENT when a non-executable file is found in PATH" do
732+
dir = tmp("spawn_path_non_executable_dir")
733+
mkdir_p dir
734+
begin
735+
exe = 'process-spawn-non-executable-in-path'
736+
File.write("#{dir}/#{exe}", "#!/bin/sh\necho hi")
737+
File.chmod(0644, "#{dir}/#{exe}")
738+
env = { "PATH" => "#{dir}#{File::PATH_SEPARATOR}#{ENV['PATH']}" }
739+
-> { Process.spawn(env, exe) }.should raise_error(Errno::ENOENT, /No such file or directory.*#{exe}/)
740+
$?.exitstatus.should == 127
741+
ensure
742+
rm_r dir
743+
end
704744
end
705745
end
706746

0 commit comments

Comments
 (0)