class Webpack::Testing::FileServer

Public Class Methods

new(root) click to toggle source
Calls superclass method
# File lib/webpack/testing/file_server.rb, line 10
def initialize(root)
  port = unused_port
  pid = File.expand_path("./tmp/rack-#{port}.pid")
  super(
    app: ::Rack::File.new(root),
    Host: default_host,
    Port: port,
    pid: pid,
    daemonize: true
  )
end

Public Instance Methods

host() click to toggle source
# File lib/webpack/testing/file_server.rb, line 22
def host
  options[:Host]
end
host_with_port() click to toggle source
# File lib/webpack/testing/file_server.rb, line 30
def host_with_port
  "#{host}:#{port}"
end
pid() click to toggle source
# File lib/webpack/testing/file_server.rb, line 34
def pid
  ::File.exist?(options[:pid]) ? ::File.read(options[:pid]).to_i : nil
end
port() click to toggle source
# File lib/webpack/testing/file_server.rb, line 26
def port
  options[:Port]
end
run(timeout: 10) { |self| ... } click to toggle source
# File lib/webpack/testing/file_server.rb, line 45
def run(timeout: 10)
  Process.fork { start } unless running?
  wait timeout

  return unless block_given?

  yield self
  stop
end
running?() click to toggle source
# File lib/webpack/testing/file_server.rb, line 38
def running?
  Socket.tcp(host, port).close
  true
rescue Errno::ECONNREFUSED
  false
end
stop() click to toggle source
# File lib/webpack/testing/file_server.rb, line 55
def stop
  return if pid.nil?

  Process.kill "SIGTERM", pid
  ::File.delete(options[:pid])
end

Private Instance Methods

default_host() click to toggle source
# File lib/webpack/testing/file_server.rb, line 74
def default_host
  env = ENV["RACK_ENV"] || "development"
  env == "development" ? "localhost" : "0.0.0.0"
end
unused_port() click to toggle source
# File lib/webpack/testing/file_server.rb, line 79
def unused_port
  Addrinfo.tcp("", 0).bind { |s| s.local_address.ip_port }
end
wait(timeout, interval: 1) click to toggle source
# File lib/webpack/testing/file_server.rb, line 64
def wait(timeout, interval: 1)
  Timeout.timeout(timeout) do
    loop do
      break if running?

      sleep interval
    end
  end
end