File: absolute_path_spec.rb

package info (click to toggle)
ruby3.1 3.1.2-7%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 132,892 kB
  • sloc: ruby: 1,154,753; ansic: 736,782; yacc: 46,445; pascal: 10,401; sh: 3,931; cpp: 1,158; python: 838; makefile: 787; asm: 462; javascript: 382; lisp: 97; sed: 94; perl: 62; awk: 36; xml: 4
file content (96 lines) | stat: -rw-r--r-- 2,727 bytes parent folder | download | duplicates (2)
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
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__)
  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