rspec-bash 0.0.3 → 0.1.0

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 (50) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +30 -0
  3. data/.travis.yml +1 -0
  4. data/Gemfile +4 -4
  5. data/README.md +87 -77
  6. data/Rakefile +1 -1
  7. data/bin/function_override_wrapper.sh.erb +6 -3
  8. data/bin/stub.rb.erb +56 -0
  9. data/lib/rspec/bash.rb +1 -0
  10. data/lib/rspec/bash/call_configuration.rb +39 -14
  11. data/lib/rspec/bash/call_log.rb +33 -43
  12. data/lib/rspec/bash/matchers/called_with_arguments.rb +7 -2
  13. data/lib/rspec/bash/stubbed_command.rb +22 -14
  14. data/lib/rspec/bash/stubbed_env.rb +15 -16
  15. data/lib/rspec/bash/util.rb +2 -0
  16. data/lib/rspec/bash/util/call_conf_argument_list_matcher.rb +47 -0
  17. data/lib/rspec/bash/util/call_log_argument_list_matcher.rb +33 -0
  18. data/return_exitstatus_spec.rb +14 -0
  19. data/rspec-bash.gemspec +2 -3
  20. data/spec/classes/call_configuration_spec.rb +296 -8
  21. data/spec/classes/call_log_spec.rb +168 -272
  22. data/spec/classes/stub_spec.rb +510 -0
  23. data/spec/classes/stubbed_command_spec.rb +26 -26
  24. data/spec/classes/stubbed_env_spec.rb +58 -64
  25. data/spec/classes/util/call_conf_argument_list_matcher_spec.rb +579 -0
  26. data/spec/classes/util/call_log_argument_list_matcher_spec.rb +211 -0
  27. data/spec/helper/shared_tmpdir.rb +6 -0
  28. data/spec/helper/string_file_io.rb +9 -0
  29. data/spec/integration/call_log/called_with_args_spec.rb +48 -0
  30. data/spec/integration/call_log/called_with_no_args_spec.rb +21 -0
  31. data/spec/integration/call_log/stdin_spec.rb +53 -0
  32. data/spec/integration/matchers/be_called_with_arguments_spec.rb +60 -0
  33. data/spec/integration/matchers/be_called_with_no_arguments_spec.rb +35 -0
  34. data/spec/integration/stubbed_command/outputs_spec.rb +262 -0
  35. data/spec/integration/stubbed_command/returns_exitstatus_spec.rb +99 -0
  36. data/spec/integration/stubbed_env/execute_with_env_vars_spec.rb +17 -0
  37. data/spec/integration/stubbed_env/execute_with_path_spec.rb +34 -0
  38. data/spec/integration/stubbed_env/execute_with_stub_wrapper_spec.rb +23 -0
  39. data/spec/spec_helper.rb +10 -0
  40. metadata +42 -24
  41. data/bin/stub +0 -62
  42. data/spec/integration/assert_called_spec.rb +0 -48
  43. data/spec/integration/assert_stdin_spec.rb +0 -39
  44. data/spec/integration/chain_args_spec.rb +0 -65
  45. data/spec/integration/change_exitstatus_spec.rb +0 -53
  46. data/spec/integration/provide_env_vars_spec.rb +0 -31
  47. data/spec/integration/replace_shell_commands_spec.rb +0 -48
  48. data/spec/integration/stub_output_spec.rb +0 -110
  49. data/spec/matchers/be_called_with_arguments_spec.rb +0 -55
  50. data/spec/matchers/be_called_with_no_arguments_spec.rb +0 -32
@@ -1,309 +1,205 @@
1
- require 'English'
2
- require 'rspec/bash'
1
+ require 'spec_helper'
2
+ include Rspec::Bash
3
+ include Rspec::Bash::Util
3
4
 
4
5
  describe 'CallLog' do
5
6
  let(:stubbed_env) { create_stubbed_env }
