Skip to content

Commit

Permalink
Fixes File.expand_path to workaround a problem "igem i rails" was run…
Browse files Browse the repository at this point in the history
…ning into.

Fixes File apis to throw correct exception type
Implements Kernel.abort
Adds Errno::ECONNREFUSED
Exception#message should call Exception#to_s
Fixes paths in rbconfig.rb to work in dev environment
  • Loading branch information
Shri Borde committed Apr 14, 2009
1 parent 5e8c6b2 commit 24ce7ba
Show file tree
Hide file tree
Showing 20 changed files with 341 additions and 158 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Merlin/External/Languages/Ruby/ruby-1.8.6
Merlin/Main/bin
Merlin/Main/Bin
Merlin/Main/External
Merlin/External.LCA_RESTRICTED/Languages/Ruby/redist-libs/ironruby
*.csproj.vspscc
*.csproj.user
*.vssscc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fails:File.expand_path converts a pathname to an absolute pathname, Ruby-Talk:18512
fails:File.expand_path expand_path for commoms unix path give a full path
fails:File.expand_path sometimes returns file system case with one argument
fails:File.expand_path sometimes returns file system case with two arguments
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
fails:String#split with Regexp treats negative limits as no limit
fails:String#split with Regexp splits between characters when regexp matches a zero-length string
fails:String#split with Regexp includes all captures in the result array
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
require File.dirname(__FILE__) + '/../../spec_helper'

describe "Exception#message" do
it "returns the exception message" do
[Exception.new.message, Exception.new("Ouch!").message].should == ["Exception", "Ouch!"]
end
before :each do
@e = Exception.new("Ouch!")
end

it "calls to_s" do
@e.should_receive(:to_s).and_return("to_s response")
@e.message.should == "to_s response"
end
end
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
require File.dirname(__FILE__) + '/../../spec_helper'

describe "Exception#to_s" do
before :each do
@e = Exception.new("Ouch!")
end

it "returns the exception message" do
@e.to_s.should == "Ouch!"
end

it "is the class name by default" do
Exception.new.to_s.should == "Exception"
end

it "can return a non-String" do
m = mock("message")
Exception.new(m).to_s.should equal(m)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
File.expand_path('a', nil).should == File.join(@base, 'a')
end

not_compliant_on :ironruby do
it "converts a pathname to an absolute pathname, Ruby-Talk:18512 " do
# Because of Ruby-Talk:18512
it "converts a pathname to an absolute pathname, Ruby-Talk:18512 " do
# Because of Ruby-Talk:18512
File.expand_path('.a').should == File.join(@base, '.a')
File.expand_path('..a').should == File.join(@base, '..a')
File.expand_path('a../b').should == File.join(@base, 'a../b')
platform_is_not :windows do
File.expand_path('a.').should == File.join(@base, 'a.')
File.expand_path('.a').should == File.join(@base, '.a')
File.expand_path('a..').should == File.join(@base, 'a..')
File.expand_path('..a').should == File.join(@base, '..a')
File.expand_path('a../b').should == File.join(@base, 'a../b')
end
end

Expand Down Expand Up @@ -66,26 +66,26 @@
end
end

# FIXME: these are insane!
it "expand_path for commoms unix path give a full path" do
File.expand_path('/tmp/').should == @rootdir + 'tmp'
File.expand_path('/tmp/../../../tmp').should == @rootdir + 'tmp'
File.expand_path('').should == Dir.pwd
File.expand_path('./////').should == Dir.pwd
File.expand_path('.').should == Dir.pwd
File.expand_path(Dir.pwd).should == Dir.pwd
File.expand_path('..').should == Dir.pwd.split('/')[0...-1].join("/")
File.expand_path('//').should == '//'
end

platform_is_not :windows do
# FIXME: these are insane!
it "expand path with " do
File.expand_path("../../bin", "/tmp/x").should == "/bin"
File.expand_path("../../bin", "/tmp").should == "/bin"
File.expand_path("../../bin", "/").should == "/bin"
it "expand path with .." do
File.expand_path("../../bin", "/tmp/x").should == @rootdir + "bin"
File.expand_path("../../bin", "/tmp").should == @rootdir + "bin"
File.expand_path("../../bin", "/").should == @rootdir + "bin"
File.expand_path("../../bin", "tmp/x").should == File.join(@base, 'bin')
end

it "expand_path for commoms unix path give a full path" do
File.expand_path('/tmp/').should =='/tmp'
File.expand_path('/tmp/../../../tmp').should == '/tmp'
File.expand_path('').should == Dir.pwd
File.expand_path('./////').should == Dir.pwd
File.expand_path('.').should == Dir.pwd
File.expand_path(Dir.pwd).should == Dir.pwd
File.expand_path('..').should == Dir.pwd.split('/')[0...-1].join("/")
File.expand_path('//').should == '//'
end

