Skip to content

Commit 1d392fa

Browse files
committed
Add Dogma pre-commit hook to check Elixir source files
Dogma is a code style linter for Elixir language. Dogma's source code is available at https://github.com/lpil/dogma This pre-commit hook executes dogma and reports errors.
1 parent 4db026a commit 1d392fa

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

config/default.yml

+8
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ PreCommit:
188188
install_command: 'npm install -g csslint'
189189
include: '**/*.css'
190190

191+
Dogma:
192+
enabled: false
193+
description: 'Analyzing with dogma'
194+
required_executable: 'mix'
195+
include:
196+
- '**/*.ex'
197+
- '**/*.exs'
198+
191199
EsLint:
192200
enabled: false
193201
description: 'Analyzing with ESLint'
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module Overcommit::Hook::PreCommit
2+
# Runs `dogma` against any modified ex files.
3+
#
4+
# @see http://eslint.org/
5+
class Dogma < Base
6+
def run
7+
result = execute command
8+
return :pass if result.success?
9+
10+
messages = []
11+
# example message:
12+
# == web/channels/user_socket.ex ==
13+
# 26: LineLength: Line length should not exceed 80 chars (was 83).
14+
# 1: ModuleDoc: Module Sample.UserSocket is missing a @moduledoc.
15+
output = result.stdout.chomp.match(/(==.+)/m)
16+
17+
if output
18+
output.captures.first.split(/\n\n/).each do |error_group|
19+
errors = error_group.split /\n/
20+
file = errors.shift.gsub /[ =]/, ''
21+
errors.each do |error|
22+
line = error.split(': ').first
23+
messages << Overcommit::Hook::Message.new(:error, file, line, "#{file}: #{error}")
24+
end
25+
end
26+
end
27+
28+
messages
29+
end
30+
end
31+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PreCommit::Dogma do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
subject { described_class.new(config, context) }
7+
8+
before do
9+
subject.stub(:applicable_files).and_return(%w[file1.ex file2.exs])
10+
end
11+
12+
context 'when dogma exits successfully' do
13+
before do
14+
result = double('result')
15+
result.stub(:success?).and_return(true)
16+
subject.stub(:execute).and_return(result)
17+
end
18+
19+
it { should pass }
20+
end
21+
22+
context 'when dogma exits unsucessfully' do
23+
let(:result) { double('result') }
24+
25+
before do
26+
result.stub(:success?).and_return(false)
27+
subject.stub(:execute).and_return(result)
28+
end
29+
30+
context 'and it reports an error' do
31+
before do
32+
result.stub(:stdout).and_return([
33+
'27 files, 3 errors!',
34+
'',
35+
'== test/support/model_case.ex ==',
36+
'47: LineLength: Line length should not exceed 80 chars (was 85).',
37+
'',
38+
'== test/test_helper.exs ==',
39+
'6: TrailingBlankLines: Blank lines detected at end of file',
40+
'5: TrailingWhitespace: Trailing whitespace detected',
41+
'',
42+
].join("\n"))
43+
result.stub(:stderr).and_return('')
44+
end
45+
46+
it { should fail_hook }
47+
end
48+
end
49+
end

0 commit comments

Comments
 (0)