6
- include Rspec::Bash
7
+ let(:mock_log_file) { StringFileIO.new }
8
+ let(:mock_log_pathname) { instance_double(Pathname) }
9
+ let(:mock_call_log_matcher) { instance_double(CallLogArgumentListMatcher) }
10
+ before(:each) do
11
+ allow(mock_call_log_matcher).to receive(:get_call_log_matches).and_return(
12
+ [
13
+ {
14
+ args: %w(first_argument second_argument),
15
+ stdin: 'first_stdin'
16
+ },
17
+ {
18
+ args: %w(first_argument second_argument),
19
+ stdin: 'second_stdin'
20
+ },
21
+ {
22
+ args: %w(first_argument second_argument),
23
+ stdin: 'third_stdin'
24
+ }
25
+ ]
26
+ )
27
+ allow(mock_call_log_matcher).to receive(:get_call_count).and_return(3)
28
+ allow(mock_call_log_matcher).to receive(:args_match?).and_return(true)
29
+ allow(CallLogArgumentListMatcher).to receive(:new).with(any_args)
30
+ .and_return(mock_call_log_matcher)
31
+ allow(mock_log_pathname).to receive(:open).with('r').and_yield(mock_log_file)
32
+ allow(mock_log_pathname).to receive(:open).with('w').and_yield(mock_log_file)
33
+ end
7
34
 
8
- context '#stdin_for_args' do
9
- it 'returns nil when no YAML file is used for call log' do
10
- @subject = Rspec::Bash::CallLog.new(anything)
11
- allow(YAML).to receive(:load_file).and_return([])
35
+ subject { CallLog.new(mock_log_pathname) }
12
36
 
13
- expect(@subject.stdin_for_args(anything)).to be nil
37
+ context '#stdin_for_args' do
38
+ it 'returns the first matching stdin via the specialized matcher' do
39
+ expect(mock_call_log_matcher).to receive(:get_call_log_matches)
40
+ .with(any_args)
41
+ expect(subject.stdin_for_args('first_argument', 'second_argument')).to eql 'first_stdin'
14
42
  end
15
- it 'returns the stdin from call log when there is a single value for stdin' do
16
- actual_call_log_list =
17
- [{
18
- 'args' => ['arbitrary argument'],
19
- 'stdin' => ['correct value'],
20
- }]
21
- @subject = Rspec::Bash::CallLog.new(anything)
22
- allow(YAML).to receive(:load_file).and_return(actual_call_log_list)
23
-
24
- expect(@subject.stdin_for_args('arbitrary argument').first).to eql 'correct value'
43
+ end
44
+ context '#call_count?' do
45
+ it 'returns the expected call count via the specialized matcher' do
46
+ expect(mock_call_log_matcher).to receive(:get_call_count)
47
+ .with(any_args)
48
+ expect(subject.call_count('first_argument', 'second_argument')).to eql 3
25
49
  end
26
- it 'returns the stdin from call log when there are multiple values for stdin' do
27
- actual_call_log_list =
28
- [{
29
- 'args' => ['arbitrary argument'],
30
- 'stdin' => ['first value', 'second value'],
31
- }]
32
- @subject = Rspec::Bash::CallLog.new(anything)
33
- allow(YAML).to receive(:load_file).and_return(actual_call_log_list)
34
-
35
- expect(@subject.stdin_for_args('arbitrary argument').sort).to eql ['first value', 'second value'].sort
50
+ end
51
+ context '#called_with_args' do
52
+ it 'returns the expected value via the specialized matcher' do
53
+ expect(mock_call_log_matcher).to receive(:args_match?)
54
+ .with(any_args)
55
+ expect(subject.called_with_args?('first_argument', 'second_argument')).to eql true
36
56
  end
