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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-bash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Brewer
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-10-11 00:00:00.000000000 Z
13
+ date: 2017-04-10 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -41,20 +41,20 @@ dependencies:
41
41
  - !ruby/object:Gem::Version
42
42
  version: '10.0'
43
43
  description: |2
44
- Stub and mock Bash commands
45
- Verify Bash calls and outputs
44
+ Test Bash scripts through stubbing and mocking Bash commands
46
45
  email:
47
46
  - ben@benbrewer.me
48
47
  - mike.david.urban@gmail.com
49
48
  executables:
50
49
  - function_override.sh.erb
51
50
  - function_override_wrapper.sh.erb
52
- - stub
51
+ - stub.rb.erb
53
52
  extensions: []
54
53
  extra_rdoc_files: []
55
54
  files:
56
55
  - .gitignore
57
56
  - .rspec
57
+ - .rubocop.yml
58
58
  - .ruby-version
59
59
  - .travis.yml
60
60
  - CHANGELOG.md
@@ -64,7 +64,7 @@ files:
64
64
  - Rakefile
65
65
  - bin/function_override.sh.erb
66
66
  - bin/function_override_wrapper.sh.erb
67
- - bin/stub
67
+ - bin/stub.rb.erb
68
68
  - lib/rspec/bash.rb
69
69
  - lib/rspec/bash/call_configuration.rb
70
70
  - lib/rspec/bash/call_log.rb
@@ -73,21 +73,32 @@ files:
73
73
  - lib/rspec/bash/matchers/called_with_no_arguments.rb
74
74
  - lib/rspec/bash/stubbed_command.rb
75
75
  - lib/rspec/bash/stubbed_env.rb
76
+ - lib/rspec/bash/util.rb
77
+ - lib/rspec/bash/util/call_conf_argument_list_matcher.rb
78
+ - lib/rspec/bash/util/call_log_argument_list_matcher.rb
79
+ - return_exitstatus_spec.rb
76
80
  - rspec-bash.gemspec
77
81
  - spec/classes/call_configuration_spec.rb
78
82
  - spec/classes/call_log_spec.rb
83
+ - spec/classes/stub_spec.rb
79
84
  - spec/classes/stubbed_command_spec.rb
80
85
  - spec/classes/stubbed_env_spec.rb
81
- - spec/integration/assert_called_spec.rb
82
- - spec/integration/assert_stdin_spec.rb
83
- - spec/integration/chain_args_spec.rb
84
- - spec/integration/change_exitstatus_spec.rb
85
- - spec/integration/provide_env_vars_spec.rb
86
- - spec/integration/replace_shell_commands_spec.rb
87
- - spec/integration/stub_output_spec.rb
88
- - spec/matchers/be_called_with_arguments_spec.rb
89
- - spec/matchers/be_called_with_no_arguments_spec.rb
86
+ - spec/classes/util/call_conf_argument_list_matcher_spec.rb
87
+ - spec/classes/util/call_log_argument_list_matcher_spec.rb
88
+ - spec/helper/shared_tmpdir.rb
89
+ - spec/helper/string_file_io.rb
90
+ - spec/integration/call_log/called_with_args_spec.rb
91
+ - spec/integration/call_log/called_with_no_args_spec.rb
92
+ - spec/integration/call_log/stdin_spec.rb
93
+ - spec/integration/matchers/be_called_with_arguments_spec.rb
94
+ - spec/integration/matchers/be_called_with_no_arguments_spec.rb
95
+ - spec/integration/stubbed_command/outputs_spec.rb
96
+ - spec/integration/stubbed_command/returns_exitstatus_spec.rb
97
+ - spec/integration/stubbed_env/execute_with_env_vars_spec.rb
98
+ - spec/integration/stubbed_env/execute_with_path_spec.rb
99
+ - spec/integration/stubbed_env/execute_with_stub_wrapper_spec.rb
90
100
  - spec/scripts/function_library.sh
