Skip to content

Commit 6c676a7

Browse files
committed
Warn when attempting to pipe commands
The `execute` helper does not support piping commands due to the complexity of supporting this in both MRI and JRuby implementations. In order to prevent misuse, fail loudly to the user if they try to use the pipe character as one of the arguments in their command. Change-Id: Id87e3173a6cb4936c2f06733c688201c1a31dada Reviewed-on: http://gerrit.causes.com/42170 Tested-by: jenkins <jenkins@causes.com> Reviewed-by: Shane da Silva <shane.dasilva@brigade.com>
1 parent 13341dc commit 6c676a7

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
prevents Overcommit hooks from running
99
* Fix bug that prevented RailsSchemaUpToDate from working in directories that
1010
contained decimals
11+
* Warn when trying to pipe commands using the `execute` helper, as this is not
12+
supported
1113

1214
## 0.16.0
1315

lib/overcommit/exceptions.rb

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class HookLoadError < StandardError; end
2020
# Raised when a {HookRunner} could not be loaded.
2121
class HookContextLoadError < StandardError; end
2222

23+
# Raised when a pipe character is used in the `execute` helper, as this was
24+
# likely used in error.
25+
class InvalidCommandArgs < StandardError; end
26+
2327
# Raised when an installation target is not a valid git repository.
2428
class InvalidGitRepo < StandardError; end
2529

lib/overcommit/utils.rb

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ def in_path?(cmd)
6363
# specified in Overcommit's Gemfile--a nasty consequence of using
6464
# `bundle exec overcommit` while developing locally.
6565
def execute(args)
66+
if args.include?('|')
67+
raise Overcommit::Exceptions::InvalidCommandArgs,
68+
'Cannot pipe commands with the `execute` helper'
69+
end
70+
6671
with_environment 'RUBYOPT' => nil do
6772
Subprocess.spawn(args)
6873
end

spec/overcommit/utils_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,25 @@
6868

6969
it { should =~ %w[CommitMsg PreCommit PostCheckout] }
7070
end
71+
72+
describe '.execute' do
73+
let(:arguments) { %w[echo -n Hello World] }
74+
subject { described_class.execute(arguments) }
75+
76+
it 'returns result with the output' do
77+
subject.stdout.should == 'Hello World'
78+
end
79+
80+
it 'returns result with the exit status' do
81+
subject.status.should == 0
82+
end
83+
84+
context 'when one of the arguments is a lone pipe character' do
85+
let(:arguments) { %w[ps aux | grep bash] }
86+
87+
it 'raises an exception' do
88+
expect { subject }.to raise_error Overcommit::Exceptions::InvalidCommandArgs
89+
end
90+
end
91+
end
7192
end

0 commit comments

Comments
 (0)