forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_splitter_spec.rb
More file actions
126 lines (103 loc) · 4.52 KB
/
command_splitter_spec.rb
File metadata and controls
126 lines (103 loc) · 4.52 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
125
126
require 'spec_helper'
describe Overcommit::CommandSplitter do
describe '.execute' do
let(:args_prefix) { %w[cmd] }
let(:max_command_length) { 10 }
let(:options) { { args: splittable_args } }
subject { described_class.execute(args_prefix, options) }
before do
described_class.stub(:max_command_length).and_return(max_command_length)
Overcommit::Subprocess.stub(:spawn).
and_return(Overcommit::Subprocess::Result.new(0, 'output', 'error'))
end
context 'with no splittable arguments' do
let(:splittable_args) { [] }
it 'raises an error' do
expect { subject }.to raise_error Overcommit::Exceptions::InvalidCommandArgs
end
end
context 'with splittable arguments under the limit' do
let(:splittable_args) { %w[1 2 3 4 5 6 7] }
it 'executes one command' do
Overcommit::Subprocess.should_receive(:spawn).once
subject
end
end
context 'with splittable arguments just over the limit' do
let(:splittable_args) { %w[1 2 3 4 5 6 7 8] }
it 'executes two commands with the appropriately split arguments' do
Overcommit::Subprocess.should_receive(:spawn).with(%w[cmd 1 2 3 4 5 6 7],
hash_excluding(:args))
Overcommit::Subprocess.should_receive(:spawn).with(%w[cmd 8],
hash_excluding(:args))
subject
end
context 'when both commands return successfully' do
it 'returns a successful result' do
subject.should be_success
subject.status.should == 0
end
it 'returns concatenated output' do
subject.stdout.should == 'output' * 2
subject.stderr.should == 'error' * 2
end
end
context 'when one command fails' do
before do
Overcommit::Subprocess.stub(:spawn).
with(%w[cmd 8], anything).
and_return(Overcommit::Subprocess::Result.new(2, 'whoa', 'bad error'))
end
it 'returns an unsuccessful result' do
subject.should_not be_success
subject.status.should == 1
end
it 'returns concatenated output' do
subject.stdout.should == 'outputwhoa'
subject.stderr.should == 'errorbad error'
end
end
end
context 'with splittable arguments well over the limit' do
let(:splittable_args) { Array.new(15) { |i| (i + 1).to_s } }
it 'executes multiple commands with the appropriately split arguments' do
Overcommit::Subprocess.should_receive(:spawn).with(%w[cmd 1 2 3 4 5 6 7],
hash_excluding(:args))
Overcommit::Subprocess.should_receive(:spawn).with(%w[cmd 8 9 10 11],
hash_excluding(:args))
Overcommit::Subprocess.should_receive(:spawn).with(%w[cmd 12 13 14],
hash_excluding(:args))
Overcommit::Subprocess.should_receive(:spawn).with(%w[cmd 15],
hash_excluding(:args))
subject
end
end
context 'with a splittable argument that on its own exceeds the limit' do
let(:splittable_args) { %w[1 2 ohmylookareallylongargument] }
it 'executes no commands and raises an exception' do
Overcommit::Subprocess.should_not_receive(:spawn)
expect { subject }.to raise_error Overcommit::Exceptions::InvalidCommandArgs
end
end
context 'with a command prefix that exceeds the limit' do
let(:args_prefix) { %w[reallylong] }
let(:splittable_args) { %w[1 2 3] }
it 'executes no commands and raises an exception' do
Overcommit::Subprocess.should_not_receive(:spawn)
expect { subject }.to raise_error Overcommit::Exceptions::InvalidCommandArgs
end
end
context 'with a standard input stream specified' do
let(:max_command_length) { 5 }
let(:args_prefix) { %w[cat] }
let(:options) { { args: %w[- - -], input: 'Hello' } }
it 'passes the same standard input to each command' do
Overcommit::Subprocess.should_receive(:spawn).with(%w[cat - -],
hash_including(input: 'Hello'))
Overcommit::Subprocess.should_receive(:spawn).with(%w[cat -],
hash_including(input: 'Hello'))
subject
end
end
end
end