Sha256: d4a939469e56220726d7d96b3163717edccd5090703c217d52823e25eefef6b5

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

require "guard/rspec/command"

module Guard
  class RSpec < Plugin
    class RSpecProcess
      class Failure < RuntimeError
      end

      attr_reader :results

      def initialize(command, formatter_tmp_file)
        @command = command
        @formatter_tmp_file = formatter_tmp_file
        @results = nil

        @exit_code = _run
        @results = _read_results
      end

      def all_green?
        exit_code.zero?
      end

      private

      def _run
        _without_bundler_env do
          exit_code = _really_run
          unless [0, Command::FAILURE_EXIT_CODE].include?(exit_code)
            fail Failure, "Failed: #{command.inspect} (exit code: #{exit_code})"
          end
          exit_code
        end
      end

      def _really_run
        env = { "GUARD_RSPEC_RESULTS_FILE" => formatter_tmp_file }
        pid = Kernel.spawn(env, command) # use spawn to stub in JRuby
        result = Process.wait2(pid)
        result.last.exitstatus
      rescue Errno::ENOENT => ex
        fail Failure, "Failed: #{command.inspect} (#{ex})"
      end

      def _read_results
        Results.new(formatter_tmp_file)
      ensure
        File.delete(formatter_tmp_file) if File.exist?(formatter_tmp_file)
      end

      def _without_bundler_env
        if defined?(::Bundler)
          ::Bundler.with_clean_env { yield }
        else
          yield
        end
      end

      private

      attr_reader :command
      attr_reader :exit_code
      attr_reader :formatter_tmp_file
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
guard-rspec-4.6.3 lib/guard/rspec/rspec_process.rb
guard-rspec-4.6.2 lib/guard/rspec/rspec_process.rb
guard-rspec-4.6.1 lib/guard/rspec/rspec_process.rb
guard-rspec-4.6.0 lib/guard/rspec/rspec_process.rb