File: local_test.rb

package info (click to toggle)
ruby-train 3.13.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,208 kB
  • sloc: ruby: 10,002; sh: 17; makefile: 8
file content (126 lines) | stat: -rw-r--r-- 3,454 bytes parent folder | download | duplicates (3)
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
require "helper"
require "train/transports/local"

describe Train::File::Local do
  # there is zero need to instantiate this OVER and over, so just do it once.
  transport  = Train::Transports::Local.new
  connection = transport.connection

  it "gets file contents" do
    res = rand.to_s
    File.stub :read, res do
      _(connection.file(rand.to_s).content).must_equal(res)
    end
  end

  {
    exist?: :exist?,
    file?: :file?,
    socket?: :socket?,
    directory?: :directory?,
    symlink?: :symlink?,
    pipe?: :pipe?,
    character_device?: :chardev?,
    block_device?: :blockdev?,
  }.each do |method, file_method|
    it "checks if file is a #{method}" do
      File.stub file_method.to_sym, true do
        _(connection.file(rand.to_s).method(method.to_sym).call).must_equal(true)
      end
    end
  end

  it "has a friendly inspect" do
    _(connection.inspect).must_equal "Train::Transports::Local::Connection[unknown]"
  end

  describe "#type" do
    it "returns the type block_device if it is block device" do
      File.stub :ftype, "blockSpecial" do
        _(connection.file(rand.to_s).type).must_equal :block_device
      end
    end

    it "returns the type character_device if it is character device" do
      File.stub :ftype, "characterSpecial" do
        _(connection.file(rand.to_s).type).must_equal :character_device
      end
    end

    it "returns the type symlink if it is symlink" do
      File.stub :ftype, "link" do
        _(connection.file(rand.to_s).type).must_equal :symlink
      end
    end

    it "returns the type file if it is file" do
      File.stub :ftype, "file" do
        _(connection.file(rand.to_s).type).must_equal :file
      end
    end

    it "returns the type directory if it is block directory" do
      File.stub :ftype, "directory" do
        _(connection.file(rand.to_s).type).must_equal :directory
      end
    end

    it "returns the type pipe if it is pipe" do
      File.stub :ftype, "fifo" do
        _(connection.file(rand.to_s).type).must_equal :pipe
      end
    end

    it "returns the type socket if it is socket" do
      File.stub :ftype, "socket" do
        _(connection.file(rand.to_s).type).must_equal :socket
      end
    end

    it "returns the unknown if not known" do
      File.stub :ftype, "unknown" do
        _(connection.file(rand.to_s).type).must_equal :unknown
      end
    end
  end

  describe "#path" do
    it "returns the path if it is not a symlink" do
      File.stub :symlink?, false do
        filename = rand.to_s
        _(connection.file(filename).path).must_equal filename
      end
    end

    it "returns the link_path if it is a symlink" do
      File.stub :symlink?, true do
        file_obj = connection.file(rand.to_s)
        file_obj.stub :link_path, "/path/to/resolved_link" do
          _(file_obj.path).must_equal "/path/to/resolved_link"
        end
      end
    end
  end

  describe "#link_path" do
    it "returns file's link path" do
      out = rand.to_s
      File.stub :realpath, out do
        File.stub :symlink?, true do
          _(connection.file(rand.to_s).link_path).must_equal out
        end
      end
    end
  end

  describe "#shallow_shlink_path" do
    it "returns file's direct link path" do
      out = rand.to_s
      File.stub :readlink, out do
        File.stub :symlink?, true do
          _(connection.file(rand.to_s).shallow_link_path).must_equal out
        end
      end
    end
  end
end