From 2236ee42845047c4102c924978ddb56b7c628bc2 Mon Sep 17 00:00:00 2001 From: Florian Thomas Date: Sat, 30 Jul 2016 15:48:21 +0200 Subject: [PATCH 1/4] migrate existing tests to `should` syntax --- test/test_plugin_manager.rb | 40 +++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/test/test_plugin_manager.rb b/test/test_plugin_manager.rb index 22ad617f..67dfa726 100644 --- a/test/test_plugin_manager.rb +++ b/test/test_plugin_manager.rb @@ -8,28 +8,34 @@ class TestPluginManager < JekyllUnitTest FileUtils.mv "Gemfile.old", "Gemfile" end - def test_requiring_from_bundler - with_env("JEKYLL_NO_BUNDLER_REQUIRE", nil) do - assert Jekyll::PluginManager.require_from_bundler, - "require_from_bundler should return true." - assert ENV["JEKYLL_NO_BUNDLER_REQUIRE"], "Gemfile plugins were not required." + context "JEKYLL_NO_BUNDLER_REQUIRE set to `nil`" do + should "require from bundler" do + with_env("JEKYLL_NO_BUNDLER_REQUIRE", nil) do + assert Jekyll::PluginManager.require_from_bundler, + "require_from_bundler should return true." + assert ENV["JEKYLL_NO_BUNDLER_REQUIRE"], "Gemfile plugins were not required." + end end end - def test_blocking_requiring_from_bundler - with_env("JEKYLL_NO_BUNDLER_REQUIRE", "true") do - assert_equal false, Jekyll::PluginManager.require_from_bundler, - "Gemfile plugins were required but shouldn't have been" - assert ENV["JEKYLL_NO_BUNDLER_REQUIRE"] - end - end - - def test_no_gemfile - with_env("JEKYLL_NO_BUNDLER_REQUIRE", nil) do - with_no_gemfile do + context "JEKYLL_NO_BUNDLER_REQUIRE set to `true`" do + should "not require from bundler" do + with_env("JEKYLL_NO_BUNDLER_REQUIRE", "true") do assert_equal false, Jekyll::PluginManager.require_from_bundler, "Gemfile plugins were required but shouldn't have been" - assert_nil ENV["JEKYLL_NO_BUNDLER_REQUIRE"] + assert ENV["JEKYLL_NO_BUNDLER_REQUIRE"] + end + end + end + + context "JEKYLL_NO_BUNDLER_REQUIRE set to `nil` and no Gemfile present" do + should "not require from bundler" do + with_env("JEKYLL_NO_BUNDLER_REQUIRE", nil) do + with_no_gemfile do + assert_equal false, Jekyll::PluginManager.require_from_bundler, + "Gemfile plugins were required but shouldn't have been" + assert_nil ENV["JEKYLL_NO_BUNDLER_REQUIRE"] + end end end end From d158d73ce1da2b1c105bdd3eaea525852b9b097a Mon Sep 17 00:00:00 2001 From: Florian Thomas Date: Sat, 30 Jul 2016 15:49:16 +0200 Subject: [PATCH 2/4] add missing tests --- test/test_plugin_manager.rb | 106 ++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/test/test_plugin_manager.rb b/test/test_plugin_manager.rb index 67dfa726..809a1a46 100644 --- a/test/test_plugin_manager.rb +++ b/test/test_plugin_manager.rb @@ -39,4 +39,110 @@ class TestPluginManager < JekyllUnitTest end end end + + context "require gems" do + should "check if configured gems are allowed" do + allow(Jekyll::External).to receive(:require_with_graceful_fail).and_return(nil) + site = double(:gems => %w(jemoji foobar)) + plugin_manager = PluginManager.new(site) + + expect(plugin_manager).to receive(:plugin_allowed?).with("jemoji") + expect(plugin_manager).to receive(:plugin_allowed?).with("foobar") + plugin_manager.require_gems + end + end + + context "site is not marked as safe" do + should "allow all plugins" do + site = double(:safe => false) + plugin_manager = PluginManager.new(site) + + assert plugin_manager.plugin_allowed?("foobar") + end + + should "require plugin files" do + site = double({ :safe => false, + :config => { "plugins_dir" => "_plugins" }, + :in_source_dir => "/tmp/" }) + plugin_manager = PluginManager.new(site) + + expect(Jekyll::External).to receive(:require_with_graceful_fail) + plugin_manager.require_plugin_files + end + end + + context "site is marked as safe" do + should "allow plugins if they are whitelisted" do + site = double({ :safe => true, :config => { "whitelist" => ["jemoji"] } }) + plugin_manager = PluginManager.new(site) + + assert plugin_manager.plugin_allowed?("jemoji") + assert !plugin_manager.plugin_allowed?("not_allowed_plugin") + end + + should "not require plugin files" do + site = double({ :safe => true }) + plugin_manager = PluginManager.new(site) + + expect(Jekyll::External).to_not receive(:require_with_graceful_fail) + plugin_manager.require_plugin_files + end + end + + context "plugins_dir is set to the default" do + should "call site's in_source_dir" do + site = double({ + :config => { + "plugins_dir" => Jekyll::Configuration::DEFAULTS["plugins_dir"] + }, + :in_source_dir => "/tmp/" + }) + plugin_manager = PluginManager.new(site) + + expect(site).to receive(:in_source_dir).with("_plugins") + plugin_manager.plugins_path + end + end + + context "plugins_dir is set to a different dir" do + should "expand plugin path" do + site = double({ :config => { "plugins_dir" => "some_other_plugins_path" } }) + plugin_manager = PluginManager.new(site) + + expect(File).to receive(:expand_path).with("some_other_plugins_path") + plugin_manager.plugins_path + end + end + + context "`paginate` config is activated" do + should "print deprecation warning if jekyll-paginate is not present" do + site = double({ :config => { "paginate" => true } }) + plugin_manager = PluginManager.new(site) + + expect(Jekyll::Deprecator).to( + receive(:deprecation_message).with(%r!jekyll-paginate!) + ) + plugin_manager.deprecation_checks + end + + should "print no deprecation warning if jekyll-paginate is present" do + site = double({ + :config => { "paginate" => true, "gems" => ["jekyll-paginate"] } + }) + plugin_manager = PluginManager.new(site) + + expect(Jekyll::Deprecator).to_not receive(:deprecation_message) + plugin_manager.deprecation_checks + end + end + + should "conscientious require" do + site = double + plugin_manager = PluginManager.new(site) + + expect(plugin_manager).to( + receive_messages([:require_plugin_files, :require_gems, :deprecation_checks]) + ) + plugin_manager.conscientious_require + end end From 539154a1583ea4a41da7a1665c1868191818602a Mon Sep 17 00:00:00 2001 From: Florian Thomas Date: Sun, 7 Aug 2016 20:54:22 +0200 Subject: [PATCH 3/4] replace `assert false, ...` with `refute` --- test/test_plugin_manager.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_plugin_manager.rb b/test/test_plugin_manager.rb index 809a1a46..c43c1272 100644 --- a/test/test_plugin_manager.rb +++ b/test/test_plugin_manager.rb @@ -21,7 +21,7 @@ class TestPluginManager < JekyllUnitTest context "JEKYLL_NO_BUNDLER_REQUIRE set to `true`" do should "not require from bundler" do with_env("JEKYLL_NO_BUNDLER_REQUIRE", "true") do - assert_equal false, Jekyll::PluginManager.require_from_bundler, + refute Jekyll::PluginManager.require_from_bundler, "Gemfile plugins were required but shouldn't have been" assert ENV["JEKYLL_NO_BUNDLER_REQUIRE"] end @@ -32,7 +32,7 @@ class TestPluginManager < JekyllUnitTest should "not require from bundler" do with_env("JEKYLL_NO_BUNDLER_REQUIRE", nil) do with_no_gemfile do - assert_equal false, Jekyll::PluginManager.require_from_bundler, + refute Jekyll::PluginManager.require_from_bundler, "Gemfile plugins were required but shouldn't have been" assert_nil ENV["JEKYLL_NO_BUNDLER_REQUIRE"] end From 70ba8c578a929e4bf1a5bf7648caf1b90fba3ebb Mon Sep 17 00:00:00 2001 From: Florian Thomas Date: Sun, 7 Aug 2016 20:54:55 +0200 Subject: [PATCH 4/4] update require_gems test --- test/test_plugin_manager.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/test/test_plugin_manager.rb b/test/test_plugin_manager.rb index c43c1272..c625c3a8 100644 --- a/test/test_plugin_manager.rb +++ b/test/test_plugin_manager.rb @@ -41,13 +41,18 @@ class TestPluginManager < JekyllUnitTest end context "require gems" do - should "check if configured gems are allowed" do - allow(Jekyll::External).to receive(:require_with_graceful_fail).and_return(nil) - site = double(:gems => %w(jemoji foobar)) + should "invoke `require_with_graceful_fail`" do + gems = %w(jemojii foobar) + + expect(Jekyll::External).to( + receive(:require_with_graceful_fail).with(gems).and_return(nil) + ) + site = double(:gems => gems) plugin_manager = PluginManager.new(site) - expect(plugin_manager).to receive(:plugin_allowed?).with("jemoji") - expect(plugin_manager).to receive(:plugin_allowed?).with("foobar") + allow(plugin_manager).to receive(:plugin_allowed?).with("foobar").and_return(true) + allow(plugin_manager).to receive(:plugin_allowed?).with("jemojii").and_return(true) + plugin_manager.require_gems end end