cucumber 4.0.0.rc.2 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +73 -1
  3. data/README.md +2 -2
  4. data/lib/cucumber/cli/options.rb +10 -4
  5. data/lib/cucumber/configuration.rb +5 -0
  6. data/lib/cucumber/deprecate.rb +29 -5
  7. data/lib/cucumber/events.rb +13 -7
  8. data/lib/cucumber/events/envelope.rb +9 -0
  9. data/lib/cucumber/events/hook_test_step_created.rb +13 -0
  10. data/lib/cucumber/events/test_case_created.rb +13 -0
  11. data/lib/cucumber/events/test_case_ready.rb +12 -0
  12. data/lib/cucumber/events/test_step_created.rb +13 -0
  13. data/lib/cucumber/events/undefined_parameter_type.rb +10 -0
  14. data/lib/cucumber/filters.rb +1 -0
  15. data/lib/cucumber/filters/broadcast_test_case_ready_event.rb +12 -0
  16. data/lib/cucumber/formatter/console.rb +33 -10
  17. data/lib/cucumber/formatter/duration_extractor.rb +1 -1
  18. data/lib/cucumber/formatter/errors.rb +6 -0
  19. data/lib/cucumber/formatter/html.rb +24 -0
  20. data/lib/cucumber/formatter/http_io.rb +146 -0
  21. data/lib/cucumber/formatter/interceptor.rb +3 -8
  22. data/lib/cucumber/formatter/io.rb +14 -8
  23. data/lib/cucumber/formatter/json.rb +17 -8
  24. data/lib/cucumber/formatter/junit.rb +1 -1
  25. data/lib/cucumber/formatter/message.rb +22 -0
  26. data/lib/cucumber/formatter/message_builder.rb +255 -0
  27. data/lib/cucumber/formatter/pretty.rb +10 -4
  28. data/lib/cucumber/formatter/progress.rb +2 -0
  29. data/lib/cucumber/formatter/query/hook_by_test_step.rb +31 -0
  30. data/lib/cucumber/formatter/query/pickle_by_test.rb +26 -0
  31. data/lib/cucumber/formatter/query/pickle_step_by_test_step.rb +26 -0
  32. data/lib/cucumber/formatter/query/step_definitions_by_test_step.rb +40 -0
  33. data/lib/cucumber/formatter/query/test_case_started_by_test_case.rb +40 -0
  34. data/lib/cucumber/gherkin/data_table_parser.rb +1 -1
  35. data/lib/cucumber/gherkin/steps_parser.rb +1 -1
  36. data/lib/cucumber/glue/hook.rb +18 -2
  37. data/lib/cucumber/glue/proto_world.rb +29 -18
  38. data/lib/cucumber/glue/registry_and_more.rb +27 -2
  39. data/lib/cucumber/glue/snippet.rb +1 -1
  40. data/lib/cucumber/glue/step_definition.rb +28 -4
  41. data/lib/cucumber/hooks.rb +8 -8
  42. data/lib/cucumber/multiline_argument.rb +1 -1
  43. data/lib/cucumber/multiline_argument/data_table.rb +17 -13
  44. data/lib/cucumber/platform.rb +1 -1
  45. data/lib/cucumber/rake/task.rb +1 -1
  46. data/lib/cucumber/runtime.rb +29 -3
  47. data/lib/cucumber/runtime/after_hooks.rb +6 -2
  48. data/lib/cucumber/runtime/before_hooks.rb +6 -2
  49. data/lib/cucumber/runtime/for_programming_languages.rb +1 -0
  50. data/lib/cucumber/runtime/step_hooks.rb +3 -2
  51. data/lib/cucumber/runtime/support_code.rb +3 -3
  52. data/lib/cucumber/runtime/user_interface.rb +2 -10
  53. data/lib/cucumber/version +1 -1
  54. metadata +108 -91
