Skip to content

Commit 9da15eb

Browse files
hajeesds
authored andcommitted
Add support for Puppet metadata-json-lint pre-commit hook
1 parent 3032812 commit 9da15eb

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## master (unreleased)
44

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

78
## 0.41.0

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ issue](https://github.com/brigade/overcommit/issues/238) for more details.
519519
* [LocalPathsInGemfile](lib/overcommit/hook/pre_commit/local_paths_in_gemfile.rb)
520520
* [Mdl](lib/overcommit/hook/pre_commit/mdl.rb)
521521
* [`*`MergeConflicts](lib/overcommit/hook/pre_commit/merge_conflicts.rb)
522+
* [MetadataJsonLint](lib/overcommit/hook/pre_commit/metadata_json_lint.rb)
522523
* [NginxTest](lib/overcommit/hook/pre_commit/nginx_test.rb)
523524
* [PhpCs](lib/overcommit/hook/pre_commit/php_cs.rb)
524525
* [PhpLint](lib/overcommit/hook/pre_commit/php_lint.rb)

config/default.yml

+9
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,15 @@ PreCommit:
427427
required_executable: 'grep'
428428
flags: ['-IHn', "^<<<<<<<[ \t]"]
429429

430+
MetadataJsonLint:
431+
enabled: false
432+
strict_license: true
433+
strict_dependencies: false
434+
fail_on_warning: true
435+
description: 'Checking Puppet module metadata'
436+
required_executable: 'metadata-json-lint'
437+
install_command: 'gem install metadata-json-lint semantic_puppet'
438+
430439
NginxTest:
431440
enabled: false
432441
description: 'Test nginx configs'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module Overcommit::Hook::PreCommit
2+
#
3+
# Run's the Puppet metadata linter. It has support for adding options
4+
# in the .overcommit.yaml
5+
#
6+
# PreCommit:
7+
# MetadataJsonLint:
8+
# enabled: true
9+
# strict_license: false
10+
# strict_dependencies: false
11+
# fail_on_warning: true
12+
# description: 'Checking module metadata'
13+
#
14+
# @see https://voxpupuli.org/blog/2014/11/06/linting-metadata-json/
15+
#
16+
class MetadataJsonLint < Base
17+
MESSAGE_REGEX = /\((?<type>.*)\).*/
18+
19+
MESSAGE_TYPE_CATEGORIZER = lambda do |type|
20+
type == 'WARN' ? :warning : :error
21+
end
22+
23+
def options
24+
[:strict_license, :strict_dependencies, :fail_on_warning].collect do |option|
25+
name = option.to_s.tr('_', '-')
26+
value = config.fetch(option.to_s) { true }
27+
if value
28+
"--#{name}"
29+
else
30+
"--no-#{name}"
31+
end
32+
end
33+
end
34+
35+
def run
36+
# When metadata.json, not modified return pass
37+
return :pass unless applicable_files.include?('metadata.json')
38+
39+
arguments = options << 'metadata.json'
40+
result = execute(command, args: arguments)
41+
output = result.stdout.chomp.gsub(/^"|"$/, '')
42+
return :pass if result.success? && output.empty?
43+
extract_messages(
44+
output.split("\n"),
45+
MESSAGE_REGEX,
46+
MESSAGE_TYPE_CATEGORIZER
47+
)
48+
end
49+
end
50+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PreCommit::MetadataJsonLint do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
subject { described_class.new(config, context) }
7+
8+
context 'metadata.json not modified' do
9+
before do
10+
subject.stub(:applicable_files).and_return(%w[file1.pp file2.pp])
11+
end
12+
13+
it { should pass }
14+
end
15+
16+
context 'metadata.json is modified' do
17+
before do
18+
subject.stub(:applicable_files).and_return(%w[file1.pp file2.pp metadata.json])
19+
end
20+
21+
context 'when metadata-json-lint exits successfully' do
22+
let(:result) { double('result') }
23+
24+
before do
25+
result.stub(:success?).and_return(true)
26+
subject.stub(:execute).and_return(result)
27+
end
28+
29+
context 'with no output' do
30+
before do
31+
result.stub(:stdout).and_return('')
32+
end
33+
34+
it { should pass }
35+
end
36+
37+
context 'and it reports a warning' do
38+
before do
39+
result.stub(:stdout).and_return(normalize_indent(<<-OUT))
40+
(WARN) requirements: The 'pe' requirement is no longer supported by the Forge.
41+
OUT
42+
end
43+
44+
it { should warn }
45+
end
46+
end
47+
48+
context 'when metadata-json-lint exits unsuccessfully' do
49+
let(:result) { double('result') }
50+
51+
before do
52+
result.stub(:success?).and_return(false)
53+
subject.stub(:execute).and_return(result)
54+
end
55+
56+
context 'and it reports a warning' do
57+
before do
58+
result.stub(:stdout).and_return(normalize_indent(<<-OUT))
59+
(WARN) requirements: The 'pe' requirement is no longer supported by the Forge.
60+
OUT
61+
end
62+
63+
it { should warn }
64+
end
65+
66+
context 'and it reports an error' do
67+
before do
68+
result.stub(:stdout).and_return(normalize_indent(<<-OUT))
69+
(ERR) requirements: The 'pe' requirement is no longer supported by the Forge.
70+
OUT
71+
end
72+
73+
it { should fail_hook }
74+
end
75+
end
76+
end
77+
end

0 commit comments

Comments
 (0)