37
- it 'returns the stdin from call log when no arguments are provided' do
38
- actual_call_log_list =
39
- [{
40
- 'args' => nil,
41
- 'stdin' => ['correct value'],
42
- }]
43
- @subject = Rspec::Bash::CallLog.new(anything)
44
- allow(YAML).to receive(:load_file).and_return(actual_call_log_list)
57
+ end
58
+ context '#called_with_no_args?' do
59
+ it 'returns false if no call log is found' do
60
+ @subject = Rspec::Bash::CallLog.new(mock_log_pathname)
61
+ @subject.call_log = []
45
62
 
46
- expect(@subject.stdin_for_args).to eql ['correct value']
63
+ expect(@subject.called_with_no_args?).to be_falsey
64
+ end
65
+ it 'returns true if no arguments are in call log' do
66
+ actual_call_log = [{
67
+ args: nil,
68
+ stdin: []
69
+ }]
70
+ @subject = Rspec::Bash::CallLog.new(mock_log_pathname)
71
+ @subject.call_log = actual_call_log
72
+
73
+ expect(@subject.called_with_no_args?).to be_truthy
74
+ end
75
+ it 'returns fails if a single argument is in call log' do
76
+ actual_call_log = [{
77
+ args: ['I am an argument'],
78
+ stdin: []
79
+ }]
80
+ @subject = Rspec::Bash::CallLog.new(mock_log_pathname)
81
+ @subject.call_log = actual_call_log
82
+
83
+ expect(@subject.called_with_no_args?).to be_falsey
84
+ end
85
+ it 'returns fails if multiple arguments is in call log' do
86
+ actual_call_log = [{
87
+ args: ['I am an argument', 'as am I'],
88
+ stdin: []
89
+ }]
90
+ @subject = Rspec::Bash::CallLog.new(mock_log_pathname)
91
+ @subject.call_log = actual_call_log
92
+
93
+ expect(@subject.called_with_no_args?).to be_falsey
47
94
  end
48
95
  end
49
96
 
50
- context '#call_count?' do
51
- context 'with no calls made at all (missing call log file)' do
52
- before(:each) do
53
- @subject = Rspec::Bash::CallLog.new('command_with_no_call_log_file')
54
- allow(YAML).to receive(:load_file).and_raise(Errno::ENOENT)
55
- end
56
-
57
- it 'does not find an un-passed argument anywhere in the series' do
58
- expect(@subject.call_count('not_an_argument')).to eql 0
59
- end
60
- end
61
- context 'with only an series of arguments provided' do
62
- context 'and a command log with only one argument' do
63
- before(:each) do
64
- actual_call_log_list =
65
- [{
66
- 'args' => ['first_argument'],
67
- 'stdin' => [],
68
- }]
69
- @subject = Rspec::Bash::CallLog.new('command_with_one_argument_log')
70
- allow(@subject).to receive(:load_call_log_list).and_return(actual_call_log_list)
71
- end
97
+ context '#add_log' do
98
+ context 'with any setup' do
99
+ subject { Rspec::Bash::CallLog.new(mock_log_pathname) }
72
100
 
73
- it 'finds the single argument anywhere in the series exactly once' do
74
- expect(@subject.call_count('first_argument')).to eql 1
101
+ context 'with no existing configuration' do
102
+ let(:expected_log) do
103
+ [
104
+ {
105
+ args: %w(first_argument second_argument),
106
+ stdin: 'first_stdin'
107
+ }
108
+ ]
75
109
  end
76
-
77
- it 'does not find an un-passed argument anywhere in the series' do
78
- expect(@subject.call_count('not_an_argument')).to eql 0
110
+ it 'adds a call log for the arguments passed in' do
111
+ subject.add_log('first_stdin', %w(first_argument second_argument))
112
+ expect(subject.call_log).to eql expected_log
79
113
  end
80
114
 
81
- it 'finds the single wildcard argument exactly once' do
82
- expect(@subject.call_count(anything)).to eql 1
115
+ it 'writes that log to its log file' do
116
+ expect(mock_log_file).to receive(:write).with(expected_log.to_yaml)
117
+ subject.add_log('first_stdin', %w(first_argument second_argument))
83
118
  end
