File: test_file.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 (215 lines) | stat: -rw-r--r-- 7,116 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
require 'common'

class FileOperationsTest < Net::SFTP::TestCase
  def setup
    @sftp = mock("sftp")
    @file = Net::SFTP::Operations::File.new(@sftp, "handle")
    @save_dollar_fslash, $/ = $/, "\n"
    @save_dollar_bslash, $\ = $\, nil
  end

  def teardown
    $/ = @save_dollar_fslash
    $\ = @save_dollar_bslash
  end

  def test_pos_assignment_should_set_position
    @file.pos = 15
    assert_equal 15, @file.pos
  end

  def test_pos_assignment_should_reset_eof
    @sftp.expects(:read!).with("handle", 0, 8192).returns(nil)
    assert !@file.eof?
    @file.read
    assert @file.eof?
    @file.pos = 0
    assert !@file.eof?
  end

  def test_close_should_close_handle_and_set_handle_to_nil
    assert_equal "handle", @file.handle
    @sftp.expects(:close!).with("handle")
    @file.close
    assert_nil @file.handle
  end

  def test_eof_should_be_false_if_at_eof_but_data_remains_in_buffer
    @sftp.expects(:read!).returns("hello world", nil)
    @file.read(1)
    assert !@file.eof?
  end

  def test_eof_should_be_true_if_at_eof_and_no_data_in_buffer
    @sftp.expects(:read!).times(2).returns("hello world", nil)
    @file.read
    assert @file.eof?
  end

  def test_read_without_argument_should_read_and_return_remainder_of_file_and_set_pos
    @sftp.expects(:read!).times(2).returns("hello world", nil)
    assert_equal "hello world", @file.read
    assert_equal 11, @file.pos
  end

  def test_read_with_argument_should_read_and_return_n_bytes_and_set_pos
    @sftp.expects(:read!).returns("hello world")
    assert_equal "hello", @file.read(5)
    assert_equal 5, @file.pos
  end

  def test_read_after_pos_assignment_should_read_from_specified_position
    @sftp.expects(:read!).with("handle", 5, 8192).returns("hello world")
    @file.pos = 5
    assert_equal "hello", @file.read(5)
    assert_equal 10, @file.pos
  end

  def test_gets_without_argument_should_read_until_first_dollar_fslash
    @sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
    assert_equal "\n", $/
    assert_equal "hello world\n", @file.gets
    assert_equal 12, @file.pos
  end

  def test_gets_with_empty_argument_should_read_until_double_dollar_fslash
    @sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
    assert_equal "\n", $/
    assert_equal "hello world\ngoodbye world\n\n", @file.gets("")
    assert_equal 27, @file.pos
  end

  def test_gets_with_argument_should_read_until_first_instance_of_argument
    @sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
    assert_equal "hello w", @file.gets("w")
    assert_equal 7, @file.pos
  end

  def test_gets_when_no_such_delimiter_exists_in_stream_should_read_to_EOF
    @sftp.expects(:read!).times(2).returns("hello world\ngoodbye world\n\nfarewell!\n", nil)
    assert_equal "hello world\ngoodbye world\n\nfarewell!\n", @file.gets("X")
    assert @file.eof?
  end

  def test_gets_when_nil_delimiter_should_fread_to_EOF
    @sftp.expects(:read!).times(2).returns("hello world\ngoodbye world\n\nfarewell!\n", nil)
    assert_equal "hello world\ngoodbye world\n\nfarewell!\n", @file.gets(nil)
    assert @file.eof?
  end

  def test_gets_with_integer_argument_should_read_number_of_bytes
    @sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
    assert_equal "hello w", @file.gets(7)
  end

  def test_gets_with_delimiter_and_limit_should_read_to_delimiter_if_less_than_limit
    @sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
    assert_equal "hello w", @file.gets("w", 11)
  end

  def test_gets_with_delimiter_and_limit_should_read_to_limit_if_less_than_delimiter
    @sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
    assert_equal "hello", @file.gets("w", 5)
  end

  def test_gets_when_no_such_delimiter_exists_in_stream_but_limit_provided_should_read_to_limit
    @sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
    assert_equal "hello w", @file.gets("z", 7)
  end

  def test_gets_when_nil_delimiter_and_limit_provided_should_read_to_limit
    @sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
    assert_equal "hello w", @file.gets(nil, 7)
  end

  def test_gets_at_EOF_should_return_nil
    @sftp.expects(:read!).returns(nil)
    assert_nil @file.gets
    assert @file.eof?
  end

  def test_readline_should_raise_exception_on_EOF
    @sftp.expects(:read!).returns(nil)
    assert_raises(EOFError) { @file.readline }
  end

  def test_rewind_should_reset_to_beginning_of_file
    @sftp.expects(:read!).times(2).returns("hello world", nil)
    @file.read
    assert @file.eof?
    @file.rewind
    assert !@file.eof?
    assert_equal 0, @file.pos
  end

  def test_rewind
    @sftp.expects(:write!).with("handle", 0, "hello world\n")
    @sftp.expects(:read!).with("handle", 12, 8192).returns("hello world\n")
    @sftp.expects(:read!).with("handle", 0, 8192).returns("hello world\n")
    @file.puts "hello world\n"
    assert_equal "hello", @file.read(5)
    @file.rewind
    assert_equal "hello world", @file.read(11)
  end

  def test_write_should_write_data_and_increment_pos_and_return_data_length
    @sftp.expects(:write!).with("handle", 0, "hello world")
    assert_equal 11, @file.write("hello world")
    assert_equal 11, @file.pos
  end

  def test_write_after_pos_assignment_should_write_at_position
    @sftp.expects(:write!).with("handle", 15, "hello world")
    @file.pos = 15
    assert_equal 11, @file.write("hello world")
    assert_equal 26, @file.pos
  end

  def test_print_with_no_arguments_should_write_nothing_if_dollar_bslash_is_nil
    assert_nil $\
    @sftp.expects(:write!).never
    @file.print
  end

  def test_print_with_no_arguments_should_write_dollar_bslash_if_dollar_bslash_is_not_nil
    $\ = "-"
    @sftp.expects(:write!).with("handle", 0, "-")
    @file.print
  end

  def test_print_with_arguments_should_write_all_arguments
    @sftp.expects(:write!).with("handle", 0, "hello")
    @sftp.expects(:write!).with("handle", 5, " ")
    @sftp.expects(:write!).with("handle", 6, "world")
    @file.print("hello", " ", "world")
  end

  def test_puts_should_recursively_puts_array_arguments
    10.times do |i|
      @sftp.expects(:write!).with("handle", i*2, i.to_s)
      @sftp.expects(:write!).with("handle", i*2+1, "\n")
    end
    @file.puts 0, [1, [2, 3], 4, [5, [6, 7, 8]]], 9
  end

  def test_puts_should_not_append_newline_if_argument_ends_in_newline
    @sftp.expects(:write!).with("handle", 0, "a")
    @sftp.expects(:write!).with("handle", 1, "\n")
    @sftp.expects(:write!).with("handle", 2, "b\n")
    @sftp.expects(:write!).with("handle", 4, "c")
    @sftp.expects(:write!).with("handle", 5, "\n")
    @file.puts "a", "b\n", "c"
  end

  def test_stat_should_return_attributes_object_for_handle
    stat = stub("stat")
    @sftp.expects(:fstat!).with("handle").returns(stat)
    assert_equal stat, @file.stat
  end

  def test_size_should_return_size_from_stat
    stat = stub(size: 1024)
    @sftp.expects(:fstat!).with("handle").returns(stat)
    assert_equal 1024, @file.size
  end
end