Skip to content

Commit

Permalink
Deprecate Kernel#open and IO support for subprocess creation/forking
Browse files Browse the repository at this point in the history
Deprecate Kernel#open and IO support for subprocess creation and
forking. This deprecates subprocess creation and forking in

- Kernel#open
- URI.open
- IO.binread
- IO.foreach
- IO.readlines
- IO.read
- IO.write

This behavior is slated to be removed in Ruby 4.0

[Feature #19630]
  • Loading branch information
flavorjones authored and eregon committed Sep 4, 2023
1 parent 2806fbb commit a38bf16
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 17 deletions.
10 changes: 10 additions & 0 deletions core/io/binread_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@
it "raises an Errno::EINVAL when not passed a valid offset" do
-> { IO.binread @fname, 0, -1 }.should raise_error(Errno::EINVAL)
end

ruby_version_is "3.3" do
# https://bugs.ruby-lang.org/issues/19630
it "warns about deprecation given a path with a pipe" do
cmd = "|echo ok"
-> {
IO.binread(cmd)
}.should complain(/IO process creation with a leading '\|'/)
end
end
end
19 changes: 17 additions & 2 deletions core/io/foreach_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@
platform_is :windows do
cmd = "|cmd.exe /C echo hello&echo line2"
end
IO.foreach(cmd) { |l| ScratchPad << l }

suppress_warning do # https://bugs.ruby-lang.org/issues/19630
IO.foreach(cmd) { |l| ScratchPad << l }
end
ScratchPad.recorded.should == ["hello\n", "line2\n"]
end

platform_is_not :windows do
it "gets data from a fork when passed -" do
parent_pid = $$

IO.foreach("|-") { |l| ScratchPad << l }
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
IO.foreach("|-") { |l| ScratchPad << l }
end

if $$ == parent_pid
ScratchPad.recorded.should == ["hello\n", "from a fork\n"]
Expand All @@ -39,6 +44,16 @@
end
end
end

ruby_version_is "3.3" do
# https://bugs.ruby-lang.org/issues/19630
it "warns about deprecation given a path with a pipe" do
cmd = "|echo ok"
-> {
IO.foreach(cmd).to_a
}.should complain(/IO process creation with a leading '\|'/)
end
end
end
end

Expand Down
34 changes: 29 additions & 5 deletions core/io/read_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,19 @@
platform_is :windows do
cmd = "|cmd.exe /C echo hello"
end
IO.read(cmd).should == "hello\n"

suppress_warning do # https://bugs.ruby-lang.org/issues/19630
IO.read(cmd).should == "hello\n"
end
end

platform_is_not :windows do
it "opens a pipe to a fork if the rest is -" do
str = IO.read("|-")
str = nil
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
str = IO.read("|-")
end

if str # parent
str.should == "hello from child\n"
else #child
Expand All @@ -185,13 +192,18 @@
platform_is :windows do
cmd = "|cmd.exe /C echo hello"
end
IO.read(cmd, 1).should == "h"

suppress_warning do # https://bugs.ruby-lang.org/issues/19630
IO.read(cmd, 1).should == "h"
end
end

platform_is_not :windows do
it "raises Errno::ESPIPE if passed an offset" do
-> {
IO.read("|sh -c 'echo hello'", 1, 1)
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
IO.read("|sh -c 'echo hello'", 1, 1)
end
}.should raise_error(Errno::ESPIPE)
end
end
Expand All @@ -202,11 +214,23 @@
# once https://bugs.ruby-lang.org/issues/12230 is fixed.
it "raises Errno::EINVAL if passed an offset" do
-> {
IO.read("|cmd.exe /C echo hello", 1, 1)
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
IO.read("|cmd.exe /C echo hello", 1, 1)
end
}.should raise_error(Errno::EINVAL)
end
end
end

ruby_version_is "3.3" do
# https://bugs.ruby-lang.org/issues/19630
it "warns about deprecation given a path with a pipe" do
cmd = "|echo ok"
-> {
IO.read(cmd)
}.should complain(/IO process creation with a leading '\|'/)
end
end
end

describe "IO.read on an empty file" do
Expand Down
21 changes: 19 additions & 2 deletions core/io/readlines_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,20 @@
platform_is :windows do
cmd = "|cmd.exe /C echo hello&echo line2"
end
lines = IO.readlines(cmd)

lines = nil
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
lines = IO.readlines(cmd)
end
lines.should == ["hello\n", "line2\n"]
end

platform_is_not :windows do
it "gets data from a fork when passed -" do
lines = IO.readlines("|-")
lines = nil
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
lines = IO.readlines("|-")
end

if lines # parent
lines.should == ["hello\n", "from a fork\n"]
Expand All @@ -199,6 +206,16 @@
end
end

ruby_version_is "3.3" do
# https://bugs.ruby-lang.org/issues/19630
it "warns about deprecation given a path with a pipe" do
cmd = "|echo ok"
-> {
IO.readlines(cmd)
}.should complain(/IO process creation with a leading '\|'/)
end
end

it_behaves_like :io_readlines, :readlines
it_behaves_like :io_readlines_options_19, :readlines
end
Expand Down
11 changes: 11 additions & 0 deletions core/io/write_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@
end
end
end

ruby_version_is "3.3" do
# https://bugs.ruby-lang.org/issues/19630
it "warns about deprecation given a path with a pipe" do
-> {
-> {
IO.write("|cat", "xxx")
}.should output_to_fd("xxx")
}.should complain(/IO process creation with a leading '\|'/)
end
end
end
end

Expand Down
36 changes: 28 additions & 8 deletions core/kernel/open_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

platform_is_not :windows, :wasi do
it "opens an io when path starts with a pipe" do
@io = open("|date")
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
@io = open("|date")
end
begin
@io.should be_kind_of(IO)
@io.read
Expand All @@ -39,21 +41,27 @@
end

it "opens an io when called with a block" do
@output = open("|date") { |f| f.read }
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
@output = open("|date") { |f| f.read }
end
@output.should_not == ''
end

it "opens an io for writing" do
-> do
bytes = open("|cat", "w") { |io| io.write(".") }
bytes.should == 1
end.should output_to_fd(".")
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
-> {
bytes = open("|cat", "w") { |io| io.write(".") }
bytes.should == 1
}.should output_to_fd(".")
end
end
end

platform_is :windows do
it "opens an io when path starts with a pipe" do
@io = open("|date /t")
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
@io = open("|date /t")
end
begin
@io.should be_kind_of(IO)
@io.read
Expand All @@ -63,11 +71,23 @@
end

it "opens an io when called with a block" do
@output = open("|date /t") { |f| f.read }
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
@output = open("|date /t") { |f| f.read }
end
@output.should_not == ''
end
end

ruby_version_is "3.3" do
# https://bugs.ruby-lang.org/issues/19630
it "warns about deprecation given a path with a pipe" do
cmd = "|echo ok"
-> {
open(cmd) { |f| f.read }
}.should complain(/Kernel#open with a leading '\|'/)
end
end

it "raises an ArgumentError if not passed one argument" do
-> { open }.should raise_error(ArgumentError)
end
Expand Down

0 comments on commit a38bf16

Please sign in to comment.