File: test_dir.rb

package info (click to toggle)
ruby-net-sftp 1%3A2.1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 528 kB
  • ctags: 860
  • sloc: ruby: 5,015; makefile: 4
file content (47 lines) | stat: -rw-r--r-- 2,088 bytes parent folder | download | duplicates (8)
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
require 'common'

class DirOperationsTest < Net::SFTP::TestCase
  def setup
    @sftp = mock("sftp")
    @dir = Net::SFTP::Operations::Dir.new(@sftp)
  end

  def test_foreach_should_iterate_over_all_entries_in_directory
    @sftp.expects(:opendir!).with("/path/to/remote").returns("handle")
    @sftp.expects(:readdir!).with("handle").returns([:e1, :e2, :e3], [:e4, :e5], nil).times(3)
    @sftp.expects(:close!).with("handle")

    entries = []
    @dir.foreach("/path/to/remote") { |entry| entries << entry }
    assert_equal [:e1, :e2, :e3, :e4, :e5], entries
  end

  def test_entries_should_return_all_entries_in_a_single_array
    @sftp.expects(:opendir!).with("/path/to/remote").returns("handle")
    @sftp.expects(:readdir!).with("handle").returns([:e1, :e2, :e3], [:e4, :e5], nil).times(3)
    @sftp.expects(:close!).with("handle")

    assert_equal [:e1, :e2, :e3, :e4, :e5], @dir.entries("/path/to/remote")
  end

  def test_glob_should_search_under_path_for_matching_entries
    @sftp.expects(:opendir!).with("/path/to/remote").returns("handle")
    @sftp.expects(:opendir!).with("/path/to/remote/e3").returns("handle-e3")
    @sftp.expects(:opendir!).with("/path/to/remote/e5").returns("handle-e5")
    @sftp.expects(:readdir!).with("handle").returns([n(".", true), n("..", true), n("e1"), n("e2"), n("e3", true)], [n("e4"), n("e5", true)], nil).times(3)
    @sftp.expects(:readdir!).with("handle-e3").returns([n(".", true), n("..", true), n("e3e1"), n("e3e2")], nil).times(2)
    @sftp.expects(:readdir!).with("handle-e5").returns([n(".", true), n("..", true), n("e5e1"), n("e5e2"), n("e5e3")], nil).times(2)
    @sftp.expects(:close!).with("handle")
    @sftp.expects(:close!).with("handle-e3")
    @sftp.expects(:close!).with("handle-e5")

    assert_equal %w(e3/e3e2 e5/e5e2), @dir.glob("/path/to/remote", "**/e?e2").map { |e| e.name }
  end

  private

    def n(name, directory=false)
      Net::SFTP::Protocol::V01::Name.new(name.to_s, "longname for #{name}",
        Net::SFTP::Protocol::V01::Attributes.new(:permissions => directory ? 040755 : 0100644))
    end
end