Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add directory support on CLI #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/coverage/inject/cli.cr
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
require "option_parser"
# require "tempfile"
require "log"

module Coverage
module CLI
Log = ::Log.for("cli")

def self.run
output_format = "HtmlReport"
filenames = [] of String
targets = [] of String
print_only = false

OptionParser.parse! do |parser|
parser.banner = "Usage: crystal-cover [options] <filename>"
parser.banner = "Usage: crystal-coverage [options] <directories or filenames>"
parser.on("-h", "--help", "show this help") { puts parser; exit }
parser.on("-o FORMAT", "--output-format=FORMAT", "The output format used (default: HtmlReport): HtmlReport, Coveralls ") { |f| output_format = f }
parser.on("-p", "--print-only", "output the generated source code") { |_p| print_only = true }
parser.on("--use-require=REQUIRE", "change the require of cover library in runtime") { |r| Coverage::SourceFile.use_require = r }
parser.unknown_args do |args|
args.each do
filenames << ARGV.shift
targets << ARGV.shift
end
end
end

raise "You must choose a file to compile" unless filenames.any?
Log.error { "You must choose at least one file to compile" } unless targets.any?

Coverage::SourceFile.outputter = "Coverage::Outputter::#{output_format.camelcase}"

filenames = targets.map do |target|
target += "/**/*.cr" if File.directory?(target)
Dir[target]
end.flatten.uniq!

first = true
output = String::Builder.new(capacity: 2**18)

filenames.each do |f|
v = Coverage::SourceFile.new(path: f, source: ::File.read(f))
output << v.to_covered_source
Expand Down