101
+ - spec/spec_helper.rb
91
102
  homepage: https://github.com/mdurban/rspec-bash
92
103
  licenses:
93
104
  - MIT
@@ -115,15 +126,22 @@ summary: Test Bash with RSpec
115
126
  test_files:
116
127
  - spec/classes/call_configuration_spec.rb
117
128
  - spec/classes/call_log_spec.rb
129
+ - spec/classes/stub_spec.rb
118
130
  - spec/classes/stubbed_command_spec.rb
119
131
  - spec/classes/stubbed_env_spec.rb
120
- - spec/integration/assert_called_spec.rb
121
- - spec/integration/assert_stdin_spec.rb
122
- - spec/integration/chain_args_spec.rb
123
- - spec/integration/change_exitstatus_spec.rb
124
- - spec/integration/provide_env_vars_spec.rb
125
- - spec/integration/replace_shell_commands_spec.rb
126
- - spec/integration/stub_output_spec.rb
127
- - spec/matchers/be_called_with_arguments_spec.rb
128
- - spec/matchers/be_called_with_no_arguments_spec.rb
132
+ - spec/classes/util/call_conf_argument_list_matcher_spec.rb
133
+ - spec/classes/util/call_log_argument_list_matcher_spec.rb
134
+ - spec/helper/shared_tmpdir.rb
135
+ - spec/helper/string_file_io.rb
136
+ - spec/integration/call_log/called_with_args_spec.rb
137
+ - spec/integration/call_log/called_with_no_args_spec.rb
138
+ - spec/integration/call_log/stdin_spec.rb
139
+ - spec/integration/matchers/be_called_with_arguments_spec.rb
140
+ - spec/integration/matchers/be_called_with_no_arguments_spec.rb
141
+ - spec/integration/stubbed_command/outputs_spec.rb
142
+ - spec/integration/stubbed_command/returns_exitstatus_spec.rb
143
+ - spec/integration/stubbed_env/execute_with_env_vars_spec.rb
144
+ - spec/integration/stubbed_env/execute_with_path_spec.rb
145
+ - spec/integration/stubbed_env/execute_with_stub_wrapper_spec.rb
129
146
  - spec/scripts/function_library.sh
