gitlab-crystalball 0.7.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -6
  3. data/lib/crystalball/example_group_map.rb +2 -1
  4. data/lib/crystalball/execution_map.rb +1 -1
  5. data/lib/crystalball/logging.rb +6 -4
  6. data/lib/crystalball/map_generator/base_strategy.rb +26 -5
  7. data/lib/crystalball/map_generator/configuration.rb +10 -4
  8. data/lib/crystalball/map_generator/coverage_strategy/execution_detector.rb +15 -1
  9. data/lib/crystalball/map_generator/coverage_strategy.rb +28 -11
  10. data/lib/crystalball/map_generator/described_class_strategy.rb +12 -4
  11. data/lib/crystalball/map_generator/helpers/path_filter.rb +33 -6
  12. data/lib/crystalball/map_generator/object_sources_detector.rb +4 -3
  13. data/lib/crystalball/map_generator/oneshot_coverage_strategy.rb +39 -0
  14. data/lib/crystalball/map_generator/strategies_collection.rb +18 -12
  15. data/lib/crystalball/map_generator.rb +63 -15
  16. data/lib/crystalball/rails/map_generator/action_view_strategy/patch.rb +1 -1
  17. data/lib/crystalball/rails/map_generator/action_view_strategy.rb +15 -4
  18. data/lib/crystalball/rails.rb +0 -1
  19. data/lib/crystalball/rspec/runner/configuration.rb +1 -1
  20. data/lib/crystalball/version.rb +1 -1
  21. data/lib/crystalball.rb +1 -1
  22. metadata +45 -25
  23. data/lib/crystalball/factory_bot.rb +0 -3
  24. data/lib/crystalball/map_generator/allocated_objects_strategy/object_tracker.rb +0 -45
  25. data/lib/crystalball/map_generator/allocated_objects_strategy.rb +0 -45
  26. data/lib/crystalball/map_generator/factory_bot_strategy/dsl_patch/factory_path_fetcher.rb +0 -30
  27. data/lib/crystalball/map_generator/factory_bot_strategy/dsl_patch.rb +0 -40
  28. data/lib/crystalball/map_generator/factory_bot_strategy/factory_gem_loader.rb +0 -27
  29. data/lib/crystalball/map_generator/factory_bot_strategy/factory_runner_patch.rb +0 -25
  30. data/lib/crystalball/map_generator/factory_bot_strategy.rb +0 -59
  31. data/lib/crystalball/map_generator/parser_strategy/processor.rb +0 -129
  32. data/lib/crystalball/map_generator/parser_strategy.rb +0 -60
  33. data/lib/crystalball/rails/map_generator/i18n_strategy/simple_patch.rb +0 -89
  34. data/lib/crystalball/rails/map_generator/i18n_strategy.rb +0 -47
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2edc0f271a5823ee73e40d74c51f6e357d578424aa059757b4ff6690316adc74
4
- data.tar.gz: b9803cc94cd0d6b8437d317e7b47d25ebec6de6bf4b4a53735b9e12c97b6dd13
3
+ metadata.gz: 0b2f24d36d9096bfc8b213b0d5c479f2a9cdd5d3eb7c67dae256ef3263e3d371
4
+ data.tar.gz: f73bf079ec17508213b4cf72445a999fff0c3e8222473d3c4b92bc8306deaa08
5
5
  SHA512:
6
- metadata.gz: 24937f95403f4ba4637a9f7f5e3558b8a7f2fc4d7759577a237de8b849e0e6a20a4a87e764d0f73f58ac5b5cf18650f7a8ef15d94aaa479b726afe2ccf8fb599
7
- data.tar.gz: 85ce3e28abde0853b087e685c7bc81dec2a0a11c136ff79b81dab558abbd55ce1c020d260d316c5c11ca865819a40f2483b33f65390a71f4ffe178e5cb931b4b
6
+ metadata.gz: 96dbdf82126afd3763f6d0baa1cb763bb79fe8e4a191eec30ad631f5a0d70fd4e0c9936b9323f3fa943c6115a91a45a3928b89ac37addaab3950991825a3972d
7
+ data.tar.gz: 14b78a6dd03fbd90729b5334ca627b22a975d1dadabd74adbe7a7db9c47d354391893d84210fbdcbb5bcb939531c8cf52c977e89ee58549975c44d7e3dd59b0f
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Crystalball
2
2
 
