Sha256: 7d91bd6a2327fbcfc031af8e7f42c8693ba948ee5a6283de184a88b1fb3ae217

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

class ThorSpy
  attr_reader :stdout, :stdin, :stderr
  attr_accessor :app

  def self.on(app, kernel = Kernel)
    new([], StringIO.new, StringIO.new, StringIO.new, kernel).with_app(app)
  end

  # Allow everything fun to be injected from the outside while defaulting to normal implementations.
  def initialize(argv = [], stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel)
    @argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel
  end

  def app
    fail 'You must set the Thor app via ThorSpy.on or #with_app before calling execute!' unless @app
    @app
  end

  def with_app(app)
    fail ArgumentError, 'app should be a class' unless app.is_a? Class
    fail ArgumentError, 'app should be a subclass of Thor' unless app < Thor
    @app = app
    self
  end

  def execute!
    exit_code = begin
      # Thor accesses these streams directly rather than letting them be injected, so we replace them...
      $stderr = @stderr
      $stdin = @stdin
      $stdout = @stdout

      fail 'You must set the Thor app' if @app.nil?

      # Run our normal Thor app the way we know and love.
      app.start(@argv)

      # Thor::Base#start does not have a return value, assume success if no exception is raised.
      0
    rescue StandardError => e
      # The ruby interpreter would pipe this to STDERR and exit 1 in the case of an unhandled exception
      b = e.backtrace
      b.unshift("#{b.shift}: #{e.message} (#{e.class})")
      @stderr.puts(b.map { |s| "\tfrom #{s}" }.join("\n"))
      1
    ensure
      # ...then we put them back.
      $stderr = STDERR
      $stdin = STDIN
      $stdout = STDOUT
    end

    # Proxy our exit code back to the injected kernel.
    @kernel.exit(exit_code)
  end

  def method_missing(meth, *args)
    # I don't think a block makes sense, unless it's stdin.
    return super if block_given?

    @argv = [meth.to_s].concat(args)
    execute!
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polytrix-0.1.0.pre spec/thor_spy.rb