rspec-bash 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,262 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'StubbedCommand' do
4
+ include Rspec::Bash
5
+ let(:stubbed_env) { create_stubbed_env }
6
+ let!(:command1_stub) { stubbed_env.stub_command('command1') }
7
+
8
+ describe '#outputs' do
9
+ describe 'target stdout' do
10
+ context 'when given no arguments to match' do
11
+ let(:output) do
12
+ command1_stub
13
+ .outputs('hello', to: :stdout)
14
+ output, = stubbed_env.execute_inline('command1 first_argument second_argument')
15
+ output
16
+ end
17
+
18
+ it 'outputs the expected output to stdout' do
19
+ expect(output).to eql 'hello'
20
+ end
21
+ end
22
+ context 'when given an exact argument match' do
23
+ let(:output) do
24
+ command1_stub
25
+ .with_args('first_argument', 'second_argument')
26
+ .outputs('hello', to: :stdout)
27
+ output, = stubbed_env.execute_inline('command1 first_argument second_argument')
28
+ output
29
+ end
30
+
31
+ it 'outputs the expected output to stdout' do
32
+ expect(output).to eql 'hello'
33
+ end
34
+ end
35
+ context 'when given an anything argument match' do
36
+ let(:output) do
37
+ command1_stub
38
+ .with_args('first_argument', anything)
39
+ .outputs('i respond to anything', to: :stdout)
40
+ output, = stubbed_env.execute_inline('command1 first_argument piglet')
41
+ output
42
+ end
43
+
44
+ it 'outputs the expected output to stdout' do
45
+ expect(output).to eql 'i respond to anything'
46
+ end
47
+ end
48
+ context 'when given any_args argument match' do
49
+ let(:output) do
50
+ command1_stub
51
+ .with_args(any_args)
52
+ .outputs('i respond to any_args', to: :stdout)
53
+ output, = stubbed_env.execute_inline('command1 poglet piglet')
54
+ output
55
+ end
56
+
57
+ it 'outputs the expected output to stdout' do
58
+ expect(output).to eql 'i respond to any_args'
59
+ end
60
+ end
61
+ context 'when given other types of RSpec::Mock::ArgumentMatcher argument match' do
62
+ let(:output) do
63
+ command1_stub
64
+ .with_args(instance_of(String), instance_of(String))
65
+ .outputs('i respond to instance_of', to: :stdout)
66
+ output, = stubbed_env.execute_inline('command1 poglet 1')
67
+ output
68
+ end
69
+
70
+ it 'outputs the expected output to stdout' do
71
+ expect(output).to eql 'i respond to instance_of'
72
+ end
73
+ end
74
+ context 'when given regex argument match' do
75
+ let(:output) do
76
+ command1_stub
77
+ .with_args(/p.glet/, /p.glet/)
78
+ .outputs('i respond to regex', to: :stdout)
79
+ output, = stubbed_env.execute_inline('command1 poglet piglet')
80
+ output
81
+ end
82
+
83
+ it 'outputs the expected output to stdout' do
84
+ expect(output).to eql 'i respond to regex'
85
+ end
86
+ end
87
+ end
88
+
89
+ # TODO: it is a bug that these require a \n in the output
90
+ describe 'target stderr' do
91
+ context 'when given no arguments to match' do
92
+ let(:error) do
93
+ command1_stub
94
+ .outputs('hello', to: :stderr)
95
+ _, error, = stubbed_env.execute_inline('command1 first_argument second_argument')
96
+ error
97
+ end
98
+
99
+ it 'outputs the expected error to stderr' do
100
+ expect(error).to eql "hello\n"
101
+ end
102
+ end
103
+ context 'when given an exact argument match' do
104
+ let(:error) do
105
+ command1_stub
106
+ .with_args('first_argument', 'second_argument')
107
+ .outputs('hello', to: :stderr)
108
+ _, error, = stubbed_env.execute_inline('command1 first_argument second_argument')
109
+ error
110
+ end
111
+
112
+ it 'outputs the expected error to stderr' do
113
+ expect(error).to eql "hello\n"
114
+ end
115
+ end
116
+ context 'when given an anything argument match' do
117
+ let(:error) do
118
+ command1_stub
119
+ .with_args('first_argument', anything)
120
+ .outputs('i respond to anything', to: :stderr)
121
+ _, error, = stubbed_env.execute_inline('command1 first_argument piglet')
122
+ error
123
+ end
124
+
125
+ it 'outputs the expected error to stderr' do
126
+ expect(error).to eql "i respond to anything\n"
127
+ end
128
+ end
129
+ context 'when given any_args argument match' do
130
+ let(:error) do
131
+ command1_stub
132
+ .with_args(any_args)
133
+ .outputs('i respond to any_args', to: :stderr)
134
+ _, error, = stubbed_env.execute_inline('command1 poglet piglet')
135
+ error
136
+ end
137
+
138
+ it 'outputs the expected error to stderr' do
139
+ expect(error).to eql "i respond to any_args\n"
140
+ end
141
+ end
142
+ context 'when given other types of RSpec::Mock::ArgumentMatcher argument match' do
143
+ let(:error) do
144
+ command1_stub
145
+ .with_args(instance_of(String), instance_of(String))
146
+ .outputs('i respond to instance_of', to: :stderr)
147
+ _, error, = stubbed_env.execute_inline('command1 poglet 1')
148
+ error
149
+ end
150
+
151
+ it 'outputs the expected error to stderr' do
152
+ expect(error).to eql "i respond to instance_of\n"
153
+ end
154
+ end
155
+ context 'when given regex argument match' do
156
+ let(:error) do
157
+ command1_stub
158
+ .with_args(/p.glet/, /p.glet/)
159
+ .outputs('i respond to regex', to: :stderr)
160
+ _, error, = stubbed_env.execute_inline('command1 poglet piglet')
161
+ error
162
+ end
163
+
164
+ it 'outputs the expected error to stderr' do
165
+ expect(error).to eql "i respond to regex\n"
166
+ end
167
+ end
168
+ end
169
+
170
+ describe 'target file path' do
171
+ let(:temp_file) { Tempfile.new('for-testing') }
172
+ context 'when given no arguments to match' do
173
+ before(:each) do
174
+ command1_stub
175
+ .outputs('hello', to: temp_file.path)
176
+ stubbed_env.execute_inline('command1 first_argument second_argument')
177
+ end
178
+
179
+ it 'outputs the expected content to the file' do
180
+ expect(temp_file.read).to eql 'hello'
181
+ end
182
+ end
183
+ context 'when given an exact argument match' do
184
+ before(:each) do
185
+ command1_stub
186
+ .with_args('first_argument', 'second_argument')
187
+ .outputs('hello', to: temp_file.path)
188
+ stubbed_env.execute_inline('command1 first_argument second_argument')
189
+ end
190
+
191
+ it 'outputs the expected content to the file' do
192
+ expect(temp_file.read).to eql 'hello'
193
+ end
194
+ end
195
+ context 'when given an anything argument match' do
196
+ before(:each) do
197
+ command1_stub
198
+ .with_args('first_argument', anything)
199
+ .outputs('i respond to anything', to: temp_file.path)
200
+ stubbed_env.execute_inline('command1 first_argument second_argument')
201
+ end
202
+
203
+ it 'outputs the expected content to the file' do
204
+ expect(temp_file.read).to eql 'i respond to anything'
205
+ end
206
+ end
207
+ context 'when given any_args argument match' do
208
+ before(:each) do
209
+ command1_stub
210
+ .with_args(any_args)
211
+ .outputs('i respond to any_args', to: temp_file.path)
212
+ stubbed_env.execute_inline('command1 poglet piglet')
213
+ end
214
+
215
+ it 'outputs the expected content to the file' do
216
+ expect(temp_file.read).to eql 'i respond to any_args'
217
+ end
218
+ end
219
+ context 'when given other types of RSpec::Mock::ArgumentMatcher argument match' do
220
+ before(:each) do
221
+ command1_stub
222
+ .with_args(instance_of(String), instance_of(String))
223
+ .outputs('i respond to instance_of', to: temp_file.path)
224
+ stubbed_env.execute_inline('command1 poglet 1')
225
+ end
226
+
227
+ it 'outputs the expected content to the file' do
228
+ expect(temp_file.read).to eql 'i respond to instance_of'
229
+ end
230
+ end
231
+ context 'when given regex argument match' do
232
+ before(:each) do
233
+ command1_stub
234
+ .with_args(/p.glet/, /p.glet/)
235
+ .outputs('i respond to regex', to: temp_file.path)
236
+ stubbed_env.execute_inline('command1 poglet piglet')
237
+ end
238
+
239
+ it 'outputs the expected content to the file' do
240
+ expect(temp_file.read).to eql 'i respond to regex'
241
+ end
242
+ end
243
+
244
+ describe 'when given a passed argument for a filename' do
245
+ let(:dynamic_file) { Pathname.new('output-piglet.txt') }
246
+ before(:each) do
247
+ command1_stub
248
+ .outputs('i have a dynamic file name', to: ['output-', :arg2, '.txt'])
249
+ stubbed_env.execute_inline('command1 poglet piglet')
250
+ end
251
+
252
+ it 'outputs the expected content to the file' do
253
+ expect(dynamic_file.read).to eql 'i have a dynamic file name'
254
+ end
255
+
256
+ after(:each) do
257
+ FileUtils.remove_entry_secure dynamic_file
258
+ end
259
+ end
260
+ end
261
+ end
262
+ end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'StubbedCommand' do
4
+ include Rspec::Bash
5
+ let(:stubbed_env) { create_stubbed_env }
6
+ let!(:command1_stub) { stubbed_env.stub_command('command1') }
7
+
8
+ context '#returns_exitstatus' do
9
+ context 'when given no exit status to return' do
10
+ let(:status) do
11
+ _, _, status = stubbed_env.execute_inline('command1 first_argument second_argument')
12
+ status.exitstatus
13
+ end
14
+
15
+ it 'exits with the appropriate exit code' do
16
+ expect(status).to be 0
17
+ end
18
+ end
19
+
20
+ context 'when given no args to match' do
21
+ let(:status) do
22
+ command1_stub
23
+ .returns_exitstatus(100)
24
+ _, _, status = stubbed_env.execute_inline('command1 first_argument second_argument')
25
+ status.exitstatus
26
+ end
27
+
28
+ it 'exits with the appropriate exit code' do
29
+ expect(status).to be 100
30
+ end
31
+ end
32
+
33
+ context 'when given an exact argument match' do
34
+ let(:status) do
35
+ command1_stub
36
+ .with_args('first_argument', 'second_argument')
37
+ .returns_exitstatus(101)
38
+ _, _, status = stubbed_env.execute_inline('command1 first_argument second_argument')
39
+ status.exitstatus
40
+ end
41
+
42
+ it 'exits with the appropriate exit code' do
43
+ expect(status).to be 101
44
+ end
45
+ end
46
+ context 'when given an anything argument match' do
47
+ let(:status) do
48
+ command1_stub
49
+ .with_args('first_argument', anything)
50
+ .returns_exitstatus(102)
51
+ _, _, status = stubbed_env.execute_inline('command1 first_argument second_argument')
52
+ status.exitstatus
53
+ end
54
+
55
+ it 'exits with the appropriate exit code' do
56
+ expect(status).to be 102
57
+ end
58
+ end
59
+ context 'when given any_args argument match' do
60
+ let(:status) do
61
+ command1_stub
62
+ .with_args(any_args)
63
+ .returns_exitstatus(103)
64
+ _, _, status = stubbed_env.execute_inline('command1 poglet piglet')
65
+ status.exitstatus
66
+ end
67
+
68
+ it 'exits with the appropriate exit code' do
69
+ expect(status).to be 103
70
+ end
71
+ end
72
+ context 'when given other types of RSpec::Mock::ArgumentMatcher argument match' do
73
+ let(:status) do
74
+ command1_stub
75
+ .with_args(instance_of(String), instance_of(String))
76
+ .returns_exitstatus(104)
77
+ _, _, status = stubbed_env.execute_inline('command1 poglet 1')
78
+ status.exitstatus
79
+ end
80
+
81
+ it 'exits with the appropriate exit code' do
82
+ expect(status).to be 104
83
+ end
84
+ end
85
+ context 'when given regex argument match' do
86
+ let(:status) do
87
+ command1_stub
88
+ .with_args(/p.glet/, /p.glet/)
89
+ .returns_exitstatus(105)
90
+ _, _, status = stubbed_env.execute_inline('command1 poglet piglet')
91
+ status.exitstatus
92
+ end
93
+
94
+ it 'exits with the appropriate exit code' do
95
+ expect(status).to be 105
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'StubbedEnv' do
4
+ include Rspec::Bash
5
+
6
+ context '#execute(..., ENV => VARIABLES)' do
7
+ let(:stubbed_env) { create_stubbed_env }
8
+
9
+ it 'exits with an error' do
10
+ stdout, = stubbed_env.execute_inline(
11
+ 'echo $SOME_ENV_VAR',
12
+ 'SOME_ENV_VAR' => 'SekretCredential'
13
+ )
14
+ expect(stdout).to eql "SekretCredential\n"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'StubbedEnv' do
4
+ include Rspec::Bash
5
+
6
+ context '#execute(path, ...)' do
7
+ describe 'running a file with non-existing commands' do
8
+ it 'exits with an error' do
9
+ `command1 "foo bar" 2>&1`
10
+ expect($CHILD_STATUS.exitstatus).not_to eq 0
11
+ end
12
+
13
+ context 'with stubbed environment' do
14
+ let(:stubbed_env) { create_stubbed_env }
15
+
16
+ it 'exits with an error' do
17
+ stubbed_env.execute_inline 'command1 "foo bar" 2>&1'
18
+ expect($CHILD_STATUS.exitstatus).not_to eq 0
19
+ end
20
+
21
+ context 'with a stubbed command' do
22
+ before do
23
+ stubbed_env.stub_command('command1')
24
+ end
25
+
26
+ it 'exits with status code 0' do
27
+ _, _, status = stubbed_env.execute_inline 'command1 "foo bar" 2>&1'
28
+ expect(status.exitstatus).to eq 0
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,23 @@
1
+ require 'English'
2
+ require 'rspec/bash'
3
+
4
+ describe 'StubbedEnv' do
5
+ include Rspec::Bash
6
+ let(:stubbed_env) { create_stubbed_env }
7
+ let!(:grep_mock) { stubbed_env.stub_command('grep') }
8
+ let!(:ls_mock) { stubbed_env.stub_command('ls') }
9
+
10
+ context '#execute(<commands that are in stub wrapper>, ...)' do
11
+ it 'does not call the grep command' do
12
+ stubbed_env.execute_inline('exit 0')
13
+
14
+ expect(grep_mock).to_not be_called
15
+ end
16
+
17
+ it 'does not call the ls command' do
18
+ stubbed_env.execute_inline('exit 0')
19
+
20
+ expect(ls_mock).to_not be_called
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,10 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'English'
5
+ require 'rspec/bash'
6
+ require 'tempfile'
7
+ require 'yaml'
8
+
9
+ require 'helper/string_file_io'
10
+ require 'helper/shared_tmpdir'