summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/popen_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/io/popen_spec.rb')
-rw-r--r--spec/ruby/core/io/popen_spec.rb101
1 files changed, 51 insertions, 50 deletions
diff --git a/spec/ruby/core/io/popen_spec.rb b/spec/ruby/core/io/popen_spec.rb
index 808dff7260..b5747bf255 100644
--- a/spec/ruby/core/io/popen_spec.rb
+++ b/spec/ruby/core/io/popen_spec.rb
@@ -1,42 +1,39 @@
-require File.expand_path('../../../spec_helper', __FILE__)
-require File.expand_path('../fixtures/classes', __FILE__)
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
+require_relative '../process/fixtures/common'
describe "IO.popen" do
+ ProcessSpecs.use_system_ruby(self)
+
before :each do
+ @fname = tmp("IO_popen_spec")
@io = nil
+ @var = "$FOO"
+ platform_is :windows do
+ @var = "%FOO%"
+ end
end
after :each do
- @io.close if @io
+ @io.close if @io and !@io.closed?
+ rm_r @fname
end
it "returns an open IO" do
@io = IO.popen(ruby_cmd('exit'), "r")
- @io.closed?.should be_false
+ @io.closed?.should == false
end
it "reads a read-only pipe" do
- @io = IO.popen(ruby_cmd('puts "foo"'), "r")
+ @io = IO.popen('echo foo', "r")
@io.read.should == "foo\n"
end
it "raises IOError when writing a read-only pipe" do
- @io = IO.popen(ruby_cmd('puts "foo"'), "r")
- lambda { @io.write('bar') }.should raise_error(IOError)
+ @io = IO.popen('echo foo', "r")
+ -> { @io.write('bar') }.should.raise(IOError)
@io.read.should == "foo\n"
end
-end
-
-describe "IO.popen" do
- before :each do
- @fname = tmp("IO_popen_spec")
- @io = nil
- end
-
- after :each do
- @io.close if @io and !@io.closed?
- rm_r @fname
- end
it "sees an infinitely looping subprocess exit when read pipe is closed" do
io = IO.popen ruby_cmd('r = loop{puts "y"; 0} rescue 1; exit r'), 'r'
@@ -55,7 +52,7 @@ describe "IO.popen" do
it "raises IOError when reading a write-only pipe" do
@io = IO.popen(ruby_cmd('IO.copy_stream(STDIN,STDOUT)'), "w")
- lambda { @io.read }.should raise_error(IOError)
+ -> { @io.read }.should.raise(IOError)
end
it "reads and writes a read/write pipe" do
@@ -80,16 +77,16 @@ describe "IO.popen" do
Process.kill "KILL", pid
@io.close
platform_is_not :windows do
- $?.signaled?.should == true
+ $?.should.signaled?
end
platform_is :windows do
- $?.exited?.should == true
+ $?.should.exited?
end
end
it "returns an instance of a subclass when called on a subclass" do
@io = IOSpecs::SubIO.popen(ruby_cmd('exit'), "r")
- @io.should be_an_instance_of(IOSpecs::SubIO)
+ @io.should.instance_of?(IOSpecs::SubIO)
end
it "coerces mode argument with #to_str" do
@@ -97,38 +94,44 @@ describe "IO.popen" do
mode.should_receive(:to_str).and_return("r")
@io = IO.popen(ruby_cmd('exit 0'), mode)
end
-end
-describe "IO.popen" do
- before :each do
- @io = nil
+ it "accepts a path using the chdir: keyword argument" do
+ path = File.dirname(@fname)
+
+ @io = IO.popen(ruby_cmd("puts Dir.pwd"), "r", chdir: path)
+ @io.read.chomp.should == path
end
- after :each do
- @io.close if @io
+ it "accepts a path using the chdir: keyword argument and a coercible path" do
+ path = File.dirname(@fname)
+ object = mock("path")
+ object.should_receive(:to_path).and_return(path)
+
+ @io = IO.popen(ruby_cmd("puts Dir.pwd"), "r", chdir: object)
+ @io.read.chomp.should == path
end
describe "with a block" do
it "yields an open IO to the block" do
IO.popen(ruby_cmd('exit'), "r") do |io|
- io.closed?.should be_false
+ io.closed?.should == false
end
end
it "yields an instance of a subclass when called on a subclass" do
IOSpecs::SubIO.popen(ruby_cmd('exit'), "r") do |io|
- io.should be_an_instance_of(IOSpecs::SubIO)
+ io.should.instance_of?(IOSpecs::SubIO)
end
end
it "closes the IO after yielding" do
io = IO.popen(ruby_cmd('exit'), "r") { |_io| _io }
- io.closed?.should be_true
+ io.closed?.should == true
end
it "allows the IO to be closed inside the block" do
io = IO.popen(ruby_cmd('exit'), 'r') { |_io| _io.close; _io }
- io.closed?.should be_true
+ io.closed?.should == true
end
it "returns the value of the block" do
@@ -136,7 +139,7 @@ describe "IO.popen" do
end
end
- with_feature :fork do
+ platform_is_not :windows do
it "starts returns a forked process if the command is -" do
io = IO.popen("-")
@@ -153,33 +156,31 @@ describe "IO.popen" do
end
end
- with_feature :encoding do
- it "has the given external encoding" do
- @io = IO.popen(ruby_cmd('exit'), external_encoding: Encoding::EUC_JP)
- @io.external_encoding.should == Encoding::EUC_JP
- end
+ it "has the given external encoding" do
+ @io = IO.popen(ruby_cmd('exit'), external_encoding: Encoding::EUC_JP)
+ @io.external_encoding.should == Encoding::EUC_JP
+ end
- it "has the given internal encoding" do
- @io = IO.popen(ruby_cmd('exit'), internal_encoding: Encoding::EUC_JP)
- @io.internal_encoding.should == Encoding::EUC_JP
- end
+ it "has the given internal encoding" do
+ @io = IO.popen(ruby_cmd('exit'), internal_encoding: Encoding::EUC_JP)
+ @io.internal_encoding.should == Encoding::EUC_JP
+ end
- it "sets the internal encoding to nil if it's the same as the external encoding" do
- @io = IO.popen(ruby_cmd('exit'), external_encoding: Encoding::EUC_JP,
- internal_encoding: Encoding::EUC_JP)
- @io.internal_encoding.should be_nil
- end
+ it "sets the internal encoding to nil if it's the same as the external encoding" do
+ @io = IO.popen(ruby_cmd('exit'), external_encoding: Encoding::EUC_JP,
+ internal_encoding: Encoding::EUC_JP)
+ @io.internal_encoding.should == nil
end
context "with a leading ENV Hash" do
it "accepts a single String command" do
- IO.popen({"FOO" => "bar"}, ruby_cmd('puts ENV["FOO"]')) do |io|
+ IO.popen({"FOO" => "bar"}, "echo #{@var}") do |io|
io.read.should == "bar\n"
end
end
it "accepts a single String command, and an IO mode" do
- IO.popen({"FOO" => "bar"}, ruby_cmd('puts ENV["FOO"]'), "r") do |io|
+ IO.popen({"FOO" => "bar"}, "echo #{@var}", "r") do |io|
io.read.should == "bar\n"
end
end