1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
# -*- encoding: utf-8 -*-
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/common', __FILE__)
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
end
with_feature :encoding do
before :each do
@external = Encoding.default_external
end
after :each do
Encoding.default_external = @external
end
end
it "converts a pathname to an absolute pathname" do
File.expand_path('').should == @base
File.expand_path('a').should == File.join(@base, 'a')
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
# See 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')
end
end
platform_is_not :windows do
it "keeps trailing dots on absolute pathname" do
# See Ruby-Talk:18512
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 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
# FIXME: do not use conditionals like this around #it blocks
unless not home = ENV['HOME']
platform_is_not :windows do
it "converts a pathname to an absolute pathname, using ~ (home) as base" do
File.expand_path('~').should == home
File.expand_path('~', '/tmp/gumby/ddd').should == home
File.expand_path('~/a', '/tmp/gumby/ddd').should == File.join(home, 'a')
end
it "does not return a frozen string" do
File.expand_path('~').frozen?.should == false
File.expand_path('~', '/tmp/gumby/ddd').frozen?.should == false
File.expand_path('~/a', '/tmp/gumby/ddd').frozen?.should == false
end
end
platform_is :windows do
it "converts a pathname to an absolute pathname, using ~ (home) as base" do
File.expand_path('~').should == home.tr("\\", '/')
File.expand_path('~', '/tmp/gumby/ddd').should == home.tr("\\", '/')
File.expand_path('~/a', '/tmp/gumby/ddd').should == File.join(home.tr("\\", '/'), 'a')
end
it "does not return a frozen string" do
File.expand_path('~').frozen?.should == false
File.expand_path('~', '/tmp/gumby/ddd').frozen?.should == false
File.expand_path('~/a', '/tmp/gumby/ddd').frozen?.should == false
end
end
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"
File.expand_path("../bin", "tmp/x").should == File.join(@base, 'tmp', 'bin')
File.expand_path("../bin", "x/../tmp").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 == ENV['HOME']
File.expand_path('~/..badfilename').should == "#{ENV['HOME']}/..badfilename"
File.expand_path('..').should == Dir.pwd.split('/')[0...-1].join("/")
File.expand_path('~/a','~/b').should == "#{ENV['HOME']}/a"
end
not_compliant_on :rubinius, :macruby do
it "does not replace multiple '/' at the beginning of the path" do
File.expand_path('////some/path').should == "////some/path"
end
end
deviates_on :rubinius, :macruby do
it "replaces multiple '/' with a single '/' at the beginning of the path" do
File.expand_path('////some/path').should == "/some/path"
end
end
it "replaces multiple '/' with a single '/'" do
File.expand_path('/some////path').should == "/some/path"
end
it "raises an ArgumentError if the path is not valid" do
lambda { File.expand_path("~a_not_existing_user") }.should raise_error(ArgumentError)
end
#it "expands ~ENV['USER'] to the user's home directory" do
# File.expand_path("~#{ENV['USER']}").should == ENV['HOME']
# File.expand_path("~#{ENV['USER']}/a").should == "#{ENV['HOME']}/a"
#end
it "does not expand ~ENV['USER'] when it's not at the start" do
File.expand_path("/~#{ENV['USER']}/a").should == "/~#{ENV['USER']}/a"
end
it "expands ../foo with ~/dir as base dir to /path/to/user/home/foo" do
File.expand_path('../foo', '~/dir').should == "#{ENV['HOME']}/foo"
end
end
ruby_version_is "1.9" do
it "accepts objects that have a #to_path method" do
File.expand_path(mock_to_path("a"), mock_to_path("#{@tmpdir}"))
end
end
it "raises a TypeError if not passed a String type" do
lambda { File.expand_path(1) }.should raise_error(TypeError)
lambda { File.expand_path(nil) }.should raise_error(TypeError)
lambda { File.expand_path(true) }.should raise_error(TypeError)
end
platform_is_not :windows do
it "expands /./dir to /dir" do
File.expand_path("/./dir").should == "/dir"
end
end
platform_is :windows do
it "expands C:/./dir to C:/dir" do
File.expand_path("C:/./dir").should == "C:/dir"
end
end
with_feature :encoding do
ruby_version_is ""..."2.0" do
it "produces a String in the default external encoding" do
Encoding.default_external = Encoding::SHIFT_JIS
File.expand_path("./a").encoding.should equal(Encoding::SHIFT_JIS)
end
end
ruby_version_is "2.0" do
it "returns a String in the same encoding as the argument" do
path = "./a".force_encoding Encoding::CP1251
File.expand_path(path).encoding.should equal(Encoding::CP1251)
end
end
it "expands a path with multi-byte characters" do
File.expand_path("Ångström").should == "#{@base}/Ångström"
end
it "raises an Encoding::CompatibilityError if the external encoding is not compatible" do
Encoding.default_external = Encoding::UTF_16BE
lambda { File.expand_path("./a") }.should raise_error(Encoding::CompatibilityError)
end
end
it "does not modify the string argument" do
str = "./a/b/../c"
File.expand_path(str, @base).should == "#{@base}/a/c"
str.should == "./a/b/../c"
end
it "does not modify a HOME string argument" do
str = "~/a"
File.expand_path(str).should == "#{home_directory.tr('\\', '/')}/a"
str.should == "~/a"
end
it "returns a String when passed a String subclass" do
str = FileSpecs::SubString.new "./a/b/../c"
path = File.expand_path(str, @base)
path.should == "#{@base}/a/c"
path.should be_an_instance_of(String)
end
end
platform_is_not :windows do
describe "File.expand_path when HOME is not set" do
before :each do
@home = ENV["HOME"]
end
after :each do
ENV["HOME"] = @home
end
it "raises an ArgumentError when passed '~' if HOME is nil" do
ENV.delete "HOME"
lambda { File.expand_path("~") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError when passed '~/' if HOME is nil" do
ENV.delete "HOME"
lambda { File.expand_path("~/") }.should raise_error(ArgumentError)
end
ruby_version_is ""..."1.8.7" do
it "returns '/' when passed '~' if HOME == ''" do
ENV["HOME"] = ""
File.expand_path("~").should == "/"
end
end
ruby_version_is "1.8.7" do
it "raises an ArgumentError when passed '~' if HOME == ''" do
ENV["HOME"] = ""
lambda { File.expand_path("~") }.should raise_error(ArgumentError)
end
end
end
end
|