Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## master (unreleased)

* Add [`metadata-json-lint`](https://voxpupuli.org/blog/2014/11/06/linting-metadata-json/) pre-commit hook
* Fix `LineEndings` pre-commit hook handling of file paths with spaces

## 0.41.0
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ issue](https://github.com/brigade/overcommit/issues/238) for more details.
* [PhpCs](lib/overcommit/hook/pre_commit/php_cs.rb)
* [PhpLint](lib/overcommit/hook/pre_commit/php_lint.rb)
* [PuppetLint](lib/overcommit/hook/pre_commit/puppet_lint.rb)
* [PuppetMetadataJsonLint](lib/overcommit/hook/pre_commit/puppet_metadata_json_lint.rb)
* [Pycodestyle](lib/overcommit/hook/pre_commit/pycodestyle.rb)
* [Pydocstyle](lib/overcommit/hook/pre_commit/pydocstyle.rb)
* [Pyflakes](lib/overcommit/hook/pre_commit/pyflakes.rb)
Expand Down
8 changes: 8 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ PreCommit:
required_executable: 'grep'
flags: ['-IHn', "^<<<<<<<[ \t]"]

PuppetMetadataJsonLint:
enabled: false
description: 'Checking module metadata'
flags: ['--strict-license', '--strict-dependencies', '--fail-on-warning']
include: 'metadata.json'
required_executable: 'metadata-json-lint'
install_command: 'gem install metadata-json-lint'

NginxTest:
enabled: false
description: 'Test nginx configs'
Expand Down
27 changes: 27 additions & 0 deletions lib/overcommit/hook/pre_commit/puppet_metadata_json_lint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Overcommit::Hook::PreCommit
#
# Run's the Puppet metadata linter. It has support for adding options
# in the .overcommit.yaml
#
# @see https://voxpupuli.org/blog/2014/11/06/linting-metadata-json/
#
class PuppetMetadataJsonLint < Base
MESSAGE_REGEX = /\((?<type>.*)\).*/

MESSAGE_TYPE_CATEGORIZER = lambda do |type|
type == 'WARN' ? :warning : :error
end

def run
result = execute(command, args: applicable_files)
output = result.stdout.chomp.gsub(/^"|"$/, '')
return :pass if result.success? && output.empty?

extract_messages(
output.split("\n"),
MESSAGE_REGEX,
MESSAGE_TYPE_CATEGORIZER
)
end
end
end
67 changes: 67 additions & 0 deletions spec/overcommit/hook/pre_commit/puppet_metadata_json_lint_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'spec_helper'

describe Overcommit::Hook::PreCommit::PuppetMetadataJsonLint do
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
let(:context) { double('context') }
subject { described_class.new(config, context) }

before do
subject.stub(:applicable_files).and_return(%w[file1.pp file2.pp metadata.json])
end

context 'when metadata-json-lint exits successfully' do
let(:result) { double('result') }

before do
result.stub(:success?).and_return(true)
subject.stub(:execute).and_return(result)
end

context 'with no output' do
before do
result.stub(:stdout).and_return('')
end

it { should pass }
end

context 'and it reports a warning' do
before do
result.stub(:stdout).and_return(normalize_indent(<<-OUT))
(WARN) requirements: The 'pe' requirement is no longer supported by the Forge.
OUT
end

it { should warn }
end
end

context 'when metadata-json-lint exits unsuccessfully' do
let(:result) { double('result') }

before do
result.stub(:success?).and_return(false)
subject.stub(:execute).and_return(result)
end

context 'and it reports a warning' do
before do
result.stub(:stdout).and_return(normalize_indent(<<-OUT))
(WARN) requirements: The 'pe' requirement is no longer supported by the Forge.
OUT
end

it { should warn }
end

context 'and it reports an error' do
before do
result.stub(:stdout).and_return(normalize_indent(<<-OUT))
(ERR) requirements: The 'pe' requirement is no longer supported by the Forge.
OUT
end

it { should fail_hook }
end
end
end