-
Notifications
You must be signed in to change notification settings - Fork 3
/
Rakefile
145 lines (127 loc) · 4.42 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# encoding: UTF-8
# -*- mode: ruby -*-
# vi: set ft=ruby :
#
# Available Rake tasks:
#
# $ rake -T
# rake clean # Clean some generated files
# rake default # Run doc, style, unit and integration tests
# rake doc # Generate Ruby documentation
# rake integration[regexp,action] # Run Test Kitchen integration tests
# rake integration:cloud[regexp,action] # Run Kitchen tests in the cloud
# rake integration:docker[regexp,action] # Run Kitchen tests using docker
# rake integration:vagrant[regexp,action] # Run Kitchen tests using vagrant
# rake style # Run all style checks
# rake style:chef # Run Chef style checks using foodcritic
# rake style:ruby # Run Ruby style checks using rubocop
# rake style:ruby:auto_correct # Auto-correct RuboCop offenses
# rake unit # Run ChefSpec unit tests
# rake yard # Generate Ruby documentation using yard
#
# More info at https://github.com/ruby/rake/blob/master/doc/rakefile.rdoc
#
require 'bundler/setup'
# Checks if we are inside Travis CI.
#
# @return [Boolean] whether we are inside Travis CI.
# @example
# travis? #=> false
def travis?
ENV['TRAVIS'] == 'true'
end
desc 'Clean some generated files'
task :clean do
%w(
Berksfile.lock
.bundle
.cache
coverage
Gemfile.lock
.kitchen
metadata.json
vendor
).each { |f| FileUtils.rm_rf(Dir.glob(f)) }
end
desc 'Generate Ruby documentation using yard'
task :yard do
require 'yard'
YARD::Rake::YardocTask.new do |t|
t.stats_options = %w(--list-undoc)
end
end
desc 'Generate Ruby documentation'
task doc: %w(yard)
namespace :style do
require 'rubocop/rake_task'
desc 'Run Ruby style checks using rubocop'
RuboCop::RakeTask.new(:ruby)
require 'foodcritic'
desc 'Run Chef style checks using foodcritic'
FoodCritic::Rake::LintTask.new(:chef)
end
desc 'Run all style checks'
task style: %w(style:chef style:ruby)
desc 'Run ChefSpec unit tests'
task :unit do
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:unit) do |t|
t.rspec_opts = '--color --format progress'
t.pattern = 'test/unit/**{,/*/**}/*_spec.rb'
end
end
desc 'Run Test Kitchen integration tests'
namespace :integration do
# Generates the `Kitchen::Config` class configuration values.
#
# @param loader_config [Hash] loader configuration options.
# @return [Hash] configuration values for the `Kitchen::Config` class.
def kitchen_config(loader_config = {})
{}.tap do |config|
unless loader_config.empty?
@loader = Kitchen::Loader::YAML.new(loader_config)
config[:loader] = @loader
end
end
end
# Gets a collection of instances.
#
# @param regexp [String] regular expression to match against instance names.
# @param config [Hash] configuration values for the `Kitchen::Config` class.
# @return [Collection<Instance>] all instances.
def kitchen_instances(regexp, config)
instances = Kitchen::Config.new(config).instances
return instances if regexp.nil? || regexp == 'all'
instances.get_all(Regexp.new(regexp))
end
# Runs a test kitchen action against some instances.
#
# @param action [String] kitchen action to run (defaults to `'test'`).
# @param regexp [String] regular expression to match against instance names.
# @param loader_config [Hash] loader configuration options.
# @return void
def run_kitchen(action, regexp, loader_config = {})
action = 'test' if action.nil?
require 'kitchen'
Kitchen.logger = Kitchen.default_file_logger
config = kitchen_config(loader_config)
kitchen_instances(regexp, config).each { |i| i.send(action) }
end
desc 'Run Test Kitchen integration tests using vagrant'
task :vagrant, [:regexp, :action] do |_t, args|
run_kitchen(args.action, args.regexp)
end
desc 'Run Test Kitchen integration tests using docker'
task :docker, [:regexp, :action] do |_t, args|
run_kitchen(args.action, args.regexp, local_config: '.kitchen.docker.yml')
end
desc 'Run Test Kitchen integration tests in the cloud'
task :cloud, [:regexp, :action] do |_t, args|
run_kitchen(args.action, args.regexp, local_config: '.kitchen.cloud.yml')
end
end
desc 'Run Test Kitchen integration tests'
task :integration, [:regexp, :action] =>
travis? ? %w(integration:docker) : %w(integration:vagrant)
desc 'Run doc, style, unit and integration tests'
task default: %w(doc style unit integration)