From 6fce3920c84dffb5a7d8e5836de5aaca8045504e Mon Sep 17 00:00:00 2001 From: Thomas Holt Date: Tue, 19 Feb 2019 15:12:32 -0500 Subject: [PATCH 1/4] add cookstyle support to fix #617 --- config/default.yml | 12 ++++++- lib/overcommit/hook/pre_commit/cook_style.rb | 35 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 lib/overcommit/hook/pre_commit/cook_style.rb diff --git a/config/default.yml b/config/default.yml index 41a1d4b4..b675705c 100644 --- a/config/default.yml +++ b/config/default.yml @@ -230,7 +230,17 @@ PreCommit: install_command: 'npm install -g coffeelint' include: '**/*.coffee' - Credo: + CookStyle: + enabled: false + description: 'Analyze with CookStyle' + required_executable: 'cookstyle' + flags: ['--format=emacs', '--force-exclusion', '--display-cop-names'] + install_command: 'gem install cookstyle' + include: + - '**/*.rb' + - '**/*.erb' + + Credo: enabled: false description: 'Analyze with credo' required_executable: 'mix' diff --git a/lib/overcommit/hook/pre_commit/cook_style.rb b/lib/overcommit/hook/pre_commit/cook_style.rb new file mode 100644 index 00000000..afa480cb --- /dev/null +++ b/lib/overcommit/hook/pre_commit/cook_style.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module Overcommit::Hook::PreCommit + # Runs `cookstyle` against any modified Chef Ruby files. + # + # @see https://docs.chef.io/cookstyle.html + class CookStyle < Base + GENERIC_MESSAGE_TYPE_CATEGORIZER = lambda do |type| + type =~ /^warn/ ? :warning : :error + end + + COP_MESSAGE_TYPE_CATEGORIZER = lambda do |type| + type.include?('W') ? :warning : :error + end + + def run + result = execute(command, args: applicable_files) + return :pass if result.success? + + generic_messages = extract_messages( + result.stderr.split("\n"), + /^(?[a-z]+)/i, + GENERIC_MESSAGE_TYPE_CATEGORIZER, + ) + + cop_messages = extract_messages( + result.stdout.split("\n"), + /^(?(?:\w:)?[^:]+):(?\d+):[^ ]+ (?[^ ]+)/, + COP_MESSAGE_TYPE_CATEGORIZER, + ) + + generic_messages + cop_messages + end + end +end From 6a7db5dea55a33849b79af892cb69a7ad9e79afe Mon Sep 17 00:00:00 2001 From: Thomas Holt Date: Wed, 20 Feb 2019 13:39:23 -0500 Subject: [PATCH 2/4] fixed indenting --- config/default.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/default.yml b/config/default.yml index b675705c..3a1877bc 100644 --- a/config/default.yml +++ b/config/default.yml @@ -230,7 +230,7 @@ PreCommit: install_command: 'npm install -g coffeelint' include: '**/*.coffee' - CookStyle: + CookStyle: enabled: false description: 'Analyze with CookStyle' required_executable: 'cookstyle' @@ -240,7 +240,7 @@ PreCommit: - '**/*.rb' - '**/*.erb' - Credo: + Credo: enabled: false description: 'Analyze with credo' required_executable: 'mix' From ac3bf14f7fcaebc213b266ec1184cc73d9a96f43 Mon Sep 17 00:00:00 2001 From: Thomas Holt Date: Mon, 25 Feb 2019 08:10:52 -0500 Subject: [PATCH 3/4] added cookstyle unit test --- .../hook/pre_commit/cook_style_spec.rb | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 spec/overcommit/hook/pre_commit/cook_style_spec.rb diff --git a/spec/overcommit/hook/pre_commit/cook_style_spec.rb b/spec/overcommit/hook/pre_commit/cook_style_spec.rb new file mode 100644 index 00000000..a37678da --- /dev/null +++ b/spec/overcommit/hook/pre_commit/cook_style_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Overcommit::Hook::PreCommit::CookStyle 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 cookstyle exits successfully' do + let(:result) { double('result') } + + before do + result.stub(success?: true, stderr: '', stdout: '') + subject.stub(:execute).and_return(result) + end + + it { should pass } + end + + context 'when cookstyle exits unsucessfully' do + let(:result) { double('result') } + + before do + result.stub(:success?).and_return(false) + subject.stub(:execute).and_return(result) + end + + it { should fail_hook } + end +end From 6d61dbc56431d9c042401b79b8cb9186fde60d15 Mon Sep 17 00:00:00 2001 From: Thomas Holt Date: Mon, 25 Feb 2019 09:12:44 -0500 Subject: [PATCH 4/4] Reverted to more like rubocop tests --- .../hook/pre_commit/cook_style_spec.rb | 69 ++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/spec/overcommit/hook/pre_commit/cook_style_spec.rb b/spec/overcommit/hook/pre_commit/cook_style_spec.rb index a37678da..a7dece87 100644 --- a/spec/overcommit/hook/pre_commit/cook_style_spec.rb +++ b/spec/overcommit/hook/pre_commit/cook_style_spec.rb @@ -20,6 +20,18 @@ end it { should pass } + + context 'and it printed warnings to stderr' do + before do + result.stub(:stderr).and_return(normalize_indent(<<-MSG)) + warning: parser/current is loading parser/ruby21, which recognizes + warning: 2.1.8-compliant syntax, but you are running 2.1.1. + warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri. + MSG + end + + it { should pass } + end end context 'when cookstyle exits unsucessfully' do @@ -30,6 +42,61 @@ subject.stub(:execute).and_return(result) end - it { should fail_hook } + context 'and it reports a warning' do + before do + result.stub(:stdout).and_return([ + 'file1.rb:1:1: W: Useless assignment to variable - my_var.', + ].join("\n")) + result.stub(:stderr).and_return('') + end + + it { should warn } + + context 'and it printed warnings to stderr' do + before do + result.stub(:stderr).and_return(normalize_indent(<<-MSG)) + warning: parser/current is loading parser/ruby21, which recognizes + warning: 2.1.8-compliant syntax, but you are running 2.1.1. + warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri. + MSG + end + + it { should warn } + end + end + + context 'and it reports an error' do + before do + result.stub(:stdout).and_return([ + 'file1.rb:1:1: C: Missing top-level class documentation', + ].join("\n")) + result.stub(:stderr).and_return('') + end + + it { should fail_hook } + + context 'and it printed warnings to stderr' do + before do + result.stub(:stderr).and_return(normalize_indent(<<-MSG)) + warning: parser/current is loading parser/ruby21, which recognizes + warning: 2.1.8-compliant syntax, but you are running 2.1.1. + warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri. + MSG + end + + it { should fail_hook } + end + end + + context 'when a generic error message is written to stderr' do + before do + result.stub(:stdout).and_return('') + result.stub(:stderr).and_return([ + 'Could not find cookstyle in any of the sources' + ].join("\n")) + end + + it { should fail_hook } + end end end