magritte 0.5.6 → 0.5.7
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.
- data/README.md +3 -2
- data/lib/magritte.rb +3 -3
- data/lib/magritte/pipe.rb +4 -5
- data/spec/fixtures/no_input.rb +9 -0
- data/spec/fixtures/prisoner_of_zenda.txt +6737 -0
- data/spec/line_buffer_spec.rb +52 -0
- data/spec/pipe_spec.rb +100 -0
- metadata +14 -10
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'magritte/line_buffer'
|
2
|
+
|
3
|
+
describe Magritte::LineBuffer do
|
4
|
+
let(:buffer) { Magritte::LineBuffer.new }
|
5
|
+
|
6
|
+
context 'when writing' do
|
7
|
+
it 'returns 0 when no line separators were written' do
|
8
|
+
buffer.write("asdf").should == 0
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'returns the number bytes written including the last record separator' do
|
12
|
+
buffer.write("asdf\nfi").should == 5
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'handles more than one line at a time' do
|
16
|
+
buffer.write("asdf\nasdf\nfi").should == 10
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when reading' do
|
21
|
+
it 'only returns data that ends with a record separator - no partial records' do
|
22
|
+
buffer.write("asdf\nasdf\nfi")
|
23
|
+
buffer.buffer.should == "asdf\nasdf\n"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'when iterating' do
|
28
|
+
it 'returns each line one at a time' do
|
29
|
+
buffer.write("asdf\nasdf\n")
|
30
|
+
buffer.should have(2).items
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'does not return partial lines' do
|
34
|
+
buffer.write("asdf\nasdf\nfi")
|
35
|
+
buffer.map {|x| x}.should == %w{ asdf asdf }
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'clears the buffer once it has been iterated' do
|
39
|
+
buffer.write("asdf\nasdf\nfi")
|
40
|
+
|
41
|
+
buffer.map {|x| x}
|
42
|
+
buffer.should have(0).items
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'supports arbitrary record separators' do
|
46
|
+
buffer = Magritte::LineBuffer.new("\r\n")
|
47
|
+
buffer.write("asdf\r\nasdf\r\nasdf\r\n")
|
48
|
+
|
49
|
+
buffer.map {|x| x}.should == %w{ asdf asdf asdf }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/pipe_spec.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'magritte/pipe'
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
describe 'Magritte::Pipe' do
|
5
|
+
let(:infile) { File.join(File.dirname(__FILE__), 'fixtures/prisoner_of_zenda.txt') }
|
6
|
+
let(:output) { StringIO.new }
|
7
|
+
let(:no_input) { File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'no_input.rb')) }
|
8
|
+
|
9
|
+
context 'handling input' do
|
10
|
+
it 'opens a file when passed a filename' do
|
11
|
+
File.should_receive(:open).with(infile)
|
12
|
+
|
13
|
+
Magritte::Pipe.from_input_file(infile)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'creates a BlockStream' do
|
17
|
+
Magritte::BlockStream.should_receive(:new)
|
18
|
+
|
19
|
+
Magritte::Pipe.from_input_stream(StringIO.new)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'wraps a string in a StringIO' do
|
23
|
+
mock_stringio = mock('stringio')
|
24
|
+
StringIO.should_receive(:new).and_return(mock_stringio)
|
25
|
+
Magritte::BlockStream.should_receive(:new).with(mock_stringio)
|
26
|
+
|
27
|
+
Magritte::Pipe.from_input_string('asdf')
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'handles nil input strings' do
|
31
|
+
expect { Magritte::Pipe.from_input_string(nil).out_to { |x| x.size }.filtering_with('cat') }.not_to raise_error
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'handling output' do
|
36
|
+
it 'raises when no output has been set' do
|
37
|
+
expect { Magritte::Pipe.from_input_file(infile).filtering_with('cat') }.to \
|
38
|
+
raise_error(RuntimeError)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'calls a block for output when out_to is passed one' do
|
42
|
+
buffer = ""
|
43
|
+
input = "12345\n67890"
|
44
|
+
Magritte::Pipe.from_input_string(input).out_to { |p| buffer += p; p.size }.filtering_with('cat')
|
45
|
+
|
46
|
+
buffer.should == input
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'raises when the output Proc does not return a size value' do
|
50
|
+
expect { Magritte::Pipe.from_input_file(infile).out_to { |p| nil }.filtering_with('cat') }.to \
|
51
|
+
raise_error('output block must return number of bytes written!')
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'handles line-by-line output blocks' do
|
55
|
+
buffer = []
|
56
|
+
Magritte::Pipe.from_input_string("1234\n56789\n")
|
57
|
+
.line_by_line
|
58
|
+
.out_to { |p| buffer << p }.filtering_with('cat')
|
59
|
+
|
60
|
+
buffer.should == %w{ 1234 56789 }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'processing data' do
|
65
|
+
it 'raises when the sub-process exits inappropriately' do
|
66
|
+
expect { Magritte::Pipe.from_input_file(infile).out_to(output).filtering_with('bash -c "exit 1"') }.to \
|
67
|
+
raise_error(Errno::EPIPE)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'raises when the sub-process cannot receive data' do
|
71
|
+
expect { Magritte::Pipe.from_input_file(infile).out_to(output).filtering_with(no_input) }.to \
|
72
|
+
raise_error(Errno::EPIPE)
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'sends all input to the sub-process and out to the output stream' do
|
76
|
+
Magritte::Pipe.from_input_file(infile).out_to(output).filtering_with('cat')
|
77
|
+
output.string.split(/\n/).should have(6737).items
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'correctly handles sub-processes that buffer heavily' do
|
81
|
+
Magritte::Pipe.from_input_file(infile).out_to(output).filtering_with('sort')
|
82
|
+
output.string.split(/\n/).should have(6737).items
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'processes the data without any artifacts or other junk added to the stream' do
|
86
|
+
Magritte::Pipe.from_input_file(infile).out_to(output).filtering_with('cat')
|
87
|
+
output.string.should == File.read(infile)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'recovers when the end of the file was not a record separator' do
|
92
|
+
pending 'this not yet implemented'
|
93
|
+
buffer = []
|
94
|
+
Magritte::Pipe.from_input_string("1234\n56789")
|
95
|
+
.line_by_line
|
96
|
+
.out_to { |p| buffer << p }.filtering_with('cat')
|
97
|
+
|
98
|
+
buffer.should == %w{ 1234 56789 }
|
99
|
+
end
|
100
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magritte
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-06-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152647480 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152647480
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &2152640880 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: 2.0.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2152640880
|
36
36
|
description: ! ' Magritte is a simple but powerful wrapper to Open3 pipes that makes
|
37
37
|
it easy to handle two-way piping of data into and out of a sub-process. Various
|
38
38
|
input IO wrappers are supported and output can either be to an IO or to a block.
|
@@ -45,13 +45,17 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
+
- LICENSE
|
49
|
+
- README.md
|
50
|
+
- lib/magritte.rb
|
48
51
|
- lib/magritte/iostreams.rb
|
49
52
|
- lib/magritte/line_buffer.rb
|
50
53
|
- lib/magritte/pipe.rb
|
51
|
-
-
|
52
|
-
-
|
53
|
-
-
|
54
|
-
|
54
|
+
- spec/fixtures/no_input.rb
|
55
|
+
- spec/fixtures/prisoner_of_zenda.txt
|
56
|
+
- spec/line_buffer_spec.rb
|
57
|
+
- spec/pipe_spec.rb
|
58
|
+
homepage: https://github.com/mydrive/magritte
|
55
59
|
licenses: []
|
56
60
|
post_install_message:
|
57
61
|
rdoc_options: []
|