Class: TTNT::TestToCodeMapping
- Inherits:
-
Object
- Object
- TTNT::TestToCodeMapping
- Defined in:
- lib/ttnt/test_to_code_mapping.rb
Overview
Mapping from test file to executed code (i.e. coverage without execution count).
Terminologies:
spectra: { filename => [line, numbers, executed], ... }
mapping: { test_file => spectra }
Constant Summary collapse
- STORAGE_SECTION =
'mapping'
Instance Attribute Summary collapse
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
Instance Method Summary collapse
-
#append_from_coverage(test, coverage) ⇒ void
Append the new mapping to test-to-code mapping file.
-
#get_tests(file:, lineno:) ⇒ Set
Get tests affected from change of file
fileat line numberlineno. -
#initialize(repo, sha = nil) ⇒ TestToCodeMapping
constructor
A new instance of TestToCodeMapping.
-
#normalize_paths(spectra) ⇒ Hash
private
Normalize all file names in a spectra.
-
#normalized_path(file) ⇒ String
private
Convert absolute path to relative path from the project (git repository) root.
-
#read! ⇒ Object
Read test-to-code mapping from storage.
-
#select_project_files(spectra) ⇒ Hash
private
Filter out the files outside of the target project using file path.
-
#spectra_from_coverage(cov) ⇒ Hash
private
Generate spectra data from Ruby coverage library’s data.
-
#write! ⇒ Object
Write test-to-code mapping to storage.
Constructor Details
#initialize(repo, sha = nil) ⇒ TestToCodeMapping
Returns a new instance of TestToCodeMapping.
21 22 23 24 25 26 |
# File 'lib/ttnt/test_to_code_mapping.rb', line 21 def initialize(repo, sha = nil) @repo = repo @storage = Storage.new(repo, sha) read! raise 'Not in a git repository' unless @repo end |
Instance Attribute Details
#mapping ⇒ Object (readonly)
Returns the value of attribute mapping.
15 16 17 |
# File 'lib/ttnt/test_to_code_mapping.rb', line 15 def mapping @mapping end |
Instance Method Details
#append_from_coverage(test, coverage) ⇒ void
This method returns an undefined value.
Append the new mapping to test-to-code mapping file.
33 34 35 36 |
# File 'lib/ttnt/test_to_code_mapping.rb', line 33 def append_from_coverage(test, coverage) spectra = normalize_paths(select_project_files(spectra_from_coverage(coverage))) @mapping[test] = spectra end |
#get_tests(file:, lineno:) ⇒ Set
Get tests affected from change of file file at line number lineno
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ttnt/test_to_code_mapping.rb', line 53 def get_tests(file:, lineno:) tests = Set.new @mapping.each do |test, spectra| lines = spectra[file] next unless lines topmost = lines.first downmost = lines.last if topmost <= lineno && lineno <= downmost tests << test end end tests end |
#normalize_paths(spectra) ⇒ Hash (private)
Normalize all file names in a spectra.
81 82 83 84 85 |
# File 'lib/ttnt/test_to_code_mapping.rb', line 81 def normalize_paths(spectra) spectra.map do |filename, lines| [normalized_path(filename), lines] end.to_h end |
#normalized_path(file) ⇒ String (private)
Convert absolute path to relative path from the project (git repository) root.
73 74 75 |
# File 'lib/ttnt/test_to_code_mapping.rb', line 73 def normalized_path(file) File.(file).sub(@repo.workdir, '') end |
#read! ⇒ Object
Read test-to-code mapping from storage.
39 40 41 |
# File 'lib/ttnt/test_to_code_mapping.rb', line 39 def read! @mapping = @storage.read(STORAGE_SECTION) end |
#select_project_files(spectra) ⇒ Hash (private)
Filter out the files outside of the target project using file path.
91 92 93 94 95 |
# File 'lib/ttnt/test_to_code_mapping.rb', line 91 def select_project_files(spectra) spectra.select do |filename, lines| filename.start_with?(@repo.workdir) end end |
#spectra_from_coverage(cov) ⇒ Hash (private)
Generate spectra data from Ruby coverage library’s data
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ttnt/test_to_code_mapping.rb', line 101 def spectra_from_coverage(cov) spectra = Hash.new { |h, k| h[k] = [] } cov.each do |filename, executions| executions.each_with_index do |execution, i| next if execution.nil? || execution == 0 spectra[filename] << i + 1 end end spectra end |
#write! ⇒ Object
Write test-to-code mapping to storage.
44 45 46 |
# File 'lib/ttnt/test_to_code_mapping.rb', line 44 def write! @storage.write!(STORAGE_SECTION, @mapping) end |