Sha256: 97d9c17f01b4a8fd29fa7089b5f5621eed9b1b39295dfa92c669a7d28e1585e2

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require "scmd/version"
require "scmd/command"

module Scmd
  # Scmd can be run in "test mode".  This means that command spies will be used
  # in place of "live" commands, each time a command is run or started will be
  # logged in a collection and option-specific spies can be added and used to
  # "stub" spies with specific attributes in specific contexts.

  def self.new(*args)
    if !ENV["SCMD_TEST_MODE"]
      Command.new(*args)
    else
      commands.get(*args)
    end
  end

  def self.commands
    raise NoMethodError unless ENV["SCMD_TEST_MODE"]
    @commands ||= begin
      require "scmd/stored_commands"
      StoredCommands.new
    end
  end

  def self.calls
    raise NoMethodError unless ENV["SCMD_TEST_MODE"]
    @calls ||= []
  end

  def self.reset
    raise NoMethodError unless ENV["SCMD_TEST_MODE"]
    calls.clear
    commands.remove_all
  end

  def self.add_command(cmd_str, &block)
    commands.add(cmd_str, &block)
  end

  class Call < Struct.new(:cmd_str, :input, :cmd)
    def initialize(cmd_spy, input)
      super(cmd_spy.cmd_str, input, cmd_spy)
    end
  end

  TimeoutError = Class.new(::RuntimeError)

  class RunError < ::RuntimeError
    def initialize(stderr, called_from = nil)
      super(stderr)
      set_backtrace(called_from || caller)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scmd-3.0.4 lib/scmd.rb