84
119
  end
85
- context 'and a command called with two arguments' do
86
- before(:each) do
87
- actual_call_log_list =
88
- [{
89
- 'args' => ['first_argument', 'second_argument'],
90
- 'stdin' => [],
91
- }]
92
- @subject = Rspec::Bash::CallLog.new('command_with_two_arguments_log')
93
- allow(@subject).to receive(:load_call_log_list).and_return(actual_call_log_list)
94
- end
95
-
96
- it 'does not find the first argument when other argument is not provided' do
97
- expect(@subject.call_count('first_argument')).to eql 0
98
- end
99
-
100
- it 'finds the first argument anywhere in the series exactly once' do
101
- expect(@subject.call_count('first_argument', anything)).to eql 1
102
- end
103
-
104
- it 'does not find the second argument when first argument is not provided' do
105
- expect(@subject.call_count('second_argument')).to eql 0
106
- end
107
-
108
- it 'finds the second argument anywhere in the series exactly once' do
109
- expect(@subject.call_count(anything, 'second_argument')).to eql 1
110
- end
111
-
112
- it 'finds two contiguous arguments in the series exactly once' do
113
- expect(@subject.call_count('first_argument', 'second_argument')).to eql 1
114
- end
115
-
116
- it 'does not find an un-passed argument anywhere in the series' do
117
- expect(@subject.call_count('not_an_argument')).to eql 0
118
- end
119
-
120
- it 'does not find a wildcard argument when other argument is not provided' do
121
- expect(@subject.call_count(anything)).to eql 0
122
- end
123
-
124
- it 'finds when both arguments are wildcards exactly once' do
125
- expect(@subject.call_count(anything, anything)).to eql 1
126
- end
127
-
128
- it 'finds when only the first argument is a wildcard exactly once' do
129
- expect(@subject.call_count(anything, 'second_argument')).to eql 1
130
- end
131
-
132
- it 'finds when only the second argument is a wildcard exactly once' do
133
- expect(@subject.call_count('first_argument', anything)).to eql 1
120
+ context 'with an existing log' do
121
+ let(:expected_log) do
122
+ [
123
+ {
124
+ args: %w(first_argument second_argument),
125
+ stdin: 'first_stdin'
126
+ },
127
+ {
128
+ args: %w(first_argument),
129
+ stdin: 'second_stdin'
130
+ }
131
+ ]
134
132
  end
135
-
136
- it 'does not find when wildcard is in wrong position' do
137
- expect(@subject.call_count('first_argument', anything, 'second_argument')).to eql 0
138
- end
139
- end
140
- context 'and a command called with three arguments' do
141
133
  before(:each) do
142
- actual_call_log_list =
143
- [{
144
- 'args' => ['first_argument', 'second_argument', 'third_argument'],
145
- 'stdin' => [],
146
- }]
147
- @subject = Rspec::Bash::CallLog.new('command_with_three_arguments_log')
148
- allow(@subject).to receive(:load_call_log_list).and_return(actual_call_log_list)
149
- end
150
-
151
- it 'does not find first argument when other arguments are not provided' do
152
- expect(@subject.call_count('first_argument')).to eql 0
153
- end
154
-
155
- it 'finds the first argument anywhere in the series exactly once' do
156
- expect(@subject.call_count('first_argument', anything, anything)).to eql 1
157
- end
158
-
159
- it 'does not find second argument when other arguments are not provided' do
160
- expect(@subject.call_count('second_argument')).to eql 0
161
- end
162
-
163
- it 'finds the second argument anywhere in the series exactly once' do
164
- expect(@subject.call_count(anything, 'second_argument', anything)).to eql 1
165
- end
166
-
167
- it 'does not find third argument when other arguments are not provided' do
168
- expect(@subject.call_count('third_argument')).to eql 0
169
- end
170
-
171
- it 'finds the third argument anywhere in the series exactly once' do
172
- expect(@subject.call_count(anything, anything, 'third_argument')).to eql 1
173
- end
174
-
175
- it 'finds three contiguous arguments in the series exactly once' do
176
- expect(@subject.call_count('first_argument', 'second_argument', 'third_argument')).to eql 1
177
- end
178
-
179
- it 'does not find two non-contiguous arguments in the series exactly once' do
180
- expect(@subject.call_count('first_argument', 'third_argument')).to eql 0
181
- end
182
-
183
- it 'does not find an un-passed argument anywhere in the series' do
184
- expect(@subject.call_count('not_an_argument')).to eql 0
185
- end
186
-
187
- it 'finds when only the first argument is a wildcard' do
188
- expect(@subject.call_count(anything, 'second_argument', 'third_argument')).to eql 1
134
+ subject.call_log = [
135
+ {
136
+ args: %w(first_argument second_argument),
137
+ stdin: 'first_stdin'
138
+ }
139
+ ]
189
140
  end
