diff --git a/README.md b/README.md index 5bc98e4c..8308d10c 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/config/default.yml b/config/default.yml index dcbd34a4..3c32609e 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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' diff --git a/lib/overcommit/hook/pre_commit/code_spell_check.rb b/lib/overcommit/hook/pre_commit/code_spell_check.rb new file mode 100644 index 00000000..a1132797 --- /dev/null +++ b/lib/overcommit/hook/pre_commit/code_spell_check.rb @@ -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 diff --git a/spec/overcommit/hook/pre_commit/code_spell_check_spec.rb b/spec/overcommit/hook/pre_commit/code_spell_check_spec.rb new file mode 100644 index 00000000..85a54b38 --- /dev/null +++ b/spec/overcommit/hook/pre_commit/code_spell_check_spec.rb @@ -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