3
- ![Build Status](https://gitlab.com/acunskis/crystalball/badges/main/pipeline.svg)
4
- ![Test Coverage](https://gitlab.com/acunskis/crystalball/badges/main/coverage.svg?job=rspec)
3
+ ![Build Status](https://gitlab.com/gitlab-org/ruby/gems/crystalball/badges/main/pipeline.svg)
4
+ ![Test Coverage](https://gitlab.com/gitlab-org/ruby/gems/crystalball/badges/main/coverage.svg?job=rspec)
5
5
 
6
6
  Crystalball is a Ruby library which implements [Regression Test Selection mechanism](https://tenderlovemaking.com/2015/02/13/predicting-test-failues.html) originally published by Aaron Patterson.
7
7
  Its main purpose is to select a minimal subset of your test suite which should be run to ensure your changes didn't break anything.
@@ -12,7 +12,7 @@ Add this line to your application's Gemfile:
12
12
 
13
13
  ```ruby
14
14
  group :test do
15
- gem 'crystalball'
15
+ gem 'gitlab-crystalball'
16
16
  end
17
17
  ```
18
18
 
@@ -30,11 +30,11 @@ Or install it yourself as:
30
30
 
31
31
  ## Usage
32
32
 
33
- Please see our [official documentation](https://gitlab.com/acunskis/crystalball/-/blob/main/docs/index.md).
33
+ Please see our [official documentation](https://gitlab.com/gitlab-org/ruby/gems/crystalball/-/blob/main/docs/index.md).
34
34
 
35
35
  ### Versioning
36
36
 
37
- We use [semantic versioning](https://semver.org/) for our [releases](https://gitlab.com/acunskis/crystalball/-/releases).
37
+ We use [semantic versioning](https://semver.org/) for our [releases](https://gitlab.com/gitlab-org/ruby/gems/crystalball/-/releases).
38
38
 
39
39
  ## Development
40
40
 
@@ -48,7 +48,6 @@ In order to release new version, manual pipeline should be triggered via Gitlab
48
48
 
49
49
  ## Contributing
50
50
 
51
- Bug reports and pull requests are welcome on GitHub at <https://github.com/toptal/crystalball>.
52
51
  This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
53
52
 
54
53
  ## License
@@ -3,9 +3,10 @@
3
3
  module Crystalball
4
4
  # Data object to store execution map for specific example
5
5
  class ExampleGroupMap
6
- attr_reader :uid, :file_path, :used_files
7
6
  extend Forwardable
8
7
 
8
+ attr_reader :uid, :file_path, :used_files
9
+
9
10
  delegate %i[push each] => :used_files
10
11
 
11
12
  # @param [Example|ExampleGroup] example - RSpec example or example group
@@ -20,7 +20,7 @@ module Crystalball
20
20
  end
21
21
 
22
22
  def to_h
23
- {type: type, commit: commit, timestamp: timestamp, version: version}
23
+ { type: type, commit: commit, timestamp: timestamp, version: version }
24
24
  end
25
25
  end
26
26
 
@@ -6,9 +6,11 @@ module Crystalball
6
6
  # This module logs information to the standard output based on the configured log level,
7
7
  # and also logs unfiltered information to the configured log file.
8
8
  module Logging
9
- def log(severity_sym, *args, &block)
10
- output_stream.log(severity(severity_sym), *args, &block)
11
- log_file_output_stream.log(severity(severity_sym), *args, &block)
9
+ def log(severity_sym, message, prefix_class_name: false)
10
+ msg = prefix_class_name ? "[#{self.class.name.split('::').last}] #{message}" : message
11
+
12
+ output_stream.log(severity(severity_sym), msg)
13
+ log_file_output_stream.log(severity(severity_sym), msg)
12
14
  end
13
15
 
14
16
  def self.extended(base)
@@ -26,7 +28,7 @@ module Crystalball
26
28
  end
27
29
 
28
30
  def output_stream
29
- @output_stream ||= ::Logger.new(STDOUT).tap do |logger|
31
+ @output_stream ||= ::Logger.new(STDOUT, progname: "crystalball").tap do |logger|
30
32
  logger.level = severity(configured_level)
31
33
  end
32
34
  end
@@ -4,19 +4,40 @@ module Crystalball
4
4
  class MapGenerator
5
5
  # Map generator strategy interface
6
6
  module BaseStrategy
7
+ include Logging
8
+
7
9
  def after_register; end
8
10
 
9
11
  def after_start; end
10
12
 
11
13
  def before_finalize; end
12
14
 
13
- # Each strategy must implement #call augmenting the used_files list and
14
- # yielding back the ExampleGroupMap.
15
- # @param [Crystalball::ExampleGroupMap] _example_map - object holding example metadata and used files
16
- # @param [RSpec::Core::Example] _example - a RSpec example
17
- def call(_example_map, _example)
15
+ # Run before the execution of the example or example group
16
+ #
17
+ # @param _example [RSpec::Core::ExampleGroup, RSpec::Core::Example]
18
+ # @return [void]
19
+ def run_before(_example)
20
+ raise NotImplementedError
21
+ end
22
+
23
+ # Run after the execution of the example or example group
24
+ #
25
+ # @param _example_map [Crystalball::ExampleGroupMap]
26
+ # @param _example [RSpec::Core::ExampleGroup, RSpec::Core::Example]
27
+ # @return [Crystalball::ExampleGroupMap]
28
+ def run_after(_example_map, _example)
18
29
  raise NotImplementedError
19
30
  end
31
+
32
+ private
33
+
34
+ # Print debug log messages with included strategy class name prefix
35
+ #
36
+ # @param msg [String]
37
+ # @return [void]
38
+ def log_debug(msg)
39
+ log(:debug, msg, prefix_class_name: true)
40
+ end
20
41
  end
21
42
  end
22
43
  end
@@ -7,15 +7,14 @@ module Crystalball
7
7
  # Configuration of map generator. Is can be accessed as a first argument inside
8
8
  # `Crystalball::MapGenerator.start! { |config| config } block.
9
9
  class Configuration
10
- attr_writer :map_storage
11
- attr_writer :map_class
10
+ attr_writer :map_storage, :map_class
12
11
  attr_accessor :commit, :version, :compact_map
13
-
14
- attr_reader :strategies
12
+ attr_reader :strategies, :hook_type
15
13
 
16
14
  def initialize
17
15
  @strategies = StrategiesCollection.new
18
16
  @compact_map = true
17
+ @hook_type = :example
19
18
  end
20
19
 
21
20
  def compact_map?
@@ -46,6 +45,13 @@ module Crystalball
46
45
  @dump_threshold = value.to_i
47
46
  end
48
47
 
48
+ def hook_type=(type)
49
+ valid_types = %i[example context]
50
+ raise ArgumentError, "hook type should be one of #{valid_types}" unless valid_types.include?(type)
51
+
52
+ @hook_type = type
53
+ end
54
+
49
55
  # Register new strategy for map generation
50
56
  #
51
57
  # @param [Crystalball::MapGenerator::BaseStrategy] strategy
@@ -8,6 +8,7 @@ module Crystalball
8
8
  # Class for detecting code execution path based on coverage information diff
9
9
  class ExecutionDetector
10
10
  include ::Crystalball::MapGenerator::Helpers::PathFilter
11
+
11
12
  # Detects files affected during example execution. Transforms absolute paths to relative.
12
13
  # Exclude paths outside of repository
13
14
  #
@@ -15,7 +16,20 @@ module Crystalball
15
16
  # @param[Array<String>] list of files affected after example execution
16
17
  # @return [Array<String>]
17
18
  def detect(before, after)
18
- filter after.reject { |file_name, after_coverage| before[file_name] == after_coverage }.keys
19
+ # `before` can be nil when specs run nested example groups inside a
20
+ # before(:context) hook, causing inner after(:context) hooks to clear
21
+ # the coverage baseline. CoverageStrategy#run_after logs a warning.
22
+ return [] unless before
23
+
24
+ after.filter_map do |file, coverage|
25
+ before_cov = before[file]&.fetch(:lines, [])
26
+ next if before_cov == coverage[:lines]
27
+
28
+ path = valid_path(file)
29
+ next unless path
30
+
31
+ path
32
+ end
19
33
  end
20
34
  end
21
35
  end
@@ -12,24 +12,41 @@ module Crystalball
12
12
 
13
13
  attr_reader :execution_detector
14
14
 
15
- def initialize(execution_detector = ExecutionDetector.new)
15
+ def initialize(execution_detector: ExecutionDetector.new)
16
16
  @execution_detector = execution_detector
17
+ @before_coverage = nil
17
18
  end
18
19
 
19
20
  def after_register
20
- Coverage.start unless Coverage.running?
21
+ return if Coverage.running?
22
+
23
+ log_debug("Starting coverage capture")
24
+ Coverage.start(lines: true)
25
+ end
26
+
27
+ def run_before(example)
28
+ log_debug("Fetching current coverage state before execution of example id: #{example.id}")
29
+ @before_coverage = Coverage.peek_result
21
30
  end
22
31
 
23
- # Adds to the example_map's used files the ones the ones in which
24
- # the coverage has changed after the tests runs.
25
- # @param [Crystalball::ExampleGroupMap] example_map - object holding example metadata and used files
26
- # @param [RSpec::Core::Example] example - a RSpec example
27
- def call(example_map, example)
28
- before = Coverage.peek_result
29
- yield example_map, example
30
- after = Coverage.peek_result
31
- example_map.push(*execution_detector.detect(before, after))
32
+ def run_after(example_map, example)
33
+ unless before_coverage
34
+ log(:warn, "[CoverageStrategy] Skipping coverage detection for #{example.id}: " \
35
+ "before_coverage is nil (nested context hooks may have cleared it)")
36
+ return
37
+ end
38
+
39
+ log_debug("Recording mappings for example id: #{example.id}")
40
+ mappings = execution_detector.detect(before_coverage, Coverage.peek_result)
41
+ log_debug("#{example.id} recorded #{mappings.size} files")
42
+ example_map.push(*mappings)
43
+ ensure
44
+ @before_coverage = nil
32
45
  end
46
+
47
+ private
48
+
49
+ attr_reader :before_coverage
33
50
  end
34
51
  end
35
52
  end
@@ -9,6 +9,7 @@ module Crystalball
9
9
  # ancestors.
10
10
  class DescribedClassStrategy
11
11
  include BaseStrategy
12
+
12
13
  extend Forwardable
13
14
 
14
15
  attr_reader :execution_detector
@@ -21,14 +22,21 @@ module Crystalball
21
22
  @execution_detector = execution_detector
22
23
  end
23
24
 
25
+ def run_before(_example); end
26
+
24
27
  # @param [Crystalball::ExampleGroupMap] example_map - object holding example metadata and used files
25
28
  # @param [RSpec::Core::Example] example - a RSpec example
26
- def call(example_map, example)
27
- yield example_map, example
28
-
29
+ def run_after(example_map, example)
30
+ log_debug("Recording mappings for example id: #{example.id}")
29
31
  described_class = example.metadata[:described_class]
30
32
 
31
- example_map.push(*execution_detector.detect([described_class])) if described_class
33
+ if described_class
34
+ mappings = execution_detector.detect([described_class])
35
+ log_debug("#{example.id} recorded #{mappings.size} files")
36
+ example_map.push(*mappings)
37
+ else
38
+ log_debug("#{example.id} did not record any mappings because it has no described_class")
39
+ end
32
40
  end
33
41
  end
34
42
  end
@@ -5,19 +5,46 @@ module Crystalball
5
5
  module Helpers
6
6
  # Helper module to filter file paths
7
7
  module PathFilter
8
- attr_reader :root_path
8
+ attr_reader :root_path, :exclude_prefixes
9
9
 
10
10
  # @param [String] root_path - absolute path to root folder of repository
11
- def initialize(root_path = Dir.pwd)
11
+ # @param [Array<String, Regexp>] exclude_prefixes - list of prefixes or patterns to filter out from paths
12
+ def initialize(root_path = Dir.pwd, exclude_prefixes: [])
12
13
  @root_path = root_path
14
+ @exclude_prefixes = exclude_prefixes
13
15
  end
14
16
 
15
17
  # @param [Array<String>] paths
16
- # @return relatve paths inside root_path only
18
+ # @return relative paths inside root_path only
17
19
  def filter(paths)
18
- paths
19
- .select { |file_name| file_name.start_with?(root_path) }
20
- .map { |file_name| file_name.sub("#{root_path}/", "") }
20
+ paths.filter_map { |path| valid_path(path) }
21
+ end
22
+
23
+ private
24
+
25
+ # Return relative path if it is a valid path
26
+ #
27
+ # @param path [String]
28
+ # @return [<String, nil>]
29
+ def valid_path(path)
30
+ return unless path.start_with?(root_path)
31
+
32
+ relative_path = path.sub("#{root_path}/", "")
33
+ return if matches_exclude_pattern?(relative_path)
34
+
35
+ relative_path
36
+ end
37
+
38
+ # Path matches exclude prefix based on string or regex conditions
39
+ #
40
+ # @param path [String]
41
+ # @return [Boolean]
42
+ def matches_exclude_pattern?(path)
43
+ exclude_prefixes&.any? do |prefix|
44
+ next path.match?(prefix) if prefix.instance_of? Regexp
45
+
46
+ path.start_with?(prefix)
47
+ end
21
48
  end
22
49
  end
23
50
  end
@@ -13,11 +13,12 @@ module Crystalball
13
13
  attr_reader :definition_tracer, :hierarchy_fetcher
14
14
 
15
15
  def initialize(
16
- root_path:,
16
+ root_path: Dir.pwd,
17
17
  definition_tracer: DefinitionTracer.new(root_path),
18
- hierarchy_fetcher: HierarchyFetcher.new
18
+ hierarchy_fetcher: HierarchyFetcher.new,
19
+ exclude_prefixes: []
19
20
  )
20
- super(root_path)
21
+ super(root_path, exclude_prefixes: exclude_prefixes)
21
22
 
22
23
  @definition_tracer = definition_tracer
23
24
  @hierarchy_fetcher = hierarchy_fetcher
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "coverage"
4
+ require "crystalball/map_generator/base_strategy"
5
+
6
+ require_relative "helpers/path_filter"
7
+
8
+ module Crystalball
9
+ class MapGenerator
10
+ # Map generator strategy based on harvesting Coverage information during example execution
11
+ class OneshotCoverageStrategy
12
+ include BaseStrategy
13
+ include Helpers::PathFilter
14
+
15
+ def after_register
16
+ raise "Coverage must not be started for oneshot_line strategy" if Coverage.running?
17
+ end
18
+
19
+ def run_before(_example)
20
+ log_debug("Starting oneshot_line coverage capture")
21
+ return Coverage.start(oneshot_lines: true) unless Coverage.running?
22
+
23
+ log(:warn, "Coverage has been already started, restarting coverage for oneshot_lines!", prefix_class_name: true)
24
+ Coverage.result(stop: true, clear: true)
25
+ Coverage.start(oneshot_lines: true)
26
+ end
27
+
28
+ # Adds to the example_map's used files the ones the ones in which
29
+ # the coverage has changed after the tests runs.
30
+ # @param [Crystalball::ExampleGroupMap] example_map - object holding example metadata and used files
31
+ # @param [RSpec::Core::Example] example - a RSpec example
32
+ def run_after(example_map, example)
33
+ paths = filter(Coverage.result(stop: true, clear: true).keys)
34
+ log_debug("#{example.id} recorded #{paths.size} files")
35
+ example_map.push(*paths)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -10,11 +10,24 @@ module Crystalball
10
10
  @strategies = strategies
11
11
  end
12
12
 
13
- # Calls every strategy on the given example group map and returns the modified example group map
14
- # @param [Crystalball::ExampleGroupMap] example_group_map - initial example group map
15
- # @return [Crystalball::ExampleGroupMap] example group map augmented by each strategy
16
- def run(example_group_map, example, &block)
17
- run_for_strategies(example_group_map, example, *_strategies.reverse, &block)
13
+ # Run before hook action for example or example group
14
+ #
15
+ # @param example [RSpec::Core::Example]
16
+ # @return [void]
17
+ def run_before(example)
18
+ _strategies.reverse_each { |strategy| strategy.run_before(example) }
19
+ end
20
+
21
+ # Run after hook action for example or example group and update example group map
22
+ #
23
+ # @param example_group_map [ExampleGroupMap]
24
+ # @param example [RSpec::Core::Example]
25
+ # @return [ExampleGroupMap]
26
+ def run_after(example_group_map, example)
27
+ _strategies.reverse_each do |strategy|
28
+ strategy.run_after(example_group_map, example)
29
+ end
30
+
18
31
  example_group_map
19
32
  end
20
33
 
@@ -31,13 +44,6 @@ module Crystalball
31
44
  def _strategies
32
45
  @strategies
33
46
  end
34
-
35
- def run_for_strategies(example_group_map, example, *strats, &block)
36
- return yield(example_group_map) if strats.empty?
37
-
38
- strat = strats.shift
39
- strat.call(example_group_map, example) { |c| run_for_strategies(c, example, *strats, &block) }
40
- end
41
47
  end
42
48
  end
43
49
  end
@@ -3,9 +3,11 @@
3
3
  module Crystalball
4
4
  # Class to generate execution map during RSpec build execution
5
5
  class MapGenerator
6
+ include Logging
6
7
  extend Forwardable
7
8
 
8
9
  attr_reader :configuration
10
+
9
11
  delegate %i[map_storage strategies dump_threshold map_class] => :configuration
10
12
 
11
13
  class << self
@@ -15,12 +17,17 @@ module Crystalball
15
17
  def start!(&block)
16
18
  generator = new(&block)
17
19
 
18
- ::RSpec.configure do |c|
19
- c.before(:suite) { generator.start! }
20
-
21
- c.around(:each) { |e| generator.refresh_for_case(e) }
22
-
23
- c.after(:suite) { generator.finalize! }
20
+ ::RSpec.configure do |config|
21
+ config.before(:suite) { generator.start! }
22
+ config.after(:suite) { generator.finalize! }
23
+
24
+ if generator.configuration.hook_type == :example
25
+ config.prepend_before(:example) { |e| generator.execute_before(e) }
26
+ config.append_after(:example) { |e| generator.execute_after(e) }
27
+ else
28
+ config.prepend_before(:context) { |e| generator.execute_before(e.class) }
29
+ config.append_after(:context) { |e| generator.execute_after(e.class) }
30
+ end
24
31
  end
25
32
  end
26
33
  end
@@ -33,6 +40,8 @@ module Crystalball
33
40
 
34
41
  # Registers strategies and prepares metadata for execution map
35
42
  def start!
43
+ log_with_prefix("Starting Crystalball execution map builder")
44
+
36
45
  self.map = nil
37
46
  map_storage.clear!
38
47
  map_storage.dump(map.metadata.to_h)
@@ -41,26 +50,47 @@ module Crystalball
41
50
  self.started = true
42
51
  end
43
52
 
44
- # Runs example and collects execution map for it
45
- def refresh_for_case(example)
46
- map << strategies.run(ExampleGroupMap.new(example), example) { example.run }
47
- check_dump_threshold
53
+ # Run before step of strategy for given example or example group
54
+ #
55
+ # @param example [RSpec::Core::ExampleGroup, RSpec::Core::Example]
56
+ # @return [void]
57
+ def execute_before(example)
58
+ strategies.run_before(example)
59
+ end
60
+
61
+ # Run after step and collect execution map
62
+ #
63
+ # @param example [RSpec::Core::ExampleGroup, RSpec::Core::Example]
64
+ # @return [void]
65
+ def execute_after(example)
66
+ result = strategies.run_after(ExampleGroupMap.new(example), example)
67
+ # do not populate map with nil values
68
+ map << result unless result.used_files.empty?
69
+ check_dump_threshold!
48
70
  end
49
71
 
50
72
  # Finalizes strategies and saves map
51
73
  def finalize!
52
74
  return unless started
53
75
 
76
+ log_with_prefix("Finalizing mappings strategies and saving final map")
54
77
  strategies.each(&:before_finalize)
55
78
 
56
- return unless map.size.positive?
79
+ unless map.size.positive?
80
+ log_with_prefix("No files recorded in the execution map")
81
+ return
82
+ end
57
83
 
58
84
  example_groups = (configuration.compact_map? ? MapCompactor.compact_map!(map) : map).example_groups
59
- map_storage.dump(example_groups)
85
+ dump_map_storage(example_groups)
60
86
  end
61
87
 
62
88
  def map
63
- @map ||= map_class.new(metadata: {commit: configuration.commit&.sha, timestamp: configuration.commit&.date&.to_i, version: configuration.version})
89
+ @map ||= map_class.new(metadata: {
90
+ commit: configuration.commit&.sha,
91
+ timestamp: configuration.commit&.date&.to_i,
92
+ version: configuration.version
93
+ })
64
94
  end
65
95
 
66
96
  private
@@ -68,17 +98,35 @@ module Crystalball
68
98
  attr_writer :map
69
99
  attr_accessor :started
70
100
 
101
+ def strategy_classes
102
+ @strategy_classes ||= strategies.map { |strategy| strategy.class.name.split("::").last }.join("|")
103
+ end
104
+
71
105
  def repo
72
106
  @repo = GitRepo.open(".") unless defined?(@repo)
73
107
  @repo
74
108
  end
75
109
 
76
- def check_dump_threshold
110
+ def check_dump_threshold!
77
111
  return if configuration.compact_map
78
112
  return unless dump_threshold.positive? && map.size >= dump_threshold
79
113
 
80
- map_storage.dump(map.example_groups)
114
+ log_with_prefix("Intermediate dump threshold (#{dump_threshold}) reached!")
115
+ dump_map_storage(map.example_groups)
116
+ end
117
+
118
+ def dump_map_storage(example_groups)
119
+ log_with_prefix("Dumping #{example_groups.size} examples to #{configuration.map_storage_path} file")
120
+ map_storage.dump(example_groups)
81
121
  map.clear!
82
122
  end
123
+
124
+ # Log message with prefix containing strategy names
125
+ #
126
+ # @param message [String]
127
+ # @return [void]
128
+ def log_with_prefix(message)
129
+ log(:info, "[#{strategy_classes}]: #{message}")
130
+ end
83
131
  end
84
132
  end
@@ -23,7 +23,7 @@ module Crystalball
23
23
  # Reverts original behavior of `ActionView::Template#compile!`
24
24
  def revert!
25
25
  ::ActionView::Template.class_eval do
26
- alias_method :compile!, :cb_original_compile! # rubocop:disable Lint/DuplicateMethods
26
+ alias_method :compile!, :cb_original_compile!
27
27
  undef_method :cb_patched_compile!
28
28
  end
29
29
  end
@@ -33,12 +33,23 @@ module Crystalball
33
33
  Patch.revert!
34
34
  end
35
35
 
36
+ # Reset affected views before test execution starts
37
+ #
38
+ # @param _example [RSpec::Core::Example, RSpec::Core::ExampleGroup]
39
+ # @return [void]
40
+ def run_before(_example)
41
+ self.class.reset_views
42
+ end
43
+
36
44
  # Adds views related to the spec to the example group map
45
+ #
37
46
  # @param [Crystalball::ExampleGroupMap] example_group_map - object holding example metadata and used files
38
- def call(example_group_map, _)
39
- self.class.reset_views
40
- yield example_group_map
41
- example_group_map.push(*filter(self.class.views))
47
+ # @return [void]
48
+ def run_after(example_group_map, example)
49
+ log_debug("Recording mappings for example id: #{example.id}")
50
+ mappings = filter(self.class.views)
51
+ log_debug("#{example.id} recorded #{mappings.size} files")
52
+ example_group_map.push(*mappings)
42
53
  end
43
54
  end
44
55
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "crystalball/rails/map_generator/action_view_strategy"
4
- require "crystalball/rails/map_generator/i18n_strategy"
5
4
  require "crystalball/active_record"
6
5
 
7
6
  module Crystalball
@@ -7,7 +7,7 @@ module Crystalball
7
7
  class Runner
8
8
  # Class for storing local runner configuration
9
9
  class Configuration
10
- def initialize(config = {}) # rubocop:disable Metrics/MethodLength
10
+ def initialize(config = {})
11
11
  @values = {
12
12
  "execution_map_path" => "tmp/crystalball_data.yml",
13
13
  "map_expiration_period" => 86_400,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Crystalball
4
- VERSION = "0.7.1"
4
+ VERSION = "1.1.3"
5
5
  end