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
@@ -0,0 +1,211 @@
1
+ require 'spec_helper'
2
+ include Rspec::Bash::Util
3
+
4
+ describe 'CallLogArgumentListMatcher' do
5
+ context '#get_call_count' do
6
+ context 'given a call log list with a with multiple sets of arguments' do
7
+ let(:call_log_list) do
8
+ [
9
+ {
10
+ stdin: 'first_stdin',
11
+ args: %w(first_argument second_argument)
12
+ },
13
+ {
14
+ stdin: 'second_stdin',
15
+ args: %w(first_argument second_argument third_argument)
16
+ },
17
+ {
18
+ stdin: 'third_stdin',
19
+ args: %w(first_argument second_argument)
20
+ }
21
+ ]
22
+ end
23
+
24
+ it 'returns the correct count for a single exact argument match' do
25
+ argument_list_to_match = %w(first_argument second_argument third_argument)
26
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
27
+ actual_match_count = subject.get_call_count(call_log_list)
28
+ expect(actual_match_count).to be 1
29
+ end
30
+
31
+ it 'returns the correct count for multiple exact argument matches' do
32
+ argument_list_to_match = %w(first_argument second_argument)
33
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
34
+ actual_match_count = subject.get_call_count(call_log_list)
35
+ expect(actual_match_count).to be 2
36
+ end
37
+
38
+ it 'returns the correct count for no argument matches' do
39
+ argument_list_to_match = %w(first_argument)
40
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
41
+ actual_match_count = subject.get_call_count(call_log_list)
42
+ expect(actual_match_count).to be 0
43
+ end
44
+
45
+ it 'returns the correct count for a single "anything" match' do
46
+ argument_list_to_match = ['first_argument', anything, 'third_argument']
47
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
48
+ actual_match_count = subject.get_call_count(call_log_list)
49
+ expect(actual_match_count).to be 1
50
+ end
51
+
52
+ it 'returns the correct count for multiple "anything" matches' do
53
+ argument_list_to_match = [anything, 'second_argument']
54
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
55
+ actual_match_count = subject.get_call_count(call_log_list)
56
+ expect(actual_match_count).to be 2
57
+ end
58
+
59
+ it 'returns the correct count for "anything" matches that are not the exact count' do
60
+ argument_list_to_match = [anything, anything, anything, anything]
61
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
62
+ actual_match_count = subject.get_call_count(call_log_list)
63
+ expect(actual_match_count).to be 0
64
+ end
65
+
66
+ it 'returns the correct count for a no expected argument list' do
67
+ subject = CallLogArgumentListMatcher.new
68
+ actual_match_count = subject.get_call_count(call_log_list)
69
+ expect(actual_match_count).to be 3
70
+ end
71
+ end
72
+ end
73
+
74
+ context '#get_call_log_matches' do
75
+ context 'given a call log list with a with multiple sets of arguments and stdin' do
76
+ let(:call_log_list) do
77
+ [
78
+ {
79
+ stdin: 'first_stdin',
80
+ args: %w(first_argument second_argument)
81
+ },
82
+ {
83
+ stdin: 'second_stdin',
84
+ args: %w(first_argument second_argument third_argument)
85
+ },
86
+ {
87
+ stdin: 'third_stdin',
88
+ args: %w(first_argument second_argument)
89
+ }
90
+ ]
91
+ end
92
+
93
+ it 'returns the correct call log entries for a single exact argument match' do
94
+ argument_list_to_match = %w(first_argument second_argument third_argument)
95
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
96
+ matches = subject.get_call_log_matches(call_log_list)
97
+ expect(matches).to eql call_log_list.values_at(1)
98
+ end
99
+
100
+ it 'returns the correct call log entries for multiple exact argument matches' do
101
+ argument_list_to_match = %w(first_argument second_argument)
102
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
103
+ matches = subject.get_call_log_matches(call_log_list)
104
+ expect(matches).to eql call_log_list.values_at(0, 2)
105
+ end
106
+
107
+ it 'returns the correct call log entries for no argument matches' do
108
+ argument_list_to_match = %w(first_argument)
109
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
110
+ matches = subject.get_call_log_matches(call_log_list)
111
+ expect(matches).to eql []
112
+ end
113
+
114
+ it 'returns the correct call log entries for a single "anything" match' do
115
+ argument_list_to_match = ['first_argument', anything, 'third_argument']
116
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
117
+ matches = subject.get_call_log_matches(call_log_list)
118
+ expect(matches).to eql call_log_list.values_at(1)
119
+ end
120
+
121
+ it 'returns the correct call log entries for multiple "anything" matches' do
122
+ argument_list_to_match = [anything, 'second_argument']
123
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
124
+ matches = subject.get_call_log_matches(call_log_list)
125
+ expect(matches).to eql call_log_list.values_at(0, 2)
126
+ end
127
+
128
+ it 'returns the correct call log entries for "anything" matches not matching count' do
129
+ argument_list_to_match = [anything, anything, anything, anything]
130
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
131
+ matches = subject.get_call_log_matches(call_log_list)
132
+ expect(matches).to eql []
133
+ end
134
+
135
+ it 'returns the correct call log entries for no expected argument list' do
136
+ subject = CallLogArgumentListMatcher.new
137
+ matches = subject.get_call_log_matches(call_log_list)
138
+ expect(matches).to eql call_log_list
139
+ end
140
+ end
141
+ end
142
+
143
+ context '#args_match?' do
144
+ context 'given a call list with a with multiple sets of arguments' do
145
+ let(:call_log_list) do
146
+ [
147
+ {
148
+ stdin: 'first_stdin',
149
+ args: %w(first_argument second_argument)
150
+ },
151
+ {
152
+ stdin: 'second_stdin',
153
+ args: %w(first_argument second_argument third_argument)
154
+ },
155
+ {
156
+ stdin: 'third_stdin',
157
+ args: %w(first_argument second_argument)
158
+ }
159
+ ]
160
+ end
161
+
162
+ it 'returns true for a single exact argument match' do
163
+ argument_list_to_match = %w(first_argument second_argument third_argument)
164
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
165
+ matches = subject.args_match?(call_log_list)
166
+ expect(matches).to be true
167
+ end
168
+
169
+ it 'returns true for multiple exact argument matches' do
170
+ argument_list_to_match = %w(first_argument second_argument)
171
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
172
+ matches = subject.args_match?(call_log_list)
173
+ expect(matches).to be true
174
+ end
175
+
176
+ it 'returns false for no argument matches' do
177
+ argument_list_to_match = %w(first_argument)
178
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
179
+ matches = subject.args_match?(call_log_list)
180
+ expect(matches).to be false
181
+ end
182
+
183
+ it 'returns true for a single "anything" match' do
184
+ argument_list_to_match = ['first_argument', anything, 'third_argument']
185
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
186
+ matches = subject.args_match?(call_log_list)
187
+ expect(matches).to be true
188
+ end
189
+
190
+ it 'returns true for multiple "anything" matches' do
191
+ argument_list_to_match = [anything, 'second_argument']
192
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
193
+ matches = subject.args_match?(call_log_list)
194
+ expect(matches).to be true
195
+ end
196
+
197
+ it 'returns false for "anything" matches that are not the exact count' do
198
+ argument_list_to_match = [anything, anything, anything, anything]
199
+ subject = CallLogArgumentListMatcher.new(*argument_list_to_match)
200
+ matches = subject.args_match?(call_log_list)
201
+ expect(matches).to be false
202
+ end
203
+
204
+ it 'returns true for no expected argument list' do
205
+ subject = CallLogArgumentListMatcher.new
206
+ matches = subject.args_match?(call_log_list)
207
+ expect(matches).to be true
208
+ end
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,6 @@
1
+ RSpec.shared_examples 'manage a :temp_directory' do
2
+ let(:temp_directory) { Dir.mktmpdir }
3
+ after(:each) do
4
+ FileUtils.remove_entry_secure temp_directory
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ class StringFileIO < StringIO
2
+ def read
3
+ string
4
+ end
5
+
6
+ def write(new_string)
7
+ self.string = new_string
8
+ end
9
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'CallLog' do
4
+ include Rspec::Bash
5
+ let(:stubbed_env) { create_stubbed_env }
6
+ let!(:first_command) { stubbed_env.stub_command('first_command') }
7
+
8
+ context '#called_with_args?' do
9
+ context 'when given multiple command calls' do
10
+ before(:each) do
11
+ stubbed_env.execute_inline(
12
+ <<-multiline_script
13
+ first_command first_argument second_argument
14
+ first_command first_argument second_argument third_argument
15
+ multiline_script
16
+ )
17
+ end
18
+
19
+ it 'matches for implied anything matches' do
20
+ expect(first_command).to be_called_with_arguments
21
+ end
22
+
23
+ it 'does not match for non-matches' do
24
+ expect(first_command).to_not be_called_with_arguments('first_argument')
25
+ end
26
+
27
+ it 'matches for exact matches' do
28
+ expect(first_command).to be_called_with_arguments('first_argument', 'second_argument')
29
+ end
30
+
31
+ it 'matches for anything matches' do
32
+ expect(first_command).to be_called_with_arguments(anything, anything, 'third_argument')
33
+ end
34
+
35
+ it 'matches for anyd_args matches' do
36
+ expect(first_command).to be_called_with_arguments(any_args)
37
+ end
38
+
39
+ it 'matches for other types of RSpec::Mock::ArgumentMatcher matches' do
40
+ expect(first_command).to be_called_with_arguments(instance_of(String), instance_of(String))
41
+ end
42
+
43
+ it 'matches for regex matches' do
44
+ expect(first_command).to be_called_with_arguments(/f..st_argument/, /se..nd_argument/)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'CallLog' do
4
+ context '#called_with_no_args?' do
5
+ include Rspec::Bash
6
+ let(:stubbed_env) { create_stubbed_env }
7
+ let!(:ls) { stubbed_env.stub_command('ls') }
8
+
9
+ before do
10
+ stubbed_env.execute_inline(
11
+ <<-multiline_script
12
+ ls
13
+ multiline_script
14
+ )
15
+ end
16
+
17
+ it 'is called with no arguments' do
18
+ expect(ls).to be_called_with_no_arguments
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'CallLog' do
4
+ include Rspec::Bash
5
+ let(:stubbed_env) { create_stubbed_env }
6
+ let!(:first_command) { stubbed_env.stub_command('first_command') }
7
+
8
+ context '#stdin' do
9
+ context 'when given multiple command calls' do
10
+ before(:each) do
11
+ stubbed_env.execute_inline(
12
+ <<-multiline_script
13
+ echo -n 'first_call' | first_command first_argument second_argument
14
+ echo -n 'second_call' | first_command first_argument second_argument third_argument
15
+ multiline_script
16
+ )
17
+ end
18
+
19
+ it 'matches for implied anything matches' do
20
+ expect(first_command.stdin).to eql 'first_call'
21
+ end
22
+
23
+ it 'does not match for non-matches' do
24
+ expect(first_command.with_args('first_argument').stdin).to be nil
25
+ end
26
+
27
+ it 'matches for exact matches' do
28
+ expect(first_command
29
+ .with_args('first_argument', 'second_argument').stdin).to eql 'first_call'
30
+ end
31
+
32
+ it 'matches for anything matches' do
33
+ expect(first_command
34
+ .with_args(anything, anything, 'third_argument').stdin).to eql 'second_call'
35
+ end
36
+
37
+ it 'matches for any_args matches' do
38
+ expect(first_command
39
+ .with_args(any_args).stdin).to eql 'first_call'
40
+ end
41
+
42
+ it 'matches for other types of RSpec::Mock::ArgumentMatcher matches' do
43
+ expect(first_command
44
+ .with_args(instance_of(String), instance_of(String)).stdin).to eql 'first_call'
45
+ end
46
+
47
+ it 'matches for regex matches' do
48
+ expect(first_command
49
+ .with_args(/f..st_argument/, /se..nd_argument/, /.*/).stdin).to eql 'second_call'
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'RSpec::Matchers' do
4
+ include Rspec::Bash
5
+ let(:stubbed_env) { create_stubbed_env }
6
+
7
+ context '.be_called_with_arguments' do
8
+ context 'with a command' do
9
+ context 'and no chain calls' do
10
+ before(:each) do
11
+ @command = stubbed_env.stub_command('stubbed_command')
12
+ @actual_stdout, @actual_stderr, @actual_status = stubbed_env.execute_inline(
13
+ <<-multiline_script
14
+ stubbed_command first_argument second_argument
15
+ multiline_script
16
+ )
17
+ end
18
+ it 'correctly identifies the called arguments' do
19
+ expect(@command).to be_called_with_arguments('first_argument', 'second_argument')
20
+ end
21
+ it 'correctly matches when wildcard is used for first argument' do
22
+ expect(@command).to be_called_with_arguments(anything, 'second_argument')
23
+ end
24
+ it 'correctly matches when wildcard is used for second argument' do
25
+ expect(@command).to be_called_with_arguments('first_argument', anything)
26
+ end
27
+ it 'correctly matches when wildcard is used for all arguments' do
28
+ expect(@command).to be_called_with_arguments(anything, anything)
29
+ end
30
+ end
31
+ context 'and the times chain call' do
32
+ before(:each) do
33
+ @command = stubbed_env.stub_command('stubbed_command')
34
+ @actual_stdout, @actual_stderr, @actual_status = stubbed_env.execute_inline(
35
+ <<-multiline_script
36
+ stubbed_command duplicated_argument once_called_argument
37
+ stubbed_command duplicated_argument irrelevant_argument
38
+ multiline_script
39
+ )
40
+ end
41
+ it 'matches when arguments are called twice' do
42
+ expect(@command)
43
+ .to be_called_with_arguments('duplicated_argument', anything).times(2)
44
+ end
45
+ it 'matches when argument is called once' do
46
+ expect(@command)
47
+ .to be_called_with_arguments(anything, 'once_called_argument').times(1)
48
+ end
49
+ it 'matches when argument combination is called once' do
50
+ expect(@command)
51
+ .to be_called_with_arguments('duplicated_argument', 'once_called_argument').times(1)
52
+ end
53
+ it 'matches when argument is not called' do
54
+ expect(@command)
55
+ .to_not be_called_with_arguments('not_called_argument').times(1)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'RSpec::Matchers' do
4
+ include Rspec::Bash
5
+ let(:stubbed_env) { create_stubbed_env }
6
+
7
+ context '.be_called_with_no_arguments' do
8
+ context 'when command is called with no args' do
9
+ before(:each) do
10
+ @command = stubbed_env.stub_command('stubbed_command')
11
+ @actual_stdout, @actual_stderr, @actual_status = stubbed_env.execute_inline(
12
+ <<-multiline_script
13
+ stubbed_command
14
+ multiline_script
15
+ )
16
+ end
17
+ it 'correctly identifies that no arguments were called' do
18
+ expect(@command).to be_called_with_no_arguments
19
+ end
20
+ end
21
+ context 'when command is called with args' do
22
+ before(:each) do
23
+ @command = stubbed_env.stub_command('stubbed_command')
24
+ @actual_stdout, @actual_stderr, @actual_status = stubbed_env.execute_inline(
25
+ <<-multiline_script
26
+ stubbed_command argument
27
+ multiline_script
28
+ )
29
+ end
30
+ it 'correctly identifies that arguments were passed into command call' do
31
+ expect(@command).to_not be_called_with_no_arguments
32
+ end
33
+ end
34
+ end
35
+ end