diff --git a/lib/jekyll/commands/serve.rb b/lib/jekyll/commands/serve.rb index 9042a4a0..c3b99cba 100644 --- a/lib/jekyll/commands/serve.rb +++ b/lib/jekyll/commands/serve.rb @@ -33,6 +33,7 @@ module Jekyll opts["serving"] = true opts["watch" ] = true unless opts.key?("watch") config = opts["config"] + opts["url"] = default_url(opts) if Jekyll.env == "development" Build.process(opts) opts["config"] = config Serve.process(opts) @@ -47,11 +48,7 @@ module Jekyll destination = opts["destination"] setup(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, opts) - launch_browser server, opts if opts["open_url"] - boot_or_detach server, opts + start_up_webrick(opts, destination) end # Do a base pre-setup of WEBRick so that everything is in place @@ -101,6 +98,17 @@ module Jekyll opts 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, opts) + launch_browser server, opts if opts["open_url"] + boot_or_detach server, opts + end + # Recreate NondisclosureName under utf-8 circumstance private @@ -127,6 +135,16 @@ module Jekyll # + def default_url(opts) + config = configuration_from_options(opts) + host = config["host"] == "127.0.0.1" ? "localhost" : config["host"] + port = config["port"] + protocol = config["ssl_cert"] && config["ssl_key"] ? "https" : "http" + "#{protocol}://#{host}:#{port}" + end + + # + private def launch_browser(server, opts) address = server_address(server, opts) diff --git a/test/test_commands_serve.rb b/test/test_commands_serve.rb index 7e8a7062..143dc2a5 100644 --- a/test/test_commands_serve.rb +++ b/test/test_commands_serve.rb @@ -19,6 +19,12 @@ class TestCommandsServe < JekyllUnitTest p ) 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 should "label itself" do @@ -79,16 +85,53 @@ class TestCommandsServe < JekyllUnitTest custom_options = { "config" => %w(_config.yml _development.yml), "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) @merc.execute(:serve, { "config" => %w(_config.yml _development.yml), "watch" => false }) 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 should "debug when verbose" do assert_equal custom_opts({ "verbose" => true })[:Logger].level, 5