Farmbot-Web-App/lib/tasks/coverage.rake

73 lines
2.4 KiB
Ruby
Raw Normal View History

2018-11-05 18:19:29 -07:00
COVERAGE_FILE_PATH = "./coverage_fe/index.html"
2018-11-05 16:20:35 -07:00
THRESHOLD = 0.001
REPO_URL = "https://api.github.com/repos/Farmbot/Farmbot-Web-App/git"\
"/refs/heads/staging"
2018-11-05 18:19:29 -07:00
CURRENT_COMMIT = ENV.fetch("CIRCLE_SHA1", "")
2018-11-05 16:20:35 -07:00
CSS_SELECTOR = ".fraction"
FRACTION_DELIM = "/"
# Fetch JSON over HTTP. Rails probably already has a helper for this :shrug:
def open_json(url)
JSON.parse(open(url).read)
end
2018-11-06 11:11:58 -07:00
def to_percent(pair)
return ((pair.head / pair.tail) * 100).round(4)
end
2018-11-05 16:20:35 -07:00
namespace :coverage do
desc "Coveralls stats stopped working :("
task run: :environment do
2018-11-06 11:11:58 -07:00
statements, branches, functions, lines = Nokogiri::HTML(open(COVERAGE_FILE_PATH))
2018-11-05 16:20:35 -07:00
.css(CSS_SELECTOR)
.map(&:text)
.map { |x| x.split(FRACTION_DELIM).map(&:to_f) }
.map { |x| Pair.new(*x) }
2018-11-06 11:11:58 -07:00
puts
puts "This build: #{CURRENT_COMMIT}"
puts "Statements: #{to_percent(statements)}%"
puts "Branches: #{to_percent(branches)}%"
puts "Functions: #{to_percent(functions)}%"
puts "Lines: #{to_percent(lines)}%"
puts
2018-11-05 18:19:29 -07:00
covered = lines.head + branches.head
total = lines.tail + branches.tail
build_percent = (covered / total) * 100
2018-11-05 16:20:35 -07:00
latest_commit_staging = open_json(REPO_URL).dig("object", "sha")
2018-11-06 11:11:58 -07:00
puts "staging: #{latest_commit_staging}"
2018-11-05 16:20:35 -07:00
build_url = "https://coveralls.io/builds/#{latest_commit_staging}.json"
2018-12-05 18:25:26 -07:00
any_build_url = "https://coveralls.io/github/FarmBot/Farmbot-Web-App.json"
2018-11-05 18:19:29 -07:00
begin
2018-12-05 18:25:26 -07:00
staging_percent = open_json(build_url).fetch("covered_percent") ||
open_json(any_build_url).fetch("covered_percent")
2018-11-05 18:19:29 -07:00
rescue OpenURI::HTTPError => exception
puts exception.message
2018-11-13 17:32:59 -07:00
puts "Error getting coveralls data."
puts "Wait for staging build to finish and try again."
puts "If error continues, perhaps a blinky test failed the staging build."
2018-11-05 18:19:29 -07:00
staging_percent = 100
end
2018-11-05 16:20:35 -07:00
2018-11-06 11:11:58 -07:00
if CURRENT_COMMIT == latest_commit_staging
2018-11-06 11:52:28 -07:00
staging_percent = build_percent
2018-11-06 11:11:58 -07:00
end
2018-11-05 16:20:35 -07:00
2018-11-06 11:52:28 -07:00
diff = (build_percent - staging_percent)
pass = (diff > -THRESHOLD)
2018-11-05 16:20:35 -07:00
puts "=" * 37
puts "COVERAGE RESULTS"
2018-12-03 14:36:43 -07:00
puts "This build: #{build_percent.round(8)}% #{CURRENT_COMMIT[0,8]}"
puts "Staging build: #{staging_percent.round(8)}% #{latest_commit_staging[0,8]}"
2018-11-05 16:20:35 -07:00
puts "=" * 37
2018-11-06 11:11:58 -07:00
puts "Difference: #{diff.round(8)}%"
2018-11-05 16:20:35 -07:00
puts "Pass?: #{pass ? "yes" : "no"}"
exit pass ? 0 : 1
end
end