Skip to content

Commit

Permalink
Allow user values to override drop-determined values (#110)
Browse files Browse the repository at this point in the history
Merge pull request 110
  • Loading branch information
benbalter authored and jekyllbot committed Sep 7, 2017
1 parent 35bf4df commit 859f86e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/jekyll-github-metadata.rb
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions lib/jekyll-github-metadata/metadata_drop.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-github-metadata/site_github_munger.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
14 changes: 9 additions & 5 deletions spec/edit_link_tag_spec.rb
Expand Up @@ -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) }
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
37 changes: 36 additions & 1 deletion spec/site_github_munger_spec.rb
Expand Up @@ -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 }
Expand All @@ -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" } }

Expand Down
2 changes: 1 addition & 1 deletion 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"
Expand Down

0 comments on commit 859f86e

Please sign in to comment.