1require_relative '../../spec_helper' 2require_relative 'fixtures/classes' 3 4describe "Kernel#`" do 5 before :each do 6 @original_external = Encoding.default_external 7 end 8 9 after :each do 10 Encoding.default_external = @original_external 11 end 12 13 it "is a private method" do 14 Kernel.should have_private_instance_method(:`) 15 end 16 17 it "returns the standard output of the executed sub-process" do 18 ip = 'world' 19 `echo disc #{ip}`.should == "disc world\n" 20 end 21 22 it "lets the standard error stream pass through to the inherited stderr" do 23 cmd = ruby_cmd('STDERR.print "error stream"') 24 lambda { 25 `#{cmd}`.should == "" 26 }.should output_to_fd("error stream", STDERR) 27 end 28 29 it "produces a String in the default external encoding" do 30 Encoding.default_external = Encoding::SHIFT_JIS 31 `echo disc`.encoding.should equal(Encoding::SHIFT_JIS) 32 end 33 34 it "raises an Errno::ENOENT if the command is not executable" do 35 lambda { `nonexistent_command` }.should raise_error(Errno::ENOENT) 36 end 37 38 platform_is_not :windows do 39 it "sets $? to the exit status of the executed sub-process" do 40 ip = 'world' 41 `echo disc #{ip}` 42 $?.should be_kind_of(Process::Status) 43 $?.stopped?.should == false 44 $?.exited?.should == true 45 $?.exitstatus.should == 0 46 $?.success?.should == true 47 `echo disc #{ip}; exit 99` 48 $?.should be_kind_of(Process::Status) 49 $?.stopped?.should == false 50 $?.exited?.should == true 51 $?.exitstatus.should == 99 52 $?.success?.should == false 53 end 54 end 55 56 platform_is :windows do 57 it "sets $? to the exit status of the executed sub-process" do 58 ip = 'world' 59 `echo disc #{ip}` 60 $?.should be_kind_of(Process::Status) 61 $?.stopped?.should == false 62 $?.exited?.should == true 63 $?.exitstatus.should == 0 64 $?.success?.should == true 65 `echo disc #{ip}& exit 99` 66 $?.should be_kind_of(Process::Status) 67 $?.stopped?.should == false 68 $?.exited?.should == true 69 $?.exitstatus.should == 99 70 $?.success?.should == false 71 end 72 end 73end 74 75describe "Kernel.`" do 76 it "tries to convert the given argument to String using #to_str" do 77 (obj = mock('echo test')).should_receive(:to_str).and_return("echo test") 78 Kernel.`(obj).should == "test\n" #` fix vim syntax highlighting 79 end 80end 81