it "raises an ArgumentError if the path is not valid" do
lambda { File.expand_path("~a_fake_file") }.should raise_error(ArgumentError)
end
Expand All @@ -96,6 +96,37 @@
end
end

platform_is :windows do
it "sometimes returns file system case with one argument" do
File.expand_path("/wInDoWs").should == "c:/Windows"
File.expand_path("/nOn-ExIsTeNt").should == "c:/nOn-ExIsTeNt"
File.expand_path("/wInDoWs/nOtEpAd.exe").should == "c:/wInDoWs/notepad.exe"
File.expand_path("/wInDoWs/sYsTeM32").should == "c:/wInDoWs/System32"
File.expand_path("/wInDoWs/nOn-ExIsTeNt").should == "c:/wInDoWs/nOn-ExIsTeNt"

File.expand_path("/./wInDoWs").should == "c:/Windows"
File.expand_path("/./wInDoWs/nOtEpAd.exe").should == "c:/wInDoWs/notepad.exe"
File.expand_path("/./wInDoWs/sYsTeM32").should == "c:/wInDoWs/System32"
File.expand_path("/./wInDoWs/nOn-ExIsTeNt").should == "c:/wInDoWs/nOn-ExIsTeNt"

File.expand_path("/./wInDoWs/../WiNdOwS/nOtEpAd.exe").should == "c:/WiNdOwS/notepad.exe"
end

it "sometimes returns file system case with two arguments" do
File.expand_path("wInDoWs", "/").should == "c:/Windows"

File.expand_path("nOtEpAd.exe", "/wInDoWs").should == "c:/Windows/notepad.exe"
File.expand_path("sYsTeM32", "/wInDoWs").should == "c:/Windows/System32"
File.expand_path("nOn-ExIsTeNt", "/wInDoWs").should == "c:/Windows/nOn-ExIsTeNt"

File.expand_path("wInDoWs/nOtEpAd.exe", "/").should == "c:/wInDoWs/notepad.exe"
File.expand_path("wInDoWs/sYsTeM32", "/").should == "c:/wInDoWs/System32"
File.expand_path("wInDoWs/nOn-ExIsTeNt", "/").should == "c:/wInDoWs/nOn-ExIsTeNt"

File.expand_path("foo", "/NoN-eXiStEnT").should == "c:/NoN-eXiStEnT/foo"
end
end

