public
Description: A process execution library which doesn't suck.
Clone URL: git://github.com/codahale/ropen.git
Search Repo:
Added the ability to spool data for stdin before a command is run.
codahale (author)
Thu May 15 14:28:31 -0700 2008
commit  4a6af63bf322aec294a84b833ec842730fa008f8
tree    4fb337e75e6ba2c0991c387dd7099fd2f37d6547
parent  04fc99c893e0666254071fbee53be27206aa8652
...
1
2
3
 
 
4
5
6
...
15
16
17
 
18
19
20
...
30
31
32
 
33
34
35
...
38
39
40
41
 
42
43
44
...
1
 
2
3
4
5
6
7
...
16
17
18
19
20
21
22
...
32
33
34
35
36
37
38
...
41
42
43
 
44
45
46
47
0
@@ -1,6 +1,7 @@
0
 require "ropen"
0
-require "ropen/pipe"
0
 require "ropen/events"
0
+require "ropen/pipe"
0
+require "ropen/spool"
0
 
0
 # TODO: document me
0
 
0
@@ -15,6 +16,7 @@
0
     end
0
     @stdout_events = Ropen::Events.new
0
     @stderr_events = Ropen::Events.new
0
+ @stdin_spool = Ropen::Spool.new
0
     yield self if block_given?
0
   end
0
   
0
@@ -30,6 +32,7 @@
0
     @stdin_io = stdin
0
     @stdout_events.run(stdout)
0
     @stderr_events.run(stderr)
0
+ @stdin_spool.replay(stdin)
0
     [@stdout_events, @stderr_events].each { |e| e.finish }
0
     Process.waitpid(pid)
0
     return @exit_status = $?
0
@@ -38,7 +41,7 @@
0
   end
0
   
0
   def stdin
0
- @stdin_io
0
+ @stdin_io || @stdin_spool
0
   end
0
   
0
   def stdout
...
 
 
 
 
 
 
...
1
2
3
4
5
6
0
@@ -1 +1,7 @@
0
+#!/usr/bin/env ruby
0
+STDIN.each_line do |line|
0
+ STDERR.puts "Input: #{line.inspect}"
0
+ STDOUT.puts line.chomp.upcase
0
+end
0
+exit(2)
...
8
9
10
 
11
12
13
...
64
65
66
67
 
68
69
70
...
86
87
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
90
91
...
8
9
10
11
12
13
14
...
65
66
67
 
68
69
70
71
...
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
0
@@ -8,6 +8,7 @@
0
     @prints_stdout = fixture(:prints_stdout)
0
     @exits_with_error = fixture(:exits_with_error)
0
     @asks_for_name = fixture(:asks_for_name)
0
+ @processes_data = fixture(:processes_data)
0
   end
0
   
0
   describe "initializing" do
0
@@ -64,7 +65,7 @@
0
     
0
   end
0
   
0
- describe "running an executable which requires input" do
0
+ describe "running an executable which requires input in response to something" do
0
     
0
     before(:each) do
0
       @cmd = Ropen::Command.new(@asks_for_name)
0
@@ -86,6 +87,36 @@
0
     it "should timeout after a specified period of waiting for input" do
0
       pending("timeout support")
0
 # lambda { @cmd.run }.should raise_error(Ropen::TimeoutError)
0
+ end
0
+
0
+ end
0
+
0
+ describe "running an execuable which requires input first" do
0
+
0
+ before(:each) do
0
+ @cmd = Ropen::Command.new(@processes_data)
0
+ end
0
+
0
+ it "should write input to the app before any output is recorded" do
0
+ stdout_lines = ""
0
+ @cmd.stdout.on_output do |line|
0
+ stdout_lines << line
0
+ end
0
+
0
+ stderr_lines = ""
0
+ @cmd.stderr.on_output do |line|
0
+ stderr_lines << line
0
+ end
0
+
0
+ @cmd.stdin.puts "test1"
0
+ @cmd.stdin.puts "test2"
0
+ @cmd.stdin.puts "test3"
0
+ @cmd.stdin.close
0
+
0
+ @cmd.run.exitstatus.should == 2
0
+
0
+ stdout_lines.split("\n").should == ["TEST1", "TEST2", "TEST3"]
0
+ stderr_lines.split("\n").should == ["Input: \"test1\\n\"", "Input: \"test2\\n\"", "Input: \"test3\\n\""]
0
     end
0
     
0
   end

Comments

    No one has commented yet.