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.

2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # frozen_string_literal: true
  2. require 'cucumber/formatter/http_io'
  3. require 'cucumber/formatter/url_reporter'
  4. require 'cucumber/cli/options'
  5. module Cucumber
  6. module Formatter
  7. module Io
  8. module_function
  9. def ensure_io(path_or_url_or_io, error_stream)
  10. return nil if path_or_url_or_io.nil?
  11. return path_or_url_or_io if io?(path_or_url_or_io)
  12. io = if url?(path_or_url_or_io)
  13. url = path_or_url_or_io
  14. reporter = url.start_with?(Cucumber::Cli::Options::CUCUMBER_PUBLISH_URL) ? URLReporter.new(error_stream) : NoReporter.new
  15. HTTPIO.open(url, nil, reporter)
  16. else
  17. File.open(path_or_url_or_io, Cucumber.file_mode('w'))
  18. end
  19. @io_objects_to_close ||= []
  20. @io_objects_to_close.push(io)
  21. io
  22. end
  23. module ClassMethods
  24. def new(*args, &block)
  25. instance = super
  26. config = args[0]
  27. if config.respond_to? :on_event
  28. config.on_event :test_run_finished do
  29. ios = instance.instance_variable_get(:@io_objects_to_close) || []
  30. ios.each do |io|
  31. at_exit do
  32. unless io.closed?
  33. io.flush
  34. io.close
  35. end
  36. end
  37. end
  38. end
  39. end
  40. instance
  41. end
  42. end
  43. def self.included(formatter_class)
  44. formatter_class.extend(ClassMethods)
  45. end
  46. def io?(path_or_url_or_io)
  47. path_or_url_or_io.respond_to?(:write)
  48. end
  49. def url?(path_or_url_or_io)
  50. path_or_url_or_io.match(/^https?:\/\//)
  51. end
  52. def ensure_file(path, name)
  53. raise "You *must* specify --out FILE for the #{name} formatter" unless path.instance_of? String
  54. raise "I can't write #{name} to a directory - it has to be a file" if File.directory?(path)
  55. raise "I can't write #{name} to a file in the non-existing directory #{File.dirname(path)}" unless File.directory?(File.dirname(path))
  56. ensure_io(path, nil)
  57. end
  58. def ensure_dir(path, name)
  59. raise "You *must* specify --out DIR for the #{name} formatter" unless path.instance_of? String
  60. raise "I can't write #{name} reports to a file - it has to be a directory" if File.file?(path)
  61. FileUtils.mkdir_p(path) unless File.directory?(path)
  62. File.absolute_path path
  63. end
  64. end
  65. end
  66. end

No Description

Contributors (1)