Class: Webpack::Testing::FileServer

Inherits:
Rack::Server
  • Object
show all
Defined in:
lib/webpack/testing/file_server.rb

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ FileServer

Returns a new instance of FileServer.



10
11
12
13
14
15
16
17
18
19
20
# 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

Instance Method Details

#hostObject



22
23
24
# File 'lib/webpack/testing/file_server.rb', line 22

def host
  options[:Host]
end

#host_with_portObject



30
31
32
# File 'lib/webpack/testing/file_server.rb', line 30

def host_with_port
  "#{host}:#{port}"
end

#pidObject



34
35
36
# File 'lib/webpack/testing/file_server.rb', line 34

def pid
  ::File.exist?(options[:pid]) ? ::File.read(options[:pid]).to_i : nil
end

#portObject



26
27
28
# File 'lib/webpack/testing/file_server.rb', line 26

def port
  options[:Port]
end

#run(timeout: 10) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



45
46
47
48
49
50
51
52
53
# 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?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/webpack/testing/file_server.rb', line 38

def running?
  Socket.tcp(host, port).close
  true
rescue Errno::ECONNREFUSED
  false
end

#stopObject



55
56
57
58
59
60
# File 'lib/webpack/testing/file_server.rb', line 55

def stop
  return if pid.nil?

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