Add tests for logging custom errors

This commit is contained in:
Pixelastic 2017-11-13 17:14:51 +01:00
parent 07e547b5d8
commit 680e09f135
12 changed files with 184 additions and 15 deletions

View File

@ -9,18 +9,36 @@ rescue Bundler::BundlerError => e
end
require 'rake'
# LINT
require 'rubocop/rake_task'
RuboCop::RakeTask.new(:lint) do |task|
task.patterns = [
'lib/**/*.rb',
# Excluding ./spec/site
'spec/*.rb',
'spec/jekyll/**/*.rb'
]
task.options = ['--display-cop-names']
end
# TEST
require 'rspec/core'
require 'rspec/core/rake_task'
desc 'Run tests (with simple progress)'
RSpec::Core::RakeTask.new(:test) do |spec|
spec.rspec_opts = '--color --format progress'
spec.pattern = FileList['spec/**/**_spec.rb']
RSpec::Core::RakeTask.new(:test) do |task|
task.rspec_opts = '--color --format progress'
task.pattern = [
'spec/*.rb',
'spec/jekyll/**/*.rb'
]
end
desc 'Run tests (with full details)'
RSpec::Core::RakeTask.new(:test_details) do |spec|
spec.rspec_opts = '--color --format documentation'
spec.pattern = FileList['spec/**/**_spec.rb']
RSpec::Core::RakeTask.new(:test_details) do |task|
task.rspec_opts = '--color --format documentation'
task.pattern = [
'spec/*.rb',
'spec/jekyll/**/*.rb'
]
end
desc 'Run tests in all Ruby versions (with full details)'
task :test_all_ruby_versions do

View File

@ -0,0 +1,13 @@
E: [✗ Error] No application ID defined
E:
E: The jekyll-algolia plugin could not find your Algolia application ID.
W:
W: Please, define it in your Jekyll _config.yml file like this:
W:
W: algolia:
W: application_id: {your_application_id}
I:
I: You can find your application ID along with all your credentials in your
I: Algolia dashboard here:
I: https://www.algolia.com/licensing

View File

@ -0,0 +1,12 @@
E: [✗ Error] No application ID defined
E:
E: The jekyll-algolia plugin could not find your Algolia application ID.
W:
W: Please, define it in your Jekyll _config.yml file like this:
W:
W: algolia:
W: application_id: {your_application_id}
I:
I: You can find your application ID along with all your credentials in your
I: Algolia dashboard here:
I: https://www.algolia.com/licensing

View File

@ -0,0 +1,19 @@
E: [✗ Error] No index name defined
E:
E: The jekyll-algolia plugin could not find the name of the Algolia index you
E: want to push your records to.
W:
W: Please, define it in your Jekyll _config.yml file like this:
W:
W: algolia:
W: index_name: {your_index_name}
W:
W: Alternatively, you can also define it as an ENV variable, like this:
W:
W: $ ALGOLIA_INDEX_NAME='{your_index_name}' jekyll algolia
W:
I: You can see all your indices from your Algolia dashboard here:
I: https://www.algolia.com/explorer
I:
I: Note that you don't have to create an index before pushing records.
I: It will be created automatically if it does not exist yet.

View File

