Class: Couve::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/couve/parser.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

RED_THRESHOLD =
33.33
GREEN_THRESHOLD =
66.66

Instance Method Summary collapse

Constructor Details

#initialize(coverage, changed_files: nil) ⇒ Parser

Returns a new instance of Parser.



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

def initialize(coverage, changed_files: nil)
  @coverage = JSON.parse(coverage, symbolize_names: true)

  if changed_files
    @coverage[:source_files].select! { |file| changed_files.include?(file[:name]) }
  else
    @coverage[:source_files].reject! { |file| file[:covered_percent] == 100 }
  end

  @coverage[:source_files].sort_by! { |file| file[:covered_percent] }
end

Instance Method Details

#low_coverage?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/couve/parser.rb', line 73

def low_coverage?
  low_coverage_files.any?
end

#low_coverage_filesObject



67
68
69
70
71
# File 'lib/couve/parser.rb', line 67

def low_coverage_files
  @coverage[:source_files]
    .select { |source_file| source_file[:covered_percent].round(2) < GREEN_THRESHOLD }
    .map { |source_file| source_file[:name] }
end

#to_htmlObject



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
# File 'lib/couve/parser.rb', line 22

def to_html
  <<~HTML
    <html>
      <body>
        <div class="container mt-5">
          <h1 class="display-5">
            Coverage report
          </h1>

          <table class="table table-hover mt-5">
            <thead>
              <tr>
                <th class="col-1" colspan="2">Coverage</th>
                <th class="col-7">File</th>
                <th class="col-3">Not covered lines</th>
              </tr>
            </thead>
            #{body}
          </table>
        </div>

        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
      </body>
    </html>
  HTML
end

#to_markdownObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/couve/parser.rb', line 50

def to_markdown
  rows = @coverage[:source_files].map do |source_file|
    percentage = source_file[:covered_percent].round(2)
    indicator = percentage_indicator(percentage)

    "| #{indicator} | #{percentage}% | #{source_file[:name]} | #{not_covered_lines(source_file)} |"
  end

  <<~MARKDOWN
    ## Coverage report

    | Rating | Coverage | File | Not covered lines |
    | :---: | ---: | :--- | :--- |
    #{rows.join("\n")}
  MARKDOWN
end