rspec-bash 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +1 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +6 -0
  6. data/CHANGELOG.md +24 -0
  7. data/Gemfile +7 -0
  8. data/LICENSE.txt +23 -0
  9. data/README.md +227 -0
  10. data/Rakefile +13 -0
  11. data/bin/function_override.sh.erb +7 -0
  12. data/bin/function_override_wrapper.sh.erb +16 -0
  13. data/bin/stub +62 -0
  14. data/lib/rspec/bash.rb +5 -0
  15. data/lib/rspec/bash/call_configuration.rb +37 -0
  16. data/lib/rspec/bash/call_log.rb +77 -0
  17. data/lib/rspec/bash/matchers.rb +2 -0
  18. data/lib/rspec/bash/matchers/called_with_arguments.rb +14 -0
  19. data/lib/rspec/bash/matchers/called_with_no_arguments.rb +5 -0
  20. data/lib/rspec/bash/stubbed_command.rb +76 -0
  21. data/lib/rspec/bash/stubbed_env.rb +99 -0
  22. data/rspec-bash.gemspec +25 -0
  23. data/spec/classes/call_configuration_spec.rb +21 -0
  24. data/spec/classes/call_log_spec.rb +309 -0
  25. data/spec/classes/stubbed_command_spec.rb +134 -0
  26. data/spec/classes/stubbed_env_spec.rb +306 -0
  27. data/spec/integration/assert_called_spec.rb +48 -0
  28. data/spec/integration/assert_stdin_spec.rb +39 -0
  29. data/spec/integration/chain_args_spec.rb +65 -0
  30. data/spec/integration/change_exitstatus_spec.rb +53 -0
  31. data/spec/integration/provide_env_vars_spec.rb +31 -0
  32. data/spec/integration/replace_shell_commands_spec.rb +48 -0
  33. data/spec/integration/stub_output_spec.rb +110 -0
  34. data/spec/matchers/be_called_with_arguments_spec.rb +55 -0
  35. data/spec/matchers/be_called_with_no_arguments_spec.rb +32 -0
  36. data/spec/scripts/function_library.sh +9 -0
  37. metadata +129 -0
