Skip to content

Commit

Permalink
refactor start script and add option for custom coverage directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Joakim Reinert committed Nov 24, 2016
1 parent 59b4668 commit 69d2a41
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions bin/codeclimate-test-reporter
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
#!/usr/bin/env ruby

require "codeclimate-test-reporter"
require "optparse"

COVERAGE_FILE = "coverage/.resultset.json".freeze

if (repo_token = ENV["CODECLIMATE_REPO_TOKEN"]) && !repo_token.empty?
if File.exist?(COVERAGE_FILE)
begin
results = JSON.parse(File.read(COVERAGE_FILE))
rescue JSON::ParserError => e
$stderr.puts "Error encountered while parsing #{COVERAGE_FILE}: #{e}"
exit(1)
end

CodeClimate::TestReporter.run(results)
else
$stderr.puts "Coverage results not found"
exit(1)
repo_token = ENV["CODECLIMATE_REPO_TOKEN"]
if repo_token.nil? || repo_token.empty?
abort "Cannot post results: environment variable CODECLIMATE_REPO_TOKEN must be set."
end

coverage_dir = "coverage"
parser = OptionParser.new do |p|
p.banner = "Usage: #{$0} [options]"
on("-d PATH", "--directory PATH", "Path to coverage results directory (default: coverage)") do |path|
coverage_dir = path
end

p.on("-h", "--help", "Show this message") do
puts p
exit
end
else
$stderr.puts "Cannot post results: environment variable CODECLIMATE_REPO_TOKEN must be set."
exit(0)
end

parser.parse!

coverage_file = File.join(coverage_dir, ".resultset.json")
abort "Coverage results not found" unless File.exist?(coverage_file)

begin
results = JSON.parse(File.read(coverage_file))
rescue JSON::ParserError => e
abort("Error encountered while parsing #{coverage_file}: #{e}")
end

CodeClimate::TestReporter.run(results)

0 comments on commit 69d2a41

Please sign in to comment.