summaryrefslogtreecommitdiff
path: root/spec/ruby/core/file/absolute_path_spec.rb
blob: e35c80ec3c5cea2235c255d1f7f5c8d61d6d996a (plain)
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
require_relative '../../spec_helper'

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

describe "File.absolute_path" do
  before :each do
    @abs = File.expand_path(__FILE__)
  end

  it "returns the argument if it's an absolute pathname" do
    File.absolute_path(@abs).should == @abs
  end

  it "resolves paths relative to the current working directory" do
    path = File.dirname(@abs)
    Dir.chdir(path) do
      File.absolute_path('hello.txt').should == File.join(Dir.pwd, 'hello.txt')
    end
  end

  it "does not expand '~' to a home directory." do
    File.absolute_path('~').should_not == File.expand_path('~')
  end

  platform_is_not :windows do
    it "does not expand '~' when given dir argument" do
      File.absolute_path('~', '/').should == '/~'
    end
  end

  it "does not expand '~user' to a home directory." do
    path = File.dirname(@abs)
    Dir.chdir(path) do
      File.absolute_path('~user').should == File.join(Dir.pwd, '~user')
    end
  end

  it "accepts a second argument of a directory from which to resolve the path" do
    File.absolute_path(__FILE__, File.dirname(__FILE__)).should == @abs
  end

  it "calls #to_path on its argument" do
    File.absolute_path(mock_to_path(@abs)).should == @abs
  end
end