Skip to content

Commit

Permalink
switch to voxpupuli-rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed May 28, 2023
1 parent e7eb6d5 commit e2c2c6d
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 168 deletions.
38 changes: 2 additions & 36 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,3 @@
---
inherit_from: .rubocop_todo.yml

require:
- rubocop-performance
- rubocop-rake
- rubocop-rspec

AllCops:
NewCops: enable
TargetRubyVersion: '2.5'
Exclude:
- vendor/**/*

Style/TrailingCommaInHashLiteral:
Enabled: True
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInArrayLiteral:
Enabled: True
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInArguments:
Enabled: True
EnforcedStyleForMultiline: comma

Gemspec/RequireMFA:
Enabled: false

Metrics:
Enabled: false

Style:
Enabled: false

Layout:
Enabled: false
inherit_gem:
voxpupuli-rubocop: rubocop.yml
81 changes: 0 additions & 81 deletions .rubocop_todo.yml

This file was deleted.

4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ group :release do
end

group :coverage, optional: ENV['COVERAGE'] != 'yes' do
gem 'codecov', :require => false
gem 'simplecov-console', :require => false
gem 'codecov', require: false
gem 'simplecov-console', require: false
end
6 changes: 2 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
desc 'Run all tests'

Check failure on line 1 in Rakefile

View workflow job for this annotation

GitHub Actions / rubocop

Style/FrozenStringLiteralComment: Missing frozen string literal comment.
task :test => %i[spec test:acceptance]
task test: %i[spec test:acceptance]

begin
require 'rubocop/rake_task'
Expand All @@ -10,9 +10,7 @@ else
# These make the rubocop experience maybe slightly less terrible
task.options = ['--display-cop-names', '--display-style-guide', '--extra-details']
# Use Rubocop's Github Actions formatter if possible
if ENV['GITHUB_ACTIONS'] == 'true'
task.formatters << 'github'
end
task.formatters << 'github' if ENV['GITHUB_ACTIONS'] == 'true'
end
end

Expand Down
4 changes: 1 addition & 3 deletions lib/metadata-json-lint/rake_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@

desc 'Run metadata-json-lint'
task :metadata_lint do
if File.exist?('metadata.json')
abort unless MetadataJsonLint.parse('metadata.json')
end
abort if File.exist?('metadata.json') && !MetadataJsonLint.parse('metadata.json')
end
3 changes: 2 additions & 1 deletion lib/metadata-json-lint/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ def semver_validator(value)
raise JSON::Schema::CustomFormatError, "must be a valid semantic version: #{e.message}"
end
elsif value.match(semver_full_regex).nil?
raise JSON::Schema::CustomFormatError, "must be a valid semantic version: Unable to parse '#{value}' as a semantic version identifier"
raise JSON::Schema::CustomFormatError,
"must be a valid semantic version: Unable to parse '#{value}' as a semantic version identifier"
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/metadata-json-lint/version_requirement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ def initialize(requirement)

