rspec-bash 0.1.1 → 0.2.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 (68) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +8 -0
  3. data/Gemfile +1 -0
  4. data/README.md +23 -0
  5. data/Rakefile +15 -4
  6. data/bin/bash_stub.sh +92 -0
  7. data/bin/bash_wrapper.sh.erb +12 -0
  8. data/bin/ruby_stub.rb +33 -0
  9. data/lib/rspec/bash.rb +5 -4
  10. data/lib/rspec/bash/command.rb +5 -0
  11. data/lib/rspec/bash/command/call_configuration.rb +76 -0
  12. data/lib/rspec/bash/command/call_configuration_manager.rb +24 -0
  13. data/lib/rspec/bash/command/call_log.rb +48 -0
  14. data/lib/rspec/bash/command/call_log_manager.rb +38 -0
  15. data/lib/rspec/bash/command/stubbed_command.rb +64 -0
  16. data/lib/rspec/bash/server.rb +3 -0
  17. data/lib/rspec/bash/server/bash_stub_marshaller.rb +19 -0
  18. data/lib/rspec/bash/server/ruby_stub_marshaller.rb +13 -0
  19. data/lib/rspec/bash/server/stub_server.rb +47 -0
  20. data/lib/rspec/bash/stubbed_env.rb +75 -54
  21. data/lib/rspec/bash/util/call_conf_argument_list_matcher.rb +5 -5
  22. data/lib/rspec/bash/util/call_log_argument_list_matcher.rb +1 -1
  23. data/lib/rspec/bash/wrapper.rb +4 -0
  24. data/lib/rspec/bash/wrapper/bash_stub_script.rb +15 -0
  25. data/lib/rspec/bash/wrapper/bash_wrapper.rb +54 -0
  26. data/lib/rspec/bash/wrapper/ruby_stub_script.rb +15 -0
  27. data/lib/rspec/bash/wrapper/stub_function.rb +36 -0
  28. data/rspec-bash.gemspec +2 -1
  29. data/spec/classes/command/call_configuration_manager_spec.rb +68 -0
  30. data/spec/classes/{call_configuration_spec.rb → command/call_configuration_spec.rb} +51 -114
  31. data/spec/classes/command/call_log_manager_spec.rb +83 -0
  32. data/spec/classes/{call_log_spec.rb → command/call_log_spec.rb} +23 -82
  33. data/spec/classes/command/stubbed_command_spec.rb +118 -0
  34. data/spec/classes/server/bash_stub_marshaller_spec.rb +38 -0
  35. data/spec/classes/server/ruby_stub_marshaller_spec.rb +31 -0
  36. data/spec/classes/server/stub_server_spec.rb +121 -0
  37. data/spec/classes/stubbed_env_spec.rb +141 -280
  38. data/spec/classes/util/call_conf_argument_list_matcher_spec.rb +17 -17
  39. data/spec/classes/util/call_log_argument_list_matcher_spec.rb +24 -18
  40. data/spec/classes/wrapper/bash_wrapper_spec.rb +37 -0
  41. data/spec/classes/wrapper/ruby_stub_script_spec.rb +204 -0
  42. data/spec/helper/string_file_io.rb +1 -1
  43. data/spec/integration/call_log/called_with_args_spec.rb +8 -4
  44. data/spec/integration/call_log/called_with_no_args_spec.rb +1 -1
  45. data/spec/integration/call_log/stdin_spec.rb +10 -4
  46. data/spec/integration/edge_cases_spec.rb +34 -0
  47. data/spec/integration/matchers/be_called_with_arguments_spec.rb +12 -13
  48. data/spec/integration/matchers/be_called_with_no_arguments_spec.rb +6 -7
  49. data/spec/integration/stubbed_command/outputs_spec.rb +111 -91
  50. data/spec/integration/stubbed_command/returns_exitstatus_spec.rb +46 -37
  51. data/spec/integration/stubbed_env/execute_with_env_vars_spec.rb +3 -4
  52. data/spec/integration/stubbed_env/execute_with_path_spec.rb +6 -7
  53. data/spec/integration/stubbed_env/execute_with_stub_wrapper_spec.rb +4 -12
  54. data/spec/integration/stubbed_env/override_spec.rb +354 -0
  55. data/spec/integration/wrapper/bash_stub_script_spec.rb +383 -0
  56. data/spec/integration/wrapper/bash_wrapper_spec.rb +48 -0
  57. data/spec/scripts/function_library.sh +9 -1
  58. data/spec/spec_helper.rb +2 -0
  59. metadata +65 -21
  60. data/bin/function_override.sh.erb +0 -7
  61. data/bin/function_override_wrapper.sh.erb +0 -19
  62. data/bin/stub.rb.erb +0 -56
  63. data/lib/rspec/bash/call_configuration.rb +0 -62
  64. data/lib/rspec/bash/call_log.rb +0 -71
  65. data/lib/rspec/bash/stubbed_command.rb +0 -88
  66. data/spec/classes/stub_spec.rb +0 -510
  67. data/spec/classes/stubbed_command_spec.rb +0 -134
  68. data/spec/integration/assert_called_spec.rb +0 -0
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.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Brewer
@@ -10,8 +10,22 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-09-15 00:00:00.000000000 Z
13
+ date: 2017-12-15 00:00:00.000000000 Z
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sparsify
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '1.1'
15
29
  - !ruby/object:Gem::Dependency
