You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

runtime.rb 8.4 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. # frozen_string_literal: true
  2. require 'fileutils'
  3. require 'cucumber/configuration'
  4. require 'cucumber/deprecate'
  5. require 'cucumber/load_path'
  6. require 'cucumber/formatter/duration'
  7. require 'cucumber/file_specs'
  8. require 'cucumber/filters'
  9. require 'cucumber/formatter/fanout'
  10. require 'cucumber/gherkin/i18n'
  11. require 'cucumber/glue/registry_wrapper'
  12. require 'cucumber/step_match_search'
  13. require 'cucumber/messages'
  14. require 'cucumber/runtime/meta_message_builder'
  15. require 'sys/uname'
  16. module Cucumber
  17. module FixRuby21Bug9285
  18. def message
  19. String(super).gsub('@ rb_sysopen ', '')
  20. end
  21. end
  22. class FileException < RuntimeError
  23. attr_reader :path
  24. def initialize(original_exception, path)
  25. @path = path
  26. super(original_exception)
  27. end
  28. end
  29. class FileNotFoundException < FileException
  30. end
  31. class FeatureFolderNotFoundException < RuntimeError
  32. def initialize(path)
  33. @path = path
  34. super
  35. end
  36. def message
  37. "No such file or directory - #{@path}"
  38. end
  39. end
  40. require 'cucumber/core'
  41. require 'cucumber/runtime/user_interface'
  42. require 'cucumber/runtime/support_code'
  43. class Runtime
  44. attr_reader :results, :support_code, :configuration
  45. include Cucumber::Core
  46. include Formatter::Duration
  47. include Runtime::UserInterface
  48. def initialize(configuration = Configuration.default)
  49. @configuration = Configuration.new(configuration)
  50. @support_code = SupportCode.new(self, @configuration)
  51. end
  52. # Allows you to take an existing runtime and change its configuration
  53. def configure(new_configuration)
  54. @configuration = Configuration.new(new_configuration)
  55. @support_code.configure(@configuration)
  56. end
  57. def run!
  58. @configuration.notify :envelope, Cucumber::Messages::Envelope.new(
  59. meta: MetaMessageBuilder.build_meta_message
  60. )
  61. load_step_definitions
  62. fire_install_plugin_hook
  63. fire_before_all_hook unless dry_run?
  64. # TODO: can we remove this state?
  65. self.visitor = report
  66. receiver = Test::Runner.new(@configuration.event_bus)
  67. compile features, receiver, filters, @configuration.event_bus
  68. @configuration.notify :test_run_finished, !failure?
  69. fire_after_all_hook unless dry_run?
  70. end
  71. def features_paths
  72. @configuration.paths
  73. end
  74. def dry_run?
  75. @configuration.dry_run?
  76. end
  77. def unmatched_step_definitions
  78. @support_code.unmatched_step_definitions
  79. end
  80. def begin_scenario(test_case)
  81. @support_code.fire_hook(:begin_scenario, test_case)
  82. end
  83. def end_scenario(_scenario)
  84. @support_code.fire_hook(:end_scenario)
  85. end
  86. # Returns Ast::DocString for +string_without_triple_quotes+.
  87. #
  88. def doc_string(string_without_triple_quotes, content_type = '', _line_offset = 0)
  89. Core::Test::DocString.new(string_without_triple_quotes, content_type)
  90. end
  91. def failure?
  92. if @configuration.wip?
  93. summary_report.test_cases.total_passed > 0
  94. else
  95. !summary_report.ok?(@configuration.strict)
  96. end
  97. end
  98. private
  99. def fire_install_plugin_hook # :nodoc:
  100. @support_code.fire_hook(:install_plugin, @configuration, registry_wrapper)
  101. end
  102. def fire_before_all_hook # :nodoc:
  103. @support_code.fire_hook(:before_all)
  104. end
  105. def fire_after_all_hook # :nodoc:
  106. @support_code.fire_hook(:after_all)
  107. end
  108. require 'cucumber/core/gherkin/document'
  109. def features
  110. @features ||= feature_files.map do |path|
  111. source = NormalisedEncodingFile.read(path)
  112. @configuration.notify :gherkin_source_read, path, source
  113. Cucumber::Core::Gherkin::Document.new(path, source)
  114. end
  115. end
  116. def feature_files
  117. filespecs.files
  118. end
  119. def filespecs
  120. @filespecs ||= FileSpecs.new(@configuration.feature_files)
  121. end
  122. class NormalisedEncodingFile
  123. COMMENT_OR_EMPTY_LINE_PATTERN = /^\s*#|^\s*$/ # :nodoc:
  124. ENCODING_PATTERN = /^\s*#\s*encoding\s*:\s*([^\s]+)/ # :nodoc:
  125. def self.read(path)
  126. new(path).read
  127. end
  128. def initialize(path)
  129. @file = File.new(path)
  130. set_encoding
  131. rescue Errno::EACCES => e
  132. raise FileNotFoundException.new(e, File.expand_path(path))
  133. rescue Errno::ENOENT
  134. raise FeatureFolderNotFoundException, path
  135. end
  136. def read
  137. @file.read.encode('UTF-8')
  138. end
  139. private
  140. def set_encoding
  141. @file.each do |line|
  142. if ENCODING_PATTERN =~ line
  143. @file.set_encoding Regexp.last_match(1)
  144. break
  145. end
  146. break unless COMMENT_OR_EMPTY_LINE_PATTERN =~ line
  147. end
  148. @file.rewind
  149. end
  150. end
  151. require 'cucumber/formatter/ignore_missing_messages'
  152. require 'cucumber/formatter/fail_fast'
  153. require 'cucumber/formatter/publish_banner_printer'
  154. require 'cucumber/core/report/summary'
  155. def report
  156. return @report if @report
  157. reports = [summary_report] + formatters
  158. reports << fail_fast_report if @configuration.fail_fast?
  159. reports << publish_banner_printer unless @configuration.publish_quiet?
  160. @report ||= Formatter::Fanout.new(reports)
  161. end
  162. def summary_report
  163. @summary_report ||= Core::Report::Summary.new(@configuration.event_bus)
  164. end
  165. def fail_fast_report
  166. @fail_fast_report ||= Formatter::FailFast.new(@configuration)
  167. end
  168. def publish_banner_printer
  169. @publish_banner_printer ||= Formatter::PublishBannerPrinter.new(@configuration)
  170. end
  171. def formatters
  172. @formatters ||=
  173. @configuration.formatter_factories do |factory, formatter_options, path_or_io|
  174. create_formatter(factory, formatter_options, path_or_io)
  175. end
  176. end
  177. def create_formatter(factory, formatter_options, path_or_io)
  178. if accept_options?(factory)
  179. return factory.new(@configuration, formatter_options) if path_or_io.nil?
  180. factory.new(@configuration.with_options(out_stream: path_or_io),
  181. formatter_options)
  182. else
  183. return factory.new(@configuration) if path_or_io.nil?
  184. factory.new(@configuration.with_options(out_stream: path_or_io))
  185. end
  186. end
  187. def accept_options?(factory)
  188. factory.instance_method(:initialize).arity > 1
  189. end
  190. require 'cucumber/core/test/filters'
  191. def filters # rubocop:disable Metrics/AbcSize
  192. tag_expressions = @configuration.tag_expressions
  193. name_regexps = @configuration.name_regexps
  194. tag_limits = @configuration.tag_limits
  195. [].tap do |filters|
  196. filters << Filters::TagLimits.new(tag_limits) if tag_limits.any?
  197. filters << Cucumber::Core::Test::TagFilter.new(tag_expressions)
  198. filters << Cucumber::Core::Test::NameFilter.new(name_regexps)
  199. filters << Cucumber::Core::Test::LocationsFilter.new(filespecs.locations)
  200. filters << Filters::Randomizer.new(@configuration.seed) if @configuration.randomize?
  201. # TODO: can we just use Glue::RegistryAndMore's step definitions directly?
  202. step_match_search = StepMatchSearch.new(@support_code.registry.method(:step_matches), @configuration)
  203. filters << Filters::ActivateSteps.new(step_match_search, @configuration)
  204. @configuration.filters.each { |filter| filters << filter }
  205. unless configuration.dry_run?
  206. filters << Filters::ApplyAfterStepHooks.new(@support_code)
  207. filters << Filters::ApplyBeforeHooks.new(@support_code)
  208. filters << Filters::ApplyAfterHooks.new(@support_code)
  209. filters << Filters::ApplyAroundHooks.new(@support_code)
  210. end
  211. filters << Filters::BroadcastTestCaseReadyEvent.new(@configuration)
  212. unless configuration.dry_run?
  213. filters << Filters::BroadcastTestRunStartedEvent.new(@configuration)
  214. filters << Filters::Quit.new
  215. filters << Filters::Retry.new(@configuration)
  216. # need to do this last so it becomes the first test step
  217. filters << Filters::PrepareWorld.new(self)
  218. end
  219. end
  220. end
  221. def load_step_definitions
  222. files = @configuration.support_to_load + @configuration.step_defs_to_load
  223. @support_code.load_files!(files)
  224. end
  225. def registry_wrapper
  226. Cucumber::Glue::RegistryWrapper.new(@support_code.registry)
  227. end
  228. def log
  229. Cucumber.logger
  230. end
  231. end
  232. end

No Description

Contributors (1)