From b9f1519a5c3ebc3a1492a953e3c476ea47296fb8 Mon Sep 17 00:00:00 2001 From: Marcos Oliveira Date: Fri, 8 Aug 2014 13:33:42 -0700 Subject: [PATCH 1/3] Separate go build and gofmt checks. --- README.md | 3 ++- lib/plugins/pre_commit/checks/go.rb | 18 +++---------- lib/plugins/pre_commit/checks/go_build.rb | 27 +++++++++++++++++++ lib/plugins/pre_commit/checks/go_fmt.rb | 25 +++++++++++++++++ .../checks/{go_test.rb => go_build_test.rb} | 10 +++---- .../plugins/pre_commit/checks/go_fmt_test.rb | 19 +++++++++++++ test/unit/pre-commit/cli_test.rb | 8 +++--- 7 files changed, 83 insertions(+), 27 deletions(-) create mode 100644 lib/plugins/pre_commit/checks/go_build.rb create mode 100644 lib/plugins/pre_commit/checks/go_fmt.rb rename test/unit/plugins/pre_commit/checks/{go_test.rb => go_build_test.rb} (60%) create mode 100644 test/unit/plugins/pre_commit/checks/go_fmt_test.rb diff --git a/README.md b/README.md index fe3460e..5ea87af 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,8 @@ These are the available checks: * rubocop (Check ruby code style using the rubocop gem. Rubocop must be installed) * before_all (Check your RSpec tests for the use of `before(:all)`) * coffeelint (Check your coffeescript files using the [coffeelint gem.](https://github.com/clutchski/coffeelint)) -* go (Runs go fmt on a go source file and fail if formatting is incorrect, then runs go build and fails if can't compile) +* gobuild (Runs go build and fails if can't compile) +* gofmt (Runs go fmt on go source files and fail if formatting is incorrect) * scss_lint (Check your SCSS files using the [scss-lint gem](https://github.com/causes/scss-lint)) * yaml (Check that your YAML is parsable) * json (Checks if JSON is parsable) diff --git a/lib/plugins/pre_commit/checks/go.rb b/lib/plugins/pre_commit/checks/go.rb index 62711f2..0f710ac 100644 --- a/lib/plugins/pre_commit/checks/go.rb +++ b/lib/plugins/pre_commit/checks/go.rb @@ -4,25 +4,13 @@ module PreCommit module Checks class Go < Plugin - def call(staged_files) - staged_files = staged_files.grep(/\.go$/) - return if staged_files.empty? - - errors = staged_files.map { |file| run_check(file) }.compact - return if errors.empty? - - errors.join("\n") + def self.includes + [:gobuild, :gofmt] end - def run_check(file) - cmd = "gofmt -l #{file} 2>&1" - result = %x[ #{cmd} ] - cmd = "go build -o /dev/null #{file} 2>&1" - result << %x[ #{cmd} ] - end def self.description - "Detects bad Go formatting and compiler errors" + "Plugins for Go code" end end end diff --git a/lib/plugins/pre_commit/checks/go_build.rb b/lib/plugins/pre_commit/checks/go_build.rb new file mode 100644 index 0000000..3209118 --- /dev/null +++ b/lib/plugins/pre_commit/checks/go_build.rb @@ -0,0 +1,27 @@ +require 'pre-commit/checks/plugin' + +module PreCommit + module Checks + class GoBuild < Plugin + + def call(staged_files) + staged_files = staged_files.grep(/\.go$/) + return if staged_files.empty? + + errors = staged_files.map { |file| run_check(file) }.compact + return if errors.empty? + + errors.join("\n") + end + + def run_check(file) + cmd = "go build -o /dev/null #{file} 2>&1" + %x[ #{cmd} ] + end + + def self.description + "Detects Go compiler errors" + end + end + end +end diff --git a/lib/plugins/pre_commit/checks/go_fmt.rb b/lib/plugins/pre_commit/checks/go_fmt.rb new file mode 100644 index 0000000..53b46a4 --- /dev/null +++ b/lib/plugins/pre_commit/checks/go_fmt.rb @@ -0,0 +1,25 @@ +module PreCommit + module Checks + class GoFmt < Plugin + + def call(staged_files) + staged_files = staged_files.grep(/\.go$/) + return if staged_files.empty? + + errors = staged_files.map { |file| run_check(file) }.compact + return if errors.empty? + + errors.join("\n") + end + + def run_check(file) + cmd = "gofmt -l #{file} 2>&1" + %x[ #{cmd} ] + end + + def self.description + "Detects bad Go formatting" + end + end + end +end diff --git a/test/unit/plugins/pre_commit/checks/go_test.rb b/test/unit/plugins/pre_commit/checks/go_build_test.rb similarity index 60% rename from test/unit/plugins/pre_commit/checks/go_test.rb rename to test/unit/plugins/pre_commit/checks/go_build_test.rb index 5227ddc..68a764b 100644 --- a/test/unit/plugins/pre_commit/checks/go_test.rb +++ b/test/unit/plugins/pre_commit/checks/go_build_test.rb @@ -1,8 +1,8 @@ require 'minitest_helper' -require 'plugins/pre_commit/checks/go' +require 'plugins/pre_commit/checks/go_build' -describe PreCommit::Checks::Go do - let(:check) {PreCommit::Checks::Go.new(nil, nil, [])} +describe PreCommit::Checks::GoBuild do + let(:check) {PreCommit::Checks::GoBuild.new(nil, nil, [])} it "succeds if nothing changed" do check.call([]).must_equal nil @@ -12,10 +12,6 @@ check.call([fixture_file('good.go')]).must_equal "" end - it "fails for bad formatted code" do - check.call([fixture_file("bad_fmt.go")]).must_match(/bad_fmt.go/) - end - it "fails for compiler errors" do check.call([fixture_file("dont_compile.go")]).must_match(/imported and not used/) end diff --git a/test/unit/plugins/pre_commit/checks/go_fmt_test.rb b/test/unit/plugins/pre_commit/checks/go_fmt_test.rb new file mode 100644 index 0000000..5142b82 --- /dev/null +++ b/test/unit/plugins/pre_commit/checks/go_fmt_test.rb @@ -0,0 +1,19 @@ +require 'minitest_helper' +require 'plugins/pre_commit/checks/go_fmt' + +describe PreCommit::Checks::GoFmt do + let(:check) {PreCommit::Checks::GoFmt.new(nil, nil, [])} + + it "succeds if nothing changed" do + check.call([]).must_equal nil + end + + it "succeeds for good code" do + check.call([fixture_file('good.go')]).must_equal "" + end + + it "fails for bad formatted code" do + check.call([fixture_file("bad_fmt.go")]).must_match(/bad_fmt.go/) + end + +end unless `which go 2>/dev/null`.empty? diff --git a/test/unit/pre-commit/cli_test.rb b/test/unit/pre-commit/cli_test.rb index 54211bb..d40984f 100644 --- a/test/unit/pre-commit/cli_test.rb +++ b/test/unit/pre-commit/cli_test.rb @@ -46,7 +46,7 @@ $stderr.string.must_equal('') $stdout.string.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED) Available providers: default(0) git(10) git_old(11) yaml(20) env(30) -Available checks : before_all ci closure coffeelint common console_log csslint debugger gemfile_path go jshint jslint json local merge_conflict migration nb_space php pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml +Available checks : before_all ci closure coffeelint common console_log csslint debugger gemfile_path go go_build go_fmt jshint jslint json local merge_conflict migration nb_space php pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml Default checks : common rails Enabled checks : common rails Evaluated checks : tabs nb_space whitespace merge_conflict debugger pry local jshint console_log migration @@ -77,7 +77,7 @@ $stderr.string.must_equal('') $stdout.string.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED) Available providers: default(0) git(10) git_old(11) yaml(20) env(30) -Available checks : before_all ci closure coffeelint common console_log csslint debugger gemfile_path go jshint jslint json local merge_conflict migration nb_space php pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml +Available checks : before_all ci closure coffeelint common console_log csslint debugger gemfile_path go go_build go_fmt jshint jslint json local merge_conflict migration nb_space php pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml Default checks : common rails Enabled checks : common rails Evaluated checks : tabs nb_space merge_conflict debugger pry local jshint console_log migration @@ -99,7 +99,7 @@ $stderr.string.must_equal('') $stdout.string.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED) Available providers: default(0) git(10) git_old(11) yaml(20) env(30) -Available checks : before_all ci closure coffeelint common console_log csslint debugger gemfile_path go jshint jslint json local merge_conflict migration nb_space php pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml +Available checks : before_all ci closure coffeelint common console_log csslint debugger gemfile_path go go_build go_fmt jshint jslint json local merge_conflict migration nb_space php pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml Default checks : common rails Enabled checks : common rails Evaluated checks : tabs nb_space merge_conflict debugger pry local jshint console_log migration @@ -121,7 +121,7 @@ $stderr.string.must_equal('') $stdout.string.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED) Available providers: default(0) git(10) git_old(11) yaml(20) env(30) -Available checks : before_all ci closure coffeelint common console_log csslint debugger gemfile_path go jshint jslint json local merge_conflict migration nb_space php pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml +Available checks : before_all ci closure coffeelint common console_log csslint debugger gemfile_path go go_build go_fmt jshint jslint json local merge_conflict migration nb_space php pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml Default checks : common rails Enabled checks : common rails Evaluated checks : tabs nb_space whitespace merge_conflict debugger pry local jshint console_log migration From 2d396bf467cb49573dd8102520ac3e19d9b9bb53 Mon Sep 17 00:00:00 2001 From: Marcos Oliveira Date: Sat, 13 Sep 2014 17:23:44 -0700 Subject: [PATCH 2/3] add go_build and go_fmt to the list of available checks --- test/unit/pre-commit/list_evaluator_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/pre-commit/list_evaluator_test.rb b/test/unit/pre-commit/list_evaluator_test.rb index 0e2e3e2..f857ded 100644 --- a/test/unit/pre-commit/list_evaluator_test.rb +++ b/test/unit/pre-commit/list_evaluator_test.rb @@ -22,7 +22,7 @@ it :list do subject.list.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED) Available providers: default(0) -Available checks : before_all ci closure coffeelint common console_log csslint debugger gemfile_path go jshint jslint json local merge_conflict migration nb_space php pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml +Available checks : before_all ci closure coffeelint common console_log csslint debugger gemfile_path go go_build go_fmt jshint jslint json local merge_conflict migration nb_space php pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml Default checks : Enabled checks : Evaluated checks : From 59504564e21172e75f10a4c3aeff7f2f07eaaa7b Mon Sep 17 00:00:00 2001 From: Michal Papis Date: Sun, 31 Jan 2016 13:10:59 +0100 Subject: [PATCH 3/3] replace Available checks with dynamicaly generated list --- test/minitest_helper.rb | 7 +++++++ test/unit/pre-commit/cli_test.rb | 8 ++++---- test/unit/pre-commit/list_evaluator_test.rb | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/test/minitest_helper.rb b/test/minitest_helper.rb index be958ce..49e3d5b 100644 --- a/test/minitest_helper.rb +++ b/test/minitest_helper.rb @@ -72,6 +72,13 @@ def sh(command, options={}) result end + def available_checks + @available_checks ||= + Dir["#{project_dir}/lib/plugins/pre_commit/checks/*.rb"].map{|path| + path.gsub(/^.*\/([^\/]*)\.rb$/, "\\1") + }.sort.join(" ") + end + end; end class MiniTest::Test diff --git a/test/unit/pre-commit/cli_test.rb b/test/unit/pre-commit/cli_test.rb index 621df43..2557138 100644 --- a/test/unit/pre-commit/cli_test.rb +++ b/test/unit/pre-commit/cli_test.rb @@ -46,7 +46,7 @@ $stderr.string.must_equal('') $stdout.string.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED) Available providers: default(0) git(10) git_old(11) yaml(20) env(30) -Available checks : before_all ci coffeelint common console_log csslint debugger gemfile_path go go_build go_fmt jshint jslint json local merge_conflict migration nb_space pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml +Available checks : #{available_checks} Default checks : common rails Enabled checks : common rails Evaluated checks : tabs nb_space whitespace merge_conflict debugger pry local jshint console_log migration @@ -77,7 +77,7 @@ $stderr.string.must_equal('') $stdout.string.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED) Available providers: default(0) git(10) git_old(11) yaml(20) env(30) -Available checks : before_all ci coffeelint common console_log csslint debugger gemfile_path go go_build go_fmt jshint jslint json local merge_conflict migration nb_space pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml +Available checks : #{available_checks} Default checks : common rails Enabled checks : common rails Evaluated checks : tabs nb_space merge_conflict debugger pry local jshint console_log migration @@ -99,7 +99,7 @@ $stderr.string.must_equal('') $stdout.string.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED) Available providers: default(0) git(10) git_old(11) yaml(20) env(30) -Available checks : before_all ci coffeelint common console_log csslint debugger gemfile_path go go_build go_fmt jshint jslint json local merge_conflict migration nb_space pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml +Available checks : #{available_checks} Default checks : common rails Enabled checks : common rails Evaluated checks : tabs nb_space merge_conflict debugger pry local jshint console_log migration @@ -121,7 +121,7 @@ $stderr.string.must_equal('') $stdout.string.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED) Available providers: default(0) git(10) git_old(11) yaml(20) env(30) -Available checks : before_all ci coffeelint common console_log csslint debugger gemfile_path go go_build go_fmt jshint jslint json local merge_conflict migration nb_space pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml +Available checks : #{available_checks} Default checks : common rails Enabled checks : common rails Evaluated checks : tabs nb_space whitespace merge_conflict debugger pry local jshint console_log migration diff --git a/test/unit/pre-commit/list_evaluator_test.rb b/test/unit/pre-commit/list_evaluator_test.rb index 844c9f1..0c3f68a 100644 --- a/test/unit/pre-commit/list_evaluator_test.rb +++ b/test/unit/pre-commit/list_evaluator_test.rb @@ -22,7 +22,7 @@ it :list do subject.list.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED) Available providers: default(0) -Available checks : before_all ci coffeelint common console_log csslint debugger gemfile_path go go_build go_fmt jshint jslint json local merge_conflict migration nb_space pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml +Available checks : #{available_checks} Default checks : Enabled checks : Evaluated checks :