Module: Couve

Defined in:
lib/couve.rb,
lib/couve/parser.rb,
lib/couve/version.rb

Defined Under Namespace

Classes: Parser

Constant Summary collapse

VERSION =
"0.8.0"

Class Method Summary collapse

Class Method Details

.changed_files(source) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/couve.rb', line 44

def self.changed_files(source)
  return nil unless source

  contents = source == "-" ? $stdin.read : File.read(source)

  contents.lines.map { |line| line.strip.delete_prefix("./") }.reject(&:empty?)
end

.parse_arguments(argv) ⇒ Object

rubocop:disable Metrics/MethodLength



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/couve.rb', line 25

def self.parse_arguments(argv) # rubocop:disable Metrics/MethodLength
  argv = argv.dup
  changed_files_source = nil
  fail_on_low_coverage = false

  OptionParser.new do |opts|
    opts.on("--changed-files PATH", "Only report the files listed in PATH (use - for stdin)") do |path|
      changed_files_source = path
    end

    opts.on("--fail-on-low-coverage",
            "Exit 1 if any reported file is below #{Couve::Parser::GREEN_THRESHOLD}% coverage") do
      fail_on_low_coverage = true
    end
  end.parse!(argv)

  [argv[0], argv[1], changed_files_source, fail_on_low_coverage]
end

.start(argv = ARGV) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/couve.rb', line 9

def self.start(argv = ARGV)
  coverage_file, output_file, changed_files_source, fail_on_low_coverage = parse_arguments(argv)

  coverage = File.read(coverage_file)
  parser = Couve::Parser.new(coverage, changed_files: changed_files(changed_files_source))

  report = output_file.end_with?(".md") ? parser.to_markdown : parser.to_html

  File.open(output_file, "w") { |f| f.puts report }

  return unless fail_on_low_coverage && parser.low_coverage?

  warn "couve: coverage below #{Couve::Parser::GREEN_THRESHOLD}% in #{parser.low_coverage_files.join(", ")}"
  exit 1
end