Skip to content

Commit 958c57f

Browse files
FooBarWidgetAJ Christensen
authored andcommitted
When a command fails, correctly display its STDOUT and STDERR output.
1 parent 123816a commit 958c57f

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

chef/lib/chef/mixin/command.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ def run_command(args={})
114114

115115
exec_processing_block = lambda do |pid, stdin, stdout, stderr|
116116
Chef::Log.debug("---- Begin output of #{args[:command]} ----")
117-
Chef::Log.debug("STDOUT: #{stdout.string.chomp!}")
118-
Chef::Log.debug("STDERR: #{stderr.string.chomp!}")
119-
command_output << "STDOUT: #{stdout.string.chomp!}"
120-
command_output << "STDERR: #{stderr.string.chomp!}"
117+
Chef::Log.debug("STDOUT: #{stdout.string.chomp}")
118+
Chef::Log.debug("STDERR: #{stderr.string.chomp}")
119+
command_output << "STDOUT: #{stdout.string.chomp}"
120+
command_output << "STDERR: #{stderr.string.chomp}"
121121
Chef::Log.debug("---- End output of #{args[:command]} ----")
122122
end
123123

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#
2+
# Author:: Hongli Lai (hongli@phusion.nl)
3+
# Copyright:: Copyright (c) 2009 Phusion
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
20+
21+
describe Chef::Mixin::Command, "popen4" do
22+
23+
include Chef::Mixin::Command
24+
25+
it "should be possible to read the child process's stdout and stderr" do
26+
popen4("sh -c 'echo hello && echo world >&2'") do |pid, stdin, stdout, stderr|
27+
stdout.read.should == "hello\n"
28+
stderr.read.should == "world\n"
29+
end
30+
end
31+
32+
end
33+
34+
describe Chef::Mixin::Command, "run_command" do
35+
36+
include Chef::Mixin::Command
37+
38+
it "logs the command's stderr and stdout output if the command failed" do
39+
begin
40+
run_command(:command => "sh -c 'echo hello; echo world >&2; false'")
41+
violated "Exception expected, but nothing raised."
42+
rescue => e
43+
e.message.should =~ /STDOUT: hello/
44+
e.message.should =~ /STDERR: world/
45+
end
46+
end
47+
48+
end

0 commit comments

Comments
 (0)