Sha256: f0de91e44317fef39cafc474fa0a50a03df15698eff4b4b7f275c2fafc1b9ee6

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module Guard
  class Spork
    class SporkInstance
      attr_reader :type, :env, :port, :options, :pid

      def initialize(type, port, env, options)
        @type = type
        @port = port
        @env = env
        @options = options
      end

      def to_s
        case type
        when :rspec
          "RSpec"
        when :cucumber
          "Cucumber"
        when :test_unit
          "Test::Unit"
        else
          type.to_s
        end
      end

      def start
        @pid = fork do
          env_exec env, command
        end
        store_pid
      end

      def stop
        Process.kill('KILL', pid)
      end

      def alive?
        return false unless pid
        Process.waitpid(pid, Process::WNOHANG).nil?
      end

      def running?
        return false unless pid
        TCPSocket.new('localhost', port).close
        true
      rescue Errno::ECONNREFUSED
        false
      end

      def command
        parts = []
        parts << "bundle exec" if use_bundler?
        parts << "spork"

        if type == :test_unit
          parts << "testunit"
        elsif type == :cucumber
          parts << "cu"
        end

        parts << "-p #{port}"
        parts.join(" ")
      end

      def env_exec(environment, command)
        if RUBY_VERSION > "1.9"
          exec environment, command
        else
          environment.each_pair { |key, value| ENV[key] = value }
          exec command
        end
      end

      private
        def use_bundler?
          options[:bundler]
        end

        def store_pid
          # We need to store away the PIDs somewhere safe since
          # Guard will destroy our class instances when Guardfile is
          # reevaluated without telling us beforehand
          ENV['SPORK_PIDS'] = [ENV['SPORK_PIDS'], pid.to_s].compact.join(",")
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
guard-spork-0.4.1 lib/guard/spork/spork_instance.rb