Skip to content

Commit e995ee5

Browse files
Dmitry Sinelnikovsds
Dmitry Sinelnikov
authored andcommitted
Add support for go fmt sds#687
1 parent f087378 commit e995ee5

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

config/default.yml

+8
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,14 @@ PreCommit:
337337
flags: ['-IEHnw']
338338
keywords: ['FContext','FDescribe','FIt','FMeasure','FSpecify','FWhen']
339339

340+
GoFmt:
341+
enabled: true
342+
description: 'Fix with go fmt'
343+
required_executable: 'go'
344+
command: ['go', 'fmt']
345+
parallelize: false
346+
include: '**/*.go'
347+
340348
GolangciLint:
341349
enabled: false
342350
description: 'Analyze with golangci-lint'
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module Overcommit::Hook::PreCommit
4+
# Runs go fmt for all modified Go files
5+
class GoFmt < Base
6+
def run
7+
errors = []
8+
applicable_files.each do |file|
9+
result = execute(command, args: [file])
10+
errors << (result.stdout + result.stderr) unless result.success?
11+
end
12+
return :pass if errors.empty?
13+
14+
[:fail, errors.join("\n")]
15+
end
16+
end
17+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe Overcommit::Hook::PreCommit::GoFmt do
6+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
7+
let(:context) { double('context') }
8+
subject { described_class.new(config, context) }
9+
10+
let(:files) do
11+
%w[
12+
pkg1/file1.go
13+
pkg1/file2.go
14+
pkg2/file1.go
15+
file1.go
16+
]
17+
end
18+
before do
19+
subject.stub(:applicable_files).and_return(files)
20+
end
21+
22+
context 'when go fmt exits successfully' do
23+
let(:result) { double('result') }
24+
25+
before do
26+
result.stub(success?: true, stderr: '', stdout: '')
27+
subject.stub(:execute).and_return(result)
28+
end
29+
30+
it 'executes go fmt for each file' do
31+
files.each do |file|
32+
expect(subject).to receive(:execute).with(subject.command, args: [file]).once
33+
end
34+
subject.run
35+
end
36+
37+
it 'passes' do
38+
expect(subject).to pass
39+
end
40+
end
41+
42+
context 'when go fmt exits unsucessfully' do
43+
let(:result) { double('result') }
44+
45+
before do
46+
result.stub(:success?).and_return(false)
47+
subject.stub(:execute).and_return(result)
48+
end
49+
50+
context 'when go fmt returns an error to stdout' do
51+
let(:error_message) { 'some go fmt error' }
52+
53+
before do
54+
result.stub(:stdout).and_return(error_message)
55+
result.stub(:stderr).and_return('')
56+
end
57+
58+
it 'executes go fmt for each file' do
59+
files.each do |file|
60+
expect(subject).to receive(:execute).with(subject.command, args: [file]).once
61+
end
62+
subject.run
63+
end
64+
65+
it 'fails' do
66+
expect(subject).to fail_hook
67+
end
68+
69+
it 'returns errors' do
70+
message = subject.run.last
71+
expect(message).to eq Array.new(files.count, error_message).join("\n")
72+
end
73+
end
74+
75+
context 'when fo fmt returns an error to stderr' do
76+
let(:error_message) { 'go: command not found' }
77+
before do
78+
result.stub(:stdout).and_return('')
79+
result.stub(:stderr).and_return(error_message)
80+
end
81+
82+
it 'executes go fmt for each file' do
83+
files.each do |file|
84+
expect(subject).to receive(:execute).with(subject.command, args: [file]).once
85+
end
86+
subject.run
87+
end
88+
89+
it 'fails' do
90+
expect(subject).to fail_hook
91+
end
92+
93+
it 'returns valid message' do
94+
message = subject.run.last
95+
expect(message).to eq Array.new(files.count, error_message).join("\n")
96+
end
97+
end
98+
end
99+
end

0 commit comments

Comments
 (0)