Sha256: 2120772a24a3ef92aee752921d5c8e353b74e6c93c3336301b7fbad5eb9ffc57

Contents?: true

Size: 1.94 KB

Versions: 10

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

require 'timeout'

module Cucumber
  class Runtime
    module UserInterface
      attr_writer :visitor

      # Suspends execution and prompts +question+ to the console (STDOUT).
      # An operator (manual tester) can then enter a line of text and hit
      # <ENTER>. The entered text is returned, and both +question+ and
      # the result is added to the output using #puts.
      #
      # If you want a beep to happen (to grab the manual tester's attention),
      # just prepend ASCII character 7 to the question:
      #
      #   ask("#{7.chr}How many cukes are in the external system?")
      #
      # If that doesn't issue a beep, you can shell out to something else
      # that makes a sound before invoking #ask.
      #
      def ask(question, timeout_seconds)
        $stdout.puts(question)
        $stdout.flush
        puts(question)

        answer = if Cucumber::JRUBY
                   jruby_gets(timeout_seconds)
                 else
                   mri_gets(timeout_seconds)
                 end

        raise("Waited for input for #{timeout_seconds} seconds, then timed out.") unless answer

        puts(answer)
        answer
      end

      # Embed +src+ of MIME type +mime_type+ into the output. The +src+ argument may
      # be a path to a file, or if it's an image it may also be a Base64 encoded image.
      # The embedded data may or may not be ignored, depending on what kind of formatter(s) are active.
      #
      def attach(src, media_type)
        @visitor.attach(src, media_type)
      end

      private

      def mri_gets(timeout_seconds)
        Timeout.timeout(timeout_seconds) do
          $stdin.gets
        end
      rescue Timeout::Error
        nil
      end

      def jruby_gets(timeout_seconds)
        answer = nil
        t = java.lang.Thread.new do
          answer = $stdin.gets
        end
        t.start
        t.join(timeout_seconds * 1000)
        answer
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/cucumber-9.0.2/lib/cucumber/runtime/user_interface.rb
cucumber-9.0.2 lib/cucumber/runtime/user_interface.rb
cucumber-9.0.1 lib/cucumber/runtime/user_interface.rb
cucumber-9.0.0 lib/cucumber/runtime/user_interface.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/cucumber-8.0.0/lib/cucumber/runtime/user_interface.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/cucumber-8.0.0/lib/cucumber/runtime/user_interface.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/cucumber-8.0.0/lib/cucumber/runtime/user_interface.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/cucumber-8.0.0/lib/cucumber/runtime/user_interface.rb
cucumber-8.0.0 lib/cucumber/runtime/user_interface.rb
cucumber-8.0.0.rc.1 lib/cucumber/runtime/user_interface.rb