@ -36,6 +36,7 @@ Gem::Specification.new do |gem|
# Files
gem.files = Dir[
'lib/**/*.rb',
'errors/*.txt',
'README.md',
'CONTRIBUTING.md',
'LICENSE.txt',

View File

@ -28,11 +28,11 @@ module Jekyll
# pages. We don't want to index those.
# Source: https://help.github.com/articles/creating-a-custom-404-page-for-your-github-pages-site/
#
# rubocop:disable Style/PredicateName
# rubocop:disable Naming/PredicateName
def self.is_404?(file)
File.basename(file.path, File.extname(file.path)) == '404'
end
# rubocop:enable Style/PredicateName
# rubocop:enable Naming/PredicateName
# Public: Check if the page is a pagination page
#

View File

@ -2,7 +2,45 @@ module Jekyll
module Algolia
# Display helpful error messages
module Logger
def self.known_message(message_id); end
# Public: Displays a log line
#
# line - Line to display. Expected to be of the following format:
# "X:Your content"
# Where X is either I, W or E for marking respectively an info, warning or
# error display
def self.log(line)
type, content = /^(I|W|E):(.*)/.match(line).captures
logger_mapping = {
'E' => :error,
'I' => :info,
'W' => :warn
}
# Jekyll logger tries to center log lines, so we force a consistent
# width of 80 chars
content = content.ljust(80, ' ')
Jekyll.logger.send(logger_mapping[type], content)
end
# Public: Displays a helpful error message for one of the knows errors
#
# message_id: A string identifying a know message
#
# It will read files in ./errors/*.txt with the matching error and
# display them using Jekyll internal logger.
def self.known_message(message_id)
file = File.expand_path(
File.join(
File.dirname(__FILE__), '../../..', 'errors', "#{message_id}.txt"
)
)
# Display each line differently
lines = File.open(file).readlines.map(&:chomp)
lines.each do |line|
log(line)
end
end
end
end
end

View File

@ -6,11 +6,11 @@ if ! git status --short | grep -q '\.rb$'; then
fi
# Do not commit any focused or excluded tests
if grep --color -r 'spec' -E -e '^( |\t)*(fit|fdescribe|xit|xdescribe)'; then
if grep --color -r 'spec' -E -e '^( |\t)*(fit|fdescribe|fcontext|xit|xdescribe|xcontext)'; then
echo '✘ You have focused and/or skipped tests'
exit 1
fi
# Match style guide
./scripts/lint || exit 1
rake lint || exit 1

View File

@ -1,2 +0,0 @@
#!/usr/bin/env bash
rubocop -F './lib/' './spec'

View File

@ -42,7 +42,7 @@ describe(Jekyll::Algolia::Extractor) do
.to have_received(:new)
.with(content, anything)
end
it 'should configure the extractor with the nodex_to_index config value' do
it 'should configure the extractor with the nodex_to_index value' do
expect(AlgoliaHTMLExtractor)
.to have_received(:new)
.with(anything, options: { css_selector: 'foo' })

View File

@ -242,7 +242,10 @@ describe(Jekyll::Algolia::FileBrowser) do
end
describe '.excerpt_txt' do
let(:expected) { 'This is the first paragraph. It is especially long because we want it to wrap on two lines.' }
let(:expected) do
'This is the first paragraph. It is especially long because we want '\
'it to wrap on two lines.'
end
subject { current.excerpt_text(file) }
context 'with a page' do

View File

@ -0,0 +1,67 @@
# rubocop:disable Metrics/BlockLength
require 'spec_helper'
describe(Jekyll::Algolia::Logger) do
let(:current) { Jekyll::Algolia::Logger }
describe '.known_message' do
let(:io) { double('IO', readlines: lines) }
let(:lines) do
[
'I: Info line',
'W: Warning line',
'E: Error line'
]
end
before do
expect(File)
.to receive(:open)
.with(/custom_message\.txt$/)
.and_return(io)
expect(current).to receive(:log).with('I: Info line')
expect(current).to receive(:log).with('W: Warning line')
expect(current).to receive(:log).with('E: Error line')
end
it { current.known_message('custom_message') }
end
describe '.log' do
context 'with an error line' do
let(:input) { 'E: Error line' }
before do
expect(Jekyll.logger)
.to receive(:error)
.with(/Error line/)
end
it { current.log(input) }
end
context 'with a warning line' do
let(:input) { 'W: Warning line' }
before do
expect(Jekyll.logger)
.to receive(:warn)
.with(/Warning line/)
end
it { current.log(input) }
end
context 'with an information line' do
let(:input) { 'I: Information line' }
before do
expect(Jekyll.logger)
.to receive(:info)
.with(/Information line/)
end
it { current.log(input) }
end
context 'with regular type' do
let(:input) { 'I: Information line' }
before do
expect(Jekyll.logger)
.to receive(:info)
.with(/^.{80,80}$/)
end
it { current.log(input) }
end
end
end