Sha256: 46f151ecce7adfab421df9d8d006215b802d68f119f18aeec19d0418c8d006da

Contents?: true

Size: 1.61 KB

Versions: 15

Compression:

Stored size: 1.61 KB

Contents

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

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
        end
        STDERR.puts "Rails server running!"
      end

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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sauce-2.4.4 lib/sauce/utilities.rb
sauce-2.4.3 lib/sauce/utilities.rb
sauce-2.4.2 lib/sauce/utilities.rb
sauce-2.4.1 lib/sauce/utilities.rb
sauce-2.4.0 lib/sauce/utilities.rb
sauce-2.3.6 lib/sauce/utilities.rb
sauce-2.3.5 lib/sauce/utilities.rb
sauce-2.3.4 lib/sauce/utilities.rb
sauce-2.3.3 lib/sauce/utilities.rb
sauce-2.3.2 lib/sauce/utilities.rb
sauce-2.2.2 lib/sauce/utilities.rb
sauce-2.2.1 lib/sauce/utilities.rb
sauce-2.2.0 lib/sauce/utilities.rb
sauce-2.1.0 lib/sauce/utilities.rb
sauce-2.0.0 lib/sauce/utilities.rb