Skip to content

Commit a596607

Browse files
committed
Merge pull request #231 from jish/marcosvm/separate_gofmt_and_go_build
Separate go build and gofmt checks.
2 parents 12436b0 + 5950456 commit a596607

File tree

9 files changed

+91
-28
lines changed

9 files changed

+91
-28
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ These are the available checks:
5252
* rubocop (Check ruby code style using the rubocop gem. Rubocop must be installed)
5353
* before_all (Check your RSpec tests for the use of `before(:all)`)
5454
* coffeelint (Check your coffeescript files using the [coffeelint gem.](https://github.com/clutchski/coffeelint))
55-
* 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)
55+
* gobuild (Runs go build and fails if can't compile)
56+
* gofmt (Runs go fmt on go source files and fail if formatting is incorrect)
5657
* scss_lint (Check your SCSS files using the [scss-lint gem](https://github.com/brigade/scss-lint))
5758
* yaml (Check that your YAML is parsable)
5859
* json (Checks if JSON is parsable)

lib/plugins/pre_commit/checks/go.rb

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,13 @@ module PreCommit
44
module Checks
55
class Go < Plugin
66

7-
def call(staged_files)
8-
staged_files = staged_files.grep(/\.go$/)
9-
return if staged_files.empty?
10-
11-
errors = staged_files.map { |file| run_check(file) }.compact
12-
return if errors.empty?
13-
14-
errors.join("\n")
7+
def self.includes
8+
[:gobuild, :gofmt]
159
end
1610

17-
def run_check(file)
18-
cmd = "gofmt -l #{file} 2>&1"
19-
result = %x[ #{cmd} ]
20-
cmd = "go build -o /dev/null #{file} 2>&1"
21-
result << %x[ #{cmd} ]
22-
end
2311

2412
def self.description
25-
"Detects bad Go formatting and compiler errors"
13+
"Plugins for Go code"
2614
end
2715
end
2816
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'pre-commit/checks/plugin'
2+
3+
module PreCommit
4+
module Checks
5+
class GoBuild < Plugin
6+
7+
def call(staged_files)
8+
staged_files = staged_files.grep(/\.go$/)
9+
return if staged_files.empty?
10+
11+
errors = staged_files.map { |file| run_check(file) }.compact
12+
return if errors.empty?
13+
14+
errors.join("\n")
15+
end
16+
17+
def run_check(file)
18+
cmd = "go build -o /dev/null #{file} 2>&1"
19+
%x[ #{cmd} ]
20+
end
21+
22+
def self.description
23+
"Detects Go compiler errors"
24+
end
25+
end
26+
end
27+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module PreCommit
2+
module Checks
3+
class GoFmt < Plugin
4+
5+
def call(staged_files)
6+
staged_files = staged_files.grep(/\.go$/)
7+
return if staged_files.empty?
8+
9+
errors = staged_files.map { |file| run_check(file) }.compact
10+
return if errors.empty?
11+
12+
errors.join("\n")
13+
end
14+
15+
def run_check(file)
16+
cmd = "gofmt -l #{file} 2>&1"
17+
%x[ #{cmd} ]
18+
end
19+
20+
def self.description
21+
"Detects bad Go formatting"
22+
end
23+
end
24+
end
25+
end

test/minitest_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ def sh(command, options={})
7272
result
7373
end
7474

75+
def available_checks
76+
@available_checks ||=
77+
Dir["#{project_dir}/lib/plugins/pre_commit/checks/*.rb"].map{|path|
78+
path.gsub(/^.*\/([^\/]*)\.rb$/, "\\1")
79+
}.sort.join(" ")
80+
end
81+
7582
end; end
7683

7784
class MiniTest::Test

test/unit/plugins/pre_commit/checks/go_test.rb renamed to test/unit/plugins/pre_commit/checks/go_build_test.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require 'minitest_helper'
2-
require 'plugins/pre_commit/checks/go'
2+
require 'plugins/pre_commit/checks/go_build'
33

4-
describe PreCommit::Checks::Go do
5-
let(:check) {PreCommit::Checks::Go.new(nil, nil, [])}
4+
describe PreCommit::Checks::GoBuild do
5+
let(:check) {PreCommit::Checks::GoBuild.new(nil, nil, [])}
66

77
it "succeds if nothing changed" do
88
check.call([]).must_equal nil
@@ -12,10 +12,6 @@
1212
check.call([fixture_file('good.go')]).must_equal ""
1313
end
1414

15-
it "fails for bad formatted code" do
16-
check.call([fixture_file("bad_fmt.go")]).must_match(/bad_fmt.go/)
17-
end
18-
1915
it "fails for compiler errors" do
2016
check.call([fixture_file("dont_compile.go")]).must_match(/imported and not used/)
2117
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'minitest_helper'
2+
require 'plugins/pre_commit/checks/go_fmt'
3+
4+
describe PreCommit::Checks::GoFmt do
5+
let(:check) {PreCommit::Checks::GoFmt.new(nil, nil, [])}
6+
7+
it "succeds if nothing changed" do
8+
check.call([]).must_equal nil
9+
end
10+
11+
it "succeeds for good code" do
12+
check.call([fixture_file('good.go')]).must_equal ""
13+
end
14+
15+
it "fails for bad formatted code" do
16+
check.call([fixture_file("bad_fmt.go")]).must_match(/bad_fmt.go/)
17+
end
18+
19+
end unless `which go 2>/dev/null`.empty?

test/unit/pre-commit/cli_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
$stderr.string.must_equal('')
4747
$stdout.string.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED)
4848
Available providers: default(0) git(10) git_old(11) yaml(20) env(30)
49-
Available checks : before_all ci coffeelint common console_log csslint debugger gemfile_path go jshint jslint json local merge_conflict migration nb_space pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml
49+
Available checks : #{available_checks}
5050
Default checks : common rails
5151
Enabled checks : common rails
5252
Evaluated checks : tabs nb_space whitespace merge_conflict debugger pry local jshint console_log migration
@@ -77,7 +77,7 @@
7777
$stderr.string.must_equal('')
7878
$stdout.string.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED)
7979
Available providers: default(0) git(10) git_old(11) yaml(20) env(30)
80-
Available checks : before_all ci coffeelint common console_log csslint debugger gemfile_path go jshint jslint json local merge_conflict migration nb_space pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml
80+
Available checks : #{available_checks}
8181
Default checks : common rails
8282
Enabled checks : common rails
8383
Evaluated checks : tabs nb_space merge_conflict debugger pry local jshint console_log migration
@@ -99,7 +99,7 @@
9999
$stderr.string.must_equal('')
100100
$stdout.string.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED)
101101
Available providers: default(0) git(10) git_old(11) yaml(20) env(30)
102-
Available checks : before_all ci coffeelint common console_log csslint debugger gemfile_path go jshint jslint json local merge_conflict migration nb_space pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml
102+
Available checks : #{available_checks}
103103
Default checks : common rails
104104
Enabled checks : common rails
105105
Evaluated checks : tabs nb_space merge_conflict debugger pry local jshint console_log migration
@@ -121,7 +121,7 @@
121121
$stderr.string.must_equal('')
122122
$stdout.string.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED)
123123
Available providers: default(0) git(10) git_old(11) yaml(20) env(30)
124-
Available checks : before_all ci coffeelint common console_log csslint debugger gemfile_path go jshint jslint json local merge_conflict migration nb_space pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml
124+
Available checks : #{available_checks}
125125
Default checks : common rails
126126
Enabled checks : common rails
127127
Evaluated checks : tabs nb_space whitespace merge_conflict debugger pry local jshint console_log migration

test/unit/pre-commit/list_evaluator_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
it :list do
2323
subject.list.gsub(/\s+\n/,"\n").must_equal(<<-EXPECTED)
2424
Available providers: default(0)
25-
Available checks : before_all ci coffeelint common console_log csslint debugger gemfile_path go jshint jslint json local merge_conflict migration nb_space pry rails rspec_focus rubocop ruby ruby_symbol_hashrockets scss_lint tabs whitespace yaml
25+
Available checks : #{available_checks}
2626
Default checks :
2727
Enabled checks :
2828
Evaluated checks :

0 commit comments

Comments
 (0)