Skip to content

Commit

Permalink
refactor(cli): Move windows-safe spawn code into its own class.
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 28, 2017
1 parent 2d3ea9d commit 0f5496c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
22 changes: 2 additions & 20 deletions lib/pact/mock_service/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,9 @@ def restart_server pidfile
end

def spawn_mock_service
require 'pact/mock_service/server/windows_safe_spawn'
pidfile = mock_service_pidfile
if pidfile.can_start?
if port_available? options.port
pid = spawn(service_spawn_command, {new_pgroup: true})
pidfile.pid = pid
Process.detach(pid)
Pact::MockService::Server::WaitForServerUp.(options.port, {ssl: options.ssl})
pidfile.write
else
raise PortUnavailableError.new("ERROR: Port #{options.port} already in use.")
end
end
Pact::MockService::Server::WindowsSafeSpawn.(service_spawn_command, pidfile, options[:port], options[:ssl])
end

def service_spawn_command
Expand All @@ -205,15 +196,6 @@ def build_service_options
switch_names = service_switch_names
options.collect{ |key, value| "#{switch_names[key]} #{value}" if switch_names[key] }.compact
end

def port_available? port
server = TCPServer.new('127.0.0.1', port)
true
rescue
false
ensure
server.close if server
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pact/mock_service/cli/pidfile.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'fileutils'
require 'pact/mock_service/os'

module Pact
module MockService
Expand Down Expand Up @@ -77,7 +78,6 @@ def waitpid
end

def kill_process

if file_exists?
begin
windows? ? windows_kill : kill_2
Expand All @@ -93,7 +93,7 @@ def kill_process
end

def windows?
/mswin|mingw|bccwin|emx|wince/ =~ RUBY_PLATFORM
Pact::MockService::OS.windows?
end

def kill_2
Expand Down
13 changes: 13 additions & 0 deletions lib/pact/mock_service/os.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Pact
module MockService
module OS

extend self

# Thanks RSpec
def windows?
!!(RbConfig::CONFIG['host_os'] =~ /cygwin|mswin|mingw|bccwin|wince|emx/)
end
end
end
end
44 changes: 44 additions & 0 deletions lib/pact/mock_service/server/windows_safe_spawn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'pact/mock_service/server/wait_for_server_up'
require 'pact/mock_service/os'

module Pact
module MockService
module Server
class WindowsSafeSpawn

class PortUnavailableError < StandardError; end

def self.call command, pidfile, port, ssl = false
if pidfile.can_start?
if port_available? port
pid = spawn(command, spawn_options)
pidfile.pid = pid
Process.detach(pid)
Server::WaitForServerUp.(port, {ssl: ssl})
pidfile.write
else
raise PortUnavailableError.new("ERROR: Port #{port} already in use.")
end
end
end

def self.spawn_options
if Pact::MockService::OS.windows?
{new_pgroup: true}
else
{}
end
end

def self.port_available? port
server = TCPServer.new('127.0.0.1', port)
true
rescue
false
ensure
server.close if server
end
end
end
end
end

0 comments on commit 0f5496c

Please sign in to comment.