Skip to content

Commit

Permalink
Merge pull request #29 from jekyll/test-metadata
Browse files Browse the repository at this point in the history
Add an integration test & test with github-pages gem
  • Loading branch information
parkr committed Feb 5, 2016
2 parents 4eca1c9 + 423d7a7 commit 67c0018
Show file tree
Hide file tree
Showing 19 changed files with 216 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.bundle
Gemfile.lock
pkg
_site
tmp
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
sudo: false
language: ruby
rvm:
- 2.0
- 2.1
- 2.1.7
- 2.2
- 2.3
matrix:
include:
- rvm: 1.9.3
- rvm: 2.3
env: JEKYLL_VERSION=2.5
- rvm: 2.1.7
env: GITHUB_PAGES=45
env:
matrix:
- JEKYLL_VERSION=2.5
- JEKYLL_VERSION=3.0
branches:
only:
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
source 'https://rubygems.org'
gemspec

group :test do
gem "webmock"
end
1 change: 1 addition & 0 deletions jekyll-github-metadata.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "netrc"
spec.add_development_dependency "rspec"
spec.add_development_dependency "jekyll", ENV["JEKYLL_VERSION"] ? "~> #{ENV["JEKYLL_VERSION"]}" : ">= 2.0"
spec.add_development_dependency "github-pages", ENV["GITHUB_PAGES"] if ENV["GITHUB_PAGES"]
end
88 changes: 50 additions & 38 deletions lib/jekyll-github-metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def clear_values!
@values = Hash.new
end

def reset!
clear_values!
@client = nil
end

def [](key)
values[key.to_s]
end
Expand All @@ -43,46 +48,53 @@ def to_liquid
def register_value(key, value)
values[key.to_s] = Value.new(key.to_s, value)
end

# Reset our values hash.
def init!
reset!

# Environment-Specific
register_value('environment', proc { environment })
register_value('hostname', proc { Pages.github_hostname })
register_value('pages_hostname', proc { Pages.pages_hostname })
register_value('api_url', proc { Pages.api_url })

register_value('versions', proc {
begin
require 'github-pages'
GitHubPages.versions
rescue LoadError; Hash.new end
})

# The Juicy Stuff
register_value('public_repositories', proc { |c,r| c.list_repos(r.owner, "type" => "public") })
register_value('organization_members', proc { |c,r| c.organization_public_members(r.owner) if r.organization_repository? })
register_value('build_revision', proc { `git rev-parse HEAD`.strip })
register_value('project_title', proc { |_,r| r.name })
register_value('project_tagline', proc { |_,r| r.tagline })
register_value('owner_name', proc { |_,r| r.owner })
register_value('owner_url', proc { |_,r| r.owner_url })
register_value('owner_gravatar_url', proc { |_,r| r.owner_gravatar_url })
register_value('repository_url', proc { |_,r| r.repository_url })
register_value('repository_nwo', proc { |_,r| r.nwo })
register_value('repository_name', proc { |_,r| r.name})
register_value('zip_url', proc { |_,r| r.zip_url })
register_value('tar_url', proc { |_,r| r.tar_url })
register_value('clone_url', proc { |_,r| r.repo_clone_url })
register_value('releases_url', proc { |_,r| r.releases_url })
register_value('issues_url', proc { |_,r| r.issues_url })
register_value('wiki_url', proc { |_,r| r.wiki_url })
register_value('language', proc { |_,r| r.language })
register_value('is_user_page', proc { |_,r| r.user_page? })
register_value('is_project_page', proc { |_,r| r.project_page? })
register_value('show_downloads', proc { |_,r| r.show_downloads? })
register_value('url', proc { |_,r| r.pages_url })
register_value('contributors', proc { |c,r| c.contributors(r.nwo) })
register_value('releases', proc { |c,r| c.releases(r.nwo) })
end
end

# Environment-Specific
register_value('environment', proc { environment })
register_value('hostname', proc { Pages.github_hostname })
register_value('pages_hostname', proc { Pages.pages_hostname })
register_value('api_url', proc { Pages.api_url })

register_value('versions', proc {
begin
require 'github-pages'
GitHubPages.versions
rescue LoadError; Hash.new end
})

