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.
- checksums.yaml +4 -4
- data/README.md +5 -6
- data/lib/crystalball/example_group_map.rb +2 -1
- data/lib/crystalball/execution_map.rb +1 -1
- data/lib/crystalball/logging.rb +6 -4
- data/lib/crystalball/map_generator/base_strategy.rb +26 -5
- data/lib/crystalball/map_generator/configuration.rb +10 -4
- data/lib/crystalball/map_generator/coverage_strategy/execution_detector.rb +15 -1
- data/lib/crystalball/map_generator/coverage_strategy.rb +28 -11
- data/lib/crystalball/map_generator/described_class_strategy.rb +12 -4
- data/lib/crystalball/map_generator/helpers/path_filter.rb +33 -6
- data/lib/crystalball/map_generator/object_sources_detector.rb +4 -3
- data/lib/crystalball/map_generator/oneshot_coverage_strategy.rb +39 -0
- data/lib/crystalball/map_generator/strategies_collection.rb +18 -12
- data/lib/crystalball/map_generator.rb +63 -15
- data/lib/crystalball/rails/map_generator/action_view_strategy/patch.rb +1 -1
- data/lib/crystalball/rails/map_generator/action_view_strategy.rb +15 -4
- data/lib/crystalball/rails.rb +0 -1
- data/lib/crystalball/rspec/runner/configuration.rb +1 -1
- data/lib/crystalball/version.rb +1 -1
- data/lib/crystalball.rb +1 -1
- metadata +45 -25
- data/lib/crystalball/factory_bot.rb +0 -3
- data/lib/crystalball/map_generator/allocated_objects_strategy/object_tracker.rb +0 -45
- data/lib/crystalball/map_generator/allocated_objects_strategy.rb +0 -45
- data/lib/crystalball/map_generator/factory_bot_strategy/dsl_patch/factory_path_fetcher.rb +0 -30
- data/lib/crystalball/map_generator/factory_bot_strategy/dsl_patch.rb +0 -40
- data/lib/crystalball/map_generator/factory_bot_strategy/factory_gem_loader.rb +0 -27
- data/lib/crystalball/map_generator/factory_bot_strategy/factory_runner_patch.rb +0 -25
- data/lib/crystalball/map_generator/factory_bot_strategy.rb +0 -59
- data/lib/crystalball/map_generator/parser_strategy/processor.rb +0 -129
- data/lib/crystalball/map_generator/parser_strategy.rb +0 -60
- data/lib/crystalball/rails/map_generator/i18n_strategy/simple_patch.rb +0 -89
- data/lib/crystalball/rails/map_generator/i18n_strategy.rb +0 -47
data/lib/crystalball.rb
CHANGED
|
@@ -16,7 +16,7 @@ require "crystalball/execution_map"
|
|
|
16
16
|
require "crystalball/map_generator"
|
|
17
17
|
require "crystalball/map_generator/configuration"
|
|
18
18
|
require "crystalball/map_generator/coverage_strategy"
|
|
19
|
-
require "crystalball/map_generator/
|
|
19
|
+
require "crystalball/map_generator/oneshot_coverage_strategy"
|
|
20
20
|
require "crystalball/map_generator/described_class_strategy"
|
|
21
21
|
require "crystalball/map_storage/yaml_storage"
|
|
22
22
|
require "crystalball/map_compactor"
|
metadata
CHANGED
|
@@ -1,28 +1,45 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: gitlab-crystalball
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 1.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
7
|
+
- Pavel Shutsin
|
|
8
|
+
- Evgenii Pecherkin
|
|
9
|
+
- Jaimerson Araujo
|
|
10
|
+
- GitLab Developer Experience Team
|
|
8
11
|
bindir: bin
|
|
9
12
|
cert_chain: []
|
|
10
|
-
date:
|
|
13
|
+
date: 2026-03-05 00:00:00.000000000 Z
|
|
11
14
|
dependencies:
|
|
12
15
|
- !ruby/object:Gem::Dependency
|
|
13
16
|
name: git
|
|
14
17
|
requirement: !ruby/object:Gem::Requirement
|
|
15
18
|
requirements:
|
|
16
|
-
- - "
|
|
19
|
+
- - "<"
|
|
17
20
|
- !ruby/object:Gem::Version
|
|
18
|
-
version:
|
|
21
|
+
version: '4'
|
|
19
22
|
type: :runtime
|
|
20
23
|
prerelease: false
|
|
21
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
25
|
requirements:
|
|
23
|
-
- - "
|
|
26
|
+
- - "<"
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '4'
|
|
29
|
+
- !ruby/object:Gem::Dependency
|
|
30
|
+
name: ostruct
|
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
|
32
|
+
requirements:
|
|
33
|
+
- - "<"
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: '1'
|
|
36
|
+
type: :runtime
|
|
37
|
+
prerelease: false
|
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - "<"
|
|
24
41
|
- !ruby/object:Gem::Version
|
|
25
|
-
version:
|
|
42
|
+
version: '1'
|
|
26
43
|
- !ruby/object:Gem::Dependency
|
|
27
44
|
name: actionview
|
|
28
45
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -51,6 +68,20 @@ dependencies:
|
|
|
51
68
|
- - "~>"
|
|
52
69
|
- !ruby/object:Gem::Version
|
|
53
70
|
version: 8.0.2
|
|
71
|
+
- !ruby/object:Gem::Dependency
|
|
72
|
+
name: climate_control
|
|
73
|
+
requirement: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - "~>"
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: 1.2.0
|
|
78
|
+
type: :development
|
|
79
|
+
prerelease: false
|
|
80
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
81
|
+
requirements:
|
|
82
|
+
- - "~>"
|
|
83
|
+
- !ruby/object:Gem::Version
|
|
84
|
+
version: 1.2.0
|
|
54
85
|
- !ruby/object:Gem::Dependency
|
|
55
86
|
name: factory_bot
|
|
56
87
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -224,31 +255,22 @@ files:
|
|
|
224
255
|
- lib/crystalball/extensions/git.rb
|
|
225
256
|
- lib/crystalball/extensions/git/base.rb
|
|
226
257
|
- lib/crystalball/extensions/git/lib.rb
|
|
227
|
-
- lib/crystalball/factory_bot.rb
|
|
228
258
|
- lib/crystalball/git_repo.rb
|
|
229
259
|
- lib/crystalball/logging.rb
|
|
230
260
|
- lib/crystalball/map_compactor.rb
|
|
231
261
|
- lib/crystalball/map_compactor/example_context.rb
|
|
232
262
|
- lib/crystalball/map_compactor/example_groups_data_compactor.rb
|
|
233
263
|
- lib/crystalball/map_generator.rb
|
|
234
|
-
- lib/crystalball/map_generator/allocated_objects_strategy.rb
|
|
235
|
-
- lib/crystalball/map_generator/allocated_objects_strategy/object_tracker.rb
|
|
236
264
|
- lib/crystalball/map_generator/base_strategy.rb
|
|
237
265
|
- lib/crystalball/map_generator/configuration.rb
|
|
238
266
|
- lib/crystalball/map_generator/coverage_strategy.rb
|
|
239
267
|
- lib/crystalball/map_generator/coverage_strategy/execution_detector.rb
|
|
240
268
|
- lib/crystalball/map_generator/described_class_strategy.rb
|
|
241
|
-
- lib/crystalball/map_generator/factory_bot_strategy.rb
|
|
242
|
-
- lib/crystalball/map_generator/factory_bot_strategy/dsl_patch.rb
|
|
243
|
-
- lib/crystalball/map_generator/factory_bot_strategy/dsl_patch/factory_path_fetcher.rb
|
|
244
|
-
- lib/crystalball/map_generator/factory_bot_strategy/factory_gem_loader.rb
|
|
245
|
-
- lib/crystalball/map_generator/factory_bot_strategy/factory_runner_patch.rb
|
|
246
269
|
- lib/crystalball/map_generator/helpers/path_filter.rb
|
|
247
270
|
- lib/crystalball/map_generator/object_sources_detector.rb
|
|
248
271
|
- lib/crystalball/map_generator/object_sources_detector/definition_tracer.rb
|
|
249
272
|
- lib/crystalball/map_generator/object_sources_detector/hierarchy_fetcher.rb
|
|
250
|
-
- lib/crystalball/map_generator/
|
|
251
|
-
- lib/crystalball/map_generator/parser_strategy/processor.rb
|
|
273
|
+
- lib/crystalball/map_generator/oneshot_coverage_strategy.rb
|
|
252
274
|
- lib/crystalball/map_generator/strategies_collection.rb
|
|
253
275
|
- lib/crystalball/map_storage/yaml_storage.rb
|
|
254
276
|
- lib/crystalball/prediction.rb
|
|
@@ -268,8 +290,6 @@ files:
|
|
|
268
290
|
- lib/crystalball/rails/helpers/schema_definition_parser/table_content_parser.rb
|
|
269
291
|
- lib/crystalball/rails/map_generator/action_view_strategy.rb
|
|
270
292
|
- lib/crystalball/rails/map_generator/action_view_strategy/patch.rb
|
|
271
|
-
- lib/crystalball/rails/map_generator/i18n_strategy.rb
|
|
272
|
-
- lib/crystalball/rails/map_generator/i18n_strategy/simple_patch.rb
|
|
273
293
|
- lib/crystalball/rails/predictor/modified_schema.rb
|
|
274
294
|
- lib/crystalball/rails/tables_map.rb
|
|
275
295
|
- lib/crystalball/rails/tables_map_generator.rb
|
|
@@ -285,14 +305,14 @@ files:
|
|
|
285
305
|
- lib/crystalball/source_diff/file_diff.rb
|
|
286
306
|
- lib/crystalball/source_diff/formatting_checker.rb
|
|
287
307
|
- lib/crystalball/version.rb
|
|
288
|
-
homepage: https://gitlab.com/
|
|
308
|
+
homepage: https://gitlab.com/gitlab-org/ruby/gems/crystalball
|
|
289
309
|
licenses: []
|
|
290
310
|
metadata:
|
|
291
|
-
bug_tracker_uri: https://gitlab.com/
|
|
292
|
-
changelog_uri: https://gitlab.com/
|
|
293
|
-
documentation_uri: https://gitlab.com/
|
|
294
|
-
source_code_uri: https://gitlab.com/
|
|
295
|
-
wiki_uri: https://gitlab.com/
|
|
311
|
+
bug_tracker_uri: https://gitlab.com/gitlab-org/ruby/gems/crystalball/-/issues
|
|
312
|
+
changelog_uri: https://gitlab.com/gitlab-org/ruby/gems/crystalball/-/releases
|
|
313
|
+
documentation_uri: https://gitlab.com/gitlab-org/ruby/gems/crystalball/-/blob/main/docs/index.md
|
|
314
|
+
source_code_uri: https://gitlab.com/gitlab-org/ruby/gems/crystalball/-/tree/main
|
|
315
|
+
wiki_uri: https://gitlab.com/gitlab-org/ruby/gems/crystalball/-/wikis/home
|
|
296
316
|
rubygems_mfa_required: 'false'
|
|
297
317
|
rdoc_options: []
|
|
298
318
|
require_paths:
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "set"
|
|
4
|
-
|
|
5
|
-
module Crystalball
|
|
6
|
-
class MapGenerator
|
|
7
|
-
class AllocatedObjectsStrategy
|
|
8
|
-
# Class to list object classes used during a block
|
|
9
|
-
class ObjectTracker
|
|
10
|
-
attr_reader :only_of
|
|
11
|
-
|
|
12
|
-
# @param [Array<Module>] only_of - classes or modules to watch on
|
|
13
|
-
def initialize(only_of: ["Object"])
|
|
14
|
-
@only_of = only_of
|
|
15
|
-
@created_object_classes = Set.new
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
# @yield a block to execute
|
|
19
|
-
# @return [Array<Object>] classes of objects allocated during the block execution
|
|
20
|
-
def used_classes_during(&block)
|
|
21
|
-
self.created_object_classes = Set.new
|
|
22
|
-
trace_point.enable(&block)
|
|
23
|
-
created_object_classes
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
private
|
|
27
|
-
|
|
28
|
-
attr_accessor :created_object_classes
|
|
29
|
-
|
|
30
|
-
def whitelisted_constants
|
|
31
|
-
@whitelisted_constants ||= only_of.map { |str| Object.const_get(str) }
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def trace_point
|
|
35
|
-
@trace_point ||= TracePoint.new(:c_call) do |tp|
|
|
36
|
-
next unless tp.method_id == :new || tp.method_id == :allocate
|
|
37
|
-
next unless whitelisted_constants.any? { |c| tp.self <= c }
|
|
38
|
-
|
|
39
|
-
created_object_classes << tp.self
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
end
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "crystalball/map_generator/base_strategy"
|
|
4
|
-
require "crystalball/map_generator/object_sources_detector"
|
|
5
|
-
require "crystalball/map_generator/allocated_objects_strategy/object_tracker"
|
|
6
|
-
|
|
7
|
-
module Crystalball
|
|
8
|
-
class MapGenerator
|
|
9
|
-
# Map generator strategy to get paths to files contains definition for all objects and its
|
|
10
|
-
# ancestors allocated during test example.
|
|
11
|
-
class AllocatedObjectsStrategy
|
|
12
|
-
include BaseStrategy
|
|
13
|
-
extend Forwardable
|
|
14
|
-
|
|
15
|
-
attr_reader :execution_detector, :object_tracker
|
|
16
|
-
|
|
17
|
-
delegate %i[after_register before_finalize] => :execution_detector
|
|
18
|
-
|
|
19
|
-
def self.build(only: [], root: Dir.pwd)
|
|
20
|
-
hierarchy_fetcher = ObjectSourcesDetector::HierarchyFetcher.new(only)
|
|
21
|
-
execution_detector = ObjectSourcesDetector.new(root_path: root, hierarchy_fetcher: hierarchy_fetcher)
|
|
22
|
-
|
|
23
|
-
new(execution_detector: execution_detector, object_tracker: ObjectTracker.new(only_of: only))
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
# @param [#detect] execution_detector
|
|
27
|
-
# @param [#created_during] object_tracker
|
|
28
|
-
def initialize(execution_detector:, object_tracker:)
|
|
29
|
-
@object_tracker = object_tracker
|
|
30
|
-
@execution_detector = execution_detector
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
# Adds to the used files every file which contain the definition of the
|
|
34
|
-
# classes of the objects allocated during the spec execution.
|
|
35
|
-
# @param [Crystalball::ExampleGroupMap] example_map - object holding example metadata and used files
|
|
36
|
-
# @param [RSpec::Core::Example] example - a RSpec example
|
|
37
|
-
def call(example_map, example)
|
|
38
|
-
classes = object_tracker.used_classes_during do
|
|
39
|
-
yield example_map, example
|
|
40
|
-
end
|
|
41
|
-
example_map.push(*execution_detector.detect(classes))
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
end
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Crystalball
|
|
4
|
-
class MapGenerator
|
|
5
|
-
class FactoryBotStrategy
|
|
6
|
-
module DSLPatch
|
|
7
|
-
# This module is used to fetch file path with factory definition from callstack
|
|
8
|
-
module FactoryPathFetcher
|
|
9
|
-
# Fetches file path with factory definition from callstack
|
|
10
|
-
#
|
|
11
|
-
# @return [String]
|
|
12
|
-
def self.fetch
|
|
13
|
-
factories_definition_paths = FactoryBotStrategy
|
|
14
|
-
.factory_bot_constant
|
|
15
|
-
.definition_file_paths
|
|
16
|
-
.map { |path| Pathname(path).expand_path.to_s }
|
|
17
|
-
|
|
18
|
-
factory_definition_call = caller.find do |method_call|
|
|
19
|
-
factories_definition_paths.any? do |path|
|
|
20
|
-
method_call.start_with?(path)
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
factory_definition_call.split(":").first
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "crystalball/map_generator/factory_bot_strategy/dsl_patch/factory_path_fetcher"
|
|
4
|
-
|
|
5
|
-
module Crystalball
|
|
6
|
-
class MapGenerator
|
|
7
|
-
class FactoryBotStrategy
|
|
8
|
-
# Module to add new `factory` method to FactoryBot::Syntax::Default::DSL and FactoryBot::Syntax::Default::ModifyDSL
|
|
9
|
-
module DSLPatch
|
|
10
|
-
class << self
|
|
11
|
-
# Patches `FactoryBot::Syntax::Default::DSL#factory` and `FactoryBot::Syntax::Default::ModifyDSL#factory`.
|
|
12
|
-
def apply!
|
|
13
|
-
classes_to_patch.each { |klass| klass.prepend DSLPatch }
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
private
|
|
17
|
-
|
|
18
|
-
def classes_to_patch
|
|
19
|
-
[
|
|
20
|
-
FactoryBotStrategy.factory_bot_constant::Syntax::Default::DSL,
|
|
21
|
-
FactoryBotStrategy.factory_bot_constant::Syntax::Default::ModifyDSL
|
|
22
|
-
]
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
# Overrides `FactoryBot::Syntax::Default::DSL#factory` and `FactoryBot::Syntax::Default::ModifyDSL#factory`.
|
|
27
|
-
# Pushes path of a factory to `FactoryBotStrategy.factory_definitions` and calls original `factory`
|
|
28
|
-
def factory(*args, &block)
|
|
29
|
-
factory_path = FactoryPathFetcher.fetch
|
|
30
|
-
name = args.first.to_s
|
|
31
|
-
|
|
32
|
-
FactoryBotStrategy.factory_definitions[name] ||= []
|
|
33
|
-
FactoryBotStrategy.factory_definitions[name] << factory_path
|
|
34
|
-
|
|
35
|
-
super
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Crystalball
|
|
4
|
-
class MapGenerator
|
|
5
|
-
class FactoryBotStrategy
|
|
6
|
-
# A helper module to load `factory_bot` or `factory_girl`
|
|
7
|
-
module FactoryGemLoader
|
|
8
|
-
class << self
|
|
9
|
-
NAMES = %w[factory_bot factory_girl].freeze
|
|
10
|
-
|
|
11
|
-
# Tries to require `factory_bot` first. Requires `factory_girl` if `factory_bot` is not available
|
|
12
|
-
# Raises `LoadError` if both of them are not available.
|
|
13
|
-
def require!
|
|
14
|
-
NAMES.any? do |factory_gem_name|
|
|
15
|
-
begin
|
|
16
|
-
require factory_gem_name
|
|
17
|
-
true
|
|
18
|
-
rescue LoadError
|
|
19
|
-
false
|
|
20
|
-
end
|
|
21
|
-
end || (raise LoadError, "Can't load `factory_bot` or `factory_girl`")
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Crystalball
|
|
4
|
-
class MapGenerator
|
|
5
|
-
class FactoryBotStrategy
|
|
6
|
-
# Module to add new `run` method to FactoryBot::FactoryRunner
|
|
7
|
-
module FactoryRunnerPatch
|
|
8
|
-
class << self
|
|
9
|
-
# Patches `FactoryBot::FactoryRunner#run`.
|
|
10
|
-
def apply!
|
|
11
|
-
FactoryBotStrategy.factory_bot_constant::FactoryRunner.prepend FactoryRunnerPatch
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
# Overrides `FactoryBot::FactoryRunner#run`. Pushes factory name to
|
|
16
|
-
# `FactoryBotStrategy.used_factories` and calls original `run`
|
|
17
|
-
def run(*)
|
|
18
|
-
factory = FactoryBotStrategy.factory_bot_constant.factory_by_name(@name)
|
|
19
|
-
FactoryBotStrategy.used_factories << factory.name.to_s
|
|
20
|
-
super
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "crystalball/map_generator/factory_bot_strategy/factory_gem_loader"
|
|
4
|
-
|
|
5
|
-
Crystalball::MapGenerator::FactoryBotStrategy::FactoryGemLoader.require!
|
|
6
|
-
|
|
7
|
-
require "crystalball/map_generator/base_strategy"
|
|
8
|
-
require "crystalball/map_generator/helpers/path_filter"
|
|
9
|
-
require "crystalball/map_generator/factory_bot_strategy/dsl_patch"
|
|
10
|
-
require "crystalball/map_generator/factory_bot_strategy/factory_runner_patch"
|
|
11
|
-
|
|
12
|
-
module Crystalball
|
|
13
|
-
class MapGenerator
|
|
14
|
-
# Map generator strategy to include list of strategies which was used in an example.
|
|
15
|
-
class FactoryBotStrategy
|
|
16
|
-
include ::Crystalball::MapGenerator::BaseStrategy
|
|
17
|
-
include ::Crystalball::MapGenerator::Helpers::PathFilter
|
|
18
|
-
|
|
19
|
-
class << self
|
|
20
|
-
def factory_bot_constant
|
|
21
|
-
defined?(::FactoryBot) ? ::FactoryBot : ::FactoryGirl
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# List of factories used by current example
|
|
25
|
-
#
|
|
26
|
-
# @return [Array<String>]
|
|
27
|
-
def used_factories
|
|
28
|
-
@used_factories ||= []
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# Map of factories to files
|
|
32
|
-
#
|
|
33
|
-
# @return [Hash<String, String>]
|
|
34
|
-
def factory_definitions
|
|
35
|
-
@factory_definitions ||= {}
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
# Reset cached list of factories
|
|
39
|
-
def reset_used_factories
|
|
40
|
-
@used_factories = []
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
def after_register
|
|
45
|
-
DSLPatch.apply!
|
|
46
|
-
FactoryRunnerPatch.apply!
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
# Adds factories related to the spec to the map
|
|
50
|
-
# @param [Crystalball::ExampleGroupMap] example_map - object holding example metadata and used files
|
|
51
|
-
# @param [RSpec::Core::Example] example - a RSpec example
|
|
52
|
-
def call(example_map, example)
|
|
53
|
-
self.class.reset_used_factories
|
|
54
|
-
yield example_map, example
|
|
55
|
-
example_map.push(*filter(self.class.used_factories.flat_map { |f| self.class.factory_definitions[f] }))
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
end
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "parser/current"
|
|
4
|
-
|
|
5
|
-
module Crystalball
|
|
6
|
-
class MapGenerator
|
|
7
|
-
class ParserStrategy
|
|
8
|
-
# Parses the given source files and adds the class and module definitions
|
|
9
|
-
# to the `consts_defined` array.
|
|
10
|
-
class Processor
|
|
11
|
-
def consts_defined_in(path)
|
|
12
|
-
self.current_scope = nil
|
|
13
|
-
self.consts_defined = []
|
|
14
|
-
parse_and_process(path)
|
|
15
|
-
consts_defined
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def consts_interacted_with_in(path)
|
|
19
|
-
self.current_scope = nil
|
|
20
|
-
self.consts_interacted_with = []
|
|
21
|
-
parse_and_process(path)
|
|
22
|
-
consts_interacted_with
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
protected
|
|
26
|
-
|
|
27
|
-
attr_accessor :consts_defined, :consts_interacted_with, :current_scope
|
|
28
|
-
|
|
29
|
-
private
|
|
30
|
-
|
|
31
|
-
def on_send(node)
|
|
32
|
-
const = filtered_children(node).detect { |c| c.type == :const }
|
|
33
|
-
return unless const
|
|
34
|
-
|
|
35
|
-
add_constant_interacted(qualified_name_from_node(const), nil)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def on_casgn(node)
|
|
39
|
-
namespace, name, = node.children
|
|
40
|
-
scope_name = namespace ? qualified_name(qualified_name_from_node(namespace), current_scope) : current_scope
|
|
41
|
-
add_constant_defined(name, scope_name)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
def on_class(node)
|
|
45
|
-
const, superclass, body = node.children
|
|
46
|
-
add_constant_interacted(qualified_name_from_node(superclass), current_scope) if superclass
|
|
47
|
-
process_class_or_module(const, body)
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
def on_module(node)
|
|
51
|
-
const, body = node.children
|
|
52
|
-
process_class_or_module(const, body)
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def process_class_or_module(const, body)
|
|
56
|
-
const_name = qualified_name_from_node(const)
|
|
57
|
-
result = add_constant_defined(const_name, current_scope)
|
|
58
|
-
self.current_scope = result if body && nested_consts?(body)
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def nested_consts?(node)
|
|
62
|
-
filtered_children(node).any? { |c| %i[const casgn].include?(c.type) || nested_consts?(c) }
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def filtered_children(node)
|
|
66
|
-
return [] unless node.is_a?(Parser::AST::Node)
|
|
67
|
-
|
|
68
|
-
node.children.grep(Parser::AST::Node)
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def parse_and_process(path)
|
|
72
|
-
node = Parser::CurrentRuby.parse(File.read(path))
|
|
73
|
-
process_node_and_children(node)
|
|
74
|
-
rescue Parser::SyntaxError
|
|
75
|
-
nil
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
# @param [AST::Node, nil] node
|
|
79
|
-
# @return [String, nil]
|
|
80
|
-
def process(node)
|
|
81
|
-
return if node.nil?
|
|
82
|
-
|
|
83
|
-
on_handler = :"on_#{node.type}"
|
|
84
|
-
__send__(on_handler, node) if respond_to?(on_handler, true)
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
def process_node_and_children(node)
|
|
88
|
-
process(node)
|
|
89
|
-
filtered_children(node).each do |child|
|
|
90
|
-
process_node_and_children(child)
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
def add_constant_defined(name, scope)
|
|
95
|
-
add_constant(name, scope, collection: consts_defined)
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
def add_constant_interacted(name, scope)
|
|
99
|
-
add_constant(name, scope, collection: consts_interacted_with)
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
def add_constant(name, scope, collection:)
|
|
103
|
-
collection ||= []
|
|
104
|
-
qualified_const_name = qualified_name(name, scope)
|
|
105
|
-
collection << qualified_const_name
|
|
106
|
-
qualified_const_name
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
# @param [Parser::AST::Node] node - :const node in format s(:const, scope, :ConstName)
|
|
110
|
-
# where scope can be `nil` or another :const node.
|
|
111
|
-
# For example, `Foo::Bar` is represented as `s(:const, s(:const, nil, :Foo), :Bar)`
|
|
112
|
-
def qualified_name_from_node(node)
|
|
113
|
-
return unless node.is_a?(Parser::AST::Node)
|
|
114
|
-
|
|
115
|
-
scope, name = node.to_a
|
|
116
|
-
return name.to_s unless scope
|
|
117
|
-
|
|
118
|
-
qualified_name(name, qualified_name_from_node(scope))
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
def qualified_name(name, scope = nil)
|
|
122
|
-
return "#{scope.sub(/\A::/, '')}::#{name}" if scope
|
|
123
|
-
|
|
124
|
-
name.to_s
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
end
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "crystalball/map_generator/parser_strategy/processor"
|
|
4
|
-
require "crystalball/map_generator/helpers/path_filter"
|
|
5
|
-
|
|
6
|
-
module Crystalball
|
|
7
|
-
class MapGenerator
|
|
8
|
-
# Map generator strategy based on parsing source files to detect constant definition
|
|
9
|
-
# and tracing method calls on those constants.
|
|
10
|
-
class ParserStrategy
|
|
11
|
-
include BaseStrategy
|
|
12
|
-
include Helpers::PathFilter
|
|
13
|
-
|
|
14
|
-
attr_reader :const_definition_paths
|
|
15
|
-
|
|
16
|
-
def initialize(root = Dir.pwd, pattern:)
|
|
17
|
-
@root_path = Pathname.new(root).realpath.to_s
|
|
18
|
-
@processor = Processor.new
|
|
19
|
-
@const_definition_paths = {}
|
|
20
|
-
@pattern = pattern
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def after_register
|
|
24
|
-
files_to_inspect.each do |path|
|
|
25
|
-
processor.consts_defined_in(path).each do |const|
|
|
26
|
-
(const_definition_paths[const] ||= []) << path
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# Parses the current example group map seeking calls to class methods and adds
|
|
32
|
-
# the classes to the map.
|
|
33
|
-
# @param [Crystalball::ExampleGroupMap] example_map - object holding example metadata and used files
|
|
34
|
-
# @param [RSpec::Core::Example] example - a RSpec example
|
|
35
|
-
def call(example_map, example)
|
|
36
|
-
paths = []
|
|
37
|
-
yield example_map, example
|
|
38
|
-
example_map.each do |path|
|
|
39
|
-
next unless path.end_with?(".rb")
|
|
40
|
-
|
|
41
|
-
used_consts = processor.consts_interacted_with_in(path)
|
|
42
|
-
paths.push(*used_files(used_consts))
|
|
43
|
-
end
|
|
44
|
-
example_map.push(*filter(paths))
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
private
|
|
48
|
-
|
|
49
|
-
attr_reader :processor, :pattern, :root_path
|
|
50
|
-
|
|
51
|
-
def used_files(used_consts)
|
|
52
|
-
const_definition_paths.select { |k, _| Array(used_consts).include?(k) }.values.flatten
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def files_to_inspect
|
|
56
|
-
Dir.glob(File.join(root_path, "**/*.rb")).grep(pattern)
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
end
|