16
30
  name: bundler
17
31
  requirement: !ruby/object:Gem::Requirement
@@ -46,9 +60,9 @@ email:
46
60
  - ben@benbrewer.me
47
61
  - mike.david.urban@gmail.com
48
62
  executables:
49
- - function_override.sh.erb
50
- - function_override_wrapper.sh.erb
51
- - stub.rb.erb
63
+ - bash_stub.sh
64
+ - bash_wrapper.sh.erb
65
+ - ruby_stub.rb
52
66
  extensions: []
53
67
  extra_rdoc_files: []
54
68
  files:
@@ -62,34 +76,52 @@ files:
62
76
  - LICENSE.txt
63
77
  - README.md
64
78
  - Rakefile
65
- - bin/function_override.sh.erb
66
- - bin/function_override_wrapper.sh.erb
67
- - bin/stub.rb.erb
79
+ - bin/bash_stub.sh
80
+ - bin/bash_wrapper.sh.erb
81
+ - bin/ruby_stub.rb
68
82
  - lib/rspec/bash.rb
69
- - lib/rspec/bash/call_configuration.rb
70
- - lib/rspec/bash/call_log.rb
83
+ - lib/rspec/bash/command.rb
84
+ - lib/rspec/bash/command/call_configuration.rb
85
+ - lib/rspec/bash/command/call_configuration_manager.rb
86
+ - lib/rspec/bash/command/call_log.rb
87
+ - lib/rspec/bash/command/call_log_manager.rb
88
+ - lib/rspec/bash/command/stubbed_command.rb
71
89
  - lib/rspec/bash/matchers.rb
72
90
  - lib/rspec/bash/matchers/called_with_arguments.rb
73
91
  - lib/rspec/bash/matchers/called_with_no_arguments.rb
74
- - lib/rspec/bash/stubbed_command.rb
92
+ - lib/rspec/bash/server.rb
93
+ - lib/rspec/bash/server/bash_stub_marshaller.rb
94
+ - lib/rspec/bash/server/ruby_stub_marshaller.rb
95
+ - lib/rspec/bash/server/stub_server.rb
75
96
  - lib/rspec/bash/stubbed_env.rb
76
97
  - lib/rspec/bash/util.rb
77
98
  - lib/rspec/bash/util/call_conf_argument_list_matcher.rb
78
99
  - lib/rspec/bash/util/call_log_argument_list_matcher.rb
100
+ - lib/rspec/bash/wrapper.rb
101
+ - lib/rspec/bash/wrapper/bash_stub_script.rb
102
+ - lib/rspec/bash/wrapper/bash_wrapper.rb
103
+ - lib/rspec/bash/wrapper/ruby_stub_script.rb
104
+ - lib/rspec/bash/wrapper/stub_function.rb
79
105
  - rspec-bash.gemspec
80
- - spec/classes/call_configuration_spec.rb
81
- - spec/classes/call_log_spec.rb
82
- - spec/classes/stub_spec.rb
83
- - spec/classes/stubbed_command_spec.rb
106
+ - spec/classes/command/call_configuration_manager_spec.rb
107
+ - spec/classes/command/call_configuration_spec.rb
108
+ - spec/classes/command/call_log_manager_spec.rb
109
+ - spec/classes/command/call_log_spec.rb
110
+ - spec/classes/command/stubbed_command_spec.rb
111
+ - spec/classes/server/bash_stub_marshaller_spec.rb
112
+ - spec/classes/server/ruby_stub_marshaller_spec.rb
113
+ - spec/classes/server/stub_server_spec.rb
84
114
  - spec/classes/stubbed_env_spec.rb
85
115
  - spec/classes/util/call_conf_argument_list_matcher_spec.rb
86
116
  - spec/classes/util/call_log_argument_list_matcher_spec.rb
