iostreams 1.10.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +20 -2
- data/Rakefile +7 -0
- data/lib/io_streams/builder.rb +10 -10
- data/lib/io_streams/bzip2/writer.rb +1 -1
- data/lib/io_streams/encode/reader.rb +2 -2
- data/lib/io_streams/encode/writer.rb +5 -5
- data/lib/io_streams/gzip/reader.rb +1 -1
- data/lib/io_streams/gzip/writer.rb +1 -1
- data/lib/io_streams/io_streams.rb +47 -21
- data/lib/io_streams/line/reader.rb +2 -2
- data/lib/io_streams/line/writer.rb +1 -1
- data/lib/io_streams/path.rb +2 -2
- data/lib/io_streams/paths/file.rb +25 -11
- data/lib/io_streams/paths/http.rb +80 -7
- data/lib/io_streams/paths/matcher.rb +3 -3
- data/lib/io_streams/paths/s3.rb +22 -3
- data/lib/io_streams/paths/sftp.rb +9 -10
- data/lib/io_streams/pgp/reader.rb +25 -7
- data/lib/io_streams/pgp/writer.rb +95 -29
- data/lib/io_streams/pgp.rb +289 -87
- data/lib/io_streams/reader.rb +4 -4
- data/lib/io_streams/record/reader.rb +3 -4
- data/lib/io_streams/record/writer.rb +3 -4
- data/lib/io_streams/row/reader.rb +1 -1
- data/lib/io_streams/row/writer.rb +1 -1
- data/lib/io_streams/stream.rb +36 -30
- data/lib/io_streams/symmetric_encryption/reader.rb +2 -2
- data/lib/io_streams/symmetric_encryption/writer.rb +4 -4
- data/lib/io_streams/tabular/header.rb +18 -6
- data/lib/io_streams/tabular/parser/array.rb +0 -10
- data/lib/io_streams/tabular/parser/csv.rb +6 -38
- data/lib/io_streams/tabular/parser/fixed.rb +5 -5
- data/lib/io_streams/tabular/parser/psv.rb +0 -12
- data/lib/io_streams/tabular.rb +5 -10
- data/lib/io_streams/utils.rb +6 -8
- data/lib/io_streams/version.rb +1 -1
- data/lib/io_streams/writer.rb +6 -6
- data/lib/io_streams/xlsx/reader.rb +1 -1
- data/lib/io_streams/zip/writer.rb +22 -10
- data/lib/iostreams.rb +0 -1
- metadata +28 -113
- data/lib/io_streams/deprecated.rb +0 -216
- data/lib/io_streams/tabular/utility/csv_row.rb +0 -105
- data/test/builder_test.rb +0 -311
- data/test/bzip2_reader_test.rb +0 -27
- data/test/bzip2_writer_test.rb +0 -56
- data/test/deprecated_test.rb +0 -121
- data/test/encode_reader_test.rb +0 -51
- data/test/encode_writer_test.rb +0 -90
- data/test/files/embedded_lines_test.csv +0 -7
- data/test/files/multiple_files.zip +0 -0
- data/test/files/spreadsheet.xlsx +0 -0
- data/test/files/test.csv +0 -4
- data/test/files/test.json +0 -3
- data/test/files/test.psv +0 -4
- data/test/files/text file.txt +0 -3
- data/test/files/text.txt +0 -3
- data/test/files/text.txt.bz2 +0 -0
- data/test/files/text.txt.gz +0 -0
- data/test/files/text.txt.gz.zip +0 -0
- data/test/files/text.zip +0 -0
- data/test/files/text.zip.gz +0 -0
- data/test/files/unclosed_quote_large_test.csv +0 -1658
- data/test/files/unclosed_quote_test.csv +0 -4
- data/test/files/unclosed_quote_test2.csv +0 -3
- data/test/files/utf16_test.csv +0 -0
- data/test/gzip_reader_test.rb +0 -27
- data/test/gzip_writer_test.rb +0 -52
- data/test/io_streams_test.rb +0 -132
- data/test/line_reader_test.rb +0 -325
- data/test/line_writer_test.rb +0 -59
- data/test/minimal_file_reader.rb +0 -25
- data/test/path_test.rb +0 -55
- data/test/paths/file_test.rb +0 -202
- data/test/paths/http_test.rb +0 -34
- data/test/paths/matcher_test.rb +0 -120
- data/test/paths/s3_test.rb +0 -220
- data/test/paths/sftp_test.rb +0 -106
- data/test/pgp_reader_test.rb +0 -46
- data/test/pgp_test.rb +0 -254
- data/test/pgp_writer_test.rb +0 -130
- data/test/record_reader_test.rb +0 -60
- data/test/record_writer_test.rb +0 -82
- data/test/row_reader_test.rb +0 -35
- data/test/row_writer_test.rb +0 -56
- data/test/stream_test.rb +0 -574
- data/test/tabular_test.rb +0 -338
- data/test/test_helper.rb +0 -40
- data/test/utils_test.rb +0 -20
- data/test/xlsx_reader_test.rb +0 -37
- data/test/zip_reader_test.rb +0 -53
- data/test/zip_writer_test.rb +0 -48
data/test/builder_test.rb
DELETED
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
require_relative "test_helper"
|
|
2
|
-
|
|
3
|
-
class BuilderTest < Minitest::Test
|
|
4
|
-
describe IOStreams::Builder do
|
|
5
|
-
let(:file_name) { "my/path/abc.bcd.xlsx.zip.gz.pgp" }
|
|
6
|
-
let(:streams) { IOStreams::Builder.new(file_name) }
|
|
7
|
-
|
|
8
|
-
describe "#option" do
|
|
9
|
-
it "adds one option" do
|
|
10
|
-
streams.option(:pgp, passphrase: "unlock-me")
|
|
11
|
-
assert_equal({pgp: {passphrase: "unlock-me"}}, streams.options)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
it "adds options in order" do
|
|
15
|
-
streams.option(:pgp, passphrase: "unlock-me")
|
|
16
|
-
streams.option(:enc, compress: false)
|
|
17
|
-
assert_equal({pgp: {passphrase: "unlock-me"}, enc: {compress: false}}, streams.options)
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
it "will not add an option if a stream was already set" do
|
|
21
|
-
streams.stream(:pgp, passphrase: "unlock-me")
|
|
22
|
-
assert_raises ArgumentError do
|
|
23
|
-
streams.option(:pgp, passphrase: "unlock-me")
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
it "will not add an invalid option" do
|
|
28
|
-
assert_raises ArgumentError do
|
|
29
|
-
streams.option(:blah, value: 23)
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
describe "with no file_name" do
|
|
34
|
-
let(:file_name) { nil }
|
|
35
|
-
|
|
36
|
-
it "prevents options being set" do
|
|
37
|
-
assert_raises ArgumentError do
|
|
38
|
-
streams.option(:pgp, passphrase: "unlock-me")
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
describe "#format" do
|
|
45
|
-
it "detects the format from the file name" do
|
|
46
|
-
streams = IOStreams::Builder.new("abc.json")
|
|
47
|
-
assert_equal :json, streams.format
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
it "is nil if the file name has no meaningful format" do
|
|
51
|
-
assert_nil streams.format
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
it "returns set format with no file_name" do
|
|
55
|
-
streams = IOStreams::Builder.new
|
|
56
|
-
streams.format = :csv
|
|
57
|
-
assert_equal :csv, streams.format
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
it "returns set format with file_name" do
|
|
61
|
-
streams = IOStreams::Builder.new("abc.json")
|
|
62
|
-
streams.format = :csv
|
|
63
|
-
assert_equal :csv, streams.format
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
it "validates bad format" do
|
|
67
|
-
assert_raises ArgumentError do
|
|
68
|
-
streams.format = :blah
|
|
69
|
-
end
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
describe "#stream" do
|
|
74
|
-
it "adds one stream" do
|
|
75
|
-
streams.stream(:pgp, passphrase: "unlock-me")
|
|
76
|
-
assert_equal({pgp: {passphrase: "unlock-me"}}, streams.streams)
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
it "adds streams in order" do
|
|
80
|
-
streams.stream(:pgp, passphrase: "unlock-me")
|
|
81
|
-
streams.stream(:enc, compress: false)
|
|
82
|
-
assert_equal({pgp: {passphrase: "unlock-me"}, enc: {compress: false}}, streams.streams)
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
it "will not add a stream if an option was already set" do
|
|
86
|
-
streams.option(:pgp, passphrase: "unlock-me")
|
|
87
|
-
assert_raises ArgumentError do
|
|
88
|
-
streams.stream(:pgp, passphrase: "unlock-me")
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
it "will not add an invalid stream" do
|
|
93
|
-
assert_raises ArgumentError do
|
|
94
|
-
streams.stream(:blah, value: 23)
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
describe "#reader" do
|
|
100
|
-
let :gzip_string do
|
|
101
|
-
io_string = StringIO.new("".b)
|
|
102
|
-
IOStreams::Gzip::Writer.stream(io_string) do |io|
|
|
103
|
-
io.write("Hello World")
|
|
104
|
-
end
|
|
105
|
-
io_string.string
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
it "directly calls block for an empty stream" do
|
|
109
|
-
string_io = StringIO.new
|
|
110
|
-
value = nil
|
|
111
|
-
streams.stream(:none)
|
|
112
|
-
streams.reader(string_io) do |io|
|
|
113
|
-
assert_equal io, string_io
|
|
114
|
-
value = 32
|
|
115
|
-
end
|
|
116
|
-
assert_equal 32, value
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
it "returns the reader" do
|
|
120
|
-
string_io = StringIO.new(gzip_string)
|
|
121
|
-
streams.stream(:gz)
|
|
122
|
-
streams.reader(string_io) do |io|
|
|
123
|
-
assert io.is_a?(::Zlib::GzipReader), io
|
|
124
|
-
end
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
it "returns the last reader" do
|
|
128
|
-
string_io = StringIO.new(gzip_string)
|
|
129
|
-
streams.stream(:encode)
|
|
130
|
-
streams.stream(:gz)
|
|
131
|
-
streams.reader(string_io) do |io|
|
|
132
|
-
assert io.is_a?(IOStreams::Encode::Reader), io
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
describe "#writer" do
|
|
138
|
-
it "directly calls block for an empty stream" do
|
|
139
|
-
string_io = StringIO.new
|
|
140
|
-
value = nil
|
|
141
|
-
streams.stream(:none)
|
|
142
|
-
streams.writer(string_io) do |io|
|
|
143
|
-
assert_equal io, string_io
|
|
144
|
-
value = 32
|
|
145
|
-
end
|
|
146
|
-
assert_equal 32, value
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
it "returns the reader" do
|
|
150
|
-
string_io = StringIO.new
|
|
151
|
-
streams.stream(:zip)
|
|
152
|
-
streams.writer(string_io) do |io|
|
|
153
|
-
assert io.is_a?(ZipTricks::Streamer::Writable), io
|
|
154
|
-
end
|
|
155
|
-
end
|
|
156
|
-
|
|
157
|
-
it "returns the last reader" do
|
|
158
|
-
string_io = StringIO.new
|
|
159
|
-
streams.stream(:encode)
|
|
160
|
-
streams.stream(:zip)
|
|
161
|
-
streams.writer(string_io) do |io|
|
|
162
|
-
assert io.is_a?(IOStreams::Encode::Writer), io
|
|
163
|
-
end
|
|
164
|
-
end
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
# Internal methods
|
|
168
|
-
|
|
169
|
-
describe "#class_for_stream" do
|
|
170
|
-
it "xlsx" do
|
|
171
|
-
assert_equal IOStreams::Xlsx::Reader, streams.send(:class_for_stream, :reader, :xlsx)
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
it "gzip" do
|
|
175
|
-
assert_equal IOStreams::Gzip::Writer, streams.send(:class_for_stream, :writer, :gzip)
|
|
176
|
-
end
|
|
177
|
-
|
|
178
|
-
it "unknown" do
|
|
179
|
-
assert_raises ArgumentError do
|
|
180
|
-
streams.send(:class_for_stream, :reader, :unknown)
|
|
181
|
-
end
|
|
182
|
-
end
|
|
183
|
-
end
|
|
184
|
-
|
|
185
|
-
describe "#parse_extensions" do
|
|
186
|
-
it "single stream" do
|
|
187
|
-
streams = IOStreams::Builder.new("my/path/abc.xlsx")
|
|
188
|
-
assert_equal %i[xlsx], streams.send(:parse_extensions)
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
it "empty" do
|
|
192
|
-
streams = IOStreams::Builder.new("my/path/abc.csv")
|
|
193
|
-
assert_equal [], streams.send(:parse_extensions)
|
|
194
|
-
end
|
|
195
|
-
|
|
196
|
-
it "handles multiple extensions" do
|
|
197
|
-
assert_equal %i[xlsx zip gz pgp], streams.send(:parse_extensions)
|
|
198
|
-
end
|
|
199
|
-
|
|
200
|
-
describe "case-insensitive" do
|
|
201
|
-
let(:file_name) { "a.XlsX.GzIp" }
|
|
202
|
-
|
|
203
|
-
it "is case-insensitive" do
|
|
204
|
-
assert_equal %i[xlsx gzip], streams.send(:parse_extensions)
|
|
205
|
-
end
|
|
206
|
-
end
|
|
207
|
-
end
|
|
208
|
-
|
|
209
|
-
describe "#pipeline" do
|
|
210
|
-
it "with stream and file name" do
|
|
211
|
-
expected = {enc: {compress: false}}
|
|
212
|
-
streams.stream(:enc, compress: false)
|
|
213
|
-
assert_equal expected, streams.pipeline
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
it "no file name, streams, or options" do
|
|
217
|
-
expected = {}
|
|
218
|
-
streams = IOStreams::Builder.new
|
|
219
|
-
assert_equal expected, streams.pipeline
|
|
220
|
-
end
|
|
221
|
-
|
|
222
|
-
it "file name without options" do
|
|
223
|
-
expected = {xlsx: {}, zip: {}, gz: {}, pgp: {}}
|
|
224
|
-
assert_equal expected, streams.pipeline
|
|
225
|
-
end
|
|
226
|
-
|
|
227
|
-
it "file name with encode option" do
|
|
228
|
-
expected = {encode: {encoding: "BINARY"}, xlsx: {}, zip: {}, gz: {}, pgp: {}}
|
|
229
|
-
streams.option(:encode, encoding: "BINARY")
|
|
230
|
-
assert_equal expected, streams.pipeline
|
|
231
|
-
end
|
|
232
|
-
|
|
233
|
-
it "file name with option" do
|
|
234
|
-
expected = {xlsx: {}, zip: {}, gz: {}, pgp: {passphrase: "unlock-me"}}
|
|
235
|
-
streams.option(:pgp, passphrase: "unlock-me")
|
|
236
|
-
assert_equal expected, streams.pipeline
|
|
237
|
-
end
|
|
238
|
-
end
|
|
239
|
-
|
|
240
|
-
describe "#remove_from_pipeline" do
|
|
241
|
-
let(:file_name) { "my/path/abc.bz2.pgp" }
|
|
242
|
-
it "removes a named stream from the pipeline" do
|
|
243
|
-
assert_equal({bz2: {}, pgp: {}}, streams.pipeline)
|
|
244
|
-
streams.remove_from_pipeline(:bz2)
|
|
245
|
-
assert_equal({pgp: {}}, streams.pipeline)
|
|
246
|
-
end
|
|
247
|
-
it "removes a named stream from the pipeline with options" do
|
|
248
|
-
streams.option(:pgp, passphrase: "unlock-me")
|
|
249
|
-
assert_equal({bz2: {}, pgp: {passphrase: "unlock-me"}}, streams.pipeline)
|
|
250
|
-
streams.remove_from_pipeline(:bz2)
|
|
251
|
-
assert_equal({pgp: {passphrase: "unlock-me"}}, streams.pipeline)
|
|
252
|
-
end
|
|
253
|
-
end
|
|
254
|
-
|
|
255
|
-
describe "#execute" do
|
|
256
|
-
it "directly calls block for an empty stream" do
|
|
257
|
-
string_io = StringIO.new
|
|
258
|
-
value = nil
|
|
259
|
-
streams.send(:execute, :writer, {}, string_io) do |io|
|
|
260
|
-
assert_equal io, string_io
|
|
261
|
-
value = 32
|
|
262
|
-
end
|
|
263
|
-
assert_equal 32, value
|
|
264
|
-
end
|
|
265
|
-
|
|
266
|
-
it "calls last block in one element stream" do
|
|
267
|
-
pipeline = {simple: {arg: "first"}}
|
|
268
|
-
string_io = StringIO.new
|
|
269
|
-
streams.send(:execute, :writer, pipeline, string_io) { |io| io.write("last") }
|
|
270
|
-
assert_equal "first>last", string_io.string
|
|
271
|
-
end
|
|
272
|
-
|
|
273
|
-
it "chains blocks in 2 element stream" do
|
|
274
|
-
pipeline = {simple: {arg: "first"}, simple2: {arg: "second"}}
|
|
275
|
-
string_io = StringIO.new
|
|
276
|
-
streams.send(:execute, :writer, pipeline, string_io) { |io| io.write("last") }
|
|
277
|
-
assert_equal "second>first>last", string_io.string
|
|
278
|
-
end
|
|
279
|
-
|
|
280
|
-
it "chains blocks in 3 element stream" do
|
|
281
|
-
pipeline = {simple: {arg: "first"}, simple2: {arg: "second"}, simple3: {arg: "third"}}
|
|
282
|
-
string_io = StringIO.new
|
|
283
|
-
streams.send(:execute, :writer, pipeline, string_io) { |io| io.write("last") }
|
|
284
|
-
assert_equal "third>second>first>last", string_io.string
|
|
285
|
-
end
|
|
286
|
-
end
|
|
287
|
-
|
|
288
|
-
class SimpleStream
|
|
289
|
-
def self.stream(io, **args)
|
|
290
|
-
yield new(io, **args)
|
|
291
|
-
end
|
|
292
|
-
|
|
293
|
-
def self.open(file_name_or_io, **args, &block)
|
|
294
|
-
file_name_or_io.is_a?(String) ? file(file_name_or_io, **args, &block) : stream(file_name_or_io, **args, &block)
|
|
295
|
-
end
|
|
296
|
-
|
|
297
|
-
def initialize(io, arg:)
|
|
298
|
-
@io = io
|
|
299
|
-
@arg = arg
|
|
300
|
-
end
|
|
301
|
-
|
|
302
|
-
def write(data)
|
|
303
|
-
@io.write("#{@arg}>#{data}")
|
|
304
|
-
end
|
|
305
|
-
end
|
|
306
|
-
|
|
307
|
-
IOStreams.register_extension(:simple, nil, SimpleStream)
|
|
308
|
-
IOStreams.register_extension(:simple2, nil, SimpleStream)
|
|
309
|
-
IOStreams.register_extension(:simple3, nil, SimpleStream)
|
|
310
|
-
end
|
|
311
|
-
end
|
data/test/bzip2_reader_test.rb
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
require_relative "test_helper"
|
|
2
|
-
|
|
3
|
-
class Bzip2ReaderTest < Minitest::Test
|
|
4
|
-
describe IOStreams::Bzip2::Reader do
|
|
5
|
-
let :file_name do
|
|
6
|
-
File.join(File.dirname(__FILE__), "files", "text.txt.bz2")
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
let :decompressed do
|
|
10
|
-
File.read(File.join(File.dirname(__FILE__), "files", "text.txt"))
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
describe ".file" do
|
|
14
|
-
it "file" do
|
|
15
|
-
result = IOStreams::Bzip2::Reader.file(file_name, &:read)
|
|
16
|
-
assert_equal decompressed, result
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
it "stream" do
|
|
20
|
-
result = File.open(file_name) do |file|
|
|
21
|
-
IOStreams::Bzip2::Reader.stream(file, &:read)
|
|
22
|
-
end
|
|
23
|
-
assert_equal decompressed, result
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
data/test/bzip2_writer_test.rb
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
require_relative "test_helper"
|
|
2
|
-
|
|
3
|
-
class Bzip2WriterTest < Minitest::Test
|
|
4
|
-
describe IOStreams::Bzip2::Writer do
|
|
5
|
-
let :temp_file do
|
|
6
|
-
Tempfile.new("iostreams")
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
let :file_name do
|
|
10
|
-
temp_file.path
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
let :decompressed do
|
|
14
|
-
File.read(File.join(File.dirname(__FILE__), "files", "text.txt"))
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
after do
|
|
18
|
-
temp_file.delete
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
describe ".file" do
|
|
22
|
-
it "file" do
|
|
23
|
-
result =
|
|
24
|
-
IOStreams::Bzip2::Writer.file(file_name) do |io|
|
|
25
|
-
io.write(decompressed)
|
|
26
|
-
io.write(decompressed)
|
|
27
|
-
53534
|
|
28
|
-
end
|
|
29
|
-
assert_equal 53534, result
|
|
30
|
-
|
|
31
|
-
File.open(file_name, "rb") do |file|
|
|
32
|
-
io = ::Bzip2::FFI::Reader.new(file)
|
|
33
|
-
result = io.read
|
|
34
|
-
temp_file.delete
|
|
35
|
-
assert_equal decompressed + decompressed, result
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
it "stream" do
|
|
40
|
-
io_string = StringIO.new("".b)
|
|
41
|
-
result =
|
|
42
|
-
IOStreams::Bzip2::Writer.stream(io_string) do |io|
|
|
43
|
-
io.write(decompressed)
|
|
44
|
-
io.write(decompressed)
|
|
45
|
-
53534
|
|
46
|
-
end
|
|
47
|
-
assert_equal 53534, result
|
|
48
|
-
|
|
49
|
-
io = StringIO.new(io_string.string)
|
|
50
|
-
rbzip2 = ::Bzip2::FFI::Reader.new(io)
|
|
51
|
-
data = rbzip2.read
|
|
52
|
-
assert_equal decompressed + decompressed, data
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
end
|
data/test/deprecated_test.rb
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
require_relative "test_helper"
|
|
2
|
-
|
|
3
|
-
IOStreams.include(IOStreams::Deprecated)
|
|
4
|
-
|
|
5
|
-
# Test deprecated api
|
|
6
|
-
class DeprecatedTest < Minitest::Test
|
|
7
|
-
describe IOStreams do
|
|
8
|
-
let :source_file_name do
|
|
9
|
-
File.join(__dir__, "files", "text.txt")
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
let :data do
|
|
13
|
-
File.read(source_file_name)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
let :temp_file do
|
|
17
|
-
Tempfile.new("iostreams")
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
let :target_file_name do
|
|
21
|
-
temp_file.path
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
let :bad_data do
|
|
25
|
-
[
|
|
26
|
-
"New M\xE9xico,NE".b,
|
|
27
|
-
"good line",
|
|
28
|
-
"New M\xE9xico,\x07SF".b
|
|
29
|
-
].join("\n").encode("BINARY")
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
let :stripped_data do
|
|
33
|
-
bad_data.gsub("\xE9".b, "").gsub("\x07", "")
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
let :multiple_zip_file_name do
|
|
37
|
-
File.join(File.dirname(__FILE__), "files", "multiple_files.zip")
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
let :zip_gz_file_name do
|
|
41
|
-
File.join(File.dirname(__FILE__), "files", "text.zip.gz")
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
let :contents_test_txt do
|
|
45
|
-
File.read(File.join(File.dirname(__FILE__), "files", "text.txt"))
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
let :contents_test_json do
|
|
49
|
-
File.read(File.join(File.dirname(__FILE__), "files", "test.json"))
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
after do
|
|
53
|
-
temp_file.delete
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
describe ".copy" do
|
|
57
|
-
it "streams" do
|
|
58
|
-
size = IOStreams.reader(source_file_name) do |source_stream|
|
|
59
|
-
IOStreams.writer(target_file_name) do |target_stream|
|
|
60
|
-
IOStreams.copy(source_stream, target_stream)
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
actual = File.read(target_file_name)
|
|
64
|
-
|
|
65
|
-
assert_equal actual, data
|
|
66
|
-
assert_equal actual.size, size
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
it "IO stream" do
|
|
70
|
-
size = File.open(source_file_name) do |source_stream|
|
|
71
|
-
IOStreams.writer(target_file_name) do |target_stream|
|
|
72
|
-
IOStreams.copy(source_stream, target_stream)
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
actual = File.read(target_file_name)
|
|
76
|
-
|
|
77
|
-
assert_equal actual, data
|
|
78
|
-
assert_equal actual.size, size
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
it "files" do
|
|
82
|
-
size = IOStreams.copy(source_file_name, target_file_name)
|
|
83
|
-
actual = File.read(target_file_name)
|
|
84
|
-
|
|
85
|
-
assert_equal actual, data
|
|
86
|
-
assert_equal actual.size, size
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
describe ".each_line" do
|
|
91
|
-
it "returns a line at a time" do
|
|
92
|
-
lines = []
|
|
93
|
-
count = IOStreams.each_line(source_file_name) { |line| lines << line }
|
|
94
|
-
assert_equal data.lines.map(&:strip), lines
|
|
95
|
-
assert_equal data.lines.count, count
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
it "strips non-printable characters" do
|
|
99
|
-
input = StringIO.new(bad_data)
|
|
100
|
-
lines = []
|
|
101
|
-
count = IOStreams.each_line(input, encoding: "UTF-8", encode_cleaner: :printable, encode_replace: "") do |line|
|
|
102
|
-
lines << line
|
|
103
|
-
end
|
|
104
|
-
assert_equal stripped_data.lines.map(&:strip), lines
|
|
105
|
-
assert_equal stripped_data.lines.count, count
|
|
106
|
-
end
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
describe ".reader" do
|
|
110
|
-
it "reads a zip file" do
|
|
111
|
-
result = IOStreams.reader(multiple_zip_file_name, streams: {zip: {entry_file_name: "test.json"}}, &:read)
|
|
112
|
-
assert_equal contents_test_json, result
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
it "reads a zip file from within a gz file" do
|
|
116
|
-
result = IOStreams.reader(zip_gz_file_name, &:read)
|
|
117
|
-
assert_equal contents_test_txt, result
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
end
|
|
121
|
-
end
|
data/test/encode_reader_test.rb
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
require_relative "test_helper"
|
|
2
|
-
|
|
3
|
-
class EncodeReaderTest < Minitest::Test
|
|
4
|
-
describe IOStreams::Encode::Reader do
|
|
5
|
-
let :bad_data do
|
|
6
|
-
[
|
|
7
|
-
"New M\xE9xico,NE".b,
|
|
8
|
-
"good line",
|
|
9
|
-
"New M\xE9xico,\x07SF".b
|
|
10
|
-
].join("\n").encode("BINARY")
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
let :cleansed_data do
|
|
14
|
-
bad_data.gsub("\xE9".b, "")
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
let :stripped_data do
|
|
18
|
-
cleansed_data.gsub("\x07", "")
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
describe "#read" do
|
|
22
|
-
describe "replacement" do
|
|
23
|
-
it "does not strip invalid characters" do
|
|
24
|
-
skip "Does not raise on JRuby" if defined?(JRuby)
|
|
25
|
-
input = StringIO.new(bad_data)
|
|
26
|
-
IOStreams::Encode::Reader.stream(input, encoding: "UTF-8") do |io|
|
|
27
|
-
assert_raises ::Encoding::UndefinedConversionError do
|
|
28
|
-
io.read.encoding
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
it "strips invalid characters" do
|
|
34
|
-
input = StringIO.new(bad_data)
|
|
35
|
-
data =
|
|
36
|
-
IOStreams::Encode::Reader.stream(input, encoding: "UTF-8", replace: "", &:read)
|
|
37
|
-
assert_equal cleansed_data, data
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
describe "printable" do
|
|
42
|
-
it "strips non-printable characters" do
|
|
43
|
-
input = StringIO.new(bad_data)
|
|
44
|
-
data =
|
|
45
|
-
IOStreams::Encode::Reader.stream(input, encoding: "UTF-8", cleaner: :printable, replace: "", &:read)
|
|
46
|
-
assert_equal stripped_data, data
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
end
|
data/test/encode_writer_test.rb
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
require_relative "test_helper"
|
|
2
|
-
|
|
3
|
-
class EncodeWriterTest < Minitest::Test
|
|
4
|
-
describe IOStreams::Encode::Writer do
|
|
5
|
-
let :bad_data do
|
|
6
|
-
[
|
|
7
|
-
"New M\xE9xico,NE".b,
|
|
8
|
-
"good line",
|
|
9
|
-
"New M\xE9xico,\x07SF".b
|
|
10
|
-
].join("\n").encode("BINARY")
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
let :cleansed_data do
|
|
14
|
-
bad_data.gsub("\xE9".b, "?")
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
let :stripped_data do
|
|
18
|
-
cleansed_data.gsub("\x07", "")
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
describe "#<<" do
|
|
22
|
-
it "file" do
|
|
23
|
-
temp_file = Tempfile.new("rocket_job")
|
|
24
|
-
file_name = temp_file.to_path
|
|
25
|
-
result =
|
|
26
|
-
IOStreams::Encode::Writer.file(file_name, encoding: "ASCII-8BIT") do |io|
|
|
27
|
-
io << bad_data
|
|
28
|
-
53534
|
|
29
|
-
end
|
|
30
|
-
assert_equal 53534, result
|
|
31
|
-
result = File.read(file_name, mode: "rb")
|
|
32
|
-
assert_equal bad_data, result
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
it "stream" do
|
|
36
|
-
io = StringIO.new("".b)
|
|
37
|
-
result =
|
|
38
|
-
IOStreams::Encode::Writer.stream(io, encoding: "ASCII-8BIT") do |encoded|
|
|
39
|
-
encoded << bad_data
|
|
40
|
-
53534
|
|
41
|
-
end
|
|
42
|
-
assert_equal 53534, result
|
|
43
|
-
assert_equal "ASCII-8BIT", io.string.encoding.to_s
|
|
44
|
-
assert_equal bad_data, io.string
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
it "stream as utf-8" do
|
|
48
|
-
io = StringIO.new("")
|
|
49
|
-
assert_raises Encoding::UndefinedConversionError do
|
|
50
|
-
IOStreams::Encode::Writer.stream(io, encoding: "UTF-8") do |encoded|
|
|
51
|
-
encoded << bad_data
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
it "stream as utf-8 with replacement" do
|
|
57
|
-
io = StringIO.new("")
|
|
58
|
-
IOStreams::Encode::Writer.stream(io, encoding: "UTF-8", replace: "?") do |encoded|
|
|
59
|
-
encoded << bad_data
|
|
60
|
-
end
|
|
61
|
-
assert_equal "UTF-8", io.string.encoding.to_s
|
|
62
|
-
assert_equal cleansed_data, io.string
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
it "stream as utf-8 with replacement and printable cleansing" do
|
|
66
|
-
io = StringIO.new("")
|
|
67
|
-
IOStreams::Encode::Writer.stream(io, encoding: "UTF-8", replace: "?", cleaner: :printable) do |encoded|
|
|
68
|
-
encoded << bad_data
|
|
69
|
-
end
|
|
70
|
-
assert_equal "UTF-8", io.string.encoding.to_s
|
|
71
|
-
assert_equal stripped_data, io.string
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
describe ".write" do
|
|
76
|
-
it "returns byte count" do
|
|
77
|
-
io_string = StringIO.new("".b)
|
|
78
|
-
count = 0
|
|
79
|
-
result =
|
|
80
|
-
IOStreams::Encode::Writer.stream(io_string, encoding: "ASCII-8BIT") do |io|
|
|
81
|
-
count += io.write(bad_data)
|
|
82
|
-
53534
|
|
83
|
-
end
|
|
84
|
-
assert_equal 53534, result
|
|
85
|
-
assert_equal bad_data, io_string.string
|
|
86
|
-
assert_equal bad_data.size, count
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
end
|
|
Binary file
|
data/test/files/spreadsheet.xlsx
DELETED
|
Binary file
|
data/test/files/test.csv
DELETED
data/test/files/test.json
DELETED
data/test/files/test.psv
DELETED
data/test/files/text file.txt
DELETED
data/test/files/text.txt
DELETED
data/test/files/text.txt.bz2
DELETED
|
Binary file
|