if defined?(SemanticPuppet::VersionRange)
@range = SemanticPuppet::VersionRange.parse(requirement)
raise ArgumentError, "Range matches no versions: \"#{requirement}\"" if @range == SemanticPuppet::VersionRange::EMPTY_RANGE
if @range == SemanticPuppet::VersionRange::EMPTY_RANGE
raise ArgumentError,
"Range matches no versions: \"#{requirement}\""
end
elsif requirement.match(/\A[a-z0-9*.\-^~><=|\t ]*\Z/i).nil?
raise ArgumentError, "Unparsable version range: \"#{requirement}\""
end
Expand Down
46 changes: 22 additions & 24 deletions lib/metadata_json_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def options
:strict_license,
:strict_dependencies,
:strict_puppet_version,
:format
:format,
).new(
true, # fail_on_warnings
true, # strict_license
Expand All @@ -30,23 +30,27 @@ def run
OptionParser.new do |opts|
opts.banner = 'Usage: metadata-json-lint [options] [metadata.json]'

opts.on('--[no-]strict-dependencies', "Fail on open-ended module version dependencies. Defaults to '#{options[:strict_dependencies]}'.") do |v|
opts.on('--[no-]strict-dependencies',
"Fail on open-ended module version dependencies. Defaults to '#{options[:strict_dependencies]}'.") do |v|
options[:strict_dependencies] = v
end

opts.on('--[no-]strict-license', "Don't fail on strict license check. Defaults to '#{options[:strict_license]}'.") do |v|
opts.on('--[no-]strict-license',
"Don't fail on strict license check. Defaults to '#{options[:strict_license]}'.") do |v|
options[:strict_license] = v
end

opts.on('--[no-]fail-on-warnings', "Fail on any warnings. Defaults to '#{options[:fail_on_warnings]}'.") do |v|
options[:fail_on_warnings] = v
end

opts.on('--[no-]strict-puppet-version', "Fail on strict Puppet Version check based on current supported Puppet versions. Defaults to '#{options[:strict_puppet_version]}'.") do |v|
opts.on('--[no-]strict-puppet-version',
"Fail on strict Puppet Version check based on current supported Puppet versions. Defaults to '#{options[:strict_puppet_version]}'.") do |v|

Check failure on line 48 in lib/metadata_json_lint.rb

View workflow job for this annotation

GitHub Actions / rubocop

Layout/LineLength: Line is too long. [153/120] (https://rubystyle.guide#max-line-length)
options[:strict_puppet_version] = v
end

opts.on('-f', '--format FORMAT', %i[text json], 'The format in which results will be output (text, json)') do |format|
opts.on('-f', '--format FORMAT', %i[text json],
'The format in which results will be output (text, json)') do |format|
options[:format] = format
end
end.parse!
Expand Down Expand Up @@ -97,9 +101,7 @@ def parse(metadata)
# From: https://docs.puppetlabs.com/puppet/latest/reference/modules_publishing.html#write-a-metadatajson-file
deprecated_fields = %w[types checksum]
deprecated_fields.each do |field|
unless parsed[field].nil?
error :deprecated_fields, "Deprecated field '#{field}' found in metadata.json."
end
error :deprecated_fields, "Deprecated field '#{field}' found in metadata.json." unless parsed[field].nil?
end

# The nested 'requirements' name of 'pe' is deprecated as well.
Expand All @@ -119,16 +121,14 @@ def parse(metadata)

case options[:format]
when :json
puts JSON.fast_generate(:result => result, :warnings => @warnings, :errors => @errors)
puts JSON.fast_generate(result: result, warnings: @warnings, errors: @errors)
else
@warnings.each { |warn| puts "(WARN) #{warn}" }
@errors.each { |err| puts "(ERROR) #{err}" }
puts result
end

if !@errors.empty? || (!@warnings.empty? && (options[:fail_on_warnings] == true))
return false
end
return false if !@errors.empty? || (!@warnings.empty? && (options[:fail_on_warnings] == true))
end

true
Expand All @@ -151,9 +151,7 @@ def validate_requirements!(requirements)
return unless requirements.is_a?(Array)

requirements.each do |requirement|
if requirement['name'] == 'pe'
warn :requirements, "The 'pe' requirement is no longer supported by the Forge."
end
warn :requirements, "The 'pe' requirement is no longer supported by the Forge." if requirement['name'] == 'pe'

begin
puppet_req = VersionRequirement.new(requirement.fetch('version_requirement', ''))
Expand All @@ -162,7 +160,7 @@ def validate_requirements!(requirements)
error :requirements, "Invalid 'version_requirement' field in metadata.json: #{e}"
end

validate_puppet_ver!(puppet_req) unless puppet_req.instance_variable_get('@requirement').nil?
validate_puppet_ver!(puppet_req) unless puppet_req.instance_variable_get(:@requirement).nil?
end

validate_requirements_unique(requirements)
Expand All @@ -179,17 +177,16 @@ def validate_puppet_ver!(requirement)
end

return unless requirement.mixed_syntax?

warn(:requirement, 'Mixing "x" or "*" version syntax with operators is not recommended in ' \
"metadata.json, use one style in the puppet version: #{requirement.instance_variable_get('@requirement')}")
"metadata.json, use one style in the puppet version: #{requirement.instance_variable_get(:@requirement)}")
end
module_function :validate_puppet_ver!

def validate_dependencies!(deps)
dep_names = []
deps.each do |dep|
if dep_names.include?(dep['name'])
warn :dependencies, "Duplicate dependencies on #{dep['name']}"
end
warn :dependencies, "Duplicate dependencies on #{dep['name']}" if dep_names.include?(dep['name'])
dep_names << dep['name']

begin
Expand All @@ -206,7 +203,7 @@ def validate_dependencies!(deps)
# See https://tickets.puppetlabs.com/browse/PUP-2781
if dep.key?('version_range')
warn :dependencies, "Dependency #{dep['name']} has a 'version_range' attribute " \
'which is no longer used by the forge.'
'which is no longer used by the forge.'
end
end
end
Expand All @@ -217,23 +214,24 @@ def validate_version_requirement!(dep, requirement)
# From: https://docs.puppet.com/puppet/latest/reference/modules_metadata.html#best-practice-set-an-upper-bound-for-dependencies
if options[:strict_dependencies] && requirement.open_ended?
msg = "Dependency #{dep['name']} has an open " \
"ended dependency version requirement #{dep['version_requirement']}"
"ended dependency version requirement #{dep['version_requirement']}"
warn(:dependencies, msg)
end

# Mixing operator and wildcard version syntax
# From: https://docs.puppet.com/puppet/latest/modules_metadata.html#version-specifiers
# Supported in Puppet 5 and higher, but the syntax is unclear and incompatible with older versions
return unless requirement.mixed_syntax?

warn(:dependencies, 'Mixing "x" or "*" version syntax with operators is not recommended in ' \
"metadata.json, use one style in the #{dep['name']} dependency: #{dep['version_requirement']}")
"metadata.json, use one style in the #{dep['name']} dependency: #{dep['version_requirement']}")
end
module_function :validate_version_requirement!

