Skip to content

Separate go build and gofmt checks. #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 3 additions & 15 deletions lib/plugins/pre_commit/checks/go.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions lib/plugins/pre_commit/checks/go_build.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions lib/plugins/pre_commit/checks/go_fmt.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions test/unit/plugins/pre_commit/checks/go_fmt_test.rb
Original file line number Diff line number Diff line change
@@ -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?
8 changes: 4 additions & 4 deletions test/unit/pre-commit/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/unit/pre-commit/list_evaluator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down