Skip to content

Instantly share code, notes, and snippets.

@theckman
Created August 15, 2016 23:07
Show Gist options
  • Save theckman/bba9a3b3d20bcffa6e6259eac3aee5e4 to your computer and use it in GitHub Desktop.
Save theckman/bba9a3b3d20bcffa6e6259eac3aee5e4 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require 'kitchen'
require 'foodcritic'
require 'rubocop/rake_task'
require 'jsonlint/rake_task'
require 'yamllint/rake_task'
require 'rspec/core/rake_task'
# set up test-kitchen
# this basically duplicates kitchen/rake_task but
# adds the test_base_path option (we should patch test-kitchen)
# this lets us put the test-kitchen files in `spec/kitchen/`
namespace :kitchen do
Kitchen.logger = Kitchen.default_file_logger(nil, false)
loader = Kitchen::Loader::YAML.new(
project_config: ENV['KITCHEN_YAML'],
local_config: ENV['KITCHEN_LOCAL_YAML'],
global_config: ENV['KITCHEN_GLOBAL_YAML']
)
chef_repo_path = File.expand_path(File.dirname(__FILE__))
config = Kitchen::Config.new(
loader: loader,
test_base_path: File.join(chef_repo_path, 'spec/kitchen')
)
# generate the tasks
config.instances.each do |instance|
desc "Run #{instance.name} test instance"
task instance.name do
instance.test(:always)
end
end
desc 'Run all test instances'
task 'all' => config.instances.map(&:name)
end
desc 'Run RSpec/ChefSpec tests'
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = '--profile'
t.pattern = %w(site_cookbooks/**/spec/*_spec.rb)
end
desc 'Run all linters'
task :linters do
Rake::Task['rubocop'].invoke
Rake::Task['foodcritic'].invoke
Rake::Task['jsonlint'].invoke
Rake::Task['yamllint'].invoke
end
desc 'Run RuboCop'
RuboCop::RakeTask.new(:rubocop) do |t|
t.options = %w(-D)
t.fail_on_error = true
t.patterns = %w(
Rakefile Berksfile Gemfile
site-cookbooks/**/*.rb spec/**/*.rb test/**/*.rb
)
end
desc 'Run FoodCritic'
FoodCritic::Rake::LintTask.new do |t|
t.options = {
cookbook_paths: 'site_cookbooks/',
role_paths: 'roles/',
environment_paths: 'environments/',
fail_tags: %w(any),
tags: [
'~solo', # disable chef-solo specific checks
'~readme', # disable all readme checks
'~FC023', # disable 'Prefer conditional attributes' check
'~FC064', # disable 'Ensure issues_url is set in metadata' check
'~FC065' # disable 'Ensure source_url is set in metadata' check
]
}
end
desc 'Run linter on JSON files'
task :jsonlint do
puts 'Running JsonLint...'
JsonLint::RakeTask.new do |t|
t.paths = %w(
data_bags/**/*.json
environments/**/*.json
roles/**/*.json
)
end
end
desc 'Run linter on YAML files'
YamlLint::RakeTask.new do |t|
t.paths = %w(
environments/common/*.yml
site_cookbooks/**/*.yml
)
end
namespace :spec do
desc 'Run RSpec/CheckSpec and TestKitchen tests'
task :test do
Rake::Task['spec'].invoke
Rake::Task['kitchen:all'].invoke
end
desc 'Run all tests (including functional tests and linters)'
task :all do
Rake::Task['linters'].invoke
Rake::Task['spec:test'].invoke
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment