em-systemcommand 0.0.2 → 0.0.3
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/lib/em-systemcommand/pipe.rb +42 -23
- data/lib/em-systemcommand/version.rb +1 -1
- data/lib/em-systemcommand.rb +2 -2
- data/spec/em-systemcommand/pipe_spec.rb +16 -3
- data/spec/em-systmcommand_spec.rb +25 -3
- metadata +11 -11
|
@@ -59,31 +59,50 @@ module EventMachine
|
|
|
59
59
|
callback.call data.dup
|
|
60
60
|
end
|
|
61
61
|
end
|
|
62
|
-
@
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
62
|
+
@linebuffer ||= []
|
|
63
|
+
@cr_offset = 0
|
|
64
|
+
|
|
65
|
+
parse_crlf data
|
|
66
|
+
|
|
67
|
+
receive_update @outputbuffer.string unless recursive
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def parse_cr data, ix
|
|
71
|
+
ln = (@linebuffer << data[0...ix]).join
|
|
72
|
+
@linebuffer.clear
|
|
73
|
+
receive_line ln
|
|
74
|
+
@outputbuffer.print ln
|
|
75
|
+
@outputbuffer.pos = @cr_offset
|
|
76
|
+
parse_crlf data[(ix+1)..-1] # receive rest data
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def parse_lf data, ix
|
|
80
|
+
ln = (@linebuffer << data[0...ix]).join
|
|
81
|
+
@linebuffer.clear
|
|
82
|
+
ln.chomp!
|
|
83
|
+
receive_line ln
|
|
84
|
+
@outputbuffer.print ln
|
|
85
|
+
@outputbuffer.pos = @outputbuffer.length
|
|
86
|
+
@outputbuffer.puts
|
|
87
|
+
@cr_offset = @outputbuffer.pos
|
|
88
|
+
parse_crlf data[(ix+1)..-1] # receive rest data
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def parse_crlf data
|
|
92
|
+
if ilf = data.index("\n")
|
|
93
|
+
# if we find a LF and that LF is after a CR we first handle
|
|
94
|
+
# the CR
|
|
95
|
+
if icr = data.index("\r") and ilf != (icr+1) and icr < ilf
|
|
96
|
+
parse_cr data, icr
|
|
80
97
|
else
|
|
81
|
-
|
|
98
|
+
parse_lf data, ilf
|
|
82
99
|
end
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
100
|
+
else
|
|
101
|
+
if icr = data.index("\r")
|
|
102
|
+
parse_cr data, icr
|
|
103
|
+
else
|
|
104
|
+
@linebuffer << data
|
|
105
|
+
@outputbuffer.print data
|
|
87
106
|
end
|
|
88
107
|
end
|
|
89
108
|
end
|
data/lib/em-systemcommand.rb
CHANGED
|
@@ -2,6 +2,19 @@ require 'spec_helper'
|
|
|
2
2
|
|
|
3
3
|
describe 'Pipe' do
|
|
4
4
|
|
|
5
|
+
context '#receive_data' do
|
|
6
|
+
it 'should correctly do a carriage return in the output buffer' do
|
|
7
|
+
EM.run do
|
|
8
|
+
EM::SystemCommand.new %q{ruby -e '$stdout.sync = true; print "12345\r"; puts "321"; print "123"; print "\r3210"; exit 0;'} do |on|
|
|
9
|
+
on.success do |process|
|
|
10
|
+
EM.stop_event_loop
|
|
11
|
+
process.stdout.output.should == "32145\n3210"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
5
18
|
context '#data callback' do
|
|
6
19
|
|
|
7
20
|
it 'should be called on data' do
|
|
@@ -58,7 +71,7 @@ describe 'Pipe' do
|
|
|
58
71
|
it 'should be called on carriage return' do
|
|
59
72
|
received = []
|
|
60
73
|
EM.run do
|
|
61
|
-
EM::SystemCommand.new
|
|
74
|
+
EM::SystemCommand.new %q{ruby -e '$stdout.sync = true; print "123\r"; print "456\r"; exit 0;'} do |on|
|
|
62
75
|
on.stdout.line do |data|
|
|
63
76
|
received << data
|
|
64
77
|
end
|
|
@@ -94,7 +107,7 @@ describe 'Pipe' do
|
|
|
94
107
|
it 'should be called on carriage return'do
|
|
95
108
|
received = []
|
|
96
109
|
EM.run do
|
|
97
|
-
EM::SystemCommand.new %q{ruby -e '$stdout.sync = true; print "123\r"; print "456\r"; exit 0;'} do |on|
|
|
110
|
+
EM::SystemCommand.new %q{ruby -e '$stdout.sync = true; print "123\r"; sleep 0.1; print "456\r"; exit 0;'} do |on|
|
|
98
111
|
on.stdout.update do |data|
|
|
99
112
|
received << data
|
|
100
113
|
end
|
|
@@ -130,7 +143,7 @@ describe 'Pipe' do
|
|
|
130
143
|
it 'should match in output buffer on receive_update' do
|
|
131
144
|
received = []
|
|
132
145
|
EM.run do
|
|
133
|
-
EM::SystemCommand.new %q{ruby -e '$stdout.sync = true; print "-123-\r"; print "-456-\r"; exit 0'} do |on|
|
|
146
|
+
EM::SystemCommand.new %q{ruby -e '$stdout.sync = true; print "-123-\r"; sleep 0.1; print "-456-\r"; exit 0'} do |on|
|
|
134
147
|
on.stdout.match /-([0-9]+)-/, in: :output do |match, number|
|
|
135
148
|
received << number
|
|
136
149
|
end
|
|
@@ -2,10 +2,10 @@ require 'spec_helper'
|
|
|
2
2
|
|
|
3
3
|
describe EM::SystemCommand do
|
|
4
4
|
|
|
5
|
-
it 'should
|
|
5
|
+
it 'should call a success callback when process succeeds' do
|
|
6
6
|
called = false
|
|
7
7
|
EM.run do
|
|
8
|
-
EM::SystemCommand.new '
|
|
8
|
+
EM::SystemCommand.new 'exit 0;' do |on|
|
|
9
9
|
on.success do |ps|
|
|
10
10
|
called = true
|
|
11
11
|
end
|
|
@@ -17,7 +17,18 @@ describe EM::SystemCommand do
|
|
|
17
17
|
end
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
-
it 'should take a
|
|
20
|
+
it 'should take a success callback with process as parameter' do
|
|
21
|
+
EM.run do
|
|
22
|
+
EM::SystemCommand.new 'exit 0;' do |on|
|
|
23
|
+
on.success do |ps|
|
|
24
|
+
EM.stop_event_loop
|
|
25
|
+
ps.should be_a EM::SystemCommand
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'should call a failure callback when process fails' do
|
|
21
32
|
called = false
|
|
22
33
|
EM.run do
|
|
23
34
|
EM::SystemCommand.new 'echo "123"; exit 1;' do |on|
|
|
@@ -32,6 +43,17 @@ describe EM::SystemCommand do
|
|
|
32
43
|
end
|
|
33
44
|
end
|
|
34
45
|
|
|
46
|
+
it 'should take a failure callback with process as parameter' do
|
|
47
|
+
EM.run do
|
|
48
|
+
EM::SystemCommand.new 'exit 1;' do |on|
|
|
49
|
+
on.failure do |ps|
|
|
50
|
+
EM.stop_event_loop
|
|
51
|
+
ps.should be_a EM::SystemCommand
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
35
57
|
it 'should have stdin pipe' do
|
|
36
58
|
EM.run do
|
|
37
59
|
ps = EM::SystemCommand.new 'echo "123"; exit 1;'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: em-systemcommand
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.3
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -13,7 +13,7 @@ date: 2012-05-03 00:00:00.000000000 Z
|
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: rspec
|
|
16
|
-
requirement: &
|
|
16
|
+
requirement: &17785880 !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: *17785880
|
|
25
25
|
- !ruby/object:Gem::Dependency
|
|
26
26
|
name: guard
|
|
27
|
-
requirement: &
|
|
27
|
+
requirement: &17785460 !ruby/object:Gem::Requirement
|
|
28
28
|
none: false
|
|
29
29
|
requirements:
|
|
30
30
|
- - ! '>='
|
|
@@ -32,10 +32,10 @@ dependencies:
|
|
|
32
32
|
version: '0'
|
|
33
33
|
type: :development
|
|
34
34
|
prerelease: false
|
|
35
|
-
version_requirements: *
|
|
35
|
+
version_requirements: *17785460
|
|
36
36
|
- !ruby/object:Gem::Dependency
|
|
37
37
|
name: guard-rspec
|
|
38
|
-
requirement: &
|
|
38
|
+
requirement: &17785040 !ruby/object:Gem::Requirement
|
|
39
39
|
none: false
|
|
40
40
|
requirements:
|
|
41
41
|
- - ! '>='
|
|
@@ -43,10 +43,10 @@ dependencies:
|
|
|
43
43
|
version: '0'
|
|
44
44
|
type: :development
|
|
45
45
|
prerelease: false
|
|
46
|
-
version_requirements: *
|
|
46
|
+
version_requirements: *17785040
|
|
47
47
|
- !ruby/object:Gem::Dependency
|
|
48
48
|
name: eventmachine
|
|
49
|
-
requirement: &
|
|
49
|
+
requirement: &17784620 !ruby/object:Gem::Requirement
|
|
50
50
|
none: false
|
|
51
51
|
requirements:
|
|
52
52
|
- - ! '>='
|
|
@@ -54,7 +54,7 @@ dependencies:
|
|
|
54
54
|
version: '0'
|
|
55
55
|
type: :runtime
|
|
56
56
|
prerelease: false
|
|
57
|
-
version_requirements: *
|
|
57
|
+
version_requirements: *17784620
|
|
58
58
|
description: ! '`em-systemcommand` is a simple abstraction for invoking system commands
|
|
59
59
|
and handling their output easily with eventmachine.'
|
|
60
60
|
email:
|
|
@@ -93,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
93
93
|
version: '0'
|
|
94
94
|
segments:
|
|
95
95
|
- 0
|
|
96
|
-
hash:
|
|
96
|
+
hash: -941213243736516933
|
|
97
97
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
98
|
none: false
|
|
99
99
|
requirements:
|
|
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
102
102
|
version: '0'
|
|
103
103
|
segments:
|
|
104
104
|
- 0
|
|
105
|
-
hash:
|
|
105
|
+
hash: -941213243736516933
|
|
106
106
|
requirements: []
|
|
107
107
|
rubyforge_project:
|
|
108
108
|
rubygems_version: 1.8.17
|