Sha256: 7c8cbb8e37032edee7a4d9e343f91032e6a7b7f602014b5cb3a290892cde53fa

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

require 'timeout'
require 'socket'
require 'childprocess'
require 'net/http'
require 'childprocess/process'

module Sauce
  module Utilities
    def silence_stream(stream)
      old_stream = stream.dup
      stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
      stream.sync = true
      yield
    ensure
      stream.reopen(old_stream)
    end

    def wait_for_server_on_port(port)
      while true
        begin
          Timeout::timeout(2) do
              socket = TCPSocket.new('127.0.0.1', port)
              socket.close unless socket.nil?
              return
          end
        rescue Errno::ECONNREFUSED, 
          Errno::EBADF,           # Windows
          Timeout::Error
        end
        sleep 2
      end
    end

    class RailsServer
      include Sauce::Utilities

      def self.is_rails_app?
        File.exists?('script/server') || File.exists?('script/rails')
      end

      def start
        STDERR.puts "Starting Rails server on port 3001..."
        if File.exists?('script/server')
          @server = ChildProcess.build("ruby", "script/server", "-e", "test", "--port", "3001")
        elsif File.exists?('script/rails')
          @server = ChildProcess.build("bundle", "exec", "rails", "server", "-e", "test", "--port", "3001")
        end
        @server.io.inherit!
        @server.start

        wait_for_server_on_port(3001)

        at_exit do
          @server.stop(3, "INT")
        end
        STDERR.puts "Rails server running!"
      end

      def stop
        begin
          @server.stop(3, "INT")
        rescue
          STDERR.puts "Rails server could not be killed. Did it fail to start?"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sauce-2.5.1 lib/sauce/utilities.rb
sauce-2.4.6 lib/sauce/utilities.rb
sauce-2.4.5 lib/sauce/utilities.rb