From 8b7f2c9bae935b1d2bf168d34efb843422514669 Mon Sep 17 00:00:00 2001 From: Diego J Date: Sun, 30 Jun 2019 01:28:16 +0200 Subject: [PATCH 1/7] [FEATURE] Code Spell Check (no tests present yet) --- README.md | 1 + config/default.yml | 8 ++++ .../hook/pre_commit/code_spell_check.rb | 36 ++++++++++++++++++ lib/overcommit/hook/pre_commit/spell_check.rb | 37 +++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 lib/overcommit/hook/pre_commit/code_spell_check.rb create mode 100644 lib/overcommit/hook/pre_commit/spell_check.rb 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..c44635a5 100644 --- a/config/default.yml +++ b/config/default.yml @@ -222,6 +222,14 @@ PreCommit: install_command: 'gem install chamber' include: *chamber_settings_files + CodeSpellCheck: + enabled: false + description: 'Check if all your code is spell-checked correctly' + 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..352cce55 --- /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('bundle exec alfonsox', args: applicable_files) + spellchecking_errors = result.split('\n') + + # Check the if there are spelling errors + return :pass if spellchecking_errors.length.zero? + + 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/lib/overcommit/hook/pre_commit/spell_check.rb b/lib/overcommit/hook/pre_commit/spell_check.rb new file mode 100644 index 00000000..5c76bb0f --- /dev/null +++ b/lib/overcommit/hook/pre_commit/spell_check.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module Overcommit::Hook::PreCommit + # + # Spell check of the code. + # + class SpellCheck < Base + def run + # Create default file config if it does not exist + + # Run rake spellcheck task + args = flags + applicable_files + result = execute(command, args: args) + spellchecking_errors = result.split('\n') + + # Check the if there are spelling errors + return :pass if spellchecking_errors.length.zero? + + 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 From 15c5a38dc81431620bede89a7a661af931f0e0fa Mon Sep 17 00:00:00 2001 From: Diego J Date: Sun, 30 Jun 2019 01:35:19 +0200 Subject: [PATCH 2/7] Allow customization of code-spellcheck command --- config/default.yml | 1 + lib/overcommit/hook/pre_commit/code_spell_check.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/config/default.yml b/config/default.yml index c44635a5..326bf22c 100644 --- a/config/default.yml +++ b/config/default.yml @@ -225,6 +225,7 @@ PreCommit: CodeSpellCheck: enabled: false description: 'Check if all your code is spell-checked correctly' + command: 'bundle exec alfonsox' install_command: 'gem install alfonsox' include: - '**/*.rb' diff --git a/lib/overcommit/hook/pre_commit/code_spell_check.rb b/lib/overcommit/hook/pre_commit/code_spell_check.rb index 352cce55..1dc34095 100644 --- a/lib/overcommit/hook/pre_commit/code_spell_check.rb +++ b/lib/overcommit/hook/pre_commit/code_spell_check.rb @@ -9,7 +9,7 @@ def run # Create default file config if it does not exist # Run spell-check - result = execute('bundle exec alfonsox', args: applicable_files) + result = execute(command, args: applicable_files) spellchecking_errors = result.split('\n') # Check the if there are spelling errors From 2cc6bd35d4d860987281fa7d818ac3ac9f6dfe82 Mon Sep 17 00:00:00 2001 From: Diego J Date: Sun, 30 Jun 2019 01:41:04 +0200 Subject: [PATCH 3/7] Remove bundle from the alfonsox CLI tool call --- config/default.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/default.yml b/config/default.yml index 326bf22c..3c32609e 100644 --- a/config/default.yml +++ b/config/default.yml @@ -225,7 +225,7 @@ PreCommit: CodeSpellCheck: enabled: false description: 'Check if all your code is spell-checked correctly' - command: 'bundle exec alfonsox' + command: 'alfonsox' install_command: 'gem install alfonsox' include: - '**/*.rb' From 2b948e44a955131b9eb182d470f8c96a178091db Mon Sep 17 00:00:00 2001 From: Diego J Date: Sun, 30 Jun 2019 02:01:54 +0200 Subject: [PATCH 4/7] Remove messages that are not from the spell-checking errors in CodeSpellCheck pre-commit hook --- lib/overcommit/hook/pre_commit/code_spell_check.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/overcommit/hook/pre_commit/code_spell_check.rb b/lib/overcommit/hook/pre_commit/code_spell_check.rb index 1dc34095..a1132797 100644 --- a/lib/overcommit/hook/pre_commit/code_spell_check.rb +++ b/lib/overcommit/hook/pre_commit/code_spell_check.rb @@ -10,10 +10,10 @@ def run # Run spell-check result = execute(command, args: applicable_files) - spellchecking_errors = result.split('\n') + return :pass if result.success? - # Check the if there are spelling errors - return :pass if spellchecking_errors.length.zero? + spellchecking_errors = result.stderr.split("\n") + spellchecking_errors.pop error_messages(spellchecking_errors) end From fd690c76120531ad1bfacbb9c5091ed3cfbed066 Mon Sep 17 00:00:00 2001 From: Diego J Date: Sun, 30 Jun 2019 02:18:24 +0200 Subject: [PATCH 5/7] Tests for code-spell-check pre-commit hook --- .../hook/pre_commit/code_spell_check_spec.rb | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 spec/overcommit/hook/pre_commit/code_spell_check_spec.rb 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..508e76ab --- /dev/null +++ b/spec/overcommit/hook/pre_commit/code_spell_check_spec.rb @@ -0,0 +1,37 @@ +# 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 From 24abdd9ab129de7212ccae244b1a714b74b7a33c Mon Sep 17 00:00:00 2001 From: Diego J Date: Tue, 2 Jul 2019 18:01:25 +0200 Subject: [PATCH 6/7] [FIX] Remove wrong pre-commit hook: spellcheck --- lib/overcommit/hook/pre_commit/spell_check.rb | 37 ------------------- 1 file changed, 37 deletions(-) delete mode 100644 lib/overcommit/hook/pre_commit/spell_check.rb diff --git a/lib/overcommit/hook/pre_commit/spell_check.rb b/lib/overcommit/hook/pre_commit/spell_check.rb deleted file mode 100644 index 5c76bb0f..00000000 --- a/lib/overcommit/hook/pre_commit/spell_check.rb +++ /dev/null @@ -1,37 +0,0 @@ -# frozen_string_literal: true - -module Overcommit::Hook::PreCommit - # - # Spell check of the code. - # - class SpellCheck < Base - def run - # Create default file config if it does not exist - - # Run rake spellcheck task - args = flags + applicable_files - result = execute(command, args: args) - spellchecking_errors = result.split('\n') - - # Check the if there are spelling errors - return :pass if spellchecking_errors.length.zero? - - 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 From 1199ae80c491a78076570181f2b44c05dd4aa1e3 Mon Sep 17 00:00:00 2001 From: Diego J Date: Tue, 2 Jul 2019 18:22:55 +0200 Subject: [PATCH 7/7] [FIX] Rubocop: Metrics/LineLength: Line is too long. --- spec/overcommit/hook/pre_commit/code_spell_check_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/overcommit/hook/pre_commit/code_spell_check_spec.rb b/spec/overcommit/hook/pre_commit/code_spell_check_spec.rb index 508e76ab..85a54b38 100644 --- a/spec/overcommit/hook/pre_commit/code_spell_check_spec.rb +++ b/spec/overcommit/hook/pre_commit/code_spell_check_spec.rb @@ -28,7 +28,9 @@ 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") + result.stub(:stderr).and_return( + "file1.rb:35: inkorrectspelling\n✗ Errors in code spellchecking" + ) subject.stub(:execute).and_return(result) end