556cbc7ec33bd60bf643dcf6ef26868e610716d8
[packages/precise/mcollective.git] / spec / unit / shell_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   describe Shell do
7     describe "#initialize" do
8       it "should set locale by default" do
9         s = Shell.new("date")
10         s.environment.should == {"LC_ALL" => "C"}
11       end
12
13       it "should merge environment and keep locale" do
14         s = Shell.new("date", :environment => {"foo" => "bar"})
15         s.environment.should == {"LC_ALL" => "C", "foo" => "bar"}
16       end
17
18       it "should allow locale to be overridden" do
19         s = Shell.new("date", :environment => {"LC_ALL" => "TEST", "foo" => "bar"})
20         s.environment.should == {"LC_ALL" => "TEST", "foo" => "bar"}
21       end
22
23       it "should set no environment when given nil" do
24         s = Shell.new("date", :environment => nil)
25         s.environment.should == {}
26       end
27
28       it "should save the command" do
29         s = Shell.new("date")
30         s.command.should == "date"
31       end
32
33       it "should check the cwd exist" do
34         expect {
35           s = Shell.new("date", :cwd => "/nonexistant")
36         }.to raise_error("Directory /nonexistant does not exist")
37       end
38
39       it "should warn of illegal stdin" do
40         expect {
41           s = Shell.new("date", :stdin => nil)
42         }.to raise_error("stdin should be a String")
43       end
44
45       it "should warn of illegal stdout" do
46         expect {
47           s = Shell.new("date", :stdout => nil)
48         }.to raise_error("stdout should support <<")
49       end
50
51       it "should warn of illegal stderr" do
52         expect {
53           s = Shell.new("date", :stderr => nil)
54         }.to raise_error("stderr should support <<")
55       end
56
57       it "should set stdout" do
58         s = Shell.new("date", :stdout => "stdout")
59         s.stdout.should == "stdout"
60       end
61
62       it "should set stderr" do
63         s = Shell.new("date", :stderr => "stderr")
64         s.stderr.should == "stderr"
65       end
66
67       it "should set stdin" do
68         s = Shell.new("date", :stdin => "hello world")
69         s.stdin.should == "hello world"
70       end
71     end
72
73     describe "#runcommand" do
74       it "should run the command" do
75         Shell.any_instance.stubs("systemu").returns(true).once.with("date", "stdout" => '', "stderr" => '', "env" => {"LC_ALL" => "C"}, 'cwd' => Dir.tmpdir)
76         s = Shell.new("date")
77         s.runcommand
78       end
79
80       it "should set stdin, stdout and status" do
81         s = Shell.new('ruby -e "STDERR.puts \"stderr\"; STDOUT.puts \"stdout\""')
82         s.runcommand
83         s.stdout.should == "stdout\n"
84         s.stderr.should == "stderr\n"
85         s.status.exitstatus.should == 0
86       end
87
88       it "should report correct exitcode" do
89         s = Shell.new('ruby -e "exit 1"')
90         s.runcommand
91
92         s.status.exitstatus.should == 1
93       end
94
95       it "shold have correct environment" do
96         s = Shell.new('ruby -e "puts ENV[\'LC_ALL\'];puts ENV[\'foo\'];"', :environment => {"foo" => "bar"})
97         s.runcommand
98         s.stdout.should == "C\nbar\n"
99       end
100
101       it "should save stdout in custom stdout variable" do
102         out = "STDOUT"
103
104         s = Shell.new('echo foo', :stdout => out)
105         s.runcommand
106
107         s.stdout.should == "STDOUTfoo\n"
108         out.should == "STDOUTfoo\n"
109       end
110
111       it "should save stderr in custom stderr variable" do
112         out = "STDERR"
113
114         s = Shell.new('ruby -e "STDERR.puts \"foo\""', :stderr => out)
115         s.runcommand
116
117         s.stderr.should == "STDERRfoo\n"
118         out.should == "STDERRfoo\n"
119       end
120
121       it "should run in the correct cwd" do
122         s = Shell.new('ruby -e "puts Dir.pwd"', :cwd => Dir.tmpdir)
123
124         s.runcommand
125
126         s.stdout.should == "#{Dir.tmpdir}\n"
127       end
128
129       it "should send the stdin" do
130         s = Shell.new('ruby -e "puts STDIN.gets"', :stdin => "hello world")
131         s.runcommand
132
133         s.stdout.should == "hello world\n"
134       end
135
136       it "should support multiple lines of stdin" do
137         s = Shell.new('ruby -e "puts STDIN.gets;puts;puts STDIN.gets"', :stdin => "first line\n2nd line")
138         s.runcommand
139
140         s.stdout.should == "first line\n\n2nd line\n"
141       end
142     end
143   end
144 end