117
+ - spec/classes/wrapper/bash_wrapper_spec.rb
118
+ - spec/classes/wrapper/ruby_stub_script_spec.rb
87
119
  - spec/helper/shared_tmpdir.rb
88
120
  - spec/helper/string_file_io.rb
89
- - spec/integration/assert_called_spec.rb
90
121
  - spec/integration/call_log/called_with_args_spec.rb
91
122
  - spec/integration/call_log/called_with_no_args_spec.rb
92
123
  - spec/integration/call_log/stdin_spec.rb
124
+ - spec/integration/edge_cases_spec.rb
93
125
  - spec/integration/matchers/be_called_with_arguments_spec.rb
94
126
  - spec/integration/matchers/be_called_with_no_arguments_spec.rb
95
127
  - spec/integration/stubbed_command/outputs_spec.rb
@@ -97,6 +129,9 @@ files:
97
129
  - spec/integration/stubbed_env/execute_with_env_vars_spec.rb
98
130
  - spec/integration/stubbed_env/execute_with_path_spec.rb
99
131
  - spec/integration/stubbed_env/execute_with_stub_wrapper_spec.rb
132
+ - spec/integration/stubbed_env/override_spec.rb
133
+ - spec/integration/wrapper/bash_stub_script_spec.rb
134
+ - spec/integration/wrapper/bash_wrapper_spec.rb
100
135
  - spec/scripts/function_library.sh
101
136
  - spec/spec_helper.rb
102
137
  homepage: https://github.com/mdurban/rspec-bash
@@ -124,19 +159,25 @@ signing_key:
124
159
  specification_version: 4
125
160
  summary: Test Bash with RSpec
126
161
  test_files:
127
- - spec/classes/call_configuration_spec.rb
128
- - spec/classes/call_log_spec.rb
129
- - spec/classes/stub_spec.rb
130
- - spec/classes/stubbed_command_spec.rb
162
+ - spec/classes/command/call_configuration_manager_spec.rb
163
+ - spec/classes/command/call_configuration_spec.rb
164
+ - spec/classes/command/call_log_manager_spec.rb
165
+ - spec/classes/command/call_log_spec.rb
166
+ - spec/classes/command/stubbed_command_spec.rb
167
+ - spec/classes/server/bash_stub_marshaller_spec.rb
168
+ - spec/classes/server/ruby_stub_marshaller_spec.rb
169
+ - spec/classes/server/stub_server_spec.rb
131
170
  - spec/classes/stubbed_env_spec.rb
132
171
  - spec/classes/util/call_conf_argument_list_matcher_spec.rb
133
172
  - spec/classes/util/call_log_argument_list_matcher_spec.rb
173
+ - spec/classes/wrapper/bash_wrapper_spec.rb
174
+ - spec/classes/wrapper/ruby_stub_script_spec.rb
134
175
  - spec/helper/shared_tmpdir.rb
135
176
  - spec/helper/string_file_io.rb
136
- - spec/integration/assert_called_spec.rb
137
177
  - spec/integration/call_log/called_with_args_spec.rb
138
178
  - spec/integration/call_log/called_with_no_args_spec.rb
139
179
  - spec/integration/call_log/stdin_spec.rb
180
+ - spec/integration/edge_cases_spec.rb
140
181
  - spec/integration/matchers/be_called_with_arguments_spec.rb
141
182
  - spec/integration/matchers/be_called_with_no_arguments_spec.rb
142
183
  - spec/integration/stubbed_command/outputs_spec.rb
@@ -144,5 +185,8 @@ test_files:
144
185
  - spec/integration/stubbed_env/execute_with_env_vars_spec.rb
145
186
  - spec/integration/stubbed_env/execute_with_path_spec.rb
146
187
  - spec/integration/stubbed_env/execute_with_stub_wrapper_spec.rb
188
+ - spec/integration/stubbed_env/override_spec.rb
189
+ - spec/integration/wrapper/bash_stub_script_spec.rb
190
+ - spec/integration/wrapper/bash_wrapper_spec.rb
147
191
  - spec/scripts/function_library.sh
