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,39 @@
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
@@ -0,0 +1,65 @@
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
@@ -0,0 +1,53 @@
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
@@ -0,0 +1,31 @@
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
@@ -0,0 +1,48 @@
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
@@ -0,0 +1,110 @@
1
+ require 'English'
2
+ require 'rspec/bash'
3
+
4
+ describe 'Stub command output' 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
+ command1
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
+ end
20
+
21
+ after do
22
+ script_path.delete
23
+ end
24
+
25
+ describe 'stubbing standard-out' do
26
+ it 'changes standard-out' do
27
+ command1_stub.outputs('hello', to: :stdout)
28
+ output, error, status = stubbed_env.execute "#{script_path} 2>/dev/null"
29
+
30
+ expect(output).to eql 'hello'
31
+ expect(error).to be_empty
32
+ end
33
+ end
34
+
35
+ describe 'stubbing standard-err' do
36
+ it 'changes standard-out' do
37
+ command1_stub.outputs('world', to: :stderr)
38
+ output, error, status = stubbed_env.execute "#{script_path} 1>/dev/null"
39
+ expect(error).to eql "world\n"
40
+ expect(output).to be_empty
41
+ end
42
+ end
43
+
44
+ describe 'stubbing contents to file' do
45
+ let(:filename) { 'test-log.nice' }
46
+ after do
47
+ f = Pathname.new(filename)
48
+ f.delete if f.exist?
49
+ end
50
+
51
+ it 'write data to a file' do
52
+ command1_stub.outputs('world', to: filename)
53
+ output, error, status = stubbed_env.execute "#{script_path}"
54
+
55
+ expect(error).to be_empty
56
+ expect(output).to be_empty
57
+ expect(Pathname.new(filename).read).to eql 'world'
58
+ end
59
+
60
+ describe 'using passed argument as filename' do
61
+ let(:script) do
62
+ <<-SCRIPT
63
+ command1 input output
64
+ SCRIPT
65
+ end
66
+
67
+ let(:passed_filename) { ['hello-', :arg2, '.foo'] }
68
+ let(:filename) { 'hello-output.foo' }
69
+
70
+ it 'writes data to an interpolated filename' do
71
+ command1_stub.outputs('world', to: passed_filename)
72
+ stubbed_env.execute "#{script_path}"
73
+
74
+ expect(Pathname.new(filename).read).to eql 'world'
75
+ end
76
+ end
77
+ end
78
+
79
+ describe 'stubbing commands with arguments passed to stdout' do
80
+ let(:script) do
81
+ <<-SCRIPT
82
+ command1 input output
83
+ SCRIPT
84
+ end
85
+
86
+ it 'outputs correctly when all arguments match' do
87
+ command1_stub.with_args('input', 'output').outputs('world', to: :stdout)
88
+ output, error, status = stubbed_env.execute "#{script_path}"
89
+
90
+ expect(error).to be_empty
91
+ expect(output).to eql 'world'
92
+ end
93
+
94
+ it 'does not output when called with extra arguments, even if some match' do
95
+ command1_stub.with_args('input', 'output', 'anything').outputs('arbitrary string', to: :stdout)
96
+ output, error, status = stubbed_env.execute "#{script_path}"
97
+
98
+ expect(error).to be_empty
99
+ expect(output).to be_empty
100
+ end
101
+
102
+ it 'does not output when called with only one matching argument out of many' do
103
+ command1_stub.with_args('input').outputs('arbitrary string', to: :stdout)
104
+ output, error, status = stubbed_env.execute "#{script_path}"
105
+
106
+ expect(error).to be_empty
107
+ expect(output).to be_empty
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,55 @@
1
+ require 'English'
2
+ require 'rspec/bash'
3
+
4
+ # TODO - the below specs test implementation, until the goofy wiring of StubbedCommand => StubbedCall => CallLog is sorted out
5
+
6
+ describe 'be_called_with_arguments' do
7
+ include Rspec::Bash
8
+ let(:stubbed_env) { create_stubbed_env }
9
+
10
+ context 'with a command' do
11
+ context 'and no chain calls' do
12
+ before(:each) do
13
+ @command = stubbed_env.stub_command('stubbed_command')
14
+ @actual_stdout, @actual_stderr, @actual_status = stubbed_env.execute_inline(<<-multiline_script
15
+ stubbed_command first_argument second_argument
16
+ multiline_script
17
+ )
18
+ end
19
+ it 'correctly identifies the called arguments' do
20
+ expect(@command).to be_called_with_arguments('first_argument', 'second_argument')
21
+ end
22
+ it 'correctly matches when wildcard is used for first argument' do
23
+ expect(@command).to be_called_with_arguments(anything, 'second_argument')
24
+ end
25
+ it 'correctly matches when wildcard is used for second argument' do
26
+ expect(@command).to be_called_with_arguments('first_argument', anything)
27
+ end
28
+ it 'correctly matches when wildcard is used for all arguments' do
29
+ expect(@command).to be_called_with_arguments(anything, anything)
30
+ end
31
+ end
32
+ context 'and the times chain call' do
33
+ before(:each) do
34
+ @command = stubbed_env.stub_command('stubbed_command')
35
+ @actual_stdout, @actual_stderr, @actual_status = stubbed_env.execute_inline(<<-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).to be_called_with_arguments('duplicated_argument', anything).times(2)
43
+ end
44
+ it 'matches when argument is called once' do
45
+ expect(@command).to be_called_with_arguments(anything, 'once_called_argument').times(1)
46
+ end
47
+ it 'matches when argument combination is called once' do
48
+ expect(@command).to be_called_with_arguments('duplicated_argument', 'once_called_argument').times(1)
49
+ end
50
+ it 'matches when argument is not called' do
51
+ expect(@command).to_not be_called_with_arguments('not_called_argument').times(1)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,32 @@
1
+ require 'English'
2
+ require 'rspec/bash'
3
+
4
+ describe 'be_called_with_no_arguments' do
5
+ include Rspec::Bash
6
+ let(:stubbed_env) { create_stubbed_env }
7
+
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(<<-multiline_script
12
+ stubbed_command
13
+ multiline_script
14
+ )
15
+ end
16
+ it 'correctly identifies that no arguments were called' do
17
+ expect(@command).to be_called_with_no_arguments
18
+ end
19
+ end
20
+ context 'when command is called with args' do
21
+ before(:each) do
22
+ @command = stubbed_env.stub_command('stubbed_command')
23
+ @actual_stdout, @actual_stderr, @actual_status = stubbed_env.execute_inline(<<-multiline_script
24
+ stubbed_command argument
25
+ multiline_script
26
+ )
27
+ end
28
+ it 'correctly identifies that arguments were passed into command call' do
29
+ expect(@command).to_not be_called_with_no_arguments
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env bash
2
+ function overridden_function {
3
+ echo 'i was not overridden'
4
+ }
5
+
6
+ function overridden_command_function {
7
+ overridden_command "${1}" "${2}"
8
+ echo 'standard error output' >&2
9
+ }