File: test_file_factory.rb

package info (click to toggle)
ruby-net-sftp 1%3A4.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 696 kB
  • sloc: ruby: 5,136; makefile: 6
file content (48 lines) | stat: -rw-r--r-- 1,554 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
require "common"

class FileFactoryTest < Net::SFTP::TestCase
  def setup
    @sftp = stub('sftp')
    @factory = Net::SFTP::Operations::FileFactory.new(@sftp)
  end

  def test_open_with_block_should_yield_and_close_handle
    @sftp.expects(:open!).with("/path/to/remote", "r", :permissions => nil).returns("handle")
    @sftp.expects(:close!).with("handle")

    called = false
    @factory.open("/path/to/remote") do |f|
      called = true
      assert_instance_of Net::SFTP::Operations::File, f
    end

    assert called
  end

  def test_open_with_block_should_close_file_even_if_exception_is_raised
    @sftp.expects(:open!).with("/path/to/remote", "r", :permissions => nil).returns("handle")
    @sftp.expects(:close!).with("handle")

    assert_raises(RuntimeError) do
      @factory.open("/path/to/remote") { |f| raise RuntimeError, "b00m" }
    end
  end

  def test_open_without_block_should_return_new_file
    @sftp.expects(:open!).with("/path/to/remote", "r", :permissions => nil).returns("handle")
    @sftp.expects(:close!).never

    f = @factory.open("/path/to/remote")
    assert_instance_of Net::SFTP::Operations::File, f
  end

  def test_directory_should_be_true_for_directory
    @sftp.expects(:lstat!).with("/path/to/dir").returns(mock('attrs', :directory? => true))
    assert @factory.directory?("/path/to/dir")
  end

  def test_directory_should_be_false_for_non_directory
    @sftp.expects(:lstat!).with("/path/to/file").returns(mock('attrs', :directory? => false))
    assert !@factory.directory?("/path/to/file")
  end
end