From 859f86e9fc6332e1f5375326805588514b419453 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Thu, 7 Sep 2017 10:18:58 -0400 Subject: [PATCH] Allow user values to override drop-determined values (#110) Merge pull request 110 --- lib/jekyll-github-metadata.rb | 2 +- lib/jekyll-github-metadata/metadata_drop.rb | 10 +++++ .../site_github_munger.rb | 4 +- spec/edit_link_tag_spec.rb | 14 ++++--- spec/site_github_munger_spec.rb | 37 ++++++++++++++++++- spec/spec_helper.rb | 2 +- 6 files changed, 59 insertions(+), 10 deletions(-) diff --git a/lib/jekyll-github-metadata.rb b/lib/jekyll-github-metadata.rb index 76804a6..22d9fad 100644 --- a/lib/jekyll-github-metadata.rb +++ b/lib/jekyll-github-metadata.rb @@ -70,7 +70,7 @@ def client def repository @repository ||= GitHubMetadata::Repository.new(repository_finder.nwo).tap do |repo| - Jekyll::GitHubMetadata.log :debug, "Generating for #{repo.nwo}" + log :debug, "Generating for #{repo.nwo}" end end diff --git a/lib/jekyll-github-metadata/metadata_drop.rb b/lib/jekyll-github-metadata/metadata_drop.rb index 76f6535..c228b1e 100644 --- a/lib/jekyll-github-metadata/metadata_drop.rb +++ b/lib/jekyll-github-metadata/metadata_drop.rb @@ -8,6 +8,16 @@ class MetadataDrop < Jekyll::Drops::Drop mutable true + # See https://github.com/jekyll/jekyll/pull/6338 + alias_method :invoke_drop, :[] + def key?(key) + if self.class.mutable? + @mutations.key?(key) + else + !key.nil? && (respond_to?(key) || fallback_data.key?(key)) + end + end + def to_s require "json" JSON.pretty_generate to_h diff --git a/lib/jekyll-github-metadata/site_github_munger.rb b/lib/jekyll-github-metadata/site_github_munger.rb index 69a6d4f..2545707 100644 --- a/lib/jekyll-github-metadata/site_github_munger.rb +++ b/lib/jekyll-github-metadata/site_github_munger.rb @@ -30,7 +30,7 @@ def github_namespace when nil drop when Hash - Jekyll::Utils.deep_merge_hashes(site.config["github"], drop) + Jekyll::Utils.deep_merge_hashes(drop, site.config["github"]) else site.config["github"] end @@ -66,5 +66,5 @@ def should_add_url_fallbacks? end Jekyll::Hooks.register :site, :after_init do |site| - Jekyll::GitHubMetadata::SiteGitHubMunger.new(site).munge! unless Jekyll.env == "test" + Jekyll::GitHubMetadata::SiteGitHubMunger.new(site).munge! end diff --git a/spec/edit_link_tag_spec.rb b/spec/edit_link_tag_spec.rb index d34e3e6..326c4c0 100644 --- a/spec/edit_link_tag_spec.rb +++ b/spec/edit_link_tag_spec.rb @@ -4,7 +4,7 @@ let(:path) { "/" } let(:source) { { "branch" => branch, "path" => path } } let(:github) { { "repository_url" => repository_url, "source" => source } } - let(:config) { { "github" => github } } + let(:config) { { "github" => github, "plugins" => ["jekyll-github-metadata"] } } let(:page) { make_page } let(:site) { make_site(config) } let(:render_context) { make_context(:page => page, :site => site) } @@ -30,10 +30,6 @@ tag end - before do - Jekyll.logger.log_level = :error - end - it "knows the page" do expect(subject.send(:page)).to be_a(Jekyll::Page) end @@ -66,6 +62,14 @@ expect(uri).to eql("#{repository_url}/edit/gh-pages/page.md") end end + + context "an arbitrary branch" do + let(:branch) { "foo" } + + it "builds the URL" do + expect(uri).to eql("#{repository_url}/edit/foo/page.md") + end + end end context "with no site.github" do diff --git a/spec/site_github_munger_spec.rb b/spec/site_github_munger_spec.rb index bae0cd2..0de9287 100644 --- a/spec/site_github_munger_spec.rb +++ b/spec/site_github_munger_spec.rb @@ -5,7 +5,8 @@ RSpec.describe(Jekyll::GitHubMetadata::SiteGitHubMunger) do let(:source) { File.expand_path("test-site", __dir__) } let(:dest) { File.expand_path("../tmp/test-site-build", __dir__) } - let(:user_config) { {} } + let(:github_namespace) { nil } + let(:user_config) { { "github" => github_namespace } } let(:site) { Jekyll::Site.new(Jekyll::Configuration.from(user_config)) } subject { described_class.new(site) } let!(:stubs) { stub_all_api_requests } @@ -16,6 +17,40 @@ subject.munge! end + context "with site.github as nil" do + it "replaces site.github with the drop" do + expect(site.config["github"]).to be_a(Liquid::Drop) + end + end + + context "without site.github" do + let(:user_config) { {} } + + it "replaces site.github with the drop" do + expect(site.config["github"]).to be_a(Liquid::Drop) + end + end + + context "with site.github as a non-hash" do + let(:github_namespace) { "foo" } + + it "doesn't munge" do + expect(site.config["github"]).to eql("foo") + end + end + + context "with site.github as a hash" do + let(:github_namespace) { { "source" => { "branch" => "foo" } } } + + it "lets user-specified values override the drop" do + expect(site.config["github"].invoke_drop("source")["branch"]).to eql("foo") + end + + it "still sets other values" do + expect(site.config["github"].invoke_drop("source")["path"]).to eql("/") + end + end + context "with site.url set" do let(:user_config) { { "url" => "http://example.com" } } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0b5deea..81ae1da 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,7 @@ +require "jekyll" require "jekyll-github-metadata" require "webmock/rspec" require "pathname" -require "jekyll" require_relative "spec_helpers/env_helper" require_relative "spec_helpers/integration_helper"