Sha256: 0eec2a09bcb0f7169eecdbb300909a59fc8aa15544ff63acd9d2adb392acafac

Contents?: true

Size: 1.1 KB

Versions: 10

Compression:

Stored size: 1.1 KB

Contents

require 'open3'

module Radius
  module RSpec
    module Processes
      def subcommand(*args)
        Subcommand.new(*args).run
      end

      class Subcommand
        TERMINAL_COLORS = /\e\[\d+m/

        attr_reader :raw_stdout, :raw_stderr, :status, :command

        def initialize(*args)
          raise 'Command cannot be empty' if args.empty?

          # Helpfully handle when the command is an array
          args.unshift(*args.shift) if Array === args.first

          @command = args
          @raw_stdout = ''
          @raw_stderr = ''
          @status = nil
        end

        def failure?
          !success?
        end

        def run
          @raw_stdout, @raw_stderr, @status = Open3.capture3(*command)
          self
        end

        def stderr
          raw_stderr.gsub(TERMINAL_COLORS, '')
        end

        def stdout
          raw_stdout.gsub(TERMINAL_COLORS, '')
        end

        def success?
          status == 0
        end
        alias_method :successful?, :success?
      end
    end
  end
end

RSpec.configure do |c|
  c.include Radius::RSpec::Processes, type: :feature
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
radius-toolbelt-0.0.10 spec/support/subcommand.rb
radius-toolbelt-0.0.9 spec/support/subcommand.rb
radius-toolbelt-0.0.8 spec/support/subcommand.rb
radius-toolbelt-0.0.7 spec/support/subcommand.rb
radius-toolbelt-0.0.6 spec/support/subcommand.rb
radius-toolbelt-0.0.5 spec/support/subcommand.rb
radius-toolbelt-0.0.4 spec/support/subcommand.rb
radius-toolbelt-0.0.3 spec/support/subcommand.rb
radius-toolbelt-0.0.2 spec/support/subcommand.rb
radius-toolbelt-0.0.1 spec/support/subcommand.rb