Skip to content
This repository has been archived by the owner on Mar 21, 2018. It is now read-only.

Commit

Permalink
refactor(coverage): Increase to 100% code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelastic committed Jul 17, 2015
1 parent f923830 commit 61057c7
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 21 deletions.
4 changes: 3 additions & 1 deletion lib/algoliasearch-jekyll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def init_with_program(prog)

subcommand.action do |args, options|
@config = configuration_from_options(options)
AlgoliaSearchJekyllPush.init_options(args, options, @config).process
AlgoliaSearchJekyllPush.init_options(args, options, @config)
.jekyll_new(@config)
.process
end
end
end
Expand Down
14 changes: 6 additions & 8 deletions lib/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ def indexable?(file)
true
end

# Run the default `jekyll build` command but overwrite the actual "write
# files on disk" part to instead push data to Algolia
def process
site = Jekyll::Site.new(@config)
# Return a patched version of a Jekyll instance
def jekyll_new(config)
site = Jekyll::Site.new(config)

# Patched version of `write` that will push to Algolia instead of writing
# on disk
def site.write
items = []
each_site_file do |file|
Expand All @@ -65,9 +66,7 @@ def site.write
AlgoliaSearchJekyllPush.push(items)
end

# This will call the build command by default, which will in turn call our
# custom .write method
site.process
site
end

# Read the API key either from ENV or from an _algolia_api_key file in
Expand Down Expand Up @@ -183,7 +182,6 @@ def push(items)
Jekyll.logger.error 'Algolia Error: HTTP Error'
Jekyll.logger.warn error.message
exit 1

end
end

Expand Down
132 changes: 132 additions & 0 deletions spec/push_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
let(:static_file) { site.file_by_name('ring.png') }
let(:document_file) { site.file_by_name('collection-item.md') }
let(:html_document_file) { site.file_by_name('collection-item.html') }
let(:items) do
[{
name: 'foo',
url: '/foo'
}, {
name: 'bar',
url: '/bar'
}]
end
let(:options) do
{
'drafts' => true
Expand Down Expand Up @@ -217,4 +226,127 @@
push.configure_index(index)
end
end

describe 'jekyll_new' do
it 'should return a patched version of site with a custom write' do
# Given
normal_site = Jekyll::Site.new(Jekyll.configuration(config))
normal_method = normal_site.method(:write).source_location

patched_site = get_site({}, mock_write_method: false, process: false)
patched_method = patched_site.method(:write).source_location

# When
# Then
expect(patched_method).not_to eq normal_method
end
end

describe 'process' do
it 'should call the site write method' do
# Given
site = get_site({}, process: false)

# When
site.process

# Then
expect(site).to have_received(:write)
end

it 'should push items to Algolia' do
# Given
site = get_site({}, mock_write_method: false, process: false)
# Keep only page_file
allow(AlgoliaSearchJekyllPush).to receive(:indexable?) do |file|
file.path == page_file.path
end
allow(AlgoliaSearchJekyllPush).to receive(:push)

# When
site.process

# Then
expect(AlgoliaSearchJekyllPush).to have_received(:push) do |arg|
expect(arg.size).to eq 6
end
end
end

describe 'push' do
let(:index_double) { double('Algolia Index').as_null_object }

before(:each) do
push.init_options(nil, options, config)
# Mock all calls to not send anything
allow(push).to receive(:check_credentials)
allow(Algolia).to receive(:init)
allow(Algolia).to receive(:move_index)
allow(Algolia::Index).to receive(:new).and_return(index_double)
allow(Jekyll.logger).to receive(:info)
end

it 'should init the Algolia client' do
# Given
stub_const('ENV', 'ALGOLIA_API_KEY' => 'APIKEY_FROM_ENV')

# When
push.push(items)

# Then
expect(Algolia).to have_received(:init).with(
application_id: 'APPID',
api_key: 'APIKEY_FROM_ENV'
)
end

it 'should create a temporary index' do
# Given

# When
push.push(items)

# Then
expect(Algolia::Index).to have_received(:new).with('INDEXNAME_tmp')
end

it 'should add elements to the temporary index' do
# Given

# When
push.push(items)

# Then
expect(index_double).to have_received(:add_objects!)
end

it 'should move the temporary index as the main one' do
# Given

# When
push.push(items)

# Then
expect(Algolia).to have_received(:move_index)
.with('INDEXNAME_tmp', 'INDEXNAME')
end

it 'should display the number of elements indexed' do
# Given

# When
push.push(items)

# Then
expect(Jekyll.logger).to have_received(:info).with(/of 2 items/i)
end

it 'should display an error if `add_objects!` failed' do
# Given
allow(index_double).to receive(:add_objects!).and_raise

expect(Jekyll.logger).to receive(:error)
expect(-> { push.push(items) }).to raise_error SystemExit
end
end
end
25 changes: 14 additions & 11 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,30 @@
config.filter_run(focus: true)
config.run_all_when_everything_filtered = true

# Build a jekyll site, creating access to @__files used internally
def get_site(config = {})
# Create a Jekyll::Site instance, patched with a `file_by_name` method
def get_site(config = {}, options = {})
default_options = {
mock_write_method: true,
process: true
}
options = default_options.merge(options)

config = config.merge(
source: File.expand_path('./spec/fixtures')
)
config = Jekyll.configuration(config)
site = Jekyll::Site.new(config)

# Keep a list of all files
def site.write
@__files = {}
site = AlgoliaSearchJekyllPush.jekyll_new(config)

def site.file_by_name(file_name)
each_site_file do |file|
@__files[file.path] = file
return file if file.path =~ /#{file_name}$/
end
end

def site.file_by_name(file_name)
@__files.find { |path, _| path =~ /#{file_name}$/ }[1]
end
allow(site).to receive(:write) if options[:mock_write_method]

site.process
site.process if options[:process]
site
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper_simplecov.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'simplecov'

SimpleCov.configure do
load_adapter 'test_frameworks'
load_profile 'test_frameworks'
end

ENV['COVERAGE'] && SimpleCov.start do
Expand Down

0 comments on commit 61057c7

Please sign in to comment.