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.

cucumber_mixin.rb 3.2 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # frozen_string_literal: true
  2. require 'autotest'
  3. require 'tempfile'
  4. require 'cucumber'
  5. require 'cucumber/cli/profile_loader'
  6. module Autotest::CucumberMixin
  7. def self.included(receiver)
  8. receiver::ALL_HOOKS << %i[run_features ran_features]
  9. end
  10. attr_accessor :features_to_run
  11. def initialize
  12. super
  13. reset_features
  14. end
  15. def run
  16. hook :initialize
  17. reset
  18. reset_features
  19. add_sigint_handler
  20. loop do # ^c handler
  21. wait_for_green
  22. if tainted
  23. rerun_all_tests
  24. rerun_all_features if all_good
  25. else
  26. hook :all_good
  27. end
  28. wait_for_changes
  29. # Once tests and features are green, reset features every
  30. # time a file is changed to see if anything breaks.
  31. reset_features
  32. rescue Interrupt
  33. break if wants_to_quit
  34. reset
  35. reset_features
  36. end
  37. hook :quit
  38. end
  39. def all_features_good
  40. features_to_run == ''
  41. end
  42. def wait_for_green
  43. loop do
  44. super
  45. run_features
  46. wait_for_changes unless all_features_good
  47. break if all_features_good
  48. end
  49. end
  50. def rerun_all_features
  51. reset_features
  52. run_features
  53. end
  54. def reset_features
  55. self.features_to_run = :all
  56. end
  57. def run_features
  58. hook :run_features
  59. Tempfile.open('autotest-cucumber') do |dirty_features_file|
  60. cmd = make_cucumber_cmd(features_to_run, dirty_features_file.path)
  61. break if cmd.empty?
  62. old_sync = $stdout.sync
  63. $stdout.sync = true
  64. self.results = []
  65. line = []
  66. begin
  67. open("| #{cmd}", 'r') do |f| # rubocop:disable Security/Open
  68. until f.eof?
  69. c = f.getc || break
  70. print(c)
  71. line << c
  72. next unless c == "\n"
  73. results << line.join
  74. line.clear
  75. end
  76. end
  77. ensure
  78. $stdout.sync = old_sync
  79. end
  80. self.features_to_run = dirty_features_file.read.strip
  81. self.tainted = true unless features_to_run == ''
  82. end
  83. hook :ran_features
  84. end
  85. def make_cucumber_cmd(features_to_run, _dirty_features_filename)
  86. return '' if features_to_run.empty?
  87. profile_loader = Cucumber::Cli::ProfileLoader.new
  88. profile = profile(profile_loader)
  89. args = created_args(features_to_run, profile)
  90. "#{Cucumber::RUBY_BINARY} #{Cucumber::BINARY} #{args}"
  91. end
  92. def profile(profile_loader)
  93. profile ||= 'autotest-all' if profile_loader.profile?('autotest-all') && features_to_run == :all
  94. profile ||= 'autotest' if profile_loader.profile?('autotest')
  95. profile || nil
  96. end
  97. def created_args(features_to_run, profile)
  98. args = if profile
  99. ['--profile', profile]
  100. else
  101. %w[--format] << (features_to_run == :all ? 'progress' : 'pretty')
  102. end
  103. # No --color option as some IDEs (Netbeans) don't output them very well (1 failed step)
  104. args += %w[--format rerun --out] << dirty_features_filename
  105. args << (features_to_run == :all ? '' : features_to_run)
  106. # All steps becom undefined during rerun unless the following is run.
  107. args << 'features/step_definitions' << 'features/support' unless features_to_run == :all
  108. args.join(' ')
  109. end
  110. end

No Description

Contributors (1)