190
-
191
- it 'finds when only the second argument is a wildcard' do
192
- expect(@subject.call_count('first_argument', anything, 'third_argument')).to eql 1
193
- end
194
-
195
- it 'finds when only the third argument is a wildcard' do
196
- expect(@subject.call_count('first_argument', 'second_argument', anything)).to eql 1
197
- end
198
-
199
- it 'finds when both the first and second arguments are wildcards' do
200
- expect(@subject.call_count(anything, anything, 'third_argument')).to eql 1
201
- end
202
-
203
- it 'finds when both the first and third arguments are wildcards' do
204
- expect(@subject.call_count(anything, 'second_argument', anything)).to eql 1
141
+ it 'adds a call log for the arguments passed in' do
142
+ subject.add_log('second_stdin', %w(first_argument))
143
+ expect(subject.call_log).to eql expected_log
205
144
  end
206
145
 
207
- it 'finds when both the second and third arguments are wildcards' do
208
- expect(@subject.call_count('first_argument', anything, anything)).to eql 1
209
- end
210
-
211
- it 'does not find when wildcard is in wrong position' do
212
- expect(@subject.call_count('first_argument', anything, 'second_argument', 'third_argument')).to eql 0
146
+ it 'writes that log to its log file' do
147
+ expect(mock_log_file).to receive(:write).with(expected_log.to_yaml)
148
+ subject.add_log('second_stdin', %w(first_argument))
213
149
  end
214
150
  end
215
- context 'with an argument called multiple times' do
216
- before(:each) do
217
- actual_call_log_list =
218
- [{
219
- 'args' => ['twice_called_arg'],
220
- 'stdin' => []
221
- },
222
- {
223
- 'args' => ['twice_called_arg'],
224
- 'stdin' => []
225
- }]
226
- @subject = Rspec::Bash::CallLog.new(anything)
227
- allow(@subject).to receive(:load_call_log_list).and_return(actual_call_log_list)
228
- end
229
- it 'returns 2 when argument is called 2 times' do
230
- expect(@subject.call_count('twice_called_arg')).to eql 2
231
- end
232
- end
233
- end
234
- end
235
-
236
- context '#called_with_args' do
237
- before(:each) do
238
- actual_call_log_list =
239
- [{
240
- 'args' => ['once_called_arg'],
241
- 'stdin' => []
242
- },
243
- {
244
- 'args' => ['twice_called_arg'],
245
- 'stdin' => []
246
- },
247
- {
248
- 'args' => ['twice_called_arg'],
249
- 'stdin' => []
250
- }]
251
- @subject = Rspec::Bash::CallLog.new(anything)
252
- allow(@subject).to receive(:load_call_log_list).and_return(actual_call_log_list)
253
- end
254
-
255
- it 'returns false when there are no matching args' do
256
- expect(@subject.called_with_args?('no-match')).to be_falsey
257
- end
258
-
259
- it 'returns true when there is a single matching arg' do
260
- expect(@subject.called_with_args?('once_called_arg', anything)).to be_truthy
261
151
  end
