Class: FeatureMap::Private::TestPyramidFile

Inherits:
Object
  • Object
show all
Defined in:
lib/feature_map/private/test_pyramid_file.rb

Overview

This class is responsible for compiling a set of feature-level test pyramid statistics into a test-pyramid.yml This file can then be used as an input to a variety of engineering team utilities (e.g. PR/release announcements, documentation generation, etc).

Defined Under Namespace

Classes: FileContentError

Constant Summary collapse

FEATURES_KEY =
'features'

Class Method Summary collapse

Class Method Details

.filepath(pathlike) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/feature_map/private/test_pyramid_file.rb', line 102

def self.filepath(pathlike)
  File
    .join(File.dirname(pathlike), File.basename(pathlike, '.*'))
    .gsub(%r{^\./}, '')
    .gsub(%r{^spec/}, '')
    .gsub(%r{^app/}, '')
end

.generate_content(unit_examples, integration_examples, regression_examples, regression_file_assignments) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/feature_map/private/test_pyramid_file.rb', line 50

def self.generate_content(unit_examples, integration_examples, regression_examples, regression_file_assignments)
  Private.feature_file_assignments.reduce({}) do |content, (feature_name, files)|
    regression_files = regression_file_assignments[feature_name] || []
    regression_count, regression_pending = regression_files.reduce([0, 0]) do |accumulated_counts, file|
      accumulated_count, accumulated_pending = accumulated_counts
      count, pending = split(regression_examples[file])

      [accumulated_count + count, accumulated_pending + pending]
    end

    pyramid = files.reduce({}) do |acc, file|
      normalized_path = filepath(file)

      unit_count, unit_pending = split(unit_examples["#{normalized_path}_spec"])
      integration_count, integration_pending = split(integration_examples[normalized_path])

      {
        'unit_count' => (acc['unit_count'] || 0) + unit_count,
        'unit_pending' => (acc['unit_pending'] || 0) + unit_pending,
        'integration_count' => (acc['integration_count'] || 0) + integration_count,
        'integration_pending' => (acc['integration_pending'] || 0) + integration_pending
      }
    end

    {
      feature_name => pyramid.merge(
        'regression_count' => regression_count,
        'regression_pending' => regression_pending
      ),
      **content
    }
  end
end

.header_commentObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/feature_map/private/test_pyramid_file.rb', line 36

def self.header_comment
  <<~HEADER
    # STOP! - DO NOT EDIT THIS FILE MANUALLY
    # This file was automatically generated by "bin/featuremap test_pyramid". The next time this file
    # is generated any changes will be lost. For more details:
    # https://github.com/Beyond-Finance/feature_map
    #
    # It is NOT recommended to commit this file into your source control. It will change or become
    # outdated frequently. Instead it should be regenerated when test pyramid statistics are required.
    # This file should be ignored by your source control, allowing the local copy to be used for other
    # feature analysis operations (e.g. documentation generation, etc).
  HEADER
end

.load_features!Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/feature_map/private/test_pyramid_file.rb', line 84

def self.load_features!
  test_coverage_content = YAML.load_file(path)

  return test_coverage_content[FEATURES_KEY] if test_coverage_content.is_a?(Hash) && test_coverage_content[FEATURES_KEY]

  raise FileContentError, "Unexpected content found in #{path}. Use `bin/featuremap test_coverage` to regenerate it and try again."
rescue Psych::SyntaxError => e
  raise FileContentError, "Invalid YAML content found at #{path}. Error: #{e.message} Use `bin/featuremap test_coverage` to generate it and try again."
rescue Errno::ENOENT
  raise FileContentError, "No feature test coverage file found at #{path}. Use `bin/featuremap test_coverage` to generate it and try again."
end

.pathObject



32
33
34
# File 'lib/feature_map/private/test_pyramid_file.rb', line 32

def self.path
  Pathname.pwd.join('.feature_map/test-pyramid.yml')
end

.split(examples) ⇒ Object



96
97
98
99
100
# File 'lib/feature_map/private/test_pyramid_file.rb', line 96

def self.split(examples)
  return [0, 0] if examples.nil?

  examples.partition { |ex| ex['status'] == 'passed' }.map(&:count)
end

.write!(unit_examples, integration_examples, regression_examples, regression_assignments) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/feature_map/private/test_pyramid_file.rb', line 16

def self.write!(unit_examples, integration_examples, regression_examples, regression_assignments)
  FileUtils.mkdir_p(path.dirname) if !path.dirname.exist?

  regression_file_assignments = regression_assignments['features']&.transform_values do |feature|
    feature['files']&.map { |file| filepath(file) } || []
  end || {}

  content = generate_content(
    unit_examples.group_by { |ex| filepath(ex['id']) },
    integration_examples.group_by { |ex| filepath(ex['id']) },
    regression_examples.group_by { |ex| filepath(ex['id']) },
    regression_file_assignments
  )
  path.write([header_comment, "\n", { FEATURES_KEY => content }.to_yaml].join)
end