# The Juicy Stuff
register_value('public_repositories', proc { |c,r| c.list_repos(r.owner, "type" => "public") })
register_value('organization_members', proc { |c,r| c.organization_public_members(r.owner) if r.organization_repository? })
register_value('build_revision', proc { `git rev-parse HEAD`.strip })
register_value('project_title', proc { |_,r| r.name })
register_value('project_tagline', proc { |_,r| r.tagline })
register_value('owner_name', proc { |_,r| r.owner })
register_value('owner_url', proc { |_,r| r.owner_url })
register_value('owner_gravatar_url', proc { |_,r| r.owner_gravatar_url })
register_value('repository_url', proc { |_,r| r.repository_url })
register_value('repository_nwo', proc { |_,r| r.nwo })
register_value('repository_name', proc { |_,r| r.name})
register_value('zip_url', proc { |_,r| r.zip_url })
register_value('tar_url', proc { |_,r| r.tar_url })
register_value('clone_url', proc { |_,r| r.repo_clone_url })
register_value('releases_url', proc { |_,r| r.releases_url })
register_value('issues_url', proc { |_,r| r.issues_url })
register_value('wiki_url', proc { |_,r| r.wiki_url })
register_value('language', proc { |_,r| r.language })
register_value('is_user_page', proc { |_,r| r.user_page? })
register_value('is_project_page', proc { |_,r| r.project_page? })
register_value('show_downloads', proc { |_,r| r.show_downloads? })
register_value('url', proc { |_,r| r.pages_url })
register_value('contributors', proc { |c,r| c.contributors(r.nwo) })
register_value('releases', proc { |c,r| c.releases(r.nwo) })
init!
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll-github-metadata/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def build_octokit_client(options = nil)
options = options || Hash.new
if ENV['JEKYLL_GITHUB_TOKEN'] || Octokit.access_token
options.merge!(:access_token => ENV['JEKYLL_GITHUB_TOKEN'] || Octokit.access_token)
elsif File.exist?(File.join(ENV['HOME'], '.netrc')) && safe_require('netrc')
elsif !ENV['NO_NETRC'] && File.exist?(File.join(ENV['HOME'], '.netrc')) && safe_require('netrc')
options.merge!(:netrc => true)
else
Jekyll.logger.warn "GitHubMetadata:", "No GitHub API authentication could be found." +
Expand Down
2 changes: 2 additions & 0 deletions lib/jekyll-github-metadata/ghp_metadata_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Jekyll
module GitHubMetadata
class GHPMetadataGenerator < Jekyll::Generator
def generate(site)
Jekyll.logger.debug "Generator:", "Calling GHPMetadataGenerator"
GitHubMetadata.repository = GitHubMetadata::Repository.new(site.config.fetch('repository'))
GitHubMetadata.init!
site.config['github'] =
case site.config['github']
when nil
Expand Down
2 changes: 1 addition & 1 deletion script/bootstrap
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
bundle install
bundle install --jobs=8 $@
22 changes: 22 additions & 0 deletions script/webmock-repopulate
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -e

curl_me() {
(set -x; curl \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-H "Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3" \
-H "Content-Type: application/json" \
-H "User-Agent: Octokit Ruby Gem 4.2.0" \
https://api.github.com${1} | gunzip > \
spec/webmock/api_get_${2}.json)
}

curl_me "/repos/jekyll/github-metadata?per_page=100" "repo"
curl_me "/repos/jekyll/github-metadata/contributors?per_page=100" "repo_contributors"
curl_me "/repos/jekyll/github-metadata/releases?per_page=100" "repo_releases"
curl_me "/orgs/jekyll" "org"
curl_me "/orgs/jekyll/public_members" "org_members"
curl_me "/users/jekyll/repos?per_page=100&type=public" "owner_repos"
curl_me "/repos/jekyll/github-metadata/pages" "repo_pages"
85 changes: 85 additions & 0 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
require "spec_helper"
require "jekyll"

RSpec.describe("integration into a jekyll site") do
SOURCE_DIR = Pathname.new(File.expand_path("../test-site", __FILE__))
DEST_DIR = Pathname.new(File.expand_path("../../tmp/test-site-build", __FILE__))

def dest_dir(*files)
DEST_DIR.join(*files)
end

before(:all) do
# Reset some stuffs
ENV['NO_NETRC'] = "true"
ENV['JEYKLL_GITHUB_TOKEN'] = "1234abc"

# Stub Requests
stub_api "/users/jekyll/repos?per_page=100&type=public", "owner_repos"
stub_api "/repos/jekyll/github-metadata", "repo"
stub_api "/repos/jekyll/github-metadata/releases?per_page=100", "repo_releases"
stub_api "/orgs/jekyll", "org"
stub_api "/orgs/jekyll/public_members?per_page=100", "org_members"
stub_api "/repos/jekyll/github-metadata/pages", "repo_pages"
stub_api "/repos/jekyll/github-metadata/contributors?per_page=100", "repo_contributors"