262
-
263
- it 'returns true when there are multiple matching args' do
264
- expect(@subject.called_with_args?('twice_called_arg')).to be_truthy
152
+ context 'with no config_path' do
153
+ subject { Rspec::Bash::CallConfiguration.new(nil, anything) }
154
+ it 'raises an error' do
155
+ expect do
156
+ subject.add_output('new_content', :stderr, %w(first_argument second_argument))
157
+ end.to raise_exception(NoMethodError)
158
+ end
265
159
  end
266
160
  end
267
-
268
- context '#called_with_no_args?' do
269
- it 'returns false if no call log is found' do
270
- @subject = Rspec::Bash::CallLog.new(anything)
271
- allow(YAML).to receive(:load_file).and_raise(Errno::ENOENT)
272
-
273
- expect(@subject.called_with_no_args?).to be_falsey
161
+ context '#call_log' do
162
+ context 'when there is no call_log_path' do
163
+ subject { Rspec::Bash::CallLog.new(nil) }
164
+ it 'returns an empty array' do
165
+ expect(subject.call_log).to eql []
166
+ end
274
167
  end
275
- it 'returns true if no arguments are in call log' do
276
- actual_call_log_list =
277
- [{
278
- 'args' => nil,
279
- 'stdin' => [],
280
- }]
281
- @subject = Rspec::Bash::CallLog.new(anything)
282
- allow(YAML).to receive(:load_file).and_return(actual_call_log_list)
283
-
284
- expect(@subject.called_with_no_args?).to be_truthy
168
+ context 'when the call log exists but is empty' do
169
+ subject { Rspec::Bash::CallLog.new(mock_log_pathname) }
170
+ before(:each) do
171
+ allow(mock_log_file).to receive(:read).and_return('')
172
+ end
173
+ it 'returns an empty array' do
174
+ expect(subject.call_log).to eql []
175
+ end
285
176
  end
286
- it 'returns fails if a single argument is in call log' do
287
- actual_call_log_list =
288
- [{
289
- 'args' => ['I am an argument'],
290
- 'stdin' => [],
291
- }]
292
- @subject = Rspec::Bash::CallLog.new(anything)
293
- allow(YAML).to receive(:load_file).and_return(actual_call_log_list)
294
-
295
- expect(@subject.called_with_no_args?).to be_falsey
177
+ context 'when the call log file open throws a file not found exception' do
178
+ subject { Rspec::Bash::CallLog.new(mock_log_pathname) }
179
+ before(:each) do
180
+ allow(mock_log_pathname).to receive(:open).with('r').and_raise(Errno::ENOENT)
181
+ end
182
+ it 'returns an empty array' do
183
+ expect(subject.call_log).to eql []
184
+ end
296
185
  end
297
- it 'returns fails if multiple arguments is in call log' do
298
- actual_call_log_list =
186
+ context 'when setup is valid' do
187
+ subject { Rspec::Bash::CallLog.new(mock_log_pathname) }
188
+ let(:log) do
299
189
  [{
300
- 'args' => ['I am an argument', 'as am I'],
301
- 'stdin' => [],
190
+ args: %w(first_argument second_argument),
191
+ stdin: 'first_stdin'
302
192
  }]
303
- @subject = Rspec::Bash::CallLog.new(anything)
304
- allow(YAML).to receive(:load_file).and_return(actual_call_log_list)
193
+ end
194
+ context 'and a call log exists' do
195
+ before(:each) do
196
+ allow(mock_log_file).to receive(:read).and_return(log.to_yaml)
197
+ end
305
198
 
306
- expect(@subject.called_with_no_args?).to be_falsey
199
+ it 'reads out what was in its configuration file' do
200
+ expect(subject.call_log).to eql log
201
+ end
202
+ end
307
203
  end
308
204
  end
309
205
  end