Skip to content

Commit a38bf16

Browse files
flavorjoneseregon
authored andcommitted
Deprecate Kernel#open and IO support for subprocess creation/forking
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]
1 parent 2806fbb commit a38bf16

File tree

6 files changed

+114
-17
lines changed

6 files changed

+114
-17
lines changed

core/io/binread_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@
4444
it "raises an Errno::EINVAL when not passed a valid offset" do
4545
-> { IO.binread @fname, 0, -1 }.should raise_error(Errno::EINVAL)
4646
end
47+
48+
ruby_version_is "3.3" do
49+
# https://bugs.ruby-lang.org/issues/19630
50+
it "warns about deprecation given a path with a pipe" do
51+
cmd = "|echo ok"
52+
-> {
53+
IO.binread(cmd)
54+
}.should complain(/IO process creation with a leading '\|'/)
55+
end
56+
end
4757
end

core/io/foreach_spec.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@
2020
platform_is :windows do
2121
cmd = "|cmd.exe /C echo hello&echo line2"
2222
end
23-
IO.foreach(cmd) { |l| ScratchPad << l }
23+
24+
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
25+
IO.foreach(cmd) { |l| ScratchPad << l }
26+
end
2427
ScratchPad.recorded.should == ["hello\n", "line2\n"]
2528
end
2629

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

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

3338
if $$ == parent_pid
3439
ScratchPad.recorded.should == ["hello\n", "from a fork\n"]
@@ -39,6 +44,16 @@
3944
end
4045
end
4146
end
47+
48+
ruby_version_is "3.3" do
49+
# https://bugs.ruby-lang.org/issues/19630
50+
it "warns about deprecation given a path with a pipe" do
51+
cmd = "|echo ok"
52+
-> {
53+
IO.foreach(cmd).to_a
54+
}.should complain(/IO process creation with a leading '\|'/)
55+
end
56+
end
4257
end
4358
end
4459

core/io/read_spec.rb

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,19 @@
165165
platform_is :windows do
166166
cmd = "|cmd.exe /C echo hello"
167167
end
168-
IO.read(cmd).should == "hello\n"
168+
169+
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
170+
IO.read(cmd).should == "hello\n"
171+
end
169172
end
170173

171174
platform_is_not :windows do
172175
it "opens a pipe to a fork if the rest is -" do
173-
str = IO.read("|-")
176+
str = nil
177+
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
178+
str = IO.read("|-")
179+
end
180+
174181
if str # parent
175182
str.should == "hello from child\n"
176183
else #child
@@ -185,13 +192,18 @@
185192
platform_is :windows do
186193
cmd = "|cmd.exe /C echo hello"
187194
end
188-
IO.read(cmd, 1).should == "h"
195+
196+
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
197+
IO.read(cmd, 1).should == "h"
198+
end
189199
end
190200

191201
platform_is_not :windows do
192202
it "raises Errno::ESPIPE if passed an offset" do
193203
-> {
194-
IO.read("|sh -c 'echo hello'", 1, 1)
204+
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
205+
IO.read("|sh -c 'echo hello'", 1, 1)
206+
end
195207
}.should raise_error(Errno::ESPIPE)
196208
end
197209
end
@@ -202,11 +214,23 @@
202214
# once https://bugs.ruby-lang.org/issues/12230 is fixed.
203215
it "raises Errno::EINVAL if passed an offset" do
204216
-> {
205-
IO.read("|cmd.exe /C echo hello", 1, 1)
217+
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
218+
IO.read("|cmd.exe /C echo hello", 1, 1)
219+
end
206220
}.should raise_error(Errno::EINVAL)
207221
end
208222
end
209223
end
224+
225+
ruby_version_is "3.3" do
226+
# https://bugs.ruby-lang.org/issues/19630
227+
it "warns about deprecation given a path with a pipe" do
228+
cmd = "|echo ok"
229+
-> {
230+
IO.read(cmd)
231+
}.should complain(/IO process creation with a leading '\|'/)
232+
end
233+
end
210234
end
211235

212236
describe "IO.read on an empty file" do

core/io/readlines_spec.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,20 @@
180180
platform_is :windows do
181181
cmd = "|cmd.exe /C echo hello&echo line2"
182182
end
183-
lines = IO.readlines(cmd)
183+
184+
lines = nil
185+
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
186+
lines = IO.readlines(cmd)
187+
end
184188
lines.should == ["hello\n", "line2\n"]
185189
end
186190

187191
platform_is_not :windows do
188192
it "gets data from a fork when passed -" do
189-
lines = IO.readlines("|-")
193+
lines = nil
194+
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
195+
lines = IO.readlines("|-")
196+
end
190197

191198
if lines # parent
192199
lines.should == ["hello\n", "from a fork\n"]
@@ -199,6 +206,16 @@
199206
end
200207
end
201208

209+
ruby_version_is "3.3" do
210+
# https://bugs.ruby-lang.org/issues/19630
211+
it "warns about deprecation given a path with a pipe" do
212+
cmd = "|echo ok"
213+
-> {
214+
IO.readlines(cmd)
215+
}.should complain(/IO process creation with a leading '\|'/)
216+
end
217+
end
218+
202219
it_behaves_like :io_readlines, :readlines
203220
it_behaves_like :io_readlines_options_19, :readlines
204221
end

core/io/write_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@
215215
end
216216
end
217217
end
218+
219+
ruby_version_is "3.3" do
220+
# https://bugs.ruby-lang.org/issues/19630
221+
it "warns about deprecation given a path with a pipe" do
222+
-> {
223+
-> {
224+
IO.write("|cat", "xxx")
225+
}.should output_to_fd("xxx")
226+
}.should complain(/IO process creation with a leading '\|'/)
227+
end
228+
end
218229
end
219230
end
220231

core/kernel/open_spec.rb

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929

3030
platform_is_not :windows, :wasi do
3131
it "opens an io when path starts with a pipe" do
32-
@io = open("|date")
32+
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
33+
@io = open("|date")
34+
end
3335
begin
3436
@io.should be_kind_of(IO)
3537
@io.read
@@ -39,21 +41,27 @@
3941
end
4042

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

4650
it "opens an io for writing" do
47-
-> do
48-
bytes = open("|cat", "w") { |io| io.write(".") }
49-
bytes.should == 1
50-
end.should output_to_fd(".")
51+
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
52+
-> {
53+
bytes = open("|cat", "w") { |io| io.write(".") }
54+
bytes.should == 1
55+
}.should output_to_fd(".")
56+
end
5157
end
5258
end
5359

5460
platform_is :windows do
5561
it "opens an io when path starts with a pipe" do
56-
@io = open("|date /t")
62+
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
63+
@io = open("|date /t")
64+
end
5765
begin
5866
@io.should be_kind_of(IO)
5967
@io.read
@@ -63,11 +71,23 @@
6371
end
6472

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

81+
ruby_version_is "3.3" do
82+
# https://bugs.ruby-lang.org/issues/19630
83+
it "warns about deprecation given a path with a pipe" do
84+
cmd = "|echo ok"
85+
-> {
86+
open(cmd) { |f| f.read }
87+
}.should complain(/Kernel#open with a leading '\|'/)
88+
end
89+
end
90+
7191
it "raises an ArgumentError if not passed one argument" do
7292
-> { open }.should raise_error(ArgumentError)
7393
end

0 commit comments

Comments
 (0)