@@ -0,0 +1,134 @@
1
+ require 'English'
2
+ require 'rspec/bash'
3
+
4
+ describe 'StubbedCommand' do
5
+ include Rspec::Bash
6
+ let(:stubbed_env) { create_stubbed_env }
7
+ before(:each) do
8
+ allow(FileUtils).to receive(:cp)
9
+ end
10
+
11
+ context '#called_with_args?' do
12
+ before(:each) do
13
+ @call_log = double(Rspec::Bash::CallLog)
14
+ allow(Rspec::Bash::CallLog).to receive(:new).and_return(@call_log)
15
+ @subject = Rspec::Bash::StubbedCommand.new('command', Dir.mktmpdir)
16
+ end
17
+ context 'with only a series of arguments' do
18
+ it 'passes the check to its CallLog\'s #called_with_args? method' do
19
+ expect(@call_log).to receive(:called_with_args?).with('first_argument', 'second_argument').and_return(true)
20
+ @subject.called_with_args?('first_argument', 'second_argument')
21
+ end
22
+ end
23
+ end
24
+
25
+ context '#with_args' do
26
+ before(:each) do
27
+ @subject = Rspec::Bash::StubbedCommand.new('command', Dir.mktmpdir)
28
+ @subject.with_args('argument_one', 'argument_two')
29
+ end
30
+ it 'sets the arguments array on the StubbedCommand to the arguments that were passed in' do
31
+ expect(@subject.arguments).to eql %w(argument_one argument_two)
32
+ end
33
+ end
34
+
35
+ context '#call_count' do
36
+ before(:each) do
37
+ @call_log = double(Rspec::Bash::CallLog)
38
+ allow(Rspec::Bash::CallLog).to receive(:new).and_return(@call_log)
39
+ @subject = Rspec::Bash::StubbedCommand.new('command', Dir.mktmpdir)
40
+ end
41
+ it 'returns value returned from call_log argument count when there are no arguments' do
42
+ expect(@call_log).to receive(:call_count).with([]).and_return('arbitrary return value')
43
+ expect(@subject.call_count([])).to eql 'arbitrary return value'
44
+ end
45
+ it 'returns value returned from call_log argument count when there is only one argument' do
46
+ expect(@call_log).to receive(:call_count).with(['only arg']).and_return('arbitrary return value')
47
+ expect(@subject.call_count ['only arg']).to eql 'arbitrary return value'
48
+ end
49
+ it 'returns value returned from call_log argument count when there are multiple arguments' do
50
+ expect(@call_log).to receive(:call_count).with(['first arg', 'second arg']).and_return('arbitrary return value')
51
+ expect(@subject.call_count ['first arg', 'second arg']).to eql 'arbitrary return value'
52
+ end
53
+ end
54
+
55
+ context '#called?' do
56
+ before(:each) do
57
+ @call_log = double(Rspec::Bash::CallLog)
58
+ allow(Rspec::Bash::CallLog).to receive(:new).and_return(@call_log)
59
+ @subject = Rspec::Bash::StubbedCommand.new('command', Dir.mktmpdir)
60
+ end
61
+ it 'returns false when there is no call_log' do
62
+ expect(@call_log).to receive(:exist?).and_return(false)
63
+ expect(@subject.called?).to be_falsey
64
+ end
65
+ it 'returns false when call_log is not called with args' do
66
+ expect(@call_log).to receive(:exist?).and_return(true)
67
+ expect(@call_log).to receive(:called_with_args?).and_return(false)
68
+ expect(@subject.called?).to be_falsey
69
+ end
70
+ it 'returns true when call_log is called with args' do
71
+ expect(@call_log).to receive(:exist?).and_return(true)
72
+ expect(@call_log).to receive(:called_with_args?).and_return(true)
73
+ expect(@subject.called?).to be_truthy
74
+ end
75
+ end
76
+
77
+ context '#stdin' do
78
+ before(:each) do
79
+ @call_log = double(Rspec::Bash::CallLog)
80
+ allow(Rspec::Bash::CallLog).to receive(:new).and_return(@call_log)
81
+ @subject = Rspec::Bash::StubbedCommand.new('command', Dir.mktmpdir)
82
+ end
83
+ it 'returns nil when there is no call_log' do
84
+ expect(@call_log).to receive(:exist?).and_return(false)
85
+ expect(@subject.stdin).to be_nil
86
+ end
87
+ it 'returns stdin from call log when call_log exists' do
88
+ expect(@call_log).to receive(:exist?).and_return(true)
89
+ expect(@call_log).to receive(:stdin_for_args).and_return('arbitrary stdin')
90
+ expect(@subject.stdin).to eql 'arbitrary stdin'
91
+ end
92
+ end
93
+
94
+ context '#returns_exitstatus' do
95
+ before(:each) do
96
+ @call_configuration = double(Rspec::Bash::CallConfiguration)
97
+ allow(Rspec::Bash::CallConfiguration).to receive(:new).and_return(@call_configuration)
98
+ @subject = Rspec::Bash::StubbedCommand.new('command', Dir.mktmpdir)
99
+ end
100
+ it 'sets the exitcode on call_configuration' do
101
+ expect(@call_configuration).to receive(:set_exitcode).with('exit code', anything)
102
+ expect(@call_configuration).to receive(:write)
103
+ @subject.returns_exitstatus 'exit code'
104
+ end
105
+ it 'returns itself' do
106
+ expect(@call_configuration).to receive(:set_exitcode)
107
+ expect(@call_configuration).to receive(:write)
108
+ expect(@subject.returns_exitstatus(anything)).to eql @subject
109
+ end
110
+ end
111
+
112
+ context '#outputs' do
113
+ before(:each) do
114
+ @call_configuration = double(Rspec::Bash::CallConfiguration)
115
+ allow(Rspec::Bash::CallConfiguration).to receive(:new).and_return(@call_configuration)
116
+ @subject = Rspec::Bash::StubbedCommand.new('command', Dir.mktmpdir)
117
+ end
118
+ it 'sets the output on the call_configuration' do
119
+ expect(@call_configuration).to receive(:set_output).with('contents', 'stderr', anything)
120
+ expect(@call_configuration).to receive(:write)
121
+ @subject.outputs('contents', to: 'stderr')
122
+ end
123
+ it 'sets the "to" value for the output to stdout by default' do
124
+ expect(@call_configuration).to receive(:set_output).with('contents', :stdout, anything)
125
+ expect(@call_configuration).to receive(:write)
126
+ @subject.outputs('contents')
127
+ end
128
+ it 'returns itself' do
129
+ expect(@call_configuration).to receive(:set_output)
130
+ expect(@call_configuration).to receive(:write)
131
+ expect(@subject.outputs(anything)).to eql @subject
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,306 @@
1
+ require 'English'
2
+ require 'rspec/bash'
3
+
4
+ describe 'StubbedEnv' do
5
+ include Rspec::Bash
6
+ let(:subject) { Rspec::Bash::StubbedEnv.new }
7
+
8
+ context '#execute_inline' do
9
+ context 'with a stubbed function' do
10
+ before(:each) do
11
+ @overridden_function = subject.stub_command('overridden_function')
12
+ @overridden_command = subject.stub_command('overridden_command')
13
+ @overridden_function.outputs('i was overridden')
14
+ end
15
+
16
+ context 'and no arguments' do
17
+ before(:each) do
18
+ @stdout, @stderr, @status = subject.execute_inline(<<-multiline_script
19
+ #!/usr/bin/env bash
20
+ function overridden_function {
21
+ echo 'i was not overridden'
22
+ }
23
+ overridden_function
24
+
25
+ echo 'standard error output' 1>&2
26
+ multiline_script
27
+ )
28
+ end
29
+
30
+ it 'calls the stubbed function' do
31
+ expect(@overridden_function).to be_called
32
+ end
33
+
34
+ it 'prints the overridden output' do
35
+ expect(@stdout).to eql('i was overridden')
36
+ end
37
+
38
+ it 'prints provided stderr output to standard error' do
39
+ expect(@stderr).to eql("standard error output\n")
40
+ end
41
+ end
42
+
43
+ context 'and simple arguments' do
44
+ before(:each) do
45
+ @stdout, @stderr, @status = subject.execute_inline(<<-multiline_script
46
+ #!/usr/bin/env bash
47
+ function overridden_function {
48
+ echo 'i was not overridden'
49
+ }
50
+ overridden_function argument_one argument_two
51
+
52
+ echo 'standard error output' 1>&2
53
+ multiline_script
54
+ )
55
+ end
56
+
57
+ it 'calls the stubbed function' do
58
+ expect(@overridden_function).to be_called_with_arguments('argument_one', 'argument_two')
59
+ end
60
+
61
+ it 'prints the overridden output' do
62
+ expect(@stdout).to eql('i was overridden')
63
+ end
64
+ end
65
+
66
+ context 'and complex arguments (spaces, etc.)' do
67
+ before(:each) do
68
+ @stdout, @stderr, @status = subject.execute_inline(<<-multiline_script
69
+ #!/usr/bin/env bash
70
+ function overridden_function {
71
+ echo 'i was not overridden'
72
+ }
73
+ overridden_function "argument one" "argument two"
74
+
75
+ echo 'standard error output' 1>&2
76
+ multiline_script
77
+ )
78
+ end
79
+
80
+ it 'calls the stubbed function' do
81
+ expect(@overridden_function).to be_called_with_arguments('argument one', 'argument two')
82
+ end
83
+
84
+ it 'prints the overridden output' do
85
+ expect(@stdout).to eql('i was overridden')
86
+ end
87
+ end
88
+ end
89
+ context 'with a stubbed command' do
90
+ before(:each) do
91
+ @overridden_command = subject.stub_command('overridden_command')
92
+ @overridden_function = subject.stub_command('overridden_function')
93
+ @overridden_command.outputs('i was overridden')
94
+ end
95
+
96
+ context 'and no arguments' do
97
+ before(:each) do
98
+ @stdout, @stderr, @status = subject.execute_inline(<<-multiline_script
99
+ #!/usr/bin/env bash
100
+ overridden_command
101
+ multiline_script
102
+ )
103
+ end
104
+
105
+ it 'calls the stubbed command' do
106
+ expect(@overridden_command).to be_called
107
+ end
108
+
109
+ it 'prints the overridden output' do
110
+ expect(@stdout).to eql('i was overridden')
111
+ end
112
+ end
113
+
114
+ context 'and simple arguments' do
115
+ before(:each) do
116
+ @stdout, @stderr, @status = subject.execute_inline(<<-multiline_script
117
+ #!/usr/bin/env bash
118
+ overridden_command argument_one argument_two
119
+ multiline_script
120
+ )
121
+ end
122
+
123
+ it 'calls the stubbed command' do
124
+ expect(@overridden_command).to be_called_with_arguments('argument_one', 'argument_two')
125
+ end
126
+
127
+ it 'prints the overridden output' do
128
+ expect(@stdout).to eql('i was overridden')
129
+ end
130
+ end
131
+
132
+ context 'and complex arguments (spaces, etc.)' do
133
+ before(:each) do
134
+ @stdout, @stderr, @status = subject.execute_inline(<<-multiline_script
135
+ #!/usr/bin/env bash
136
+ overridden_command "argument one" "argument two"
137
+ multiline_script
138
+ )
139
+ end
140
+
141
+ it 'calls the stubbed command' do
142
+ expect(@overridden_command).to be_called_with_arguments('argument one', 'argument two')
143
+ end
144
+
145
+ it 'prints the overridden output' do
146
+ expect(@stdout).to eql('i was overridden')
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ context '#execute_function' do
153
+ context 'with a stubbed function' do
154
+ before(:each) do
155
+ @overridden_function = subject.stub_command('overridden_function')
156
+ @overridden_command = subject.stub_command('overridden_command')
157
+ @overridden_function.outputs('i was overridden')
158
+ @overridden_function.outputs('standard error output', to: :stderr)
159
+ end
160
+
161
+ context 'and no arguments' do
162
+ before(:each) do
163
+ @stdout, @stderr, @status = subject.execute_function(
164
+ './spec/scripts/function_library.sh',
165
+ 'overridden_function'
166
+ )
167
+ end
168
+
169
+ it 'calls the stubbed function' do
170
+ expect(@overridden_function).to be_called
171
+ end
172
+
173
+ it 'prints the overridden output' do
174
+ expect(@stdout).to eql('i was overridden')
175
+ end
176
+
177
+ it 'prints provided stderr output to standard error' do
178
+ expect(@stderr).to eql("standard error output\n")
179
+ end
180
+ end
181
+
182
+ context 'and simple arguments' do
183
+ before(:each) do
184
+ @stdout, @stderr, @status = subject.execute_function(
185
+ './spec/scripts/function_library.sh',
186
+ 'overridden_function argument_one argument_two'
187
+ )
188
+ end
189
+
190
+ it 'calls the stubbed function' do
191
+ expect(@overridden_function).to be_called_with_arguments('argument_one', 'argument_two')
192
+ end
193
+
194
+ it 'prints the overridden output' do
195
+ expect(@stdout).to eql('i was overridden')
196
+ end
197
+ end
198
+
199
+ context 'and complex arguments (spaces, etc.)' do
200
+ before(:each) do
201
+ @stdout, @stderr, @status = subject.execute_function(
202
+ './spec/scripts/function_library.sh',
203
+ 'overridden_function "argument one" "argument two"'
204
+ )
205
+ end
206
+
207
+ it 'calls the stubbed function' do
208
+ expect(@overridden_function).to be_called_with_arguments('argument one', 'argument two')
209
+ end
210
+
211
+ it 'prints the overridden output' do
212
+ expect(@stdout).to eql('i was overridden')
213
+ end
214
+ end
215
+ end
216
+ context 'with a stubbed command' do
217
+ before(:each) do
218
+ @overridden_function = subject.stub_command('overridden_function')
219
+ @overridden_command = subject.stub_command('overridden_command')
220
+ @overridden_command.outputs('i was overridden')
221
+ end
222
+
223
+ context 'and no arguments' do
224
+ before(:each) do
225
+ @stdout, @stderr, @status = subject.execute_function(
226
+ './spec/scripts/function_library.sh',
227
+ 'overridden_command_function'
228
+ )
229
+ end
230
+
231
+ it 'calls the stubbed command' do
232
+ expect(@overridden_command).to be_called
233
+ end
234
+
235
+ it 'prints the overridden output' do
236
+ expect(@stdout).to eql('i was overridden')
237
+ end
238
+
239
+ it 'prints provided stderr output to standard error' do
240
+ expect(@stderr).to eql("standard error output\n")
241
+ end
242
+ end
243
+
244
+ context 'and simple arguments' do
245
+ before(:each) do
246
+ @stdout, @stderr, @status = subject.execute_function(
247
+ './spec/scripts/function_library.sh',
248
+ 'overridden_command_function argument_one argument_two'
249
+ )
250
+ end
251
+
252
+ it 'calls the stubbed command' do
253
+ expect(@overridden_command).to be_called_with_arguments('argument_one', 'argument_two')
254
+ end
255
+
256
+ it 'prints the overridden output' do
257
+ expect(@stdout).to eql('i was overridden')
258
+ end
259
+ end
260
+
261
+ context 'and complex arguments (spaces, etc.)' do
262
+ before(:each) do
263
+ @stdout, @stderr, @status = subject.execute_function(
264
+ './spec/scripts/function_library.sh',
265
+ 'overridden_command_function "argument one" "argument two"'
266
+ )
267
+ end
268
+
269
+ it 'calls the stubbed command' do
270
+ expect(@overridden_command).to be_called_with_arguments('argument one', 'argument two')
271
+ end
272
+
273
+ it 'prints the overridden output' do
274
+ expect(@stdout).to eql('i was overridden')
275
+ end
276
+ end
277
+ end
278
+ end
279
+
280
+ describe 'creating a stubbed env' do
281
+ it 'extends the PATH with the stubbed folder first' do
282
+ expect { create_stubbed_env }.to change { ENV['PATH'] }
283
+ end
284
+
285
+ it 'creates a folder to place the stubbed commands in' do
286
+ env = create_stubbed_env
287
+ expect(Pathname.new(env.dir)).to exist
288
+ expect(Pathname.new(env.dir)).to be_directory
289
+ end
290
+ end
291
+
292
+ describe '#cleanup' do
293
+ it 'restores the environment variable PATH' do
294
+ original_path = ENV['PATH']
295
+ env = create_stubbed_env
296
+
297
+ expect { env.cleanup }.to change { ENV['PATH'] }.to original_path
298
+ end
299
+
300
+ it 'removes the folder with stubbed commands' do
301
+ env = create_stubbed_env
302
+ env.cleanup
303
+ expect(Pathname.new(env.dir)).not_to exist
304
+ end
305
+ end
306
+ end
@@ -0,0 +1,48 @@
1
+ require 'English'
2
+ require 'rspec/bash'
3
+
4
+ describe 'Assert called' do
5
+ include Rspec::Bash
6
+ let(:stubbed_env) { create_stubbed_env }
7
+ let!(:first_command) { stubbed_env.stub_command('first_command') }
8
+
9
+ let(:script) do
10
+ <<-SCRIPT
11
+ first_command "foo bar"
12
+ SCRIPT
13
+ end
14
+ let(:script_path) { Pathname.new '/tmp/test_script.sh' }
15
+
16
+ before do
17
+ script_path.open('w') { |f| f.puts script }
18
+ script_path.chmod 0777
19
+
20
+ stubbed_env.execute script_path.to_s
21
+ end
22
+
23
+ after do
24
+ script_path.delete
25
+ end
26
+
27
+ describe 'assert called' do
28
+ it 'returns called status' do
29
+ expect(first_command).to be_called
30
+ end
31
+
32
+ context 'assert with args' do
33
+ it 'returns called status' do
34
+ expect(first_command).to be_called_with_arguments('foo bar')
35
+ expect(first_command).to be_called_with_arguments(anything)
36
+ expect(first_command).not_to be_called_with_arguments('foot')
37
+ end
38
+ end
39
+
40
+ describe 'assertion message' do
41
+ it 'provides a helpful message' do
42
+ expect(first_command.inspect).to eql '<Stubbed "first_command">'
43
+ expect(first_command.with_args('foo bar').inspect).to \
44
+ eql '<Stubbed "first_command" args: "foo bar">'
45
+ end
46
+ end
47
+ end
48
+ end