Class: Danger::DangerSlather

Inherits:
Plugin
  • Object
show all
Defined in:
lib/slather/plugin.rb

Overview

Show code coverage of the project and by file. Add warnings or fail the Build if a minimum coverage are not achieved. It uses Slather Framework for calculate coverage, so it's required to configurate the slather object before using it.

Examples:

Require a minimum file coverage of 30%, a project coverage of 60% and show all modified files coverage

slather.configure("Path/to/my/project.xcodeproj", "MyScheme")
slather.notify_if_coverage_is_less_than(minimum_coverage: 60)
slather.notify_if_modified_file_is_less_than(minimum_coverage: 30)
slather.show_coverage

See Also:

  • BrunoMazzo/danger-slather

Instance Method Summary collapse

Instance Method Details

#configure(xcodeproj_path, scheme, options: {}) ⇒ void

This method returns an undefined value.

Required method to configure slather. It's required at least the path to the project and the scheme used with code coverage enabled



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/slather/plugin.rb', line 19

def configure(xcodeproj_path, scheme, options: {})
  @project = Slather::Project.open(xcodeproj_path)
  @project.scheme = scheme
  @project.workspace = options[:workspace]
  @project.build_directory = options[:build_directory]
  @project.ignore_list = options[:ignore_list]
  @project.ci_service = options[:ci_service]
  @project.coverage_access_token = options[:coverage_access_token]
  @project.coverage_service = options[:coverage_service] || :terminal
  @project.source_directory = options[:source_directory]
  @project.output_directory = options[:output_directory]
  @project.input_format = options[:input_format]
  @project.binary_file = options[:binary_file]
  @project.decimals = options[:decimals]
  @project.configure
  @project.post if options[:post]
end

#modified_files_coverage_tableString

Build a coverage markdown table of the modified files coverage

Returns:

  • (String)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/slather/plugin.rb', line 116

def modified_files_coverage_table
  unless @project.nil?
    line = ''
    if modified_files_coverage.count > 0
      line << "File | Coverage\n"
      line << "-----|-----\n"
      modified_files_coverage.each do |coverage_file|
        file_name = coverage_file.source_file_pathname_relative_to_repo_root.to_s
        percentage = @project.decimal_f([coverage_file.percentage_lines_tested])
        line << "#{file_name} | **`#{percentage}%`**\n"
      end
    end
    return line
  end
end

#notify_if_coverage_is_less_than(options) ⇒ Array<String>

Method to check if the coverage of the project is at least a minumum

Parameters:

  • options (Hash)

    a hash with the options

Options Hash (options):

  • :minimum_coverage (Float)

    the minimum code coverage required

  • :notify_level (Symbol)

    the level of notification

Returns:

  • (Array<String>)


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/slather/plugin.rb', line 58

def notify_if_coverage_is_less_than(options)
  minimum_coverage = options[:minimum_coverage]
  notify_level = options[:notify_level] || :fail
  if total_coverage < minimum_coverage
    notify_message = "Total coverage less than #{minimum_coverage}%"
    if notify_level == :fail
      fail notify_message
    else
      warn notify_message
    end
  end
end

#notify_if_modified_file_is_less_than(options) ⇒ Array<String>

Method to check if the coverage of modified files is at least a minumum

Parameters:

  • options (Hash)

    a hash with the options

Options Hash (options):

  • :minimum_coverage (Float)

    the minimum code coverage required for a file

  • :notify_level (Symbol)

    the level of notification

Returns:

  • (Array<String>)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/slather/plugin.rb', line 76

def notify_if_modified_file_is_less_than(options)
  minimum_coverage = options[:minimum_coverage]
  notify_level = options[:notify_level] || :fail

  if modified_files_coverage.count > 0
    files_to_notify = modified_files_coverage.select do |file|
      file.percentage_lines_tested < minimum_coverage
    end
    notify_messages = files_to_notify.map do |file|
      "#{file.source_file_pathname_relative_to_repo_root} has less than #{minimum_coverage}% code coverage"
    end

    notify_messages.each do |message|
      if notify_level == :fail
        fail message
      else
        warn message
      end
    end
  end
end

#show_coverageArray<String>

Show a header with the total coverage and coverage table

Returns:

  • (Array<String>)


142
143
144
145
146
147
148
149
150
# File 'lib/slather/plugin.rb', line 142

def show_coverage
  unless @project.nil?
    line = "## Code coverage\n"
    line << total_coverage_markdown
    line << modified_files_coverage_table
    line << '> Powered by [Slather](https://github.com/SlatherOrg/slather)'
    markdown line
  end
end

#show_modified_files_coverageArray<String>

Show the table build by modified_files_coverage_table

Returns:

  • (Array<String>)


134
135
136
137
138
# File 'lib/slather/plugin.rb', line 134

def show_modified_files_coverage
  unless @project.nil?
    markdown modified_files_coverage_table
  end
end

#show_total_coverageArray<String>

Show a header with the total coverage of the project

Returns:

  • (Array<String>)


108
109
110
111
112
# File 'lib/slather/plugin.rb', line 108

def show_total_coverage
  unless @project.nil?
    markdown total_coverage_markdown
  end
end

#total_coverageFloat

Total coverage of the project

Returns:

  • (Float)


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/slather/plugin.rb', line 39

def total_coverage
  unless @project.nil?
    @total_coverage ||= begin
      total_project_lines = 0
      total_project_lines_tested = 0
      @project.coverage_files.each do |coverage_file|
        total_project_lines_tested += coverage_file.num_lines_tested
        total_project_lines += coverage_file.num_lines_testable
      end
      @total_coverage = (total_project_lines_tested / total_project_lines.to_f) * 100.0
    end
  end
end