147
+ - spec/spec_helper.rb
data/bin/stub DELETED
@@ -1,62 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'pathname'
3
- require 'yaml'
4
-
5
- command = File.basename(__FILE__)
6
- folder = File.dirname(__FILE__)
7
-
8
- call_logs = Pathname.new(folder).join("#{command}_calls.yml")
9
- call_logs.open('a') do |f|
10
- f.puts '- args:'
11
- ARGV.each { |arg| f.puts " - #{arg.inspect}" }
12
- f.puts " stdin: #{$stdin.read.inspect}" unless STDIN.tty?
13
- end
14
-
15
- def interpolate_filename(elements)
16
- return elements if elements.is_a? String
17
- return nil unless elements.is_a? Array
18
-
19
- elements.map do |element|
20
- case element
21
- when String then element
22
- when Symbol then interpolate_argument(element)
23
- end
24
- end.join
25
- end
26
-
27
- def interpolate_argument(name)
28
- return unless (data = /^arg(\d+)$/.match(name.to_s))
29
- ARGV[data[1].to_i - 1]
30
- end
31
-
32
- call_configurations = Pathname.new(folder).join("#{command}_stub.yml")
33
- if call_configurations.exist?
34
- config = YAML.load call_configurations.read
35
-
36
- matching_calls = []
37
- config.each do |call_config|
38
- if !call_config[:args].empty?
39
- next unless (call_config[:args].eql? ARGV)
40
- end
41
- matching_calls << {
42
- config: call_config,
43
- specific: call_config[:args].length
44
- }
45
- end
46
- exit 0 if matching_calls.empty?
47
-
48
- call_config = matching_calls.sort do |a, b|
49
- b[:specific] <=> a[:specific]
50
- end.first[:config]
51
- (call_config[:outputs] || []).each do |data|
52
- $stdout.print data[:content] if data[:target] == :stdout
53
- $stderr.print data[:content] if data[:target] == :stderr
54
-
55
- output_filename = interpolate_filename(data[:target])
56
- next unless output_filename
57
- Pathname.new(output_filename).open('w') do |f|
58
- f.print data[:content]
59
- end
60
- end
61
- exit call_config[:statuscode] || 0
62
- end
@@ -1,48 +0,0 @@
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
@@ -1,39 +0,0 @@
1
- require 'English'
2
- require 'rspec/bash'
3
-
4
- describe 'Assert stdin' do
5
- include Rspec::Bash
6
- let(:stubbed_env) { create_stubbed_env }
7
- let!(:command1_stub) { stubbed_env.stub_command('command1') }
8
-
9
- let(:script) do
10
- <<-SCRIPT
11
- echo "foo bar" | command1
12
- echo "baz" | command1 'hello'
13
- SCRIPT
14
- end
15
- let(:script_path) { Pathname.new '/tmp/test_script.sh' }
16
-
17
- before do
18
- script_path.open('w') { |f| f.puts script }
19
- script_path.chmod 0777
20
-
21
- stubbed_env.execute script_path.to_s
22
- end
23
-
24
- after do
25
- script_path.delete
26
- end
27
-
28
- describe '#stdin' do
29
- it 'returns the stdin' do
30
- expect(command1_stub.stdin).to match 'foo bar'
31
- end
32
-
33
- context 'with arguments' do
34
- it 'returns the stdin' do
35
- expect(command1_stub.with_args('hello').stdin).to match 'baz'
36
- end
37
- end
38
- end
39
- end
@@ -1,65 +0,0 @@
1
- require 'English'
2
- require 'rspec/bash'
3
-
4
- describe 'Assert called' do
5
- context 'checking command is called with argument sequence' do
6
- include Rspec::Bash
7
- let(:stubbed_env) { create_stubbed_env }
8
- let!(:bundle) {
9
- stubbed_env.stub_command('bundle')
10
- }
11
-
12
- let(:script) do
13
- <<-SCRIPT
14
- bundle exec rake foo:bar
15
- SCRIPT
16
- end
17
- let(:script_path) { Pathname.new '/tmp/test_script.sh' }
18
-
19
- before do
20
- script_path.open('w') { |f| f.puts script }
21
- script_path.chmod 0777
22
-
23
- stubbed_env.execute script_path.to_s
24
- end
25
-
26
- after do
27
- script_path.delete
28
- end
29
-
30
- it 'is called with correct argument sequence' do
31
- expect(bundle).to be_called_with_arguments('exec', 'rake', 'foo:bar')
32
- expect(bundle).to be_called_with_arguments('exec', anything, 'foo:bar')
33
- expect(bundle).not_to be_called_with_arguments('exec', 'rake', 'foo')
34
- end
35
- end
36
- context 'checking command is called with no arguments' do
37
- include Rspec::Bash
38
- let(:stubbed_env) { create_stubbed_env }
39
- let!(:ls) {
40
- stubbed_env.stub_command('ls')
41
- }
42
-
43
- let(:script) do
44
- <<-SCRIPT
45
- ls
46
- SCRIPT
47
- end
48
- let(:script_path) { Pathname.new '/tmp/no_arg_test_script.sh' }
49
-
50
- before do
51
- script_path.open('w') { |f| f.puts script }
52
- script_path.chmod 0777
53
-
54
- stubbed_env.execute script_path.to_s
55
- end
56
-
57
- after do
58
- script_path.delete
59
- end
60
-
61
- it 'is called with no arguments' do
62
- expect(ls).to be_called_with_no_arguments
63
- end
64
- end
65
- end
@@ -1,53 +0,0 @@
1
- require 'English'
2
- require 'rspec/bash'
3
-
4
- describe 'Change exitstatus' do
5
- include Rspec::Bash
6
- let(:stubbed_env) { create_stubbed_env }
7
- let!(:command1_stub) { stubbed_env.stub_command('command1') }
8
- let(:script) do
9
- <<-SCRIPT
10
- command1 "foo bar"
11
- SCRIPT
12
- end
13
- let(:script_path) { Pathname.new '/tmp/test_script.sh' }
14
-
15
- before do
16
- script_path.open('w') { |f| f.puts script }
17
- script_path.chmod 0777
18
- end
19
-
20
- after do
21
- script_path.delete
22
- end
23
-
24
- describe 'default exitstatus' do
25
- it 'is 0' do
26
- output, error, status = stubbed_env.execute script_path.to_s
27
- expect(status.exitstatus).to eq 0
28
- end
29
- end
30
-
31
- describe 'changing exitstatus' do
32
- before do
33
- command1_stub.returns_exitstatus(4)
34
- end
35
-
36
- it 'returns the stubbed exitstatus' do
37
- output, error, status = stubbed_env.execute script_path.to_s
38
- expect(status.exitstatus).to eq 4
39
- end
40
-
41
- context 'with specific args only' do
42
- before do
43
- command1_stub.with_args('foo bar').returns_exitstatus(2)
44
- command1_stub.with_args('bar').returns_exitstatus(6)
45
- end
46
-
47
- it 'returns the stubbed exitstatus' do
48
- output, error, status = stubbed_env.execute script_path.to_s
49
- expect(status.exitstatus).to eq 2
50
- end
51
- end
52
- end
53
- end
@@ -1,31 +0,0 @@
1
- require 'English'
2
- require 'rspec/bash'
3
-
4
- describe 'Provide environment vars' do
5
- include Rspec::Bash
6
- let(:script) do
7
- <<-SCRIPT
8
- echo $SOME_ENV_VAR
9
- SCRIPT
10
- end
11
- let(:script_path) { Pathname.new '/tmp/test_script.sh' }
12
-
13
- before do
14
- script_path.open('w') { |f| f.puts script }
15
- script_path.chmod 0777
16
- end
17
-
18
- after do
19
- script_path.delete
20
- end
21
-
22
- let(:stubbed_env) { create_stubbed_env }
23
-
24
- it 'exits with an error' do
25
- o, _e, _s = stubbed_env.execute(
26
- script_path,
27
- 'SOME_ENV_VAR' => 'SekretCredential'
28
- )
29
- expect(o).to eql "SekretCredential\n"
30
- end
31
- end
@@ -1,48 +0,0 @@
1
- require 'English'
2
- require 'rspec/bash'
3
-
4
- describe 'Replace shell commands' do
5
- include Rspec::Bash
6
- let(:script) do
7
- <<-SCRIPT
8
- command1 "foo bar"
9
- SCRIPT
10
- end
11
- let(:script_path) { Pathname.new '/tmp/test_script.sh' }
12
-
13
- before do
14
- script_path.open('w') { |f| f.puts script }
15
- script_path.chmod 0777
16
- end
17
-
18
- after do
19
- script_path.delete
20
- end
21
-
22
- describe 'running a file with non-existing commands' do
23
- it 'exits with an error' do
24
- `#{script_path} 2>&1`
25
- expect($CHILD_STATUS.exitstatus).not_to eq 0
26
- end
27
-
28
- context 'with stubbed environment' do
29
- let(:stubbed_env) { create_stubbed_env }
30
-
31
- it 'exits with an error' do
32
- stubbed_env.execute "#{script_path} 2>&1"
33
- expect($CHILD_STATUS.exitstatus).not_to eq 0
34
- end
35
-
36
- context 'with a stubbed command' do
37
- before do
38
- stubbed_env.stub_command('command1')
39
- end
40
-
41
- it 'exits with status code 0' do
42
- _o, _e, s = stubbed_env.execute "#{script_path} 2>&1"
43
- expect(s.exitstatus).to eq 0
44
- end
45
- end
46
- end
47
- end
48
- end