Skip to content

Commit

Permalink
Merge pull request #78 from jekyll/to_s_for_drop
Browse files Browse the repository at this point in the history
Add MetadataDrop#to_s which outputs pretty JSON representation
  • Loading branch information
parkr authored Oct 15, 2016
2 parents 3decb76 + 1c1c13a commit de9fa36
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/jekyll-github-metadata/metadata_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ def initialize(site)
super(nil)
end

def to_s
require "json"
JSON.pretty_generate to_h
end
alias_method :to_str, :to_s

def content_methods
super - %w(to_s to_str)
end

def keys
super.sort
end
Expand Down
26 changes: 25 additions & 1 deletion spec/metadata_drop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@

RSpec.describe(Jekyll::GitHubMetadata::MetadataDrop) do
let(:overrides) { { "repository" => "jekyll/another-repo" } }
let(:config) { Jekyll::Configuration::DEFAULTS.merge(overrides) }
let(:config) { Jekyll::Configuration.from(overrides) }
let(:site) { Jekyll::Site.new config }
subject { described_class.new(site) }

context "in Liquid" do
before(:each) do
stub_all_api_requests
site.config.delete("repository")
site.config["github"] = subject
end

it "renders as a pretty JSON object in Liquid" do
require "json"
payload = site.site_payload
expect(payload["site"]["github"]).to be_instance_of(described_class)
expect(
Liquid::Template.parse("{{ site.github }}").render!(
payload, :registers => { :site => site }
)
).to eql(JSON.pretty_generate(subject.to_h))
end
end

context "with no repository set" do
before(:each) do
site.config.delete("repository")
Expand All @@ -31,6 +50,11 @@
:ssh => "git@github.com:foo/bar.git"
}.each do |type, url|
context "with a #{type} git URL" do
before(:each) do
site.config.delete("repository")
ENV["PAGES_REPO_NWO"] = nil
end

it "parses the name with owner from the git URL" do
allow(subject).to receive(:git_remote_url).and_return(url)
expect(subject.send(:nwo, site)).to eql("foo/bar")
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ def env_args_to_hash(*args)
WebMock.disable_net_connect!
config.include EnvHelper

config.before(:all) do
FileUtils.mkdir_p("tmp")
end

config.before(:each) do
Jekyll::GitHubMetadata.reset!
Jekyll::GitHubMetadata.logger = Logger.new(StringIO.new) unless ENV["DEBUG"]
Expand Down

0 comments on commit de9fa36

Please sign in to comment.