7 describe "#initialize" do
8 it "should set locale by default" do
10 s.environment.should == {"LC_ALL" => "C"}
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"}
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"}
23 it "should set no environment when given nil" do
24 s = Shell.new("date", :environment => nil)
25 s.environment.should == {}
28 it "should save the command" do
30 s.command.should == "date"
33 it "should check the cwd exist" do
35 s = Shell.new("date", :cwd => "/nonexistant")
36 }.to raise_error("Directory /nonexistant does not exist")
39 it "should warn of illegal stdin" do
41 s = Shell.new("date", :stdin => nil)
42 }.to raise_error("stdin should be a String")
45 it "should warn of illegal stdout" do
47 s = Shell.new("date", :stdout => nil)
48 }.to raise_error("stdout should support <<")
51 it "should warn of illegal stderr" do
53 s = Shell.new("date", :stderr => nil)
54 }.to raise_error("stderr should support <<")
57 it "should set stdout" do
58 s = Shell.new("date", :stdout => "stdout")
59 s.stdout.should == "stdout"
62 it "should set stderr" do
63 s = Shell.new("date", :stderr => "stderr")
64 s.stderr.should == "stderr"
67 it "should set stdin" do
68 s = Shell.new("date", :stdin => "hello world")
69 s.stdin.should == "hello world"
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)
80 it "should set stdin, stdout and status" do
81 s = Shell.new('ruby -e "STDERR.puts \"stderr\"; STDOUT.puts \"stdout\""')
83 s.stdout.should == "stdout\n"
84 s.stderr.should == "stderr\n"
85 s.status.exitstatus.should == 0
88 it "should report correct exitcode" do
89 s = Shell.new('ruby -e "exit 1"')
92 s.status.exitstatus.should == 1
95 it "shold have correct environment" do
96 s = Shell.new('ruby -e "puts ENV[\'LC_ALL\'];puts ENV[\'foo\'];"', :environment => {"foo" => "bar"})
98 s.stdout.should == "C\nbar\n"
101 it "should save stdout in custom stdout variable" do
104 s = Shell.new('echo foo', :stdout => out)
107 s.stdout.should == "STDOUTfoo\n"
108 out.should == "STDOUTfoo\n"
111 it "should save stderr in custom stderr variable" do
114 s = Shell.new('ruby -e "STDERR.puts \"foo\""', :stderr => out)
117 s.stderr.should == "STDERRfoo\n"
118 out.should == "STDERRfoo\n"
121 it "should run in the correct cwd" do
122 s = Shell.new('ruby -e "puts Dir.pwd"', :cwd => Dir.tmpdir)
126 s.stdout.should == "#{Dir.tmpdir}\n"
129 it "should send the stdin" do
130 s = Shell.new('ruby -e "puts STDIN.gets"', :stdin => "hello world")
133 s.stdout.should == "hello world\n"
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")
140 s.stdout.should == "first line\n\n2nd line\n"