# Run Jekyll
Jekyll.logger.log_level = :error
Jekyll::Commands::Build.process({
"source" => SOURCE_DIR.to_s,
"destination" => DEST_DIR.to_s,
"gems" => %w{jekyll-github-metadata},
"repository" => "jekyll/github-metadata"
})
end
subject { SafeYAML::load(dest_dir("rendered.txt").read) }

{
"environment" => "development",
"hostname" => "https://github.com",
"pages_hostname" => "github.io",
"api_url" => "https://api.github.com",
"versions" => proc {
begin
require 'github-pages'
GitHubPages.versions
rescue LoadError
{}
end
}.call,
"public_repositories" => Regexp.new('"id"=>17261694, "name"=>"atom-jekyll"'),
"organization_members" => Regexp.new('"login"=>"parkr", "id"=>237985'),
"build_revision" => /[a-f0-9]{40}/,
"project_title" => "github-metadata",
"project_tagline" => ":octocat: `site.github`",
"owner_name" => "jekyll",
"owner_url" => "https://github.com/jekyll",
"owner_gravatar_url" => "https://github.com/jekyll.png",
"repository_url" => "https://github.com/jekyll/github-metadata",
"repository_nwo" => "jekyll/github-metadata",
"repository_name" => "github-metadata",
"zip_url" => "https://github.com/jekyll/github-metadata/zipball/gh-pages",
"tar_url" => "https://github.com/jekyll/github-metadata/tarball/gh-pages",
"clone_url" => "https://github.com/jekyll/github-metadata.git",
"releases_url" => "https://github.com/jekyll/github-metadata/releases",
"issues_url" => "https://github.com/jekyll/github-metadata/issues",
"wiki_url" => nil, # disabled
"language" => "Ruby",
"is_user_page" => false,
"is_project_page" => true,
"show_downloads" => true,
"url" => "http://jekyll.github.io/github-metadata/",
"contributors" => /"login"=>"parkr", "id"=>237985/,
"releases" => /"tag_name"=>"v1.1.0"/,
}.each do |key, value|
it "contains the correct #{key}" do
expect(subject).to have_key(key)
if value.is_a? Regexp
expect(subject[key].to_s).to match value
else
expect(subject[key]).to eql value
end
end
end

end
34 changes: 33 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
require 'jekyll-github-metadata'
require 'webmock/rspec'
require 'pathname'

SPEC_DIR = Pathname.new(File.expand_path("../", __FILE__))

module WebMockHelper
def stub_api(path, filename)
WebMock.disable_net_connect!
stub_request(:get, "https://api.github.com#{path}").
with(:headers => {
'Accept' => 'application/vnd.github.v3+json',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'User-Agent' => "Octokit Ruby Gem #{Octokit::VERSION}"
}).
to_return(
:status => 200,
:headers => {
'Transfer-Encoding' => 'chunked',
'Content-Type' => 'application/json; charset=utf-8',
'Vary' => 'Accept-Encoding',
'Content-Encoding' => 'gzip',
'X-GitHub-Media-Type' => 'github.v3; format=json'
},
:body => SPEC_DIR.
join("webmock/api_get_#{filename}.json").read
)
end
end

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
Expand All @@ -25,7 +54,7 @@

# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true
config.warnings = false

# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
Expand Down Expand Up @@ -53,4 +82,7 @@
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed

config.include WebMockHelper
WebMock.disable_net_connect!
end
3 changes: 3 additions & 0 deletions spec/test-site/rendered.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
---
{{ site.github | jsonify }}
1 change: 1 addition & 0 deletions spec/webmock/api_get_org.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"login":"jekyll","id":3083652,"url":"https://api.github.com/orgs/jekyll","repos_url":"https://api.github.com/orgs/jekyll/repos","events_url":"https://api.github.com/orgs/jekyll/events","hooks_url":"https://api.github.com/orgs/jekyll/hooks","issues_url":"https://api.github.com/orgs/jekyll/issues","members_url":"https://api.github.com/orgs/jekyll/members{/member}","public_members_url":"https://api.github.com/orgs/jekyll/public_members{/member}","avatar_url":"https://avatars.githubusercontent.com/u/3083652?v=3","description":"Jekyll is a blog-aware, static site generator in Ruby.","name":"Jekyll","company":null,"blog":"http://jekyllrb.com","location":"","email":"","public_repos":43,"public_gists":0,"followers":0,"following":0,"html_url":"https://github.com/jekyll","created_at":"2012-12-19T19:37:35Z","updated_at":"2016-01-26T20:22:17Z","type":"Organization","total_private_repos":3,"owned_private_repos":3,"private_gists":0,"disk_usage":110870,"collaborators":0,"billing_email":"[email protected]","plan":{"name":"bronze","space":976562499,"private_repos":10}}
Loading

0 comments on commit 67c0018

Please sign in to comment.