summaryrefslogtreecommitdiff
path: root/tooling/lib/tooling/test_map_generator.rb
blob: f96f33ff07400e8aeea4d8e89231abc2a09147af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

require 'set'
require 'yaml'

module Tooling
  class TestMapGenerator
    def initialize
      @mapping = Hash.new { |h, k| h[k] = Set.new }
    end

    def parse(yaml_files)
      Array(yaml_files).each do |yaml_file|
        data = File.read(yaml_file)
        metadata, example_groups = data.split("---\n").reject(&:empty?).map { |yml| YAML.safe_load(yml, [Symbol]) }

        if example_groups.nil?
          puts "No examples in #{yaml_file}! Metadata: #{metadata}"
          next
        end

        example_groups.each do |example_id, files|
          files.each do |file|
            spec_file = strip_example_uid(example_id)
            @mapping[file] << spec_file.delete_prefix('./')
          end
        end
      end
    end

    def mapping
      @mapping.transform_values { |set| set.to_a }
    end

    private

    def strip_example_uid(example_id)
      example_id.gsub(/\[.+\]/, '')
    end
  end
end