148
192
  - spec/spec_helper.rb
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- function <%=function_command_binding_for_template%> {
4
- <%=function_command_path_binding_for_template%> "${@}"
5
- }
6
-
7
- readonly -f <%=function_command_binding_for_template%> &> /dev/null
@@ -1,19 +0,0 @@
1
- /usr/bin/env bash -c '
2
- # load in command and function overrides
3
- system_ls=$(which ls)
4
- system_grep=$(which grep)
5
-
6
- for override_file in $(${system_ls} -d <%=function_override_path_binding_for_template%> 2> /dev/null); do
7
- source ${override_file}
8
- done
9
-
10
- (
11
- <%=execution_binding_for_template%>
12
- ) 2> <%=wrapped_error_path_binding_for_template%>
13
- command_exit_code=$?
14
-
15
- # filter stderr for readonly problems
16
- ${system_grep} -v "readonly function" <%=wrapped_error_path_binding_for_template%> >&2
17
-
18
- # return original exit code
19
- exit ${command_exit_code}'
data/bin/stub.rb.erb DELETED
@@ -1,56 +0,0 @@
1
- #!/usr/bin/env ruby
2
- $LOAD_PATH.unshift('<%= rspec_bash_library_path_for_template %>')
3
-
4
- require 'rspec/bash'
5
- require 'pathname'
6
- require 'yaml'
7
-
8
- include Rspec::Bash
9
-
10
- command = File.basename(__FILE__)
11
- folder = File.dirname(__FILE__)
12
-
13
- call_log_path = Pathname.new(folder).join("#{command}_calls.yml")
14
- call_log = CallLog.new(call_log_path)
15
- call_log.add_log(STDIN.tty? ? '' : $stdin.read, ARGV)
16
-
17
- def interpolate_filename(elements)
18
- return elements if elements.is_a? String
19
- return nil unless elements.is_a? Array
20
-
21
- elements.map do |element|
22
- case element
23
- when String then
24
- element
25
- when Symbol then
26
- interpolate_argument(element)
27
- end
28
- end.join
29
- end
30
-
31
- def interpolate_argument(name)
32
- return unless (data = /^arg(\d+)$/.match(name.to_s))
33
- ARGV[data[1].to_i - 1]
34
- end
35
-
36
- call_conf_path = Pathname.new(folder).join("#{command}_stub.yml")
37
- if call_conf_path.exist?
38
- call_conf = CallConfiguration.new(call_conf_path, command)
39
- config = call_conf.call_configuration
40
-
41
- call_conf_arg_matcher = Util::CallConfArgumentListMatcher.new(config)
42
- best_matching_call_conf = call_conf_arg_matcher.get_best_call_conf(*ARGV)
43
- exit 0 if best_matching_call_conf.empty?
44
-
45
- (best_matching_call_conf[:outputs] || []).each do |data|
46
- $stdout.print data[:content] if data[:target] == :stdout
47
- $stderr.print data[:content] if data[:target] == :stderr
48
-
49
- output_filename = interpolate_filename(data[:target])
50
- next unless output_filename
51
- Pathname.new(output_filename).open('w') do |f|
52
- f.print data[:content]
53
- end
54
- end
55
- exit best_matching_call_conf[:exitcode] || 0
56
- end
@@ -1,62 +0,0 @@
1
- require 'yaml'
2
-
3
- module Rspec
4
- module Bash
5
- class CallConfiguration
6
- attr_reader :command
7
-
8
- def initialize(config_path, command)
9
- @config_path = config_path
10
- @configuration = []
11
- @command = command
12
- end
13
-
14
- def set_exitcode(exitcode, args = [])
15
- current_conf = create_or_get_conf(args)
16
- current_conf[:exitcode] = exitcode
17
- write @configuration
18
- end
19
-
20
- def add_output(content, target, args = [])
21
- current_conf = create_or_get_conf(args)
22
- current_conf[:outputs] << {
23
- target: target,
24
- content: content
25
- }
26
- write @configuration
27
- end
28
-
29
- def call_configuration
30
- @config_path.open('r') do |conf_file|
31
- YAML.load(conf_file.read) || []
32
- end
33
- rescue NoMethodError, Errno::ENOENT
34
- return []
35
- end
36
-
37
- def call_configuration=(new_conf)
38
- write new_conf
39
- end
40
-
41
- private
42
-
43
- def write(call_conf_to_write)
44
- @config_path.open('w') do |conf_file|
45
- conf_file.write call_conf_to_write.to_yaml
46
- end
47
- end
48
-
49
- def create_or_get_conf(args)
50
- @configuration = call_configuration
51
- new_conf = {
52
- args: args,
53
- exitcode: 0,
54
- outputs: []
55
- }
56
- current_conf = @configuration.select { |conf| conf[:args] == args }
57
- @configuration << new_conf if current_conf.empty?
58
- current_conf.first || new_conf
59
- end
60
- end
61
- end
62
- end
@@ -1,71 +0,0 @@
1
- module Rspec
2
- module Bash
3
- class CallLog
4
- def initialize(call_log_path)
5
- @call_log_path = call_log_path
6
- end
7
-
8
- def exist?
9
- @call_log_path.exist?
10
- end
11
-
12
- def stdin_for_args(*argument_list)
13
- call_argument_list_matcher = Util::CallLogArgumentListMatcher.new(*argument_list)
14
- matching_call_log_list = call_argument_list_matcher.get_call_log_matches(call_log)
15
- matching_call_log_list.first[:stdin] unless matching_call_log_list.empty?
16
- end
17
-
18
- def call_count(*argument_list)
19
- call_argument_list_matcher = Util::CallLogArgumentListMatcher.new(*argument_list)
20
- call_argument_list_matcher.get_call_count(call_log)
21
- end
22
-
23
- def called_with_args?(*argument_list)
24
- call_argument_list_matcher = Util::CallLogArgumentListMatcher.new(*argument_list)
25
- call_argument_list_matcher.args_match?(call_log)
26
- end
27
-
28
- def called_with_no_args?
29
- return false if call_log.empty?
30
-
31
- call_log.all? do |call_log|
32
- argument_list = call_log[:args] || []
33
- argument_list.empty?
34
- end
35
- end
36
-
37
- def add_log(stdin, argument_list)
38
- updated_log = call_log
39
- updated_log << {
40
- args: argument_list,
41
- stdin: stdin
42
- }
43
- write updated_log
44
- end
45
-
46
- def call_log
47
- @call_log_path.open('r') do |call_log|
48
- YAML.load(call_log.read) || []
49
- end
50
- rescue NoMethodError, Errno::ENOENT
51
- return []
52
- end
53
-
54
- def call_log=(new_log)
55
- write new_log
56
- end
57
-
58
- def call_log_arguments
59
- call_log.map { |call_log| call_log[:args] || [] }.compact
60
- end
61
-
62
- private
63
-
64
- def write(call_log_to_write)
65
- @call_log_path.open('w') do |call_log|
66
- call_log.write call_log_to_write.to_yaml
67
- end
68
- end
69
- end
70
- end
71
- end
@@ -1,88 +0,0 @@
1
- module Rspec
2
- module Bash
3
- class StubbedCommand
4
- attr_reader :call_log, :arguments, :path
5
-
6
- def initialize(command, dir)
7
- @path = create_stub_file(command, dir)
8
- @arguments = []
9
- @call_configuration = CallConfiguration.new(
10
- Pathname.new(dir).join("#{command}_stub.yml"),
11
- command
12
- )
13
- @call_log = CallLog.new(
14
- Pathname.new(dir).join("#{command}_calls.yml")
15
- )
16
- end
17
-
18
- def with_args(*args)
19
- @arguments = args
20
- self
21
- end
22
-
23
- def called?
24
- @call_log.exist? && @call_log.called_with_args?(*@args)
25
- end
26
-
27
- def called_with_no_args?
28
- @call_log.called_with_no_args?
29
- end
30
-
31
- def called_with_args?(*args)
32
- @call_log.called_with_args?(*args)
33
- end
34
-
35
- def call_count(*arg)
36
- @call_log.call_count(*arg)
37
- end
38
-
39
- def command
40
- @call_configuration.command
41
- end
42
-
43
- def returns_exitstatus(exitcode)
44
- @call_configuration.set_exitcode(exitcode, @arguments)
45
- self
46
- end
47
-
48
- def outputs(contents, to: :stdout)
49
- @call_configuration.add_output(contents, to, @arguments)
50
- self
51
- end
52
-
53
- def stdin
54
- @call_log.stdin_for_args(*@arguments) if @call_log.exist?
55
- end
56
-
57
- def inspect
58
- if @arguments.any?
59
- "<Stubbed #{@call_configuration.command.inspect} " \
60
- "args: #{@arguments.join(' ').inspect}>"
61
- else
62
- "<Stubbed #{@call_configuration.command.inspect}>"
63
- end
64
- end
65
-
66
- private
67
-
68
- def create_stub_file(command_name, directory)
69
- command_path = File.join(directory, command_name)
70
-
71
- stub_template_path = File.expand_path(
72
- 'stub.rb.erb', "#{File.dirname(__FILE__)}/../../../bin"
73
- )
74
- template = ERB.new File.read(stub_template_path), nil, '%'
75
- rspec_bash_library_path_for_template = project_root.join('lib')
76
- stub_content = template.result(binding)
77
- File.open(command_path, 'w') { |file| file.write(stub_content) }
78
- File.chmod(0755, command_path)
79
-
80
- command_path
81
- end
82
-
83
- def project_root
84
- Pathname.new(File.dirname(File.expand_path(__FILE__))).join('..', '..', '..')
85
- end
86
- end
87
- end
88
- end