|
1 | 1 | # encoding: UTF-8 |
2 | 2 | # -*- mode: ruby -*- |
3 | 3 | # vi: set ft=ruby : |
4 | | -# Based on magic_shell cookbook code, thanks @sethvargo. |
5 | 4 |
|
6 | | -# More info at https://github.com/jimweirich/rake/blob/master/doc/rakefile.rdoc |
| 5 | +# |
| 6 | +# Available Rake tasks: |
| 7 | +# |
| 8 | +# $ rake -T |
| 9 | +# rake clean # Clean some generated files |
| 10 | +# rake default # Run doc, style, unit and integration tests |
| 11 | +# rake doc # Generate Ruby documentation |
| 12 | +# rake integration # Run Test Kitchen integration tests |
| 13 | +# rake integration:cloud # Run Test Kitchen tests in the cloud |
| 14 | +# rake integration:docker # Run Test Kitchen tests using docker |
| 15 | +# rake integration:vagrant # Run Test Kitchen tests using vagrant |
| 16 | +# rake style # Run all style checks |
| 17 | +# rake style:chef # Run Chef style checks using foodcritic |
| 18 | +# rake style:ruby # Run Ruby style checks using rubocop |
| 19 | +# rake style:ruby:auto_correct # Auto-correct RuboCop offenses |
| 20 | +# rake unit # Run ChefSpec unit tests |
| 21 | +# rake yard # Generate Ruby documentation using yard |
| 22 | +# |
| 23 | +# More info at https://github.com/ruby/rake/blob/master/doc/rakefile.rdoc |
| 24 | +# |
7 | 25 |
|
8 | 26 | require 'bundler/setup' |
9 | 27 |
|
| 28 | +# Checks if we are inside Travis CI. |
| 29 | +# |
| 30 | +# @return [Boolean] whether we are inside Travis CI. |
| 31 | +# @example |
| 32 | +# travis? #=> false |
| 33 | +def travis? |
| 34 | + ENV['TRAVIS'] == 'true' |
| 35 | +end |
| 36 | + |
| 37 | +desc 'Clean some generated files' |
| 38 | +task :clean do |
| 39 | + %w( |
| 40 | + Berksfile.lock |
| 41 | + .bundle |
| 42 | + .cache |
| 43 | + Gemfile.lock |
| 44 | + .kitchen |
| 45 | + metadata.json |
| 46 | + ).each { |f| FileUtils.rm_rf(Dir.glob(f)) } |
| 47 | +end |
| 48 | + |
| 49 | +desc 'Generate Ruby documentation using yard' |
| 50 | +task :yard do |
| 51 | + require 'yard' |
| 52 | + YARD::Rake::YardocTask.new do |t| |
| 53 | + t.stats_options = %w(--list-undoc) |
| 54 | + end |
| 55 | +end |
| 56 | + |
| 57 | +desc 'Generate Ruby documentation' |
| 58 | +task doc: %w(yard) |
| 59 | + |
10 | 60 | namespace :style do |
11 | 61 | require 'rubocop/rake_task' |
12 | | - desc 'Run Ruby style checks' |
| 62 | + desc 'Run Ruby style checks using rubocop' |
13 | 63 | RuboCop::RakeTask.new(:ruby) |
14 | 64 |
|
15 | 65 | require 'foodcritic' |
16 | | - desc 'Run Chef style checks' |
| 66 | + desc 'Run Chef style checks using foodcritic' |
17 | 67 | FoodCritic::Rake::LintTask.new(:chef) |
18 | 68 | end |
19 | 69 |
|
20 | 70 | desc 'Run all style checks' |
21 | 71 | task style: %w(style:chef style:ruby) |
22 | 72 |
|
23 | | -require 'rspec/core/rake_task' |
24 | 73 | desc 'Run ChefSpec unit tests' |
25 | | -RSpec::Core::RakeTask.new(:unit) do |t| |
26 | | - t.rspec_opts = '--color --format progress' |
| 74 | +task :unit do |
| 75 | + require 'rspec/core/rake_task' |
| 76 | + RSpec::Core::RakeTask.new(:unit) do |t| |
| 77 | + t.rspec_opts = '--color --format progress' |
| 78 | + t.pattern = 'spec/**{,/*/**}/*_spec.rb' |
| 79 | + end |
27 | 80 | end |
28 | 81 |
|
29 | | -desc 'Run Test Kitchen integration tests' |
30 | | -task :integration do |
31 | | - require 'kitchen' |
32 | | - Kitchen.logger = Kitchen.default_file_logger |
33 | | - Kitchen::Config.new.instances.each do |instance| |
34 | | - instance.test(:always) |
| 82 | +namespace :integration do |
| 83 | + def kitchen_config(loader_config = {}) |
| 84 | + {}.tap do |config| |
| 85 | + unless loader_config.empty? |
| 86 | + @loader = Kitchen::Loader::YAML.new(loader_config) |
| 87 | + config[:loader] = @loader |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + def run_kitchen(loader_config = {}) |
| 93 | + require 'kitchen' |
| 94 | + Kitchen.logger = Kitchen.default_file_logger |
| 95 | + config = kitchen_config(loader_config) |
| 96 | + Kitchen::Config.new(config).instances.each { |i| i.test(:always) } |
| 97 | + end |
| 98 | + |
| 99 | + desc 'Run Test Kitchen integration tests using vagrant' |
| 100 | + task :vagrant do |
| 101 | + run_kitchen |
35 | 102 | end |
36 | | -end |
37 | 103 |
|
38 | | -namespace :travis do |
39 | | - desc 'Run tests on Travis' |
40 | | - task ci: %w(style unit) |
| 104 | + desc 'Run Test Kitchen integration tests using docker' |
| 105 | + task :docker do |
| 106 | + run_kitchen(local_config: '.kitchen.docker.yml') |
| 107 | + end |
| 108 | + |
| 109 | + desc 'Run Test Kitchen integration tests in the cloud' |
| 110 | + task :cloud do |
| 111 | + run_kitchen(local_config: '.kitchen.cloud.yml') |
| 112 | + end |
41 | 113 | end |
42 | 114 |
|
43 | | -task default: %w(style unit integration) |
| 115 | +desc 'Run Test Kitchen integration tests' |
| 116 | +task integration: travis? ? %w(integration:docker) : %w(integration:vagrant) |
| 117 | + |
| 118 | +desc 'Run doc, style, unit and integration tests' |
| 119 | +task default: %w(doc style unit integration) |
0 commit comments