forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_spec.rb
More file actions
254 lines (197 loc) · 6.79 KB
/
utils_spec.rb
File metadata and controls
254 lines (197 loc) · 6.79 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
require 'spec_helper'
require 'securerandom'
describe Overcommit::Utils do
describe '.script_path' do
subject { described_class.script_path('some-script') }
it 'points to the libexec scripts directory' do
subject.should end_with File.join('libexec', 'some-script')
end
end
describe '.repo_root' do
let(:repo_dir) { repo }
subject { described_class.repo_root }
around do |example|
Dir.chdir(repo_dir) do
example.run
end
end
it 'returns the path to the repository root' do
# realpath is so spec passes on Mac OS X
subject.should == File.realpath(repo_dir)
end
context 'when there is no .git directory' do
before do
FileUtils.rm_rf('.git', secure: true)
end
it 'raises an exception' do
expect { subject }.to raise_error Overcommit::Exceptions::InvalidGitRepo
end
end
end
describe '.git_dir' do
let(:repo_dir) { repo }
subject { described_class.git_dir }
around do |example|
Dir.chdir(repo_dir) do
example.run
end
end
context 'when .git is a directory' do
it 'returns the path to the directory' do
subject.should end_with File.join(repo_dir, '.git')
end
end
context 'when .git directory is not located in the repository' do
let(:git_dir) { Dir.mktmpdir }
let(:repo_dir) { repo(git_dir: git_dir) }
it 'returns the path of the external Git directory' do
# realpath is so spec passes on Mac OS X
subject.should == File.realpath(git_dir)
end
end
end
describe '.strip_color_codes' do
subject { described_class.strip_color_codes(text) }
context 'with an empty string' do
let(:text) { '' }
it { should == '' }
end
context 'with a string with no escape sequences' do
let(:text) { 'A normal string' }
it { should == text }
end
context 'with a string with escape sequences' do
let(:text) { "A \e[31mcolored string\e[39m" }
it { should == 'A colored string' }
end
end
describe '.snake_case' do
it 'converts camel case to underscores' do
described_class.snake_case('HelloWorld').should == 'hello_world'
end
it 'leaves underscored strings as is' do
described_class.snake_case('hello_world').should == 'hello_world'
end
it 'converts namespaced class names to paths' do
described_class.snake_case('SomeModule::SomeOtherModule::SomeClass').
should == 'some_module/some_other_module/some_class'
end
end
describe '.camel_case' do
it 'converts underscored strings to camel case' do
described_class.camel_case('hello_world').should == 'HelloWorld'
end
it 'leaves already camel-cased strings as is' do
described_class.camel_case('HelloWorld').should == 'HelloWorld'
end
it 'converts hyphenated strings to camel case' do
described_class.camel_case('hello-world').should == 'HelloWorld'
end
end
describe '.supported_hook_types' do
subject { described_class.supported_hook_types }
# rubocop:disable Metrics/LineLength
it { should =~ %w[commit-msg pre-commit post-checkout post-commit post-merge post-rewrite pre-push pre-rebase] }
# rubocop:enable Metrics/LineLength
end
describe '.supported_hook_type_classes' do
subject { described_class.supported_hook_type_classes }
# rubocop:disable Metrics/LineLength
it { should =~ %w[CommitMsg PreCommit PostCheckout PostCommit PostMerge PostRewrite PrePush PreRebase] }
# rubocop:enable Metrics/LineLength
end
describe '.parent_command' do
subject { described_class.parent_command }
before do
Process.stub(:ppid) { Process.pid }
end
it { should =~ /rspec/ }
end
describe '.execute' do
let(:arguments) { %w[echo Hello World] }
subject { described_class.execute(arguments) }
it 'returns result with the output' do
subject.stdout.should == "Hello World\n"
end
it 'returns result with the exit status' do
subject.status.should == 0
end
context 'when one of the arguments is a lone pipe character' do
let(:arguments) { %w[ps aux | grep bash] }
it 'raises an exception' do
expect { subject }.to raise_error Overcommit::Exceptions::InvalidCommandArgs
end
end
context 'when given an input stream' do
let(:arguments) { ['cat', '-'] }
let(:input) { 'Hello world' }
subject { described_class.execute(arguments, input: input) }
it 'passes the input to the standard input stream' do
subject.stdout.should == "Hello world\n"
end
end
context 'when given a list of arguments to execute in chunks' do
let(:arguments) { ['echo'] }
let(:splittable_args) { %w[1 2 3] }
subject { described_class.execute(arguments, args: splittable_args) }
it 'invokes CommandSplitter.execute' do
Overcommit::CommandSplitter.
should_receive(:execute).
with(arguments, args: splittable_args).
and_return(double(status: 0, stdout: '', stderr: ''))
subject
end
it 'returns a result' do
subject.should be_success
subject.stdout.should == "1 2 3\n"
subject.stderr.should == ''
end
end
end
describe '.execute_in_background' do
let(:arguments) { %w[touch some-file] }
subject { described_class.execute_in_background(arguments) }
around do |example|
directory do
example.run
end
end
it 'executes the command' do
wait_until { subject.exited? } # Make sure process terminated before checking
File.exist?('some-file').should == true
end
end
describe '.with_environment' do
let(:var_name) { "OVERCOMMIT_TEST_VAR_#{SecureRandom.hex}" }
shared_examples_for 'with_environment' do
it 'sets the value of the variable within the block' do
described_class.with_environment var_name => 'modified_value' do
ENV[var_name].should == 'modified_value'
end
end
end
context 'when setting an environment variable that was not already set' do
it_should_behave_like 'with_environment'
it 'deletes the value once the block has exited' do
described_class.with_environment var_name => 'modified_value' do
# Do something...
end
ENV[var_name].should be_nil
end
end
context 'when setting an environment variable that was already set' do
around do |example|
ENV[var_name] = 'previous_value'
example.run
ENV.delete(var_name)
end
it_should_behave_like 'with_environment'
it 'restores the old value once the block has exited' do
described_class.with_environment var_name => 'modified_value' do
# Do something...
end
ENV[var_name].should == 'previous_value'
end
end
end
end