iostreams 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/Rakefile +7 -7
- data/lib/io_streams/builder.rb +4 -3
- data/lib/io_streams/bzip2/reader.rb +1 -1
- data/lib/io_streams/bzip2/writer.rb +1 -1
- data/lib/io_streams/deprecated.rb +2 -3
- data/lib/io_streams/encode/reader.rb +5 -8
- data/lib/io_streams/encode/writer.rb +1 -1
- data/lib/io_streams/io_streams.rb +5 -2
- data/lib/io_streams/line/reader.rb +2 -1
- data/lib/io_streams/path.rb +4 -4
- data/lib/io_streams/paths/http.rb +8 -10
- data/lib/io_streams/paths/matcher.rb +10 -11
- data/lib/io_streams/paths/s3.rb +6 -6
- data/lib/io_streams/paths/sftp.rb +38 -23
- data/lib/io_streams/pgp.rb +45 -35
- data/lib/io_streams/pgp/reader.rb +4 -6
- data/lib/io_streams/pgp/writer.rb +1 -2
- data/lib/io_streams/reader.rb +2 -2
- data/lib/io_streams/record/writer.rb +2 -4
- data/lib/io_streams/row/writer.rb +3 -5
- data/lib/io_streams/stream.rb +6 -6
- data/lib/io_streams/symmetric_encryption/reader.rb +1 -3
- data/lib/io_streams/symmetric_encryption/writer.rb +2 -6
- data/lib/io_streams/tabular.rb +12 -10
- data/lib/io_streams/tabular/header.rb +4 -4
- data/lib/io_streams/tabular/parser/array.rb +2 -4
- data/lib/io_streams/tabular/parser/csv.rb +3 -5
- data/lib/io_streams/tabular/parser/fixed.rb +4 -3
- data/lib/io_streams/tabular/parser/hash.rb +2 -4
- data/lib/io_streams/tabular/parser/json.rb +2 -4
- data/lib/io_streams/tabular/parser/psv.rb +5 -7
- data/lib/io_streams/tabular/utility/csv_row.rb +9 -17
- data/lib/io_streams/utils.rb +3 -3
- data/lib/io_streams/utils/reliable_http.rb +98 -0
- data/lib/io_streams/version.rb +1 -1
- data/lib/io_streams/writer.rb +1 -1
- data/lib/io_streams/xlsx/reader.rb +5 -5
- data/lib/io_streams/zip/reader.rb +1 -1
- data/lib/io_streams/zip/writer.rb +2 -2
- data/lib/iostreams.rb +34 -34
- data/test/builder_test.rb +74 -74
- data/test/bzip2_reader_test.rb +8 -13
- data/test/bzip2_writer_test.rb +8 -9
- data/test/deprecated_test.rb +25 -29
- data/test/encode_reader_test.rb +14 -18
- data/test/encode_writer_test.rb +29 -30
- data/test/gzip_reader_test.rb +8 -13
- data/test/gzip_writer_test.rb +10 -11
- data/test/io_streams_test.rb +35 -35
- data/test/line_reader_test.rb +35 -39
- data/test/line_writer_test.rb +8 -9
- data/test/minimal_file_reader.rb +1 -1
- data/test/path_test.rb +24 -24
- data/test/paths/file_test.rb +42 -42
- data/test/paths/http_test.rb +5 -5
- data/test/paths/matcher_test.rb +11 -12
- data/test/paths/s3_test.rb +44 -46
- data/test/paths/sftp_test.rb +18 -18
- data/test/pgp_reader_test.rb +13 -15
- data/test/pgp_test.rb +43 -44
- data/test/pgp_writer_test.rb +27 -28
- data/test/record_reader_test.rb +9 -10
- data/test/record_writer_test.rb +10 -11
- data/test/row_reader_test.rb +5 -6
- data/test/row_writer_test.rb +7 -8
- data/test/stream_test.rb +60 -62
- data/test/tabular_test.rb +111 -111
- data/test/test_helper.rb +22 -22
- data/test/utils_test.rb +7 -7
- data/test/xlsx_reader_test.rb +12 -12
- data/test/zip_reader_test.rb +14 -21
- data/test/zip_writer_test.rb +10 -10
- metadata +4 -3
data/test/encode_reader_test.rb
CHANGED
@@ -1,52 +1,48 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "test_helper"
|
2
2
|
|
3
3
|
class EncodeReaderTest < Minitest::Test
|
4
4
|
describe IOStreams::Encode::Reader do
|
5
5
|
let :bad_data do
|
6
6
|
[
|
7
7
|
"New M\xE9xico,NE".b,
|
8
|
-
|
8
|
+
"good line",
|
9
9
|
"New M\xE9xico,\x07SF".b
|
10
|
-
].join("\n").encode(
|
10
|
+
].join("\n").encode("BINARY")
|
11
11
|
end
|
12
12
|
|
13
13
|
let :cleansed_data do
|
14
|
-
bad_data.gsub("\xE9".b,
|
14
|
+
bad_data.gsub("\xE9".b, "")
|
15
15
|
end
|
16
16
|
|
17
17
|
let :stripped_data do
|
18
|
-
cleansed_data.gsub("\x07",
|
18
|
+
cleansed_data.gsub("\x07", "")
|
19
19
|
end
|
20
20
|
|
21
|
-
describe
|
22
|
-
describe
|
23
|
-
it
|
21
|
+
describe "#read" do
|
22
|
+
describe "replacement" do
|
23
|
+
it "does not strip invalid characters" do
|
24
24
|
skip "Does not raise on JRuby" if defined?(JRuby)
|
25
25
|
input = StringIO.new(bad_data)
|
26
|
-
IOStreams::Encode::Reader.stream(input, encoding:
|
26
|
+
IOStreams::Encode::Reader.stream(input, encoding: "UTF-8") do |io|
|
27
27
|
assert_raises ::Encoding::UndefinedConversionError do
|
28
28
|
io.read.encoding
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
it
|
33
|
+
it "strips invalid characters" do
|
34
34
|
input = StringIO.new(bad_data)
|
35
35
|
data =
|
36
|
-
IOStreams::Encode::Reader.stream(input, encoding:
|
37
|
-
io.read
|
38
|
-
end
|
36
|
+
IOStreams::Encode::Reader.stream(input, encoding: "UTF-8", replace: "", &:read)
|
39
37
|
assert_equal cleansed_data, data
|
40
38
|
end
|
41
39
|
end
|
42
40
|
|
43
|
-
describe
|
44
|
-
it
|
41
|
+
describe "printable" do
|
42
|
+
it "strips non-printable characters" do
|
45
43
|
input = StringIO.new(bad_data)
|
46
44
|
data =
|
47
|
-
IOStreams::Encode::Reader.stream(input, encoding:
|
48
|
-
io.read
|
49
|
-
end
|
45
|
+
IOStreams::Encode::Reader.stream(input, encoding: "UTF-8", cleaner: :printable, replace: "", &:read)
|
50
46
|
assert_equal stripped_data, data
|
51
47
|
end
|
52
48
|
end
|
data/test/encode_writer_test.rb
CHANGED
@@ -1,82 +1,81 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "test_helper"
|
2
2
|
|
3
3
|
class EncodeWriterTest < Minitest::Test
|
4
4
|
describe IOStreams::Encode::Writer do
|
5
5
|
let :bad_data do
|
6
6
|
[
|
7
7
|
"New M\xE9xico,NE".b,
|
8
|
-
|
8
|
+
"good line",
|
9
9
|
"New M\xE9xico,\x07SF".b
|
10
|
-
].join("\n").encode(
|
10
|
+
].join("\n").encode("BINARY")
|
11
11
|
end
|
12
12
|
|
13
13
|
let :cleansed_data do
|
14
|
-
bad_data.gsub("\xE9".b,
|
14
|
+
bad_data.gsub("\xE9".b, "?")
|
15
15
|
end
|
16
16
|
|
17
17
|
let :stripped_data do
|
18
|
-
cleansed_data.gsub("\x07",
|
18
|
+
cleansed_data.gsub("\x07", "")
|
19
19
|
end
|
20
20
|
|
21
|
-
describe
|
22
|
-
it
|
23
|
-
temp_file = Tempfile.new(
|
21
|
+
describe "#<<" do
|
22
|
+
it "file" do
|
23
|
+
temp_file = Tempfile.new("rocket_job")
|
24
24
|
file_name = temp_file.to_path
|
25
|
-
IOStreams::Encode::Writer.file(file_name, encoding:
|
25
|
+
IOStreams::Encode::Writer.file(file_name, encoding: "ASCII-8BIT") do |io|
|
26
26
|
io << bad_data
|
27
27
|
end
|
28
|
-
result = File.read(file_name, mode:
|
28
|
+
result = File.read(file_name, mode: "rb")
|
29
29
|
assert_equal bad_data, result
|
30
30
|
end
|
31
31
|
|
32
|
-
it
|
33
|
-
io = StringIO.new(
|
34
|
-
IOStreams::Encode::Writer.stream(io, encoding:
|
32
|
+
it "stream" do
|
33
|
+
io = StringIO.new("".b)
|
34
|
+
IOStreams::Encode::Writer.stream(io, encoding: "ASCII-8BIT") do |encoded|
|
35
35
|
encoded << bad_data
|
36
36
|
end
|
37
|
-
assert_equal
|
37
|
+
assert_equal "ASCII-8BIT", io.string.encoding.to_s
|
38
38
|
assert_equal bad_data, io.string
|
39
39
|
end
|
40
40
|
|
41
|
-
it
|
42
|
-
io = StringIO.new(
|
41
|
+
it "stream as utf-8" do
|
42
|
+
io = StringIO.new("")
|
43
43
|
assert_raises Encoding::UndefinedConversionError do
|
44
|
-
IOStreams::Encode::Writer.stream(io, encoding:
|
44
|
+
IOStreams::Encode::Writer.stream(io, encoding: "UTF-8") do |encoded|
|
45
45
|
encoded << bad_data
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
it
|
51
|
-
io = StringIO.new(
|
52
|
-
IOStreams::Encode::Writer.stream(io, encoding:
|
50
|
+
it "stream as utf-8 with replacement" do
|
51
|
+
io = StringIO.new("")
|
52
|
+
IOStreams::Encode::Writer.stream(io, encoding: "UTF-8", replace: "?") do |encoded|
|
53
53
|
encoded << bad_data
|
54
54
|
end
|
55
|
-
assert_equal
|
55
|
+
assert_equal "UTF-8", io.string.encoding.to_s
|
56
56
|
assert_equal cleansed_data, io.string
|
57
57
|
end
|
58
58
|
|
59
|
-
it
|
60
|
-
io = StringIO.new(
|
61
|
-
IOStreams::Encode::Writer.stream(io, encoding:
|
59
|
+
it "stream as utf-8 with replacement and printable cleansing" do
|
60
|
+
io = StringIO.new("")
|
61
|
+
IOStreams::Encode::Writer.stream(io, encoding: "UTF-8", replace: "?", cleaner: :printable) do |encoded|
|
62
62
|
encoded << bad_data
|
63
63
|
end
|
64
|
-
assert_equal
|
64
|
+
assert_equal "UTF-8", io.string.encoding.to_s
|
65
65
|
assert_equal stripped_data, io.string
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
-
describe
|
70
|
-
it
|
71
|
-
io_string = StringIO.new(
|
69
|
+
describe ".write" do
|
70
|
+
it "returns byte count" do
|
71
|
+
io_string = StringIO.new("".b)
|
72
72
|
count = 0
|
73
|
-
IOStreams::Encode::Writer.stream(io_string, encoding:
|
73
|
+
IOStreams::Encode::Writer.stream(io_string, encoding: "ASCII-8BIT") do |io|
|
74
74
|
count += io.write(bad_data)
|
75
75
|
end
|
76
76
|
assert_equal bad_data, io_string.string
|
77
77
|
assert_equal bad_data.size, count
|
78
78
|
end
|
79
79
|
end
|
80
|
-
|
81
80
|
end
|
82
81
|
end
|
data/test/gzip_reader_test.rb
CHANGED
@@ -1,32 +1,27 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "test_helper"
|
2
2
|
|
3
3
|
class GzipReaderTest < Minitest::Test
|
4
4
|
describe IOStreams::Gzip::Reader do
|
5
5
|
let :file_name do
|
6
|
-
File.join(File.dirname(__FILE__),
|
6
|
+
File.join(File.dirname(__FILE__), "files", "text.txt.gz")
|
7
7
|
end
|
8
8
|
|
9
9
|
let :decompressed do
|
10
|
-
Zlib::GzipReader.open(file_name
|
10
|
+
Zlib::GzipReader.open(file_name, &:read)
|
11
11
|
end
|
12
12
|
|
13
|
-
describe
|
14
|
-
it
|
15
|
-
result = IOStreams::Gzip::Reader.file(file_name)
|
16
|
-
io.read
|
17
|
-
end
|
13
|
+
describe ".open" do
|
14
|
+
it "file" do
|
15
|
+
result = IOStreams::Gzip::Reader.file(file_name, &:read)
|
18
16
|
assert_equal decompressed, result
|
19
17
|
end
|
20
18
|
|
21
|
-
it
|
19
|
+
it "stream" do
|
22
20
|
result = File.open(file_name) do |file|
|
23
|
-
IOStreams::Gzip::Reader.stream(file)
|
24
|
-
io.read
|
25
|
-
end
|
21
|
+
IOStreams::Gzip::Reader.stream(file, &:read)
|
26
22
|
end
|
27
23
|
assert_equal decompressed, result
|
28
24
|
end
|
29
25
|
end
|
30
|
-
|
31
26
|
end
|
32
27
|
end
|
data/test/gzip_writer_test.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "test_helper"
|
2
2
|
|
3
3
|
class GzipWriterTest < Minitest::Test
|
4
4
|
describe IOStreams::Gzip::Writer do
|
5
5
|
let :temp_file do
|
6
|
-
Tempfile.new(
|
6
|
+
Tempfile.new("iostreams")
|
7
7
|
end
|
8
8
|
|
9
9
|
let :file_name do
|
@@ -11,35 +11,34 @@ class GzipWriterTest < Minitest::Test
|
|
11
11
|
end
|
12
12
|
|
13
13
|
let :decompressed do
|
14
|
-
File.read(File.join(File.dirname(__FILE__),
|
14
|
+
File.read(File.join(File.dirname(__FILE__), "files", "text.txt"))
|
15
15
|
end
|
16
16
|
|
17
17
|
after do
|
18
18
|
temp_file.delete
|
19
19
|
end
|
20
20
|
|
21
|
-
describe
|
22
|
-
it
|
21
|
+
describe ".file" do
|
22
|
+
it "file" do
|
23
23
|
IOStreams::Gzip::Writer.file(file_name) do |io|
|
24
24
|
io.write(decompressed)
|
25
25
|
end
|
26
|
-
result = Zlib::GzipReader.open(file_name
|
26
|
+
result = Zlib::GzipReader.open(file_name, &:read)
|
27
27
|
temp_file.delete
|
28
28
|
assert_equal decompressed, result
|
29
29
|
end
|
30
30
|
|
31
|
-
it
|
32
|
-
io_string = StringIO.new(
|
31
|
+
it "stream" do
|
32
|
+
io_string = StringIO.new("".b)
|
33
33
|
IOStreams::Gzip::Writer.stream(io_string) do |io|
|
34
34
|
io.write(decompressed)
|
35
35
|
end
|
36
|
-
io
|
37
|
-
gz
|
36
|
+
io = StringIO.new(io_string.string)
|
37
|
+
gz = Zlib::GzipReader.new(io)
|
38
38
|
data = gz.read
|
39
39
|
gz.close
|
40
40
|
assert_equal decompressed, data
|
41
41
|
end
|
42
42
|
end
|
43
|
-
|
44
43
|
end
|
45
44
|
end
|
data/test/io_streams_test.rb
CHANGED
@@ -1,78 +1,78 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "test_helper"
|
2
2
|
|
3
3
|
module IOStreams
|
4
4
|
class PathTest < Minitest::Test
|
5
5
|
describe IOStreams do
|
6
|
-
describe
|
7
|
-
it
|
8
|
-
path = ::File.expand_path(::File.join(__dir__,
|
6
|
+
describe ".root" do
|
7
|
+
it "return default path" do
|
8
|
+
path = ::File.expand_path(::File.join(__dir__, "../tmp/default"))
|
9
9
|
assert_equal path, IOStreams.root.to_s
|
10
10
|
end
|
11
11
|
|
12
|
-
it
|
13
|
-
path = ::File.expand_path(::File.join(__dir__,
|
12
|
+
it "return downloads path" do
|
13
|
+
path = ::File.expand_path(::File.join(__dir__, "../tmp/downloads"))
|
14
14
|
assert_equal path, IOStreams.root(:downloads).to_s
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
describe
|
19
|
-
it
|
18
|
+
describe ".join" do
|
19
|
+
it "returns path" do
|
20
20
|
assert_equal IOStreams.root.to_s, IOStreams.join.to_s
|
21
21
|
end
|
22
22
|
|
23
|
-
it
|
24
|
-
assert_equal ::File.join(IOStreams.root.to_s,
|
23
|
+
it "adds path to root" do
|
24
|
+
assert_equal ::File.join(IOStreams.root.to_s, "test"), IOStreams.join("test").to_s
|
25
25
|
end
|
26
26
|
|
27
|
-
it
|
28
|
-
assert_equal ::File.join(IOStreams.root.to_s,
|
27
|
+
it "adds paths to root" do
|
28
|
+
assert_equal ::File.join(IOStreams.root.to_s, "test", "second", "third"), IOStreams.join("test", "second", "third").to_s
|
29
29
|
end
|
30
30
|
|
31
|
-
it
|
32
|
-
path = ::File.join(IOStreams.root.to_s,
|
33
|
-
assert_equal path, IOStreams.join(
|
31
|
+
it "returns path and filename" do
|
32
|
+
path = ::File.join(IOStreams.root.to_s, "file.xls")
|
33
|
+
assert_equal path, IOStreams.join("file.xls").to_s
|
34
34
|
end
|
35
35
|
|
36
|
-
it
|
37
|
-
path = ::File.join(IOStreams.root.to_s,
|
38
|
-
assert_equal path, IOStreams.join(
|
36
|
+
it "adds path to root and filename" do
|
37
|
+
path = ::File.join(IOStreams.root.to_s, "test", "file.xls")
|
38
|
+
assert_equal path, IOStreams.join("test", "file.xls").to_s
|
39
39
|
end
|
40
40
|
|
41
|
-
it
|
42
|
-
path = ::File.join(IOStreams.root.to_s,
|
43
|
-
assert_equal path, IOStreams.join(
|
41
|
+
it "adds paths to root" do
|
42
|
+
path = ::File.join(IOStreams.root.to_s, "test", "second", "third", "file.xls")
|
43
|
+
assert_equal path, IOStreams.join("test", "second", "third", "file.xls").to_s
|
44
44
|
end
|
45
45
|
|
46
|
-
it
|
47
|
-
path = ::File.join(IOStreams.root.to_s,
|
46
|
+
it "return path as sent in when full path" do
|
47
|
+
path = ::File.join(IOStreams.root.to_s, "file.xls")
|
48
48
|
assert_equal path, IOStreams.join(path).to_s
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
describe
|
53
|
-
it
|
54
|
-
path = IOStreams.path(
|
52
|
+
describe ".path" do
|
53
|
+
it "default" do
|
54
|
+
path = IOStreams.path("a.xyz")
|
55
55
|
assert path.is_a?(IOStreams::Paths::File), path
|
56
56
|
end
|
57
57
|
|
58
|
-
it
|
59
|
-
skip
|
60
|
-
IOStreams.path(
|
58
|
+
it "s3" do
|
59
|
+
skip "TODO"
|
60
|
+
IOStreams.path("s3://a.xyz")
|
61
61
|
assert_equal :s3, path
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
-
describe
|
66
|
-
it
|
67
|
-
result = IOStreams.temp_file(
|
65
|
+
describe ".temp_file" do
|
66
|
+
it "returns value from block" do
|
67
|
+
result = IOStreams.temp_file("base", ".ext") { |_path| 257 }
|
68
68
|
assert_equal 257, result
|
69
69
|
end
|
70
70
|
|
71
|
-
it
|
71
|
+
it "supplies new temp file_name" do
|
72
72
|
path1 = nil
|
73
73
|
path2 = nil
|
74
|
-
IOStreams.temp_file(
|
75
|
-
IOStreams.temp_file(
|
74
|
+
IOStreams.temp_file("base", ".ext") { |path| path1 = path }
|
75
|
+
IOStreams.temp_file("base", ".ext") { |path| path2 = path }
|
76
76
|
refute_equal path1.to_s, path2.to_s
|
77
77
|
assert path1.is_a?(IOStreams::Paths::File), path1
|
78
78
|
assert path2.is_a?(IOStreams::Paths::File), path2
|
data/test/line_reader_test.rb
CHANGED
@@ -1,25 +1,23 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "test_helper"
|
2
2
|
|
3
3
|
class LineReaderTest < Minitest::Test
|
4
4
|
describe IOStreams::Line::Reader do
|
5
5
|
let :file_name do
|
6
|
-
File.join(File.dirname(__FILE__),
|
6
|
+
File.join(File.dirname(__FILE__), "files", "text.txt")
|
7
7
|
end
|
8
8
|
|
9
9
|
let :csv_file do
|
10
|
-
File.join(File.dirname(__FILE__),
|
10
|
+
File.join(File.dirname(__FILE__), "files", "embedded_lines_test.csv")
|
11
11
|
end
|
12
12
|
|
13
13
|
let :unclosed_quote_file do
|
14
|
-
File.join(File.dirname(__FILE__),
|
14
|
+
File.join(File.dirname(__FILE__), "files", "unclosed_quote_test.csv")
|
15
15
|
end
|
16
16
|
|
17
17
|
let :data do
|
18
18
|
data = []
|
19
|
-
File.open(file_name,
|
20
|
-
|
21
|
-
data << file.readline.strip
|
22
|
-
end
|
19
|
+
File.open(file_name, "rt") do |file|
|
20
|
+
data << file.readline.strip until file.eof?
|
23
21
|
end
|
24
22
|
data
|
25
23
|
end
|
@@ -31,10 +29,9 @@ class LineReaderTest < Minitest::Test
|
|
31
29
|
# "John","Firstname\n is John","234568"
|
32
30
|
# "Zack","Firstname is Zack","234568\n"
|
33
31
|
#
|
34
|
-
describe
|
35
|
-
describe
|
36
|
-
|
37
|
-
it 'fails to keep embedded lines if flag is not set' do
|
32
|
+
describe "embedded_within_quotes" do
|
33
|
+
describe "csv file" do
|
34
|
+
it "fails to keep embedded lines if flag is not set" do
|
38
35
|
lines = []
|
39
36
|
IOStreams::Line::Reader.file(csv_file) do |io|
|
40
37
|
io.each do |line|
|
@@ -44,7 +41,7 @@ class LineReaderTest < Minitest::Test
|
|
44
41
|
assert_equal 7, lines.count
|
45
42
|
end
|
46
43
|
|
47
|
-
it
|
44
|
+
it "keeps embedded lines if flag is set" do
|
48
45
|
lines = []
|
49
46
|
IOStreams::Line::Reader.file(csv_file, embedded_within: '"') do |io|
|
50
47
|
io.each do |line|
|
@@ -54,7 +51,7 @@ class LineReaderTest < Minitest::Test
|
|
54
51
|
assert_equal 4, lines.count
|
55
52
|
end
|
56
53
|
|
57
|
-
it
|
54
|
+
it "raises error for unclosed quote" do
|
58
55
|
assert_raises(RuntimeError) do
|
59
56
|
IOStreams::Line::Reader.file(unclosed_quote_file, embedded_within: '"') do |io|
|
60
57
|
io.each do |line|
|
@@ -65,8 +62,8 @@ class LineReaderTest < Minitest::Test
|
|
65
62
|
end
|
66
63
|
end
|
67
64
|
|
68
|
-
describe
|
69
|
-
it
|
65
|
+
describe "#each" do
|
66
|
+
it "each_line file" do
|
70
67
|
lines = []
|
71
68
|
count = IOStreams::Line::Reader.file(file_name) do |io|
|
72
69
|
io.each { |line| lines << line }
|
@@ -75,7 +72,7 @@ class LineReaderTest < Minitest::Test
|
|
75
72
|
assert_equal data.size, count
|
76
73
|
end
|
77
74
|
|
78
|
-
it
|
75
|
+
it "each_line stream" do
|
79
76
|
lines = []
|
80
77
|
count = File.open(file_name) do |file|
|
81
78
|
IOStreams::Line::Reader.stream(file) do |io|
|
@@ -108,7 +105,7 @@ class LineReaderTest < Minitest::Test
|
|
108
105
|
end
|
109
106
|
end
|
110
107
|
|
111
|
-
[
|
108
|
+
["@", "BLAH"].each do |delimiter|
|
112
109
|
it "reads delimited #{delimiter.inspect}" do
|
113
110
|
lines = []
|
114
111
|
stream = StringIO.new(data.join(delimiter))
|
@@ -120,10 +117,10 @@ class LineReaderTest < Minitest::Test
|
|
120
117
|
end
|
121
118
|
end
|
122
119
|
|
123
|
-
it
|
120
|
+
it "reads binary delimited" do
|
124
121
|
delimiter = "\x01"
|
125
122
|
lines = []
|
126
|
-
stream = StringIO.new(data.join(delimiter).encode(
|
123
|
+
stream = StringIO.new(data.join(delimiter).encode("ASCII-8BIT"))
|
127
124
|
count = IOStreams::Line::Reader.stream(stream, buffer_size: 15, delimiter: delimiter) do |io|
|
128
125
|
io.each { |line| lines << line }
|
129
126
|
end
|
@@ -131,12 +128,12 @@ class LineReaderTest < Minitest::Test
|
|
131
128
|
assert_equal data.size, count
|
132
129
|
end
|
133
130
|
|
134
|
-
describe
|
135
|
-
let(:short_line) {
|
136
|
-
let(:longer_line) {
|
131
|
+
describe "#readline" do
|
132
|
+
let(:short_line) { "0123456789" }
|
133
|
+
let(:longer_line) { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" }
|
137
134
|
let(:delimiter) { "\r\n" }
|
138
135
|
|
139
|
-
it
|
136
|
+
it "reads delimiter in first block, no delimiter at end" do
|
140
137
|
data = [short_line, longer_line].join(delimiter)
|
141
138
|
buffer_size = short_line.length + delimiter.size + (longer_line.size / 2)
|
142
139
|
|
@@ -153,7 +150,7 @@ class LineReaderTest < Minitest::Test
|
|
153
150
|
end
|
154
151
|
end
|
155
152
|
|
156
|
-
it
|
153
|
+
it "reads delimiter in second block, no delimiter at end" do
|
157
154
|
data = [longer_line, short_line, short_line].join(delimiter)
|
158
155
|
buffer_size = (longer_line.length + delimiter.size + 5) / 2
|
159
156
|
|
@@ -169,7 +166,7 @@ class LineReaderTest < Minitest::Test
|
|
169
166
|
end
|
170
167
|
end
|
171
168
|
|
172
|
-
it
|
169
|
+
it "reads delimiter split across first and second blocks" do
|
173
170
|
data = [longer_line, short_line, short_line].join(delimiter)
|
174
171
|
buffer_size = longer_line.length + 1
|
175
172
|
|
@@ -185,8 +182,8 @@ class LineReaderTest < Minitest::Test
|
|
185
182
|
end
|
186
183
|
end
|
187
184
|
|
188
|
-
it
|
189
|
-
delimiter =
|
185
|
+
it "reads file with no matching delimiter" do
|
186
|
+
delimiter = "@"
|
190
187
|
data = [longer_line, short_line, longer_line].join(delimiter) + delimiter
|
191
188
|
buffer_size = longer_line.length + 1
|
192
189
|
|
@@ -200,7 +197,7 @@ class LineReaderTest < Minitest::Test
|
|
200
197
|
end
|
201
198
|
end
|
202
199
|
|
203
|
-
it
|
200
|
+
it "reads small file with no matching delimiter" do
|
204
201
|
data = short_line
|
205
202
|
buffer_size = short_line.length + 100
|
206
203
|
|
@@ -214,8 +211,8 @@ class LineReaderTest < Minitest::Test
|
|
214
211
|
end
|
215
212
|
end
|
216
213
|
|
217
|
-
it
|
218
|
-
delimiter =
|
214
|
+
it "reads last line with the delimiter as the last character" do
|
215
|
+
delimiter = "@"
|
219
216
|
data = [longer_line, short_line, longer_line].join(delimiter) + delimiter
|
220
217
|
buffer_size = longer_line.length + 1
|
221
218
|
|
@@ -231,7 +228,7 @@ class LineReaderTest < Minitest::Test
|
|
231
228
|
end
|
232
229
|
end
|
233
230
|
|
234
|
-
it
|
231
|
+
it "reads last line with the multi-byte delimiter as the last bytes" do
|
235
232
|
data = [longer_line, short_line, longer_line].join(delimiter) + delimiter
|
236
233
|
buffer_size = longer_line.length + 1
|
237
234
|
|
@@ -247,10 +244,10 @@ class LineReaderTest < Minitest::Test
|
|
247
244
|
end
|
248
245
|
end
|
249
246
|
|
250
|
-
describe
|
247
|
+
describe "read 1 char at a time" do
|
251
248
|
let(:buffer_size) { 1 }
|
252
249
|
|
253
|
-
it
|
250
|
+
it "delimiter at the end" do
|
254
251
|
data = [longer_line, short_line, longer_line].join(delimiter) + delimiter
|
255
252
|
|
256
253
|
stream = StringIO.new(data)
|
@@ -265,7 +262,7 @@ class LineReaderTest < Minitest::Test
|
|
265
262
|
end
|
266
263
|
end
|
267
264
|
|
268
|
-
it
|
265
|
+
it "no delimiter at the end" do
|
269
266
|
data = [longer_line, short_line, longer_line].join(delimiter)
|
270
267
|
|
271
268
|
stream = StringIO.new(data)
|
@@ -281,21 +278,20 @@ class LineReaderTest < Minitest::Test
|
|
281
278
|
end
|
282
279
|
end
|
283
280
|
|
284
|
-
it
|
281
|
+
it "reads empty file" do
|
285
282
|
stream = StringIO.new
|
286
283
|
IOStreams::Line::Reader.stream(stream) do |io|
|
287
284
|
assert io.eof?
|
288
285
|
end
|
289
286
|
end
|
290
287
|
|
291
|
-
it
|
292
|
-
data =
|
288
|
+
it "prevents denial of service" do
|
289
|
+
data = "a" * IOStreams::Line::Reader::MAX_BLOCKS_MULTIPLIER + "a"
|
293
290
|
stream = StringIO.new(data)
|
294
291
|
assert_raises IOStreams::Errors::DelimiterNotFound do
|
295
292
|
IOStreams::Line::Reader.stream(stream, buffer_size: 1) do |io|
|
296
293
|
end
|
297
294
|
end
|
298
|
-
|
299
295
|
end
|
300
296
|
end
|
301
297
|
end
|