it "raises an ArgumentError is not passed one or two arguments" do
lambda { File.expand_path }.should raise_error(ArgumentError)
lambda { File.expand_path '../', 'tmp', 'foo' }.should raise_error(ArgumentError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def close
class << f
alias_method(:close_orig, :close)
def close
close_orig
raise IOError
end
end
Expand Down Expand Up @@ -508,6 +509,10 @@ def close
lambda { File.open(false) }.should raise_error(TypeError)
lambda { File.open(nil) }.should raise_error(TypeError)
end

it "raises Errno::EINVAL if filename is empty" do
lambda { File.open("") }.should raise_error(Errno::EINVAL)
end

it "raises a SystemCallError if passed an invalid Integer type" do
lambda { File.open(-1) }.should raise_error(SystemCallError)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "fileutils"
require File.dirname(__FILE__) + '/../../spec_helper'

describe "File.rename" do
Expand All @@ -10,8 +9,8 @@

File.delete(@old) if File.exist?(@old)
File.delete(@new) if File.exist?(@new)
FileUtils.remove_dir(@dir) if File.exist?(@dir)
FileUtils.remove_dir(@new_dir) if File.exist?(@new_dir)
FileUtils.rm_rf(@dir) if File.exist?(@dir)
FileUtils.rm_rf(@new_dir) if File.exist?(@new_dir)

File.open(@old,"w+") {|f| f.puts "hello" }
FileUtils.mkdir(@dir)
Expand All @@ -20,8 +19,8 @@
after :each do
File.delete(@old) if File.exist?(@old)
File.delete(@new) if File.exist?(@new)
FileUtils.remove_dir(@dir) if File.exist?(@dir)
FileUtils.remove_dir(@new_dir) if File.exist?(@new_dir)
FileUtils.rm_rf(@dir) if File.exist?(@dir)
FileUtils.rm_rf(@new_dir) if File.exist?(@new_dir)
end

it "renames a file " do
Expand All @@ -39,7 +38,6 @@
it "raises an Errno::ENOENT if the source does not exist" do
File.delete(@old)
lambda { File.rename(@old, @new) }.should raise_error(Errno::ENOENT)
lambda { File.rename("non-existent", "non-existent") }.should raise_error(Errno::ENOENT)
end

it "overwrites destination if the destination already exists" do
Expand Down Expand Up @@ -94,6 +92,11 @@
File.exists?(@old).should == true
end

it "raises Errno::ENOENT if filename is empty" do
lambda { File.rename(@old, "") }.should raise_error(Errno::ENOENT)
lambda { File.rename("", @new) }.should raise_error(Errno::ENOENT)
end

it "raises an ArgumentError if not passed two arguments" do
lambda { File.rename }.should raise_error(ArgumentError)
lambda { File.rename(@file) }.should raise_error(ArgumentError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
lambda { File.send(@method, 'bogus') }.should raise_error(Errno::ENOENT)
end

it "raises Errno::ENOENT if filename is empty" do
lambda { File.send(@method, "") }.should raise_error(Errno::ENOENT)
end

it "coerces a given parameter into a string if possible" do
class Coercable
def to_str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
it "is a private method" do
Kernel.private_instance_methods.should include("abort")
end

it "prints the message to stderr" do
ruby_exe("abort('abort message')").chomp.should == ''
ruby_exe("abort('abort message')", :args => "2>&1").chomp.should == 'abort message'
end

it "does not allow the message to be nil or String-like object" do
lambda { abort(nil) }.should raise_error(TypeError)

m = mock('message')
m.should_not_receive(:to_str)
lambda { abort(m) }.should raise_error(TypeError)
end

it "sets the exit code to 1" do
ruby_exe("abort")
$?.exitstatus.should == 1
end
end

describe "Kernel.abort" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
it "accepts String-like objects for path" do
file = mock('filename')
file.should_receive(:to_str).and_return(@file)
open(file) { :in_block }.should == :in_block
open(file) { }
end

it "allows nil for mode" do
Expand All @@ -98,7 +98,7 @@
it "allows String-like objects for mode" do
mode = mock('mode')
mode.should_receive(:to_str).and_return("r")
open(@file, mode) { :in_block }.should == :in_block
open(@file, mode) { }
end

it "allows nil for perm" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"1,2,,3,4,,".split(',', -5).should == ["1", "2", "", "3", "4", "", ""]
" a b c\nd ".split(" ", -1).should == ["", "a", "b", "c\nd", ""]
",".split(",", -1).should == ["", ""]
"".split("x", -1).should == []
end

it "defaults to $; when string isn't given or nil" do
Expand Down Expand Up @@ -202,6 +203,7 @@ def obj.to_int() 2 end
"1,2,,3,4,,".split(/,/, -5).should == ["1", "2", "", "3", "4", "", ""]
" a b c\nd ".split(/\s+/, -1).should == ["", "a", "b", "c", "d", ""]
",".split(/,/, -1).should == ["", ""]
"".split(/x/, -1).should == []
end

it "defaults to $; when regexp isn't given or nil" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ namespace IronRuby.Builtins {
public static class Errno {
// Errno.constants.sort

internal static FileNotFoundException/*!*/ CreateENOENT() {
return new FileNotFoundException();
}

internal static FileNotFoundException/*!*/ CreateENOENT(string message, Exception inner) {
return new FileNotFoundException(message, inner);
}
Expand Down Expand Up @@ -183,6 +187,21 @@ protected NotConnectedError(System.Runtime.Serialization.SerializationInfo info,
#endif
}

[RubyClass("ECONNREFUSED"), Serializable]
public class ConnectionRefusedError : ExternalException {
private const string/*!*/ M = "No connection could be made because the target machine actively refused it.";

public ConnectionRefusedError() : this(null, null) { }
public ConnectionRefusedError(string message) : this(message, null) { }
public ConnectionRefusedError(string message, Exception inner) : base(MakeMessage(message, M), inner) { }
public ConnectionRefusedError(MutableString message) : base(MakeMessage(ref message, M)) { RubyExceptionData.InitializeException(this, message); }

#if !SILVERLIGHT
protected ConnectionRefusedError(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
#endif
}

[RubyClass("ECONNRESET"), Serializable]
public class ConnectionResetError : ExternalException {
private const string/*!*/ M = "An existing connection was forcibly closed by the remote host.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,15 @@ public static RubyArray SetBacktrace(Exception/*!*/ self, RubyArray backtrace) {
}

[RubyMethod("message")]
public static object GetMessage(Exception/*!*/ self) {
return RubyExceptionData.GetInstance(self).Message;
public static object GetMessage(UnaryOpStorage/*!*/ stringReprStorage, Exception/*!*/ self) {
var site = stringReprStorage.GetCallSite("to_s");
return site.Target(site, self);
}

[RubyMethod("to_s")]
[RubyMethod("to_str")]
public static MutableString/*!*/ GetMessage(ConversionStorage<MutableString>/*!*/ tosStorage, Exception/*!*/ self) {
return Protocols.ConvertToString(tosStorage, GetMessage(self));
public static object StringRepresentation(Exception/*!*/ self) {
return RubyExceptionData.GetInstance(self).Message;
}

[RubyMethod("inspect", RubyMethodAttributes.PublicInstance)]
Expand Down
Loading

0 comments on commit 24ce7ba

Please sign in to comment.