Skip to content

Commit

Permalink
Reimplemented File.expand_path such that it does not use System.IO.Pa…
Browse files Browse the repository at this point in the history
…th. This allows us to get better compatibility with MRI.

The motivating reason was that RSpec does File.expand_path("filename:linenumber"), and the old implementation complained that : is not valid in filenames, whereas MRI allows such input
Fixed "[nil].uniq" - Cucumber was running into this.
Renamed scripts\ruby19.bat to ruby1.9.bat as "mspec -tr19" seems to have changed.
  • Loading branch information
Shri Borde committed Jun 3, 2009
1 parent 3734e67 commit ec9eae2
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 107 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
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
fails:File.expand_path expands /./dir to /dir
fails:File.expand_path converts a pathname to an absolute pathname, Ruby-Talk:18512
fails:File.expand_path returns file system case only for the last component
fails:File.expand_path returns file system case only for the last component of both arguments
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ def self.eql?(o) taint; o.taint; true; end
a[1].tainted?.should == true
end

it "handles array with nil" do
[nil, 1, nil].uniq.should == [nil, 1]
end

it "returns subclass instance on Array subclasses" do
ArraySpecs::MyArray[1, 2, 3].uniq.class.should == ArraySpecs::MyArray
end
Expand Down Expand Up @@ -115,6 +119,12 @@ def self.eql?(o) taint; o.taint; true; end
[ "a", "b", "c" ].uniq!.should == nil
end

it "handles array with nil" do
a = [nil, 1, nil]
a.uniq!
a.should == [nil, 1]
end

ruby_version_is "" ... "1.9" do
it "raises a TypeError on a frozen array if modification would take place" do
dup_ary = [1, 1, 2]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@

describe "File.expand_path" do
before :each do
platform_is :windows do
@base = `cd`.chomp.tr '\\', '/'
@tmpdir = "c:/tmp"
@rootdir = "c:/"
end

platform_is_not :windows do
@base = Dir.pwd
@tmpdir = "/tmp"
@rootdir = "/"
end
@base = Dir.pwd
platform_is(:windows) { @rootdir = "c:/" }
platform_is_not(:windows) { @rootdir = "/" }
@tmpdir = @rootdir + "tmp"
end

it "converts a pathname to an absolute pathname" do
Expand All @@ -28,21 +21,23 @@
File.expand_path('a../b').should == File.join(@base, 'a../b')
end

it "converts a pathname with . in filename to an absolute pathname, Ruby-Talk:18512 " do
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

platform_is_not :windows do
it "converts a pathname with trailing . to an absolute pathname, Ruby-Talk:18512 " 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

it "converts a pathname to an absolute pathname, using a complete path" do
File.expand_path("", "#{@tmpdir}").should == "#{@tmpdir}"
File.expand_path("a", "#{@tmpdir}").should =="#{@tmpdir}/a"
File.expand_path("../a", "#{@tmpdir}/xxx").should == "#{@tmpdir}/a"
File.expand_path(".", "#{@rootdir}").should == "#{@rootdir}"
platform_is :windows do
it "converts a pathname with trailing . to an absolute pathname, Ruby-Talk:18512 " do
File.expand_path('a.').should == File.join(@base, 'a')
File.expand_path('a..').should == File.join(@base, 'a')
end
end

it "converts a pathname to an absolute pathname, using ~ (home) as base" do
Expand Down Expand Up @@ -86,10 +81,10 @@

platform_is_not :windows do
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')
File.expand_path("/tmp/x/../../bin").should == @rootdir + "bin"
File.expand_path("/tmp/../../bin").should == @rootdir + "bin"
File.expand_path("/../../bin").should == @rootdir + "bin"
File.expand_path("tmp/x/../../bin").should == File.join(@base, 'bin')
end

it "raises an ArgumentError if the path is not valid" do
Expand All @@ -103,7 +98,7 @@
end

platform_is :windows do
it "sometimes returns file system case with one argument" do
it "returns file system case only for the last component" 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"
Expand All @@ -118,7 +113,7 @@
File.expand_path("/./wInDoWs/../WiNdOwS/nOtEpAd.exe").should == "c:/WiNdOwS/notepad.exe"
end

