2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-20 02:11:10 -05:00
|
|
|
require "isolation/abstract_unit"
|
2017-08-19 01:38:31 -04:00
|
|
|
require "console_helpers"
|
2017-01-20 02:11:10 -05:00
|
|
|
require "rails/command"
|
|
|
|
require "rails/commands/server/server_command"
|
|
|
|
|
|
|
|
module ApplicationTests
|
|
|
|
class ServerTest < ActiveSupport::TestCase
|
|
|
|
include ActiveSupport::Testing::Isolation
|
2017-08-19 01:38:31 -04:00
|
|
|
include ConsoleHelpers
|
2017-01-20 02:11:10 -05:00
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2017-08-19 01:38:31 -04:00
|
|
|
test "restart rails server with custom pid file path" do
|
|
|
|
skip "PTY unavailable" unless available_pty?
|
|
|
|
|
2017-12-24 16:25:10 -05:00
|
|
|
File.open("#{app_path}/config/boot.rb", "w") do |f|
|
2018-05-21 08:04:01 -04:00
|
|
|
f.puts "ENV['BUNDLE_GEMFILE'] = '#{Bundler.default_gemfile}'"
|
2020-03-29 19:30:52 -04:00
|
|
|
f.puts 'require "bundler/setup"'
|
2017-12-24 16:25:10 -05:00
|
|
|
end
|
|
|
|
|
2018-09-10 16:29:43 -04:00
|
|
|
primary, replica = PTY.open
|
2017-08-19 01:38:31 -04:00
|
|
|
pid = nil
|
|
|
|
|
2019-02-05 09:50:06 -05:00
|
|
|
Bundler.with_original_env do
|
2019-03-21 18:49:38 -04:00
|
|
|
pid = Process.spawn("bin/rails server -b localhost -P tmp/dummy.pid", chdir: app_path, in: replica, out: replica, err: replica)
|
2019-02-08 20:50:58 -05:00
|
|
|
assert_output("Listening", primary)
|
2019-02-05 09:50:06 -05:00
|
|
|
|
2019-02-08 20:50:58 -05:00
|
|
|
rails("restart")
|
2019-02-05 09:50:06 -05:00
|
|
|
|
2019-02-08 20:50:58 -05:00
|
|
|
assert_output("Restarting", primary)
|
2019-09-25 12:53:52 -04:00
|
|
|
assert_output("Listening", primary)
|
2019-02-08 20:50:58 -05:00
|
|
|
ensure
|
|
|
|
kill(pid) if pid
|
2017-08-19 01:38:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-30 08:15:18 -04:00
|
|
|
test "run +server+ blocks after the server starts" do
|
|
|
|
skip "PTY unavailable" unless available_pty?
|
|
|
|
|
|
|
|
File.open("#{app_path}/config/boot.rb", "w") do |f|
|
|
|
|
f.puts "ENV['BUNDLE_GEMFILE'] = '#{Bundler.default_gemfile}'"
|
|
|
|
f.puts 'require "bundler/setup"'
|
|
|
|
end
|
|
|
|
|
|
|
|
add_to_config(<<~CODE)
|
|
|
|
server do
|
|
|
|
puts 'Hello world'
|
|
|
|
end
|
|
|
|
CODE
|
|
|
|
|
|
|
|
primary, replica = PTY.open
|
|
|
|
pid = nil
|
|
|
|
|
|
|
|
Bundler.with_original_env do
|
|
|
|
pid = Process.spawn("bin/rails server -b localhost", chdir: app_path, in: replica, out: primary, err: replica)
|
|
|
|
assert_output("Hello world", primary)
|
|
|
|
assert_output("Listening", primary)
|
|
|
|
ensure
|
|
|
|
kill(pid) if pid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-19 01:38:31 -04:00
|
|
|
private
|
|
|
|
def kill(pid)
|
|
|
|
Process.kill("TERM", pid)
|
|
|
|
Process.wait(pid)
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
end
|
2017-01-20 02:11:10 -05:00
|
|
|
end
|
|
|
|
end
|