Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ChefCorrectness/InvalidVersionMetadata cop #309

Merged
merged 2 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ ChefCorrectness/EmptyMetadataField:
Include:
- '**/metadata.rb'

ChefCorrectness/InvalidVersionMetadata:
Description: Cookbook metadata.rb version field should follow X.Y.Z version format.
Enabled: true
VersionAdded: '5.8.0'
Include:
- '**/metadata.rb'

###############################
# ChefDeprecations: Resolving Deprecations that block upgrading Chef Infra Client
###############################
Expand Down
48 changes: 48 additions & 0 deletions lib/rubocop/cop/chef/correctness/invalid_version_metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# Copyright:: 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

module RuboCop
module Cop
module Chef
module ChefCorrectness
# Cookbook metadata.rb version field should follow X.Y.Z version format.
#
# @example
#
# # bad
# version '1.2.3.4'
#
# # good
# version '1.2.3'
#
class InvalidVersionMetadata < Cop
MSG = 'Cookbook metadata.rb version field should follow X.Y.Z version format.'.freeze

def_node_matcher :version?, '(send nil? :version $str ...)'

def on_send(node)
version?(node) do |ver|
if ver.value !~ /\A\d+\.\d+(\.\d+)?\z/ # entirely borrowed from Foodcritic.
add_offense(ver, location: :expression, message: MSG, severity: :refactor)
end
end
end
end
end
end
end
end
34 changes: 34 additions & 0 deletions spec/rubocop/cop/chef/correctness/invalid_version_metadata_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# Copyright:: 2019, Chef Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'

describe RuboCop::Cop::Chef::ChefCorrectness::InvalidVersionMetadata, :config do
subject(:cop) { described_class.new(config) }

it 'registers an offense when a cookbook sets has an invalid version in metadata.rb' do
expect_offense(<<~RUBY)
version '1.2.3.4'
tas50 marked this conversation as resolved.
Show resolved Hide resolved
^^^^^^^^^ Cookbook metadata.rb version field should follow X.Y.Z version format.
RUBY
end

it "doesn't register an offense with a valid version in metadata.rb" do
expect_no_offenses(<<~RUBY)
version '1.2.3'
RUBY
end
end