Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ issue](https://github.com/sds/overcommit/issues/238) for more details.
* [BundleOutdated](lib/overcommit/hook/pre_commit/bundle_outdated.rb)
* [`*`CaseConflicts](lib/overcommit/hook/pre_commit/case_conflicts.rb)
* [ChamberSecurity](lib/overcommit/hook/pre_commit/chamber_security.rb)
* [CodeSpellCheck](lib/overcommit/hook/pre_commit/code_spell_check.rb)
* [CoffeeLint](lib/overcommit/hook/pre_commit/coffee_lint.rb)
* [Credo](lib/overcommit/hook/pre_commit/credo.rb)
* [CssLint](lib/overcommit/hook/pre_commit/css_lint.rb)
Expand Down
9 changes: 9 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ PreCommit:
install_command: 'gem install chamber'
include: *chamber_settings_files

CodeSpellCheck:
enabled: false
description: 'Check if all your code is spell-checked correctly'
command: 'alfonsox'
install_command: 'gem install alfonsox'
include:
- '**/*.rb'
- '**/*.erb'

CoffeeLint:
enabled: false
description: 'Analyze with coffeelint'
Expand Down
36 changes: 36 additions & 0 deletions lib/overcommit/hook/pre_commit/code_spell_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module Overcommit::Hook::PreCommit
# Runs `alfonsox` spell-checking tool against any modified code file.
#
# @see https://github.com/diegojromerolopez/alfonsox
class CodeSpellCheck < Base
def run
# Create default file config if it does not exist

# Run spell-check
result = execute(command, args: applicable_files)
return :pass if result.success?

spellchecking_errors = result.stderr.split("\n")
spellchecking_errors.pop

error_messages(spellchecking_errors)
end

private

# Create the error messages
def error_messages(spellchecking_errors)
messages = []
spellchecking_errors.each do |spellchecking_error_i|
error_location, word = spellchecking_error_i.split(' ')
error_file_path, line = error_location.split(':')
messages << Overcommit::Hook::Message.new(
:error, error_file_path, line, "#{error_location}: #{word}"
)
end
messages
end
end
end
39 changes: 39 additions & 0 deletions spec/overcommit/hook/pre_commit/code_spell_check_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require 'spec_helper'

describe Overcommit::Hook::PreCommit::CodeSpellCheck do
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
let(:context) { double('context') }
subject { described_class.new(config, context) }

before do
subject.stub(:applicable_files).and_return(%w[file1.rb file2.rb])
end

context 'when code-spell-check exists successfully' do
before do
result = double('result')
result.stub(:success?).and_return(true)
result.stub(:stdout).and_return('')
result.stub(:stderr).and_return('')
subject.stub(:execute).and_return(result)
end

it { should pass }
end

context 'when code-spell-check exists unsuccessfully via standard error' do
before do
result = double('result')
result.stub(:success?).and_return(false)
result.stub(:stdout).and_return('')
result.stub(:stderr).and_return(
"file1.rb:35: inkorrectspelling\n✗ Errors in code spellchecking"
)
subject.stub(:execute).and_return(result)
end

it { should fail_hook }
end
end