Class: Danger::DangerSlather
- Inherits:
-
Plugin
- Object
- Plugin
- Danger::DangerSlather
- 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.
Instance Method Summary collapse
-
#configure(xcodeproj_path, scheme, options: {}) ⇒ void
Required method to configure slather.
-
#modified_files_coverage_table ⇒ String
Build a coverage markdown table of the modified files coverage.
-
#notify_if_coverage_is_less_than(options) ⇒ Array<String>
Method to check if the coverage of the project is at least a minumum.
-
#notify_if_modified_file_is_less_than(options) ⇒ Array<String>
Method to check if the coverage of modified files is at least a minumum.
-
#show_coverage ⇒ Array<String>
Show a header with the total coverage and coverage table.
-
#show_modified_files_coverage ⇒ Array<String>
Show the table build by modified_files_coverage_table.
-
#show_total_coverage ⇒ Array<String>
Show a header with the total coverage of the project.
-
#total_coverage ⇒ Float
Total coverage of the project.
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 = [:workspace] @project.build_directory = [:build_directory] @project.ignore_list = [:ignore_list] @project.ci_service = [:ci_service] @project.coverage_access_token = [:coverage_access_token] @project.coverage_service = [:coverage_service] || :terminal @project.source_directory = [:source_directory] @project.output_directory = [:output_directory] @project.input_format = [:input_format] @project.binary_file = [:binary_file] @project.decimals = [:decimals] @project.configure @project.post if [:post] end |
#modified_files_coverage_table ⇒ String
Build a coverage markdown table of the modified files coverage
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
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() minimum_coverage = [:minimum_coverage] notify_level = [:notify_level] || :fail if total_coverage < minimum_coverage = "Total coverage less than #{minimum_coverage}%" if notify_level == :fail fail else warn 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
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() minimum_coverage = [:minimum_coverage] notify_level = [: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 = files_to_notify.map do |file| "#{file.source_file_pathname_relative_to_repo_root} has less than #{minimum_coverage}% code coverage" end .each do || if notify_level == :fail fail else warn end end end end |
#show_coverage ⇒ Array<String>
Show a header with the total coverage and coverage table
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_coverage ⇒ Array<String>
Show the table build by modified_files_coverage_table
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_coverage ⇒ Array<String>
Show a header with the total coverage of the project
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_coverage ⇒ Float
Total coverage of the project
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 |