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.

main.rb 2.6 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # frozen_string_literal: true
  2. require 'optparse'
  3. require 'cucumber'
  4. require 'logger'
  5. require 'cucumber/cli/configuration'
  6. module Cucumber
  7. module Cli
  8. class Main
  9. class << self
  10. def execute(args)
  11. new(args).execute!
  12. end
  13. end
  14. def initialize(args, out = $stdout, err = $stderr, kernel = Kernel)
  15. @args = args
  16. @out = out
  17. @err = err
  18. @kernel = kernel
  19. end
  20. def execute!(existing_runtime = nil)
  21. trap_interrupt
  22. runtime = runtime(existing_runtime)
  23. runtime.run!
  24. if Cucumber.wants_to_quit
  25. exit_unable_to_finish
  26. elsif runtime.failure?
  27. exit_tests_failed
  28. else
  29. exit_ok
  30. end
  31. rescue SystemExit => e
  32. @kernel.exit(e.status)
  33. rescue FileNotFoundException => e
  34. @err.puts(e.message)
  35. @err.puts("Couldn't open #{e.path}")
  36. exit_unable_to_finish
  37. rescue FeatureFolderNotFoundException => e
  38. @err.puts("#{e.message}. You can use `cucumber --init` to get started.")
  39. exit_unable_to_finish
  40. rescue ProfilesNotDefinedError, YmlLoadError, ProfileNotFound => e
  41. @err.puts(e.message)
  42. exit_unable_to_finish
  43. rescue Errno::EACCES, Errno::ENOENT => e
  44. @err.puts("#{e.message} (#{e.class})")
  45. exit_unable_to_finish
  46. rescue Exception => e # rubocop:disable Lint/RescueException
  47. @err.puts("#{e.message} (#{e.class})")
  48. @err.puts(e.backtrace.join("\n"))
  49. exit_unable_to_finish
  50. end
  51. def configuration
  52. @configuration ||= Configuration.new(@out, @err).tap do |configuration|
  53. configuration.parse!(@args)
  54. Cucumber.logger = configuration.log
  55. end
  56. end
  57. private
  58. def exit_ok
  59. @kernel.exit 0
  60. end
  61. def exit_tests_failed
  62. @kernel.exit 1
  63. end
  64. def exit_unable_to_finish
  65. @kernel.exit 2
  66. end
  67. # stops the program immediately, without running at_exit blocks
  68. def exit_unable_to_finish!
  69. @kernel.exit! 2
  70. end
  71. def trap_interrupt
  72. trap('INT') do
  73. exit_unable_to_finish! if Cucumber.wants_to_quit
  74. Cucumber.wants_to_quit = true
  75. $stderr.puts "\nExiting... Interrupt again to exit immediately."
  76. exit_unable_to_finish
  77. end
  78. end
  79. def runtime(existing_runtime)
  80. return Runtime.new(configuration) unless existing_runtime
  81. existing_runtime.configure(configuration)
  82. existing_runtime
  83. end
  84. end
  85. end
  86. end

No Description

Contributors (1)