Sha256: 490602209fa0e3e8bdedbee7433520ea0fbd6be90304af4c90012f48fbee1cb8

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module Mutant
  # The outer world IO objects mutant does interact with
  class World
    include Adamantium, Anima.new(
      :condition_variable,
      :environment_variables,
      :gem,
      :gem_method,
      :io,
      :json,
      :kernel,
      :load_path,
      :marshal,
      :mutex,
      :object_space,
      :open3,
      :pathname,
      :process,
      :random,
      :recorder,
      :stderr,
      :stdout,
      :tempfile,
      :thread,
      :time,
      :timer
    )

    INSPECT = '#<Mutant::World>'

    private_constant(*constants(false))

    # Object inspection
    #
    # @return [String]
    def inspect
      INSPECT
    end

    class CommandStatus
      include Adamantium, Anima.new(:process_status, :stderr, :stdout)
    end # CommandStatus

    # Capture stdout of a command
    #
    # @param [Array<String>] command
    #
    # @return [Either<CommandStatus,CommandStatus>]
    def capture_command(command)
      stdout, stderr, process_status = open3.capture3(*command, binmode: true)

      (process_status.success? ? Either::Right : Either::Left).new(
        CommandStatus.new(
          process_status:,
          stderr:,
          stdout:
        )
      )
    end

    # Try const get
    #
    # @param [String]
    #
    # @return [Class|Module|nil]
    #
    # rubocop:disable Lint/SuppressedException
    def try_const_get(name)
      kernel.const_get(name)
    rescue NameError
    end
    # rubocop:enable Lint/SuppressedException

    # Deadline
    #
    # @param [Float, nil] allowed_time
    def deadline(allowed_time)
      if allowed_time
        Timer::Deadline.new(
          allowed_time:,
          timer:
        )
      else
        Timer::Deadline::None.new
      end
    end

    def record(name, &)
      recorder.record(name, &)
    end

    def process_warmup
      process.warmup if process.respond_to?(:warmup)
    end
  end # World
end # Mutant

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mutant-0.12.4 lib/mutant/world.rb
mutant-0.12.3 lib/mutant/world.rb