public
Description: A process execution library which doesn't suck.
Clone URL: git://github.com/codahale/ropen.git
commit  4a6af63bf322aec294a84b833ec842730fa008f8
tree    4fb337e75e6ba2c0991c387dd7099fd2f37d6547
parent  04fc99c893e0666254071fbee53be27206aa8652
ropen / spec / ropen / command_spec.rb
100644 124 lines (97 sloc) 3.47 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
123
124
require File.join(File.dirname(__FILE__), "..", "spec_helper")
 
require "ropen/command"
 
describe Ropen::Command do
  
  before(:each) do
    @prints_stdout = fixture(:prints_stdout)
    @exits_with_error = fixture(:exits_with_error)
    @asks_for_name = fixture(:asks_for_name)
    @processes_data = fixture(:processes_data)
  end
  
  describe "initializing" do
    before(:each) do
      @full_prints_stdout = File.expand_path(@prints_stdout)
    end
    
    it "should expand the path of the given executable" do
      File.should_receive(:expand_path).with(@prints_stdout).and_return(@full_prints_stdout)
      cmd = Ropen::Command.new(@prints_stdout)
      cmd.executable.should == @full_prints_stdout
    end
    
    it "should raise a Ropen::InvalidExecutableError if the executable doesn't exist" do
      lambda {
        Ropen::Command.new("bleepblorp")
      }.should raise_error(Ropen::InvalidExecutableError, "bleepblorp does not exist")
    end
    
    it "should accept a set of arguments" do
      cmd = Ropen::Command.new(@prints_stdout, "--color", "fleagle")
      cmd.arguments.should == ["--color", "fleagle"]
    end
    
    it "should allow for additional configuration via a block" do
      args = nil
      Ropen::Command.new(@prints_stdout, "--color", "fleagle") do |cmd|
        args = cmd.arguments
      end
      args.should == ["--color", "fleagle"]
    end
  end
  
  describe "running a simple executable" do
    
    before(:each) do
      @cmd = Ropen::Command.new(@exits_with_error)
    end
    
    it "should return the process' exit status" do
      @cmd.run.exitstatus.should == 1
      @cmd.exit_status.should_not be(:success)
    end
    
    it "should call events placed on output streams" do
      @cmd.stdout.on_output do |line|
        line.should == "This is stdout.\n"
      end
      @cmd.stderr.on_output do |line|
        line.should == "This is stderr.\n"
      end
      @cmd.run
    end
    
  end
  
  describe "running an executable which requires input in response to something" do
    
    before(:each) do
      @cmd = Ropen::Command.new(@asks_for_name)
    end
    
    it "should allow data to be written on stdin" do
      @cmd.stdout.on_output do |line|
        line.should == "You entered: MONGO\n"
      end
      @cmd.stderr.on_output do |line|
        if line =~ /Enter your name/
          @cmd.stdin.puts "MONGO"
          @cmd.stdin.flush
        end
      end
      @cmd.run
    end
    
    it "should timeout after a specified period of waiting for input" do
      pending("timeout support")
# lambda { @cmd.run }.should raise_error(Ropen::TimeoutError)
    end
    
  end
  
  describe "running an execuable which requires input first" do
    
    before(:each) do
      @cmd = Ropen::Command.new(@processes_data)
    end
    
    it "should write input to the app before any output is recorded" do
      stdout_lines = ""
      @cmd.stdout.on_output do |line|
        stdout_lines << line
      end
      
      stderr_lines = ""
      @cmd.stderr.on_output do |line|
        stderr_lines << line
      end
      
      @cmd.stdin.puts "test1"
      @cmd.stdin.puts "test2"
      @cmd.stdin.puts "test3"
      @cmd.stdin.close
      
      @cmd.run.exitstatus.should == 2
      
      stdout_lines.split("\n").should == ["TEST1", "TEST2", "TEST3"]
      stderr_lines.split("\n").should == ["Input: \"test1\\n\"", "Input: \"test2\\n\"", "Input: \"test3\\n\""]
    end
    
  end
  
end