From df3c89075bb407d8e96d7aef44e16a33c89fce78 Mon Sep 17 00:00:00 2001 From: Marcin Sawicki Date: Sat, 15 Nov 2014 09:11:27 -0800 Subject: [PATCH] FC054, check for mismatched cookbook names --- ...ld_match_cookbook_dir_name_in_metadata.feature | 15 +++++++++++++++ features/step_definitions/cookbook_steps.rb | 9 +++++++++ features/support/command_helpers.rb | 1 + lib/foodcritic/rules.rb | 10 ++++++++++ 4 files changed, 35 insertions(+) create mode 100644 features/054_name_should_match_cookbook_dir_name_in_metadata.feature diff --git a/features/054_name_should_match_cookbook_dir_name_in_metadata.feature b/features/054_name_should_match_cookbook_dir_name_in_metadata.feature new file mode 100644 index 00000000..d5ae0484 --- /dev/null +++ b/features/054_name_should_match_cookbook_dir_name_in_metadata.feature @@ -0,0 +1,15 @@ +Feature: Name should match cookbook dir name in metadata + + In order to avoid complications where a cookbook repository name differs from the cookbook name + As a developer + I want to set the cookbook name within the metadata + + Scenario: Name mismatched in metadata + Given a cookbook with metadata that includes a mismatched cookbook name + When I check the cookbook + Then the name should match cookbook dir name warning 054 should be displayed against the metadata file + + Scenario: Name mismatched in metadata + Given a cookbook with metadata that includes a matched cookbook name + When I check the cookbook + Then the name should match cookbook dir name warning 054 should not be displayed against the metadata file diff --git a/features/step_definitions/cookbook_steps.rb b/features/step_definitions/cookbook_steps.rb index 59154e97..69a2ce74 100644 --- a/features/step_definitions/cookbook_steps.rb +++ b/features/step_definitions/cookbook_steps.rb @@ -1321,6 +1321,15 @@ def in_tier?(*tier) } end +Given /^a cookbook with metadata that includes a (matched|mismatched) cookbook name$/ do |match| + write_metadata %Q{ + name 'example' + } if match == 'matched' + write_metadata %Q{ + name 'bogart' + } if match == 'mismatched' +end + Given /^a directory that contains a role file ([^ ]+) in (json|ruby) that defines role name (.*)$/ do |file_name, format, role_name| role(:role_name => %Q{"#{role_name}"}, :file_name => file_name, :format => format.to_sym) end diff --git a/features/support/command_helpers.rb b/features/support/command_helpers.rb index 45942f6a..2665b6d8 100644 --- a/features/support/command_helpers.rb +++ b/features/support/command_helpers.rb @@ -64,6 +64,7 @@ def assertions 'FC051' => 'Template partials loop indefinitely', 'FC052' => 'Metadata uses the unimplemented "suggests" keyword', 'FC053' => 'Metadata uses the unimplemented "recommends" keyword', + 'FC054' => 'Name should match cookbook dir name in metadata', 'FCTEST001' => 'Test Rule' } diff --git a/lib/foodcritic/rules.rb b/lib/foodcritic/rules.rb index b6a469ca..2418d9ec 100644 --- a/lib/foodcritic/rules.rb +++ b/lib/foodcritic/rules.rb @@ -769,3 +769,13 @@ def invalid_name(ast) ast.xpath(%Q(//command[ident/@value='recommends'])) end end + +rule 'FC054', 'Name should match cookbook dir name in metadata' do + tags %w(annoyances metadata) + applies_to { |version| version >= gem_version('12.0.0') } + metadata do |ast, filename| + unless cookbook_name(filename) == filename.split(File::SEPARATOR)[-2] + [file_match(filename)] + end + end +end