def format_error(check, msg)
case options[:format]
when :json
{ :check => check, :msg => msg }
{ check: check, msg: msg }
else
"#{check}: #{msg}"
end
Expand Down
7 changes: 1 addition & 6 deletions metadata-json-lint.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Gem::Specification.new do |s|

s.files = `git ls-files -z`.split("\x0")
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
s.test_files = s.files.grep(%r{^(tests|spec)/})

s.homepage = 'https://github.com/voxpupuli/metadata-json-lint'
s.license = 'Apache-2.0'
Expand All @@ -20,9 +19,5 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'spdx-licenses', '~> 1.0'
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec'
s.add_development_dependency 'rubocop', '~> 1.28.0'
s.add_development_dependency 'rubocop-performance', '~> 1.10'
s.add_development_dependency 'rubocop-rake', '~> 0.2'
s.add_development_dependency 'rubocop-rspec', '>= 1.44'

s.add_development_dependency 'voxpupuli-rubocop', '~> 1.2'
end
10 changes: 6 additions & 4 deletions spec/metadata_json_lint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,27 @@
context 'with pe' do
let :requirements do
[
{ 'name' => 'pe' }
{ 'name' => 'pe' },
]
end

it do
expect(described_class).to receive('warn').with(:requirements, "The 'pe' requirement is no longer supported by the Forge.")
expect(described_class).to receive('warn').with(:requirements,
"The 'pe' requirement is no longer supported by the Forge.")
expect { described_class.validate_requirements!(requirements) }.not_to raise_error
end
end

context 'with invalid requirement' do
let :requirements do
[
{ 'name' => 'puppet', 'version_requirement' => 'a' }
{ 'name' => 'puppet', 'version_requirement' => 'a' },
]
end

it do
expect(described_class).to receive('error').with(:requirements, "Invalid 'version_requirement' field in metadata.json: Unparsable version range: \"a\"")
expect(described_class).to receive('error').with(:requirements,
"Invalid 'version_requirement' field in metadata.json: Unparsable version range: \"a\"")
expect { described_class.validate_requirements!(requirements) }.not_to raise_error
end
end
Expand Down

0 comments on commit e2c2c6d

Please sign in to comment.