Class: GitlabQuality::TestTooling::CodeCoverage::TestMap

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab_quality/test_tooling/code_coverage/test_map.rb

Constant Summary collapse

SEPARATOR =
'/'
MARKER =
1

Instance Method Summary collapse

Constructor Details

#initialize(compact_map) ⇒ TestMap

Returns a new instance of TestMap.

Examples:

Example of compact_map for a file tested by 3 spec files

{
  "app/models/user.rb" => {
    "spec" => {
      "models" => {
        "user_spec.rb" => 1  # MARKER (1) indicates a leaf node
      }
    },
    "ee" => {
      "spec" => {
        "lib" => {
           "ee" => {
             "gitlab" => {
               "background_migration" => {
                 "delete_invalid_epic_issues_spec.rb"=>1,
                 "backfill_security_policies_spec.rb"=>1
               }
             }
           }
        }
      }
    }
  }
}

Parameters:

  • compact_map (Hash)

    A nested hash structure where keys are source files and values are tree structures with test file paths



38
39
40
# File 'lib/gitlab_quality/test_tooling/code_coverage/test_map.rb', line 38

def initialize(compact_map)
  @compact_map = compact_map
end

Instance Method Details

#source_to_testsHash<String, Array<String>>

Returns Source files mapped to all test files testing them.

Examples:

Return value

{
  "path/to/file1.rb" => [
    "spec/path/to/file1_spec.rb",
    "spec/path/to/another/file1_spec.rb"
  ],
  ...
}

Returns:

  • (Hash<String, Array<String>>)

    Source files mapped to all test files testing them



52
53
54
# File 'lib/gitlab_quality/test_tooling/code_coverage/test_map.rb', line 52

def source_to_tests
  @source_to_tests ||= @compact_map.transform_values { |tree| traverse(tree).to_a.uniq }
end

#test_to_sourcesHash<String, Array<String>>

Returns Test files mapped to all source files tested by them.

Examples:

Return value

{
  "spec/path/to/file1_spec.rb" => [
    "path/to/file1.rb",
    "path/to/file2.rb"
  ],
  ...
}

Returns:

  • (Hash<String, Array<String>>)

    Test files mapped to all source files tested by them



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gitlab_quality/test_tooling/code_coverage/test_map.rb', line 66

def test_to_sources
  @test_to_sources ||= begin
    test_to_sources = Hash.new { |hash, key| hash[key] = [] }

    @compact_map.each do |source_file, tree|
      traverse(tree).to_a.each do |test_file|
        test_to_sources[test_file] << source_file
      end
    end

    test_to_sources
  end
end