Class: Coverband::Utils::SourceFile
- Inherits:
-
Object
- Object
- Coverband::Utils::SourceFile
- Defined in:
- lib/coverband/utils/source_file.rb,
lib/coverband/utils/source_file/line.rb
Defined Under Namespace
Classes: Line
Constant Summary collapse
- NOT_AVAILABLE =
"not available"
Instance Attribute Summary collapse
-
#coverage ⇒ Object
readonly
The array of coverage data received from the Coverage.result.
-
#coverage_posted ⇒ Object
readonly
The array of coverage timedata received from the Coverage.result.
-
#filename ⇒ Object
readonly
The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb).
-
#first_updated_at ⇒ Object
readonly
the date this version of the file first started to record coverage.
-
#last_updated_at ⇒ Object
readonly
the date this version of the file last saw any coverage activity.
-
#never_loaded ⇒ Object
readonly
meta data that the file was never loaded during boot or runtime.
Instance Method Summary collapse
- #build_lines ⇒ Object
-
#coverage_exceeding_source_warn ⇒ Object
Warning to identify condition from Issue #56.
-
#covered_lines ⇒ Object
Returns all covered lines as Coverband::Utils::SourceFile::Line.
- #covered_lines_count ⇒ Object
-
#covered_percent ⇒ Object
The coverage for this file in percent.
- #covered_strength ⇒ Object
- #formatted_covered_percent ⇒ Object
-
#initialize(filename, file_data) ⇒ SourceFile
constructor
A new instance of SourceFile.
-
#line(number) ⇒ Object
Access Coverband::Utils::SourceFile::Line source lines by line number.
- #line_coverage(index) ⇒ Object
- #line_coverage_posted(index) ⇒ Object
-
#lines ⇒ Object
(also: #source_lines)
Returns all source lines for this file as instances of Coverband::Utils::SourceFile::Line, and thus including coverage data.
-
#lines_of_code ⇒ Object
Returns the number of relevant lines (covered + missed).
- #lines_strength ⇒ Object
-
#missed_lines ⇒ Object
Returns all lines that should have been, but were not covered as instances of Coverband::Utils::SourceFile::Line.
- #missed_lines_count ⇒ Object
-
#never_lines ⇒ Object
Returns all lines that are not relevant for coverage as Coverband::Utils::SourceFile::Line instances.
- #never_lines_count ⇒ Object
- #no_lines? ⇒ Boolean
-
#process_skipped_lines(lines) ⇒ Object
Will go through all source files and mark lines that are wrapped within # :nocov: comment blocks as skipped.
-
#project_filename ⇒ Object
The path to this source file relative to the projects directory.
- #relative_path ⇒ Object
- #relevant_lines ⇒ Object
- #runtime_relavant_calculations(runtime_relavant_lines) ⇒ Object
-
#short_name ⇒ Object
a bug that existed in simplecov was not checking that root was at the start of the file name I had previously patched this in my local Rails app.
-
#skipped_lines ⇒ Object
Returns all lines that were skipped as Coverband::Utils::SourceFile::Line instances.
- #skipped_lines_count ⇒ Object
-
#src ⇒ Object
(also: #source)
The source code for this file.
Constructor Details
#initialize(filename, file_data) ⇒ SourceFile
Returns a new instance of SourceFile.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/coverband/utils/source_file.rb', line 30 def initialize(filename, file_data) @filename = filename @runtime_relavant_lines = nil if file_data.is_a?(Hash) @coverage = file_data["data"] @coverage_posted = file_data["timedata"] || [] # NOTE: only implement timedata for HashRedisStore @first_updated_at = @last_updated_at = NOT_AVAILABLE @first_updated_at = Time.at(file_data["first_updated_at"]) if file_data["first_updated_at"] @last_updated_at = Time.at(file_data["last_updated_at"]) if file_data["last_updated_at"] @never_loaded = file_data["never_loaded"] || false else # TODO: Deprecate this code path this was backwards compatibility from 3-4 @coverage = file_data @first_updated_at = NOT_AVAILABLE @last_updated_at = NOT_AVAILABLE end end |
Instance Attribute Details
#coverage ⇒ Object (readonly)
The array of coverage data received from the Coverage.result
18 19 20 |
# File 'lib/coverband/utils/source_file.rb', line 18 def coverage @coverage end |
#coverage_posted ⇒ Object (readonly)
The array of coverage timedata received from the Coverage.result
20 21 22 |
# File 'lib/coverband/utils/source_file.rb', line 20 def coverage_posted @coverage_posted end |
#filename ⇒ Object (readonly)
The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb)
16 17 18 |
# File 'lib/coverband/utils/source_file.rb', line 16 def filename @filename end |
#first_updated_at ⇒ Object (readonly)
the date this version of the file first started to record coverage
23 24 25 |
# File 'lib/coverband/utils/source_file.rb', line 23 def first_updated_at @first_updated_at end |
#last_updated_at ⇒ Object (readonly)
the date this version of the file last saw any coverage activity
25 26 27 |
# File 'lib/coverband/utils/source_file.rb', line 25 def last_updated_at @last_updated_at end |
#never_loaded ⇒ Object (readonly)
meta data that the file was never loaded during boot or runtime
27 28 29 |
# File 'lib/coverband/utils/source_file.rb', line 27 def never_loaded @never_loaded end |
Instance Method Details
#build_lines ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/coverband/utils/source_file.rb', line 75 def build_lines coverage_exceeding_source_warn if coverage.size > src.size lines = src.map.with_index(1) { |src, i| Coverband::Utils::SourceFile::Line.new( src, i, never_loaded ? 0 : coverage[i - 1], (never_loaded || !coverage_posted.is_a?(Array)) ? nil : coverage_posted[i - 1] ) } process_skipped_lines(lines) end |
#coverage_exceeding_source_warn ⇒ Object
Warning to identify condition from Issue #56
91 92 93 |
# File 'lib/coverband/utils/source_file.rb', line 91 def coverage_exceeding_source_warn warn "Warning: coverage data from Coverage [#{coverage.size}] exceeds line count in #{filename} [#{src.size}]" end |
#covered_lines ⇒ Object
Returns all covered lines as Coverband::Utils::SourceFile::Line
136 137 138 |
# File 'lib/coverband/utils/source_file.rb', line 136 def covered_lines @covered_lines ||= lines.select(&:covered?) end |
#covered_lines_count ⇒ Object
140 141 142 143 |
# File 'lib/coverband/utils/source_file.rb', line 140 def covered_lines_count return @covered_lines.size if defined?(@covered_lines) && @covered_lines lines.count(&:covered?) end |
#covered_percent ⇒ Object
The coverage for this file in percent. 0 if the file has no relevant lines
101 102 103 104 105 106 107 108 |
# File 'lib/coverband/utils/source_file.rb', line 101 def covered_percent return 100.0 if no_lines? return 0.0 if relevant_lines.zero? # handle edge case where runtime in dev can go over 100% [Float(covered_lines_count * 100.0 / relevant_lines.to_f), 100.0].min&.round(2) end |
#covered_strength ⇒ Object
114 115 116 117 118 |
# File 'lib/coverband/utils/source_file.rb', line 114 def covered_strength return 0.0 if relevant_lines.zero? round_float(lines_strength / relevant_lines.to_f, 1) end |
#formatted_covered_percent ⇒ Object
110 111 112 |
# File 'lib/coverband/utils/source_file.rb', line 110 def formatted_covered_percent covered_percent&.round(2) end |
#line(number) ⇒ Object
Access Coverband::Utils::SourceFile::Line source lines by line number
96 97 98 |
# File 'lib/coverband/utils/source_file.rb', line 96 def line(number) lines[number - 1] end |
#line_coverage(index) ⇒ Object
145 146 147 |
# File 'lib/coverband/utils/source_file.rb', line 145 def line_coverage(index) lines[index]&.coverage end |
#line_coverage_posted(index) ⇒ Object
149 150 151 |
# File 'lib/coverband/utils/source_file.rb', line 149 def line_coverage_posted(index) lines[index]&.coverage_posted end |
#lines ⇒ Object Also known as: source_lines
Returns all source lines for this file as instances of Coverband::Utils::SourceFile::Line, and thus including coverage data. Aliased as :source_lines
70 71 72 |
# File 'lib/coverband/utils/source_file.rb', line 70 def lines @lines ||= build_lines end |
#lines_of_code ⇒ Object
Returns the number of relevant lines (covered + missed)
186 187 188 189 190 191 192 |
# File 'lib/coverband/utils/source_file.rb', line 186 def lines_of_code @lines_of_code ||= if @covered_lines && @missed_lines @covered_lines.size + @missed_lines.size else lines.count { |l| l.covered? || l.missed? } end end |
#lines_strength ⇒ Object
124 125 126 127 128 |
# File 'lib/coverband/utils/source_file.rb', line 124 def lines_strength lines.sum do |line| line.coverage || 0 end end |
#missed_lines ⇒ Object
Returns all lines that should have been, but were not covered as instances of Coverband::Utils::SourceFile::Line
155 156 157 |
# File 'lib/coverband/utils/source_file.rb', line 155 def missed_lines @missed_lines ||= lines.select(&:missed?) end |
#missed_lines_count ⇒ Object
159 160 161 162 |
# File 'lib/coverband/utils/source_file.rb', line 159 def missed_lines_count return @missed_lines.size if defined?(@missed_lines) && @missed_lines lines.count(&:missed?) end |
#never_lines ⇒ Object
Returns all lines that are not relevant for coverage as Coverband::Utils::SourceFile::Line instances
166 167 168 |
# File 'lib/coverband/utils/source_file.rb', line 166 def never_lines @never_lines ||= lines.select(&:never?) end |
#never_lines_count ⇒ Object
170 171 172 173 |
# File 'lib/coverband/utils/source_file.rb', line 170 def never_lines_count return @never_lines.size if defined?(@never_lines) && @never_lines lines.count(&:never?) end |
#no_lines? ⇒ Boolean
120 121 122 |
# File 'lib/coverband/utils/source_file.rb', line 120 def no_lines? lines.length.zero? || (lines.length == never_lines.size) end |
#process_skipped_lines(lines) ⇒ Object
Will go through all source files and mark lines that are wrapped within # :nocov: comment blocks as skipped.
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/coverband/utils/source_file.rb', line 196 def process_skipped_lines(lines) skipping = false lines.each do |line| if Coverband::Utils::LinesClassifier.no_cov_line?(line.src) skipping = !skipping line.skipped! elsif skipping line.skipped! end end end |
#project_filename ⇒ Object
The path to this source file relative to the projects directory
56 57 58 |
# File 'lib/coverband/utils/source_file.rb', line 56 def project_filename @filename.sub(/^#{Coverband.configuration.root}/, "") end |
#relative_path ⇒ Object
216 217 218 |
# File 'lib/coverband/utils/source_file.rb', line 216 def relative_path RelativeFileConverter.convert(filename) end |
#relevant_lines ⇒ Object
130 131 132 133 |
# File 'lib/coverband/utils/source_file.rb', line 130 def relevant_lines return @runtime_relavant_lines if @runtime_relavant_lines lines_of_code end |
#runtime_relavant_calculations(runtime_relavant_lines) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/coverband/utils/source_file.rb', line 48 def runtime_relavant_calculations(runtime_relavant_lines) @runtime_relavant_lines = runtime_relavant_lines yield self ensure @runtime_relavant_lines = nil end |
#short_name ⇒ Object
a bug that existed in simplecov was not checking that root was at the start of the file name I had previously patched this in my local Rails app
212 213 214 |
# File 'lib/coverband/utils/source_file.rb', line 212 def short_name filename.delete_prefix("#{Coverband.configuration.root}/") end |
#skipped_lines ⇒ Object
Returns all lines that were skipped as Coverband::Utils::SourceFile::Line instances
176 177 178 |
# File 'lib/coverband/utils/source_file.rb', line 176 def skipped_lines @skipped_lines ||= lines.select(&:skipped?) end |
#skipped_lines_count ⇒ Object
180 181 182 183 |
# File 'lib/coverband/utils/source_file.rb', line 180 def skipped_lines_count return @skipped_lines.size if defined?(@skipped_lines) && @skipped_lines lines.count(&:skipped?) end |
#src ⇒ Object Also known as: source
The source code for this file. Aliased as :source
61 62 63 64 65 |
# File 'lib/coverband/utils/source_file.rb', line 61 def src # We intentionally read source code lazily to # suppress reading unused source code. @src ||= File.open(filename, "rb", &:readlines) end |