Merge pull request #5431 from Crunch09/update_url_in_dev_environment

Merge pull request 5431
This commit is contained in:
jekyllbot 2016-10-05 09:59:01 -07:00 committed by GitHub
commit d879840cc5
2 changed files with 81 additions and 13 deletions

View File

@ -33,6 +33,7 @@ module Jekyll
opts["serving"] = true opts["serving"] = true
opts["watch" ] = true unless opts.key?("watch") opts["watch" ] = true unless opts.key?("watch")
config = opts["config"] config = opts["config"]
opts["url"] = default_url(opts) if Jekyll.env == "development"
Build.process(opts) Build.process(opts)
opts["config"] = config opts["config"] = config
Serve.process(opts) Serve.process(opts)
@ -47,11 +48,7 @@ module Jekyll
destination = opts["destination"] destination = opts["destination"]
setup(destination) setup(destination)
server = WEBrick::HTTPServer.new(webrick_opts(opts)).tap { |o| o.unmount("") } start_up_webrick(opts, destination)
server.mount(opts["baseurl"], Servlet, destination, file_handler_opts)
Jekyll.logger.info "Server address:", server_address(server, opts)
launch_browser server, opts if opts["open_url"]
boot_or_detach server, opts
end end
# Do a base pre-setup of WEBRick so that everything is in place # Do a base pre-setup of WEBRick so that everything is in place
@ -101,6 +98,22 @@ module Jekyll
opts opts
end end
#
private
def start_up_webrick(opts, destination)
server = WEBrick::HTTPServer.new(webrick_opts(opts)).tap { |o| o.unmount("") }
server.mount(opts["baseurl"], Servlet, destination, file_handler_opts)
Jekyll.logger.info "Server address:", server_address(
server.config[:SSLEnable],
server.config[:BindAddress],
server.config[:Port],
opts["baseurl"]
)
launch_browser server, opts if opts["open_url"]
boot_or_detach server, opts
end
# Recreate NondisclosureName under utf-8 circumstance # Recreate NondisclosureName under utf-8 circumstance
private private
@ -116,17 +129,29 @@ module Jekyll
# #
private private
def server_address(server, opts) def server_address(prefix, address, port, baseurl = nil)
format("%{prefix}://%{address}:%{port}%{baseurl}", { format("%{prefix}://%{address}:%{port}%{baseurl}", {
:prefix => server.config[:SSLEnable] ? "https" : "http", :prefix => prefix ? "https" : "http",
:baseurl => opts["baseurl"] ? "#{opts["baseurl"]}/" : "", :address => address,
:address => server.config[:BindAddress], :port => port,
:port => server.config[:Port] :baseurl => baseurl ? "#{baseurl}/" : ""
}) })
end end
# #
private
def default_url(opts)
config = configuration_from_options(opts)
server_address(
config["ssl_cert"] && config["ssl_key"],
config["host"] == "127.0.0.1" ? "localhost" : config["host"],
config["port"]
)
end
#
private private
def launch_browser(server, opts) def launch_browser(server, opts)
address = server_address(server, opts) address = server_address(server, opts)

View File

@ -19,6 +19,12 @@ class TestCommandsServe < JekyllUnitTest
p p
) )
end end
Jekyll.sites.clear
allow(SafeYAML).to receive(:load_file).and_return({})
allow(Jekyll::Commands::Build).to receive(:build).and_return("")
end
teardown do
Jekyll.sites.clear
end end
should "label itself" do should "label itself" do
@ -79,16 +85,53 @@ class TestCommandsServe < JekyllUnitTest
custom_options = { custom_options = {
"config" => %w(_config.yml _development.yml), "config" => %w(_config.yml _development.yml),
"serving" => true, "serving" => true,
"watch" => false # for not having guard output when running the tests "watch" => false, # for not having guard output when running the tests
"url" => "http://localhost:4000"
} }
allow(SafeYAML).to receive(:load_file).and_return({})
allow(Jekyll::Commands::Build).to receive(:build).and_return("")
expect(Jekyll::Commands::Serve).to receive(:process).with(custom_options) expect(Jekyll::Commands::Serve).to receive(:process).with(custom_options)
@merc.execute(:serve, { "config" => %w(_config.yml _development.yml), @merc.execute(:serve, { "config" => %w(_config.yml _development.yml),
"watch" => false }) "watch" => false })
end end
context "in development environment" do
setup do
expect(Jekyll).to receive(:env).and_return("development")
expect(Jekyll::Commands::Serve).to receive(:start_up_webrick)
end
should "set the site url by default to `http://localhost:4000`" do
@merc.execute(:serve, { "watch" => false, "url" => "https://jekyllrb.com/" })
assert_equal 1, Jekyll.sites.count
assert_equal "http://localhost:4000", Jekyll.sites.first.config["url"]
end
should "take `host`, `port` and `ssl` into consideration if set" do
@merc.execute(:serve, {
"watch" => false,
"host" => "example.com",
"port" => "9999",
"url" => "https://jekyllrb.com/",
"ssl_cert" => "foo",
"ssl_key" => "bar"
})
assert_equal 1, Jekyll.sites.count
assert_equal "https://example.com:9999", Jekyll.sites.first.config["url"]
end
end
context "not in development environment" do
should "not update the site url" do
expect(Jekyll).to receive(:env).and_return("production")
expect(Jekyll::Commands::Serve).to receive(:start_up_webrick)
@merc.execute(:serve, { "watch" => false, "url" => "https://jekyllrb.com/" })
assert_equal 1, Jekyll.sites.count
assert_equal "https://jekyllrb.com/", Jekyll.sites.first.config["url"]
end
end
context "verbose" do context "verbose" do
should "debug when verbose" do should "debug when verbose" do
assert_equal custom_opts({ "verbose" => true })[:Logger].level, 5 assert_equal custom_opts({ "verbose" => true })[:Logger].level, 5