it "sometimes returns file system case with two arguments" do
it "returns file system case only for the last component of both arguments" do
File.expand_path("wInDoWs", "/").should == "c:/Windows"

File.expand_path("nOtEpAd.exe", "/wInDoWs").should == "c:/Windows/notepad.exe"
Expand All @@ -131,6 +126,34 @@

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

it "allows back slashes" do
File.expand_path('\foo\bar').should == @rootdir + "foo/bar"
end

it "supports drive letter for relative path" do
File.expand_path("c:foo").should == File.expand_path("foo")
File.expand_path("x:foo").should == "x:/foo"
end

it "supports different drive letters" do
File.expand_path("x:/foo").should == "x:/foo"
end
end

it "leaves alone characters like : (line number separator in backtraces) which are invalid on some platforms" do
File.expand_path("foo.ext:123").should == @base + "/foo.ext:123"
File.expand_path("foo:xxx").should == @base + "/foo:xxx"
File.expand_path("/dir1:xxx/dir2:xxx/../foo:xxx").should == @rootdir + "dir1:xxx/foo:xxx"
File.expand_path("/foo/...").should == @rootdir + "foo/..."
end

it "expands /./dir to /dir" do
File.expand_path("/./dir").should == @rootdir + "dir"
end

it "allows extra .." do
File.expand_path(@rootdir + "/../../foo").should == @rootdir + "foo"
end

it "raises an ArgumentError is not passed one or two arguments" do
Expand All @@ -143,8 +166,32 @@
lambda { File.expand_path(nil) }.should raise_error(TypeError)
lambda { File.expand_path(true) }.should raise_error(TypeError)
end
end

it "expands /./dir to /dir" do
File.expand_path("/./dir").should == "/dir"
describe "File.expand_path(file_name, dir_string)" do
before :each do
@base = Dir.pwd
platform_is(:windows) { @rootdir = "c:/" }
platform_is_not(:windows) { @rootdir = "/" }
@tmpdir = @rootdir + "tmp"
end
end

it "converts a pathname to an absolute pathname with dir_string" do
File.expand_path('', '').should == @base
File.expand_path('', 'a').should == @base + '/a'
File.expand_path('b', 'a').should == @base + '/a/b'
File.expand_path('b', '/a').should == @rootdir + 'a/b'
end

it "converts a pathname to an absolute pathname, using a complete path" do
File.expand_path("", "#{@tmpdir}").should == "#{@tmpdir}"
File.expand_path("a", "#{@tmpdir}").should =="#{@tmpdir}/a"
File.expand_path("../a", "#{@tmpdir}/xxx").should == "#{@tmpdir}/a"
File.expand_path(".", "#{@rootdir}").should == "#{@rootdir}"
end

it "ignores dir_string if file_name is an absolute path" do
File.expand_path('/foo', '/bar').should == @rootdir + "foo"
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public RubyDir([NotNull]MutableString/*!*/ dirname) {
} catch (Exception ex) {
throw ToRubyException(ex, strName, DirectoryOperation.Open);
}
_dirName = MutableString.Create(NormalizePathSeparators(strName));
_dirName = MutableString.Create(RubyUtils.CanonicalizePath(strName));
_closed = false;
_pos = -2;
}
Expand Down Expand Up @@ -169,7 +169,7 @@ public static object ForEach(BlockParam block, object self, [NotNull]MutableStri
[RubyMethod("getwd", RubyMethodAttributes.PublicSingleton)]
[RubyMethod("pwd", RubyMethodAttributes.PublicSingleton)]
public static MutableString/*!*/ GetCurrentDirectory(object self) {
return MutableString.Create(NormalizePathSeparators(Directory.GetCurrentDirectory()));
return MutableString.Create(RubyUtils.CanonicalizePath(Directory.GetCurrentDirectory()));
}

#region glob
Expand Down Expand Up @@ -371,10 +371,6 @@ private static Exception ToRubyException(Exception/*!*/ ex, string path, Directo
return RubyExceptions.CreateSystemCallError(String.Format("unknown scenario - {0}, {1}, {2}", exceptionType, path, op));
}

private static string/*!*/ NormalizePathSeparators(string/*!*/ path) {
return path.Replace("\\", "/");
}

#endregion
}
}
Loading

0 comments on commit ec9eae2

Please sign in to comment.