Class: TTNT::TestToCodeMapping

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(repo, sha = nil) ⇒ TestToCodeMapping

Returns a new instance of TestToCodeMapping.

Parameters:

  • repo (Rugged::Reposiotry)

    repository to save test-to-code mapping (only repo.workdir is used to determine where to save the mapping file)

  • sha (String) (defaults to: nil)

    sha of commit from which mapping is read. nil means to read from current working tree. see Storage for more.



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

#mappingObject (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.

Parameters:

  • test (String)

    test file for which the coverage data is produced

  • coverage (Hash)

    coverage data generated using Coverage.start and Coverage.result



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

Parameters:

  • file (String)

    file name which might have effects on some tests

  • lineno (Integer)

    line number in the file which might have effects on some tests

Returns:

  • (Set)

    a set of test files which might be affected by the change in file at 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.

Parameters:

  • spectra (Hash)

    spectra data

Returns:

  • (Hash)

    spectra whose keys (file names) are normalized



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.

Parameters:

  • file (String)

    file name (absolute path)

Returns:

  • (String)

    normalized file path



73
74
75
# File 'lib/ttnt/test_to_code_mapping.rb', line 73

def normalized_path(file)
  File.expand_path(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.

Parameters:

  • spectra (Hash)

    spectra data

Returns:

  • (Hash)

    spectra with only files inside the target project



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

Parameters:

  • cov (Hash)

    coverage data generated using Coverage.result

Returns:

  • (Hash)

    spectra 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