diff options
author | David RodrÃguez <deivid.rodriguez@riseup.net> | 2019-09-05 13:00:50 +0200 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2019-09-05 20:00:50 +0900 |
commit | 2a166cfea22b90e39e3fe9bafab6b806ed4813f6 (patch) | |
tree | d08e04f9c8e56e70a959733f50c8ec4903336f4a /spec/ruby/core/file | |
parent | d9e6315177be2a1264213a1e7cb215312a23384a (diff) | |
download | ruby-2a166cfea22b90e39e3fe9bafab6b806ed4813f6.tar.gz |
Add `File.absolute_path?` (#2198)
In order to check whether a path is absolute or not in a portable way.
[Feature #15868]
Diffstat (limited to 'spec/ruby/core/file')
-rw-r--r-- | spec/ruby/core/file/absolute_path_spec.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/spec/ruby/core/file/absolute_path_spec.rb b/spec/ruby/core/file/absolute_path_spec.rb index d6f3f060c7..52839cf1cc 100644 --- a/spec/ruby/core/file/absolute_path_spec.rb +++ b/spec/ruby/core/file/absolute_path_spec.rb @@ -1,5 +1,58 @@ require_relative '../../spec_helper' +ruby_version_is "2.7" do + describe "File.absolute_path?" do + before :each do + @abs = File.expand_path(__FILE__) + end + + it "returns true if it's an absolute pathname" do + File.absolute_path?(@abs).should be_true + end + + it "returns false if it's a relative path" do + File.absolute_path?(File.basename(__FILE__)).should be_false + end + + it "returns false if it's a tricky relative path" do + File.absolute_path?("C:foo\\bar").should be_false + end + + it "does not expand '~' to a home directory." do + File.absolute_path?('~').should be_false + end + + it "does not expand '~user' to a home directory." do + path = File.dirname(@abs) + Dir.chdir(path) do + File.absolute_path?('~user').should be_false + end + end + + it "calls #to_path on its argument" do + mock = mock_to_path(File.expand_path(__FILE__)) + + File.absolute_path?(mock).should be_true + end + + platform_is_not :windows do + it "takes into consideration the platform's root" do + File.absolute_path?("C:\\foo\\bar").should be_false + File.absolute_path?("C:/foo/bar").should be_false + File.absolute_path?("/foo/bar\\baz").should be_true + end + end + + platform_is :windows do + it "takes into consideration the platform path separator(s)" do + File.absolute_path?("C:\\foo\\bar").should be_true + File.absolute_path?("C:/foo/bar").should be_true + File.absolute_path?("/foo/bar\\baz").should be_false + end + end + end +end + describe "File.absolute_path" do before :each do @abs = File.expand_path(__FILE__) |