forked from drrb/puppet-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
330 lines (285 loc) · 10 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# -*- encoding: utf-8 -*-
# Puppet Library
# Copyright (C) 2014 drrb
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'json'
require 'yaml'
require 'net/http'
SUPPORTED_RUBY_VERSIONS = %w[1.8 1.9 2.0 2.1 system]
# Capybara needs Nokogiri, which needs 1.9+
ACCEPTANCE_TEST_INCOMPATIBLE_RUBY_VERSIONS = %w[1.8]
class String
def green
"\e[32m#{self}\e[0m"
end
def red
"\e[31m#{self}\e[0m"
end
def yellow
"\e[33m#{self}\e[0m"
end
end
def ruby_version_supports_acceptance_tests?(version = RUBY_VERSION)
! ACCEPTANCE_TEST_INCOMPATIBLE_RUBY_VERSIONS.find do |bad_version|
version.start_with? bad_version
end
end
def offline?
Net::HTTP.get_response(URI.parse("https://forgeapi.puppetlabs.com"))
return false
rescue
return true
end
if RUBY_VERSION.start_with? "1.8"
namespace :coveralls do
task :push do
puts "Skipping Coverage pushing because this Ruby version doesn't support it"
end
end
else
require 'coveralls/rake/task'
Coveralls::RakeTask.new
end
desc "Run the specs"
RSpec::Core::RakeTask.new(:spec)
if ruby_version_supports_acceptance_tests?
require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:features)
else
task :features do
puts "Skipping acceptance tests because this version of Ruby doesn't support them"
end
end
desc "Run all the tests"
RSpec::Core::RakeTask.new(:test) do |rspec|
rspec.pattern = "{spec,test}/**/*_{spec,integration_test}.rb"
end
task :test => :features
desc "Run the integration tests"
RSpec::Core::RakeTask.new(:integration_test) do |rspec|
rspec.pattern = "test/**/*_integration_test.rb"
tags = []
tags << "~online" if offline?
tags << "~no_1_8" if RUBY_VERSION.start_with? "1.8"
unless tags.empty?
rspec.rspec_opts = tags.map { |tag| "--tag #{tag}" }.join(" ")
end
end
task :default => [:test, 'coveralls:push']
desc "Check it works on all local rubies"
task :verify => [ 'check-license', 'test-imports' ] do
versions = SUPPORTED_RUBY_VERSIONS
puts "\nRunning Specs".green
spec_results = versions.map do |ruby_version|
puts "\n- Ruby #{ruby_version}".green
system "rvm #{ruby_version} do bundle exec rake spec"
end
puts "\nRunning Integration Tests".green
integration_test_results = versions.map do |ruby_version|
puts "\n- Ruby #{ruby_version}".green
system "rvm #{ruby_version} do bundle exec rake integration_test"
end
puts "\nRunning Acceptance Tests".green
acceptance_test_results = versions.map do |ruby_version|
puts "\n- Ruby #{ruby_version}".green
system "rvm #{ruby_version} do bundle exec rake features"
end
gem_versions = versions.map do |ruby_version|
`rvm #{ruby_version} do gem --version`.strip
end
puts "\nResults:\n".green
results = [ gem_versions, spec_results, integration_test_results, acceptance_test_results ].transpose
puts "+---------+----------+-------+-------+-------+"
puts "| Version | Gem Ver. | Specs | ITs | ATs |"
puts "+---------+----------+-------+-------+-------+"
versions.zip(results).each do |(version, (gem_version, spec_result, integration_test_result, acceptance_test_result))|
v = version.ljust(7)
g = gem_version.ljust(8)
s = spec_result ? "pass".green : "fail".red
i = integration_test_result ? "pass".green : "fail".red
if ruby_version_supports_acceptance_tests? version
a = acceptance_test_result ? "pass".green : "fail".red
else
a = "skip".yellow
end
puts "| #{v} | #{g} | #{s} | #{i} | #{a} |"
end
puts "+---------+----------+-------+-------+-------+"
versions.zip(results).each do |(version, (gem_version, spec_result, integration_test_result, acceptance_test_result))|
unless spec_result
fail "Specs failed with Ruby #{version}"
end
unless integration_test_result
fail "Integration tests failed with Ruby #{version}"
end
unless acceptance_test_result
fail "Acceptance tests failed with Ruby #{version}"
end
end
end
desc "Run simple checks"
task :check => [ "check-license", "test-imports" ]do
end
desc "Check all files for license headers"
task "check-license" do
print "Checking that all program files contain license headers"
files = `git ls-files`.split "\n"
ignored_files = File.read(".licenseignore").split("\n") << ".licenseignore"
offending_files = files.reject { |file| File.read(file).include? "WITHOUT ANY WARRANTY" } - ignored_files
ok = offending_files.empty?
result = if ok
"OK".green
else
"FAIL".red
end
puts " [ #{result} ]"
abort("ERROR: THE FOLLOWING FILES HAVE NO LICENSE HEADERS: \n" + offending_files.join("\n")) unless ok
end
desc "Print the version number"
task "version" do
puts PuppetLibrary::VERSION
end
desc "Release Puppet Library"
task "push-release" => ["check-license", :verify] do
puts "Releasing #{PuppetLibrary::VERSION}"
Rake::Task[:release].invoke
upload_release_notes(PuppetLibrary::VERSION)
increment_version
git_push
end
desc "Increment version number and commit it"
task "increment-version" do
increment_version
end
desc "Register release with Github"
task "register-release", [:version] do |task, args|
upload_release_notes(args[:version])
end
desc "Import files individually to make sure they can be imported externally"
task "test-imports" do
puts "Testing imports:"
Dir.chdir "lib" do
paths = Dir["**/*.rb"].map {|f| f.sub /\.rb$/, "" }
paths = paths.sort_by {|p| p.count "/"}
errors = []
paths.each do |path|
print "importing #{path}..."
output = `bundle exec ruby -e '$LOAD_PATH.unshift(File.expand_path(".")); require "#{path}"' 2>&1`
print " ["
if $?.success?
print "OK".green
else
print "FAIL".red
errors << "#{path}: #{output.red}"
end
puts "]"
end
unless errors.empty?
puts
puts
fail <<-EOFAILURE
Failed to import some files:
#{errors.join "\n\n"}
EOFAILURE
end
end
end
def upload_release_notes(version)
puts "Registering release notes for #{version}"
github = Github.new
unless version =~ /\d+\.\d+\.\d+/
raise "Bad version: '#{version}'"
end
unless system("git tag | grep v#{version} > /dev/null")
raise "Couldn't find tag 'v#{version}'"
end
tag = "v#{version}"
changelog = YAML.load_file(File.expand_path("CHANGELOG.yml", File.dirname(__FILE__)))
release_notes = changelog.find {|release| release["tag"] == tag}
changes = release_notes ? release_notes["changes"] : []
description = changes.map { |change| "- #{change}" }.join("\n")
data = {
"tag_name" => tag,
"target_commitish" => "master",
"name" => "Version #{version}",
"body" => description,
"draft" => false,
"prerelease" => false
}
release = github.get("/repos/drrb/puppet-library/releases").find {|release| release["tag_name"] == tag}
if release
puts "Release #{tag} exists. Updating it..."
github.patch("/repos/drrb/puppet-library/releases/#{release['id']}", data)
else
puts "Creating release #{tag}..."
github.post("/repos/drrb/puppet-library/releases", data)
end
puts "Done"
end
def git_push
system "git push" or fail "Couldn't push release"
end
def increment_version
major, minor, patch = PuppetLibrary::VERSION.split(".").map {|n| n.to_i}
new_version = "#{major}.#{minor + 1}.0"
puts "Updating version number to #{new_version}"
system(%q[sed -i '' -E 's/VERSION = ".*"/VERSION = "] + new_version + %q["/' lib/puppet_library/version.rb]) or fail "Couldn't update version"
PuppetLibrary::VERSION.replace new_version
system "git commit lib/puppet_library/version.rb --message='[release] Incremented version number'" or fail "Couldn't commit new version number"
end
class Github
def initialize
@base_url = "https://api.github.com"
api_key_file = File.expand_path("~/.github/release.apikey")
raise "Put your github credentials in ~/.github/release.apikey as 'username:key'" unless File.exist? api_key_file
@username, @key = File.read(api_key_file).strip.split ":"
end
def post(path, data, &block)
puts "POST #{path}"
request = Net::HTTP::Post.new(path)
request.body = data.to_json
send request
end
def patch(path, data, &block)
puts "PATCH #{path}"
request = Net::HTTP::Patch.new(path)
request.body = data.to_json
response = send(request, &block)
end
def get(path, &block)
puts "GET #{path}"
request = Net::HTTP::Get.new(path)
send request
end
def send(request)
request.add_field('Content-Type', 'application/json')
request.basic_auth @username, @key
uri = URI.parse(@base_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.request(request)
puts response.code
if block_given?
yield(response)
else
unless response.code =~ /^2../
raise "Request returned non-success:\n#{response.body}"
end
JSON.parse(response.body)
end
end
end