@@ -0,0 +1,146 @@
1
+ require 'net/http'
2
+ require 'tempfile'
3
+
4
+ module Cucumber
5
+ module Formatter
6
+ class HTTPIO
7
+ class << self
8
+ # Returns an IO that will write to a HTTP request's body
9
+ def open(url, https_verify_mode = nil)
10
+ @https_verify_mode = https_verify_mode
11
+ uri, method, headers = CurlOptionParser.parse(url)
12
+ IOHTTPBuffer.new(uri, method, headers, https_verify_mode)
13
+ end
14
+ end
15
+ end
16
+
17
+ class CurlOptionParser
18
+ def self.parse(options)
19
+ chunks = options.split(/\s/).compact
20
+ http_method = 'PUT'
21
+ url = chunks[0]
22
+ headers = ''
23
+
24
+ last_flag = nil
25
+ chunks.each do |chunk|
26
+ if ['-X', '--request'].include?(chunk)
27
+ last_flag = '-X'
28
+ next
29
+ end
30
+
31
+ if chunk == '-H'
32
+ last_flag = '-H'
33
+ next
34
+ end
35
+
36
+ if last_flag == '-X'
37
+ http_method = chunk
38
+ last_flag = nil
39
+ end
40
+
41
+ headers += chunk if last_flag == '-H'
42
+ end
43
+
44
+ [
45
+ url,
46
+ http_method,
47
+ make_headers(headers)
48
+ ]
49
+ end
50
+
51
+ def self.make_headers(headers)
52
+ hash_headers = {}
53
+ str_scanner = /("(?<key>[^":]+)\s*:\s*(?<value>[^":]+)")|('(?<key1>[^':]+)\s*:\s*(?<value1>[^':]+)')/
54
+
55
+ headers.scan(str_scanner) do |header|
56
+ header = header.compact!
57
+ hash_headers[header[0]] = header[1]&.strip
58
+ end
59
+
60
+ hash_headers
61
+ end
62
+ end
63
+
64
+ class IOHTTPBuffer
65
+ attr_reader :uri, :method, :headers
66
+
67
+ def initialize(uri, method, headers = {}, https_verify_mode = nil)
68
+ @uri = URI(uri)
69
+ @method = method
70
+ @headers = headers
71
+ @write_io = Tempfile.new('cucumber', encoding: 'UTF-8')
72
+ @https_verify_mode = https_verify_mode
73
+ end
74
+
75
+ def close
76
+ post_content(@uri, @method, @headers)
77
+ @write_io.close
78
+ end
79
+
80
+ def write(data)
81
+ @write_io.write(data)
82
+ end
83
+
84
+ def flush
85
+ @write_io.flush
86
+ end
87
+
88
+ def closed?
89
+ @write_io.closed?
90
+ end
91
+
92
+ private
93
+
94
+ def post_content(uri, method, headers, attempt = 10)
95
+ content = @write_io
96
+ http = build_client(uri, @https_verify_mode)
97
+
98
+ raise StandardError, "request to #{uri} failed (too many redirections)" if attempt <= 0
99
+ req = build_request(
100
+ uri,
101
+ method,
102
+ headers.merge(
103
+ 'Content-Length' => content.size.to_s
104
+ )
105
+ )
106
+
107
+ content.rewind
108
+ req.body_stream = content
109
+
110
+ begin
111
+ response = http.request(req)
112
+ rescue SystemCallError
113
+ # We may get the redirect response before pushing the file.
114
+ response = http.request(build_request(uri, method, headers))
115
+ end
116
+
117
+ case response
118
+ when Net::HTTPSuccess
119
+ response
120
+ when Net::HTTPRedirection
121
+ post_content(URI(response['Location']), method, headers, attempt - 1)
122
+ else
123
+ raise StandardError, "request to #{uri} failed with status #{response.code}"
124
+ end
125
+ end
126
+
127
+ def build_request(uri, method, headers)
128
+ method_class_name = "#{method[0].upcase}#{method[1..-1].downcase}"
129
+ req = Net::HTTP.const_get(method_class_name).new(uri)
130
+ headers.each do |header, value|
131
+ req[header] = value
132
+ end
133
+ req
134
+ end
135
+
136
+ def build_client(uri, https_verify_mode)
137
+ http = Net::HTTP.new(uri.hostname, uri.port)
138
+ if uri.scheme == 'https'
139
+ http.use_ssl = true
140
+ http.verify_mode = https_verify_mode if https_verify_mode
141
+ end
142
+ http
143
+ end
144
+ end
145
+ end
146
+ end
@@ -9,17 +9,18 @@ module Cucumber
9
9
  @pipe = pipe
10
10
  @buffer = StringIO.new
11
11
  @wrapped = true
12
+ @lock = Mutex.new
12
13
  end
13
14
 
14
15
  def write(str)
15
- lock.synchronize do
16
+ @lock.synchronize do
16
17
  @buffer << str if @wrapped
17
18
  return @pipe.write(str)
18
19
  end
19
20
  end
20
21
 
21
22
  def buffer_string
22
- lock.synchronize do
23
+ @lock.synchronize do
23
24
  return @buffer.string.dup
24
25
  end
25
26
  end
@@ -67,12 +68,6 @@ module Cucumber
67
68
  return $stdout
68
69
  end
69
70
  end
70
-
71
- private
72
-
73
- def lock
74
- @lock ||= Mutex.new
75
- end
76
71
  end
77
72
  end
78
73
  end
@@ -1,21 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'cucumber/formatter/http_io'
4
+
3
5
  module Cucumber
4
6
  module Formatter
5
7
  module Io
6
8
  module_function
7
9
 
8
- def ensure_io(path_or_io)
9
- return nil if path_or_io.nil?
10
- return path_or_io if path_or_io.respond_to?(:write)
11
- file = File.open(path_or_io, Cucumber.file_mode('w'))
10
+ def ensure_io(path_or_url_or_io)
11
+ return nil if path_or_url_or_io.nil?
12
+ return path_or_url_or_io if path_or_url_or_io.respond_to?(:write)
13
+ io = if path_or_url_or_io.match(%r{^https?://})
14
+ HTTPIO.open(path_or_url_or_io)
15
+ else
16
+ File.open(path_or_url_or_io, Cucumber.file_mode('w'))
17
+ end
12
18
  at_exit do
13
- unless file.closed?
14
- file.flush
15
- file.close
19
+ unless io.closed?
20
+ io.flush
21
+ io.close
16
22
  end
17
23
  end
18
- file
24
+ io
19
25
  end
20
26
 
21
27
  def ensure_file(path, name)
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'multi_json'
3
+ require 'json'
4
4
  require 'base64'
5
5
  require 'cucumber/formatter/backtrace_filter'
6
6
  require 'cucumber/formatter/io'
7
7
  require 'cucumber/formatter/ast_lookup'
8
+ require 'cucumber/deprecate'
8
9
 
9
10
  module Cucumber
10
11
  module Formatter
@@ -13,6 +14,14 @@ module Cucumber
13
14
  include Io
14
15
 
15
16
  def initialize(config)
17
+ Cucumber::Deprecate::CliOption.deprecate(
18
+ config.error_stream,
19
+ '--format=json',
20
+ "Please use --format=message and stand-alone json-formatter.\n" \
21
+ 'json-formatter homepage: https://github.com/cucumber/cucumber/tree/master/json-formatter#cucumber-json-formatter',
22
+ '5.0.0'
23
+ )
24
+
16
25
  @io = ensure_io(config.out_stream)
17
26
  @ast_lookup = AstLookup.new(config)
18
27
  @feature_hashes = []
@@ -77,14 +86,14 @@ module Cucumber
77
86
  end
78
87
 
79
88
  def on_test_run_finished(_event)
80
- @io.write(MultiJson.dump(@feature_hashes, pretty: true))
81
- end
82
-
83
- def puts(message)
84
- test_step_output << message
89
+ @io.write(JSON.generate(@feature_hashes, pretty: true))
85
90
  end
86
91
 
87
- def embed(src, mime_type, _label)
92
+ def attach(src, mime_type)
93
+ if mime_type == 'text/x.cucumber.log+plain'
94
+ test_step_output << src
95
+ return
96
+ end
88
97
  if File.file?(src)
89
98
  content = File.open(src, 'rb', &:read)
90
99
  data = encode64(content)
@@ -173,7 +182,7 @@ module Cucumber
173
182
  end
174
183
 
175
184
  def create_doc_string_hash(doc_string)
176
- content_type = doc_string.content_type || ''
185
+ content_type = doc_string.media_type || ''
177
186
  {
178
187
  value: doc_string.content,
179
188
  content_type: content_type,
@@ -239,7 +239,7 @@ module Cucumber
239
239
  duration.tap { |dur| @test_case_duration = dur.nanoseconds / 10**9.0 }
240
240
  end
241
241
 
242
- def embed(*) end
242
+ def attach(*) end
243
243
  end
244
244
  end
245
245
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cucumber/formatter/io'
4
+ require 'cucumber/formatter/message_builder'
5
+
6
+ module Cucumber
7
+ module Formatter
8
+ # The formatter used for <tt>--format message</tt>
9
+ class Message < MessageBuilder
10
+ include Io
11
+
12
+ def initialize(config)
13
+ @io = ensure_io(config.out_stream)
14
+ super(config)
15
+ end
16
+
17
+ def output_envelope(envelope)
18
+ envelope.write_ndjson_to(@io)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,255 @@
1
+ require 'base64'
2
+ require 'cucumber/formatter/backtrace_filter'
3
+ require 'cucumber/formatter/query/hook_by_test_step'
4
+ require 'cucumber/formatter/query/pickle_by_test'
5
+ require 'cucumber/formatter/query/pickle_step_by_test_step'
6
+ require 'cucumber/formatter/query/step_definitions_by_test_step'
7
+ require 'cucumber/formatter/query/test_case_started_by_test_case'
8
+
9
+ module Cucumber
10
+ module Formatter
11
+ class MessageBuilder
12
+ include Cucumber::Messages::TimeConversion
13
+
14
+ def initialize(config)
15
+ @config = config
16
+
17
+ @hook_by_test_step = Query::HookByTestStep.new(config)
18
+ @pickle_by_test = Query::PickleByTest.new(config)
19
+ @pickle_step_by_test_step = Query::PickleStepByTestStep.new(config)
20
+ @step_definitions_by_test_step = Query::StepDefinitionsByTestStep.new(config)
21
+ @test_case_started_by_test_case = Query::TestCaseStartedByTestCase.new(config)
22
+
23
+ config.on_event :envelope, &method(:on_envelope)
24
+ config.on_event :gherkin_source_read, &method(:on_gherkin_source_read)
25
+ config.on_event :test_case_ready, &method(:on_test_case_ready)
26
+ config.on_event :test_run_started, &method(:on_test_run_started)
27
+ config.on_event :test_case_started, &method(:on_test_case_started)
28
+ config.on_event :test_step_started, &method(:on_test_step_started)
29
+ config.on_event :test_step_finished, &method(:on_test_step_finished)
30
+ config.on_event :test_case_finished, &method(:on_test_case_finished)
31
+ config.on_event :test_run_finished, &method(:on_test_run_finished)
32
+ config.on_event :undefined_parameter_type, &method(:on_undefined_parameter_type)
33
+
34
+ @test_case_by_step_id = {}
35
+ @current_test_case_started_id = nil
36
+ @current_test_step_id = nil
37
+ end
38
+
39
+ def output_message
40
+ raise 'To be implemented'
41
+ end
42
+
43
+ def attach(src, media_type)
44
+ attachment_data = {
45
+ test_step_id: @current_test_step_id,
46
+ test_case_started_id: @current_test_case_started_id,
47
+ media_type: media_type
48
+ }
49
+
50
+ if media_type.start_with?('text/')
51
+ attachment_data[:content_encoding] = Cucumber::Messages::Attachment::ContentEncoding::IDENTITY
52
+ attachment_data[:body] = src
53
+ else
54
+ body = src.respond_to?(:read) ? src.read : src
55
+
56
+ attachment_data[:content_encoding] = Cucumber::Messages::Attachment::ContentEncoding::BASE64
57
+ attachment_data[:body] = Base64.strict_encode64(body)
58
+ end
59
+
60
+ message = Cucumber::Messages::Envelope.new(
61
+ attachment: Cucumber::Messages::Attachment.new(**attachment_data)
62
+ )
63
+
64
+ output_envelope(message)
65
+ end
66
+
67
+ private
68
+
69
+ def on_envelope(event)
70
+ output_envelope(event.envelope)
71
+ end
72
+
73
+ def on_gherkin_source_read(event)
74
+ message = Cucumber::Messages::Envelope.new(
75
+ source: Cucumber::Messages::Source.new(
76
+ uri: event.path,
77
+ data: event.body,
78
+ media_type: 'text/x.cucumber.gherkin+plain'
79
+ )
80
+ )
81
+
82
+ output_envelope(message)
83
+ end
84
+
85
+ def on_test_case_ready(event)
86
+ event.test_case.test_steps.each do |step|
87
+ @test_case_by_step_id[step.id] = event.test_case
88
+ end
89
+
90
+ message = Cucumber::Messages::Envelope.new(
91
+ test_case: Cucumber::Messages::TestCase.new(
92
+ id: event.test_case.id,
93
+ pickle_id: @pickle_by_test.pickle_id(event.test_case),
94
+ test_steps: event.test_case.test_steps.map { |step| test_step_to_message(step) }
95
+ )
96
+ )
97
+
98
+ output_envelope(message)
99
+ end
100
+
101
+ def test_step_to_message(step)
102
+ return hook_step_to_message(step) if step.hook?
103
+
104
+ Cucumber::Messages::TestCase::TestStep.new(
105
+ id: step.id,
106
+ pickle_step_id: @pickle_step_by_test_step.pickle_step_id(step),
107
+ step_definition_ids: @step_definitions_by_test_step.step_definition_ids(step),
108
+ step_match_arguments_lists: step_match_arguments_lists(step)
109
+ )
110
+ end
111
+
112
+ def hook_step_to_message(step)
113
+ Cucumber::Messages::TestCase::TestStep.new(
114
+ id: step.id,
115
+ hook_id: @hook_by_test_step.hook_id(step)
116
+ )
117
+ end
118
+
119
+ def step_match_arguments_lists(step)
120
+ match_arguments = step_match_arguments(step)
121
+ [Cucumber::Messages::TestCase::TestStep::StepMatchArgumentsList.new(
122
+ step_match_arguments: match_arguments
123
+ )]
124
+ rescue Cucumber::Formatter::TestStepUnknownError
125
+ []
126
+ end
127
+
128
+ def step_match_arguments(step)
129
+ @step_definitions_by_test_step.step_match_arguments(step).map do |argument|
130
+ Cucumber::Messages::TestCase::TestStep::StepMatchArgumentsList::StepMatchArgument.new(
131
+ group: argument_group_to_message(argument.group),
132
+ parameter_type_name: argument.parameter_type.name
133
+ )
134
+ end
135
+ end
136
+
137
+ def argument_group_to_message(group)
138
+ Cucumber::Messages::TestCase::TestStep::StepMatchArgumentsList::StepMatchArgument::Group.new(
139
+ start: group.start,
140
+ value: group.value,
141
+ children: group.children.map { |child| argument_group_to_message(child) }
142
+ )
143
+ end
144
+
145
+ def on_test_run_started(*)
146
+ message = Cucumber::Messages::Envelope.new(
147
+ test_run_started: Cucumber::Messages::TestRunStarted.new(
148
+ timestamp: time_to_timestamp(Time.now)
149
+ )
150
+ )
151
+
152
+ output_envelope(message)
153
+ end
154
+
155
+ def on_test_case_started(event)
156
+ @current_test_case_started_id = test_case_started_id(event.test_case)
157
+
158
+ message = Cucumber::Messages::Envelope.new(
159
+ test_case_started: Cucumber::Messages::TestCaseStarted.new(
160
+ id: test_case_started_id(event.test_case),
161
+ test_case_id: event.test_case.id,
162
+ timestamp: time_to_timestamp(Time.now),
163
+ attempt: @test_case_started_by_test_case.attempt_by_test_case(event.test_case)
164
+ )
165
+ )
166
+
167
+ output_envelope(message)
168
+ end
169
+
170
+ def on_test_step_started(event)
171
+ @current_test_step_id = event.test_step.id
172
+ test_case = @test_case_by_step_id[event.test_step.id]
173
+
174
+ message = Cucumber::Messages::Envelope.new(
175
+ test_step_started: Cucumber::Messages::TestStepStarted.new(
176
+ test_step_id: event.test_step.id,
177
+ test_case_started_id: test_case_started_id(test_case),
178
+ timestamp: time_to_timestamp(Time.now)
179
+ )
180
+ )
181
+
182
+ output_envelope(message)
183
+ end
184
+
185
+ def on_test_step_finished(event)
186
+ test_case = @test_case_by_step_id[event.test_step.id]
187
+ result = event
188
+ .result
189
+ .with_filtered_backtrace(Cucumber::Formatter::BacktraceFilter)
190
+
191
+ result_message = result.to_message
192
+ if result.failed? || result.pending?
193
+ result_message = Cucumber::Messages::TestStepFinished::TestStepResult.new(
194
+ status: result_message.status,
195
+ duration: result_message.duration,
196
+ message: create_error_message(result)
197
+ )
198
+ end
199
+
200
+ message = Cucumber::Messages::Envelope.new(
201
+ test_step_finished: Cucumber::Messages::TestStepFinished.new(
202
+ test_step_id: event.test_step.id,
203
+ test_case_started_id: test_case_started_id(test_case),
204
+ test_step_result: result_message,
205
+ timestamp: time_to_timestamp(Time.now)
206
+ )
207
+ )
208
+
209
+ output_envelope(message)
210
+ end
211
+
212
+ def create_error_message(result)
213
+ message_element = result.failed? ? result.exception : result
214
+ message = "#{message_element.message} (#{message_element.class})"
215
+ ([message] + message_element.backtrace).join("\n")
216
+ end
217
+
218
+ def on_test_case_finished(event)
219
+ message = Cucumber::Messages::Envelope.new(
220
+ test_case_finished: Cucumber::Messages::TestCaseFinished.new(
221
+ test_case_started_id: test_case_started_id(event.test_case),
222
+ timestamp: time_to_timestamp(Time.now)
223
+ )
224
+ )
225
+
226
+ output_envelope(message)
227
+ end
228
+
229
+ def on_test_run_finished(*)
230
+ message = Cucumber::Messages::Envelope.new(
231
+ test_run_finished: Cucumber::Messages::TestRunFinished.new(
232
+ timestamp: time_to_timestamp(Time.now)
233
+ )
234
+ )
235
+
236
+ output_envelope(message)
237
+ end
238
+
239
+ def on_undefined_parameter_type(event)
240
+ message = Cucumber::Messages::Envelope.new(
241
+ undefined_parameter_type: Cucumber::Messages::UndefinedParameterType.new(
242
+ name: event.type_name,
243
+ expression: event.expression
244
+ )
245
+ )
246
+
247
+ output_envelope(message)
248
+ end
249
+
250
+ def test_case_started_id(test_case)
251
+ @test_case_started_by_test_case.test_case_started_id_by_test_case(test_case)
252
+ end
253
+ end
254
+ end
255
+ end