Skip to content
This repository was archived by the owner on Dec 24, 2021. It is now read-only.

Commit e977093

Browse files
Version bump v1.4.0
- Destroys all generated cookbooks after each test - Fixes missing dependencies - Fixes tons of rubocop errors - Centralizes "good" cookbook layout definition to one spot - Refactors tests to start from "good" layout - More resilient regex for detecting a date in comments - Skip checking for comments in `metadata.rb` - Calls `cookstyle` directly instead of `rubocop --require cookstyle` - Checks for copyright, date, and license in header comment block - Adds support for detecting if license is BSD 3-clause - Reads `.foodcritic` file, passing through directives
1 parent 7f3d55b commit e977093

29 files changed

+385
-341
lines changed

.rubocop.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,22 @@ Metrics/MethodLength:
2929
Enabled: false
3030

3131
Metrics/PerceivedComplexity:
32-
Max: 10
32+
Max: 12
3333

34-
Style/BlockDelimiters:
34+
Naming/FileName:
3535
Exclude:
36-
- 'spec/**/*_spec.rb'
36+
- '.simplecov'
3737

38-
Style/FileName:
38+
Style/BlockDelimiters:
3939
Exclude:
40-
- '.simplecov'
40+
- 'spec/**/*_spec.rb'
4141

42+
# These two are for supporting usage of the `paint` gem
4243
Style/FormatString:
4344
Enabled: false
4445

46+
Style/FormatStringToken:
47+
Enabled: false
48+
4549
Style/FrozenStringLiteralComment:
4650
Enabled: false

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@
33
We follow [Semantic Versioning](http://semver.org/) as a way of measuring stability of an update. This
44
means we will never make a backwards-incompatible change within a major version of the project.
55

6-
## _[UNRELEASED]_
6+
## v1.4.0 (2018-09-15)
77

88
- Adds Simplecov for measuring test coverage
99
- Increases test coverage
1010
- [BUG] Spelling inconsistencies (`Pass` -> `Passed`, `Fail` -> `Failed`, `Foodcritic` -> `FoodCritic`)
11+
- Centralizes generating test data for a cookbook (or monolithic repository) to one spot
12+
- Fixes tons of rubocop errors
13+
- [BUG] Undeclared dependency: `foodcritic`
14+
- Upgrades dependencies to be based on what is available as part of ChefDK v3.1.0
15+
- [BUG] Processes `*.rb` files in non-profile InSpec directory, when InSpec only processes `*_test.rb`
16+
- Adds support for detecting BSD licenses
17+
- Supports reading `.foodcritic` file for rule skips
1118

1219
## v1.3.2 (2017-06-08)
1320

Rakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ task :_guard_against_focus do
3333
abort "PRECONDITION FAILURE:\n\t- #{errors.join("\n\t- ")}" unless errors.empty?
3434
end
3535

36-
task ci: %w(_guard_against_focus spec)
36+
task ci: %w[_guard_against_focus spec]
3737

38-
task default: %w(spec)
38+
task default: %w[spec]

lib/rordan_gramsay/chef_tasks/lint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Meta-task for all linting tasks
44
namespace :lint do
5-
task all: %w(lint:rubocop lint:foodcritic lint:comments lint:tests lint:kitchen)
5+
task all: %w[lint:rubocop lint:foodcritic lint:comments lint:tests lint:kitchen]
66
end
77
desc 'Lints cookbook using all tools'
88
task lint: ['lint:all']

lib/rordan_gramsay/chef_tasks/lint/foodcritic.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
task :foodcritic do
88
puts Paint['Inspecting files with FoodCritic', :yellow, :bold]
99

10+
tags = if File.exist?('.foodcritic')
11+
File.read('.foodcritic').lines.map(&:strip)
12+
else
13+
%w[~FC001 ~FC003 ~FC019 ~FC023 ~FC064 ~FC065 ~FC066 ~license]
14+
end
15+
1016
task_obj = RordanGramsay::Lint::Foodcritic.new(
1117
fail_tags: ['any'],
12-
tags: %w(~FC001 ~FC003 ~FC019 ~FC023 ~FC064 ~FC065 ~FC066 ~license)
18+
tags: tags
1319
)
1420

1521
task_obj.call

lib/rordan_gramsay/chef_tasks/master_repo.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def run_command(cmd, log)
1818
# Finished giving output or otherwise a broken pipe
1919
end
2020
end
21+
# rubocop:enable Lint/HandleExceptions
2122

2223
log.info 'Cleaning up...'
2324
end

lib/rordan_gramsay/cli.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def call
2727
when 'init'
2828
action_init!
2929
else
30-
$stderr.puts "Unknown action: #{@action.inspect}"
31-
$stderr.puts @parser
30+
warn "Unknown action: #{@action.inspect}"
31+
warn @parser
3232
end
3333
@action_status = :complete
3434
end
@@ -42,20 +42,20 @@ def action_init!
4242
when /(?:chef[_-])?(?:master|server)[_-]?rakefile$/i
4343
init_master_rakefile!
4444
else
45-
$stderr.puts "Unknown subject: #{@subject.inspect}"
46-
$stderr.puts @parser
45+
warn "Unknown subject: #{@subject.inspect}"
46+
warn @parser
4747
end
4848
end
4949

5050
def cookbook_rakefile_contents
51-
<<-EOF
52-
require '#{GEM_NAME}/chef_tasks/kitchen'
53-
require '#{GEM_NAME}/chef_tasks/lint'
54-
require '#{GEM_NAME}/chef_tasks/test'
55-
56-
task default: ['lint']
57-
# Further rake tasks go here
58-
EOF
51+
<<~RAKEFILE
52+
require '#{GEM_NAME}/chef_tasks/kitchen'
53+
require '#{GEM_NAME}/chef_tasks/lint'
54+
require '#{GEM_NAME}/chef_tasks/test'
55+
56+
task default: ['lint']
57+
# Further rake tasks go here
58+
RAKEFILE
5959
end
6060

6161
def init_rakefile!
@@ -71,9 +71,9 @@ def init_rakefile!
7171
end
7272

7373
def master_rakefile_contents
74-
<<-EOF
75-
require '#{GEM_NAME}/chef_tasks/master_repo'
76-
EOF
74+
<<~RAKEFILE
75+
require '#{GEM_NAME}/chef_tasks/master_repo'
76+
RAKEFILE
7777
end
7878

7979
def init_master_rakefile!

lib/rordan_gramsay/lint/cookbook_comments_checker.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class CookbookFile
1616
def initialize(file_name)
1717
@file_name = file_name
1818
# Limit read to first 30 lines of file
19-
@lines = File.foreach(file_name).first(30)
19+
@lines = ::File.foreach(file_name).first(30)
2020
end
2121

2222
# Detects if authorship line is included
@@ -35,7 +35,7 @@ def author?
3535
def copyright?
3636
@copyright ||= @lines.any? do |line|
3737
break true if line =~ /(?:^| )\(c\)(?:$| )/i # copyright symbol as "(c)"
38-
break true if line.includes? '©' # Actual copyright symbol
38+
break true if line.include? '©' # Actual copyright symbol
3939
break true if line =~ /copyright/i
4040
break true if line =~ /&copy;/i # HTML entity is still indicative of copyright
4141

@@ -46,10 +46,11 @@ def copyright?
4646
# Detects if there is a date (e.g., copyright date or date of authorship) included
4747
def date?
4848
@date ||= @lines.any? do |line|
49-
break true if line =~ /(?:mon(?:day)?|tue(?:s(?:day)?)?|wed(?:nes(?:day)?)?|thu(?:rs(?:day)?)?|fri(?:day)?|sat(?:urday)?|sun(?:day)?)/i
50-
break true if line =~ %r{(?:/|-|\(| |^)20[012][0-9](?:/|-|\)| |$)}i # some year between 2000 and 2029
49+
break true if line =~ %r{(?:/|-|\(| |^)(?:20|19)[0-9]{2}(?:/|-|\)| |$)}i # some year between 1900 and 2099
5150
# Similar to ISO date format (e.g., 2017-03-14, 2010/11/19, 2001/9/3)
52-
break true if line =~ %r{(?<year>(?:20|19)[0-9][0-9])[/-](?<month>0?[1-9]|1[0-2])[/-](?<day>0?[1-9]|[1,2][0-9]|3[01])}i
51+
break true if line =~ %r{(?<year>(?:20|19)[0-9]{2})[/-](?<month>0?[1-9]|1[0-2])[/-](?<day>0?[1-9]|[1,2][0-9]|3[01])}i
52+
# Just a given year
53+
break true if line =~ /\b(?:20|19)[0-9]{2}\b/i
5354

5455
false
5556
end
@@ -62,6 +63,7 @@ def license?
6263
break true if line =~ /all rights(?: reserved)?/i # "All rights reserved"
6364

6465
# Popular licenses
66+
break true if line =~ /(?:^|\W)\(?BSD(?:v?3)\)?(?:$|\W)/ # "BSD", "BSD3", "BSDv3" or wrapping any of those in parentheses
6567
break true if line =~ /(?:^|\W)\(?MIT\)?(?:$|\W)/ # "MIT" or "(MIT)"
6668
break true if line =~ /(?:^|\W)Apache(?:$|\W)/i # "Apache"
6769
break true if line =~ /(?:^|\W)Creative Commons(?:$|\W)/i
@@ -90,9 +92,9 @@ def call
9092
@files.each do |file|
9193
errors = []
9294
# errors << :author unless file.author?
93-
# errors << :license unless file.license?
94-
# errors << :copyright unless file.copyright?
95-
# errors << :date unless file.date?
95+
errors << :license unless file.license?
96+
errors << :copyright unless file.copyright?
97+
errors << :date unless file.date?
9698
errors << :comments_at_top_of_file unless file.leading_comment_lines?
9799

98100
# Compile the error message
@@ -117,7 +119,7 @@ def error_count
117119
end
118120

119121
def files
120-
Dir.glob("#{base_dir}/{attributes,recipes,libraries}/**/*.rb")
122+
::Dir.glob(::File.join(base_dir, '{attributes,recipes,resources,libraries}', '**', '*.rb'))
121123
end
122124

123125
private

lib/rordan_gramsay/lint/foodcritic.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def rules
8989
def default_opts
9090
{
9191
cookbook_paths: [Dir.pwd],
92-
exclude_paths: %w(test/**/* spec/**/* features/**/*),
92+
exclude_paths: %w[test/**/* spec/**/* features/**/*],
9393
fail_tags: ['any'],
9494
tags: []
9595
}

lib/rordan_gramsay/lint/kitchen_checker.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def inspec?
2626
# Just in case default for the box is not set to headless,
2727
# all Windows VMs should have "gui: false" by default
2828
def no_windows_gui?
29-
@inspec ||= begin
29+
@no_windows_gui ||= begin
3030
if @lines.any? { |line| line =~ /winrm/i }
3131
@lines.any? { |line| line =~ /^.*(?<!#).*\s+gui:\s+(?:false|no?|off)\s*$/i }
3232
else

0 commit comments

Comments
 (0)