factory_trace 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7a4fdc48b79a78601b3d2340cbcdc79d8b360774b9902529e46a1451b8b8d17
4
- data.tar.gz: cb65b8f2ba7ddcb4330c5c5a1655c2a608a8d53073ce48c819f07c9ea8ef848a
3
+ metadata.gz: 2a8964b9b2f6e4000d28ab545d781474d9c18668dbdb96a34de0c03b4282237f
4
+ data.tar.gz: 72a2d76a083cf9266e5296b3c538d7f9f49102acdd1328cd385fc1f9e8e3e6a8
5
5
  SHA512:
6
- metadata.gz: 0674a565410c715b9ef283259dfb18a40b121b781ebcb19f09b1f91993f5393e7cdacb6f9bf32dd02fb8e680042997345a9669229c87f16a0bca8ebdfd4fe82e
7
- data.tar.gz: 5a9c2d9fec08b8b0aea8935411b99cee0f863efb1e95bb78f868c7bf6f8ee44d9dbf49f28a46123505ed03071001fd728a646b37a3ef4fabdde1137c9561eb28
6
+ metadata.gz: 6096b82a57a43ab7ae87ef886f98ccda54dc0babb5e7a211d15a81464086e1fe479226af114fa745e5a47ce209df2fdcf0e81136f2058959e428147b98de248a
7
+ data.tar.gz: b683fd747531cbc50851834f2bbf0aa4a2f17c03e8eadbd7bc5ac2571312abd9046d2d4a46bd8499d0640e9f5e43e46e952583794a3a454ccc3c286029f42f6e
@@ -7,6 +7,7 @@ require 'factory_trace/configuration'
7
7
  require 'factory_trace/version'
8
8
  require 'factory_trace/helpers/converter'
9
9
  require 'factory_trace/helpers/statusable'
10
+ require 'factory_trace/helpers/caller'
10
11
  require 'factory_trace/tracker'
11
12
 
12
13
  require 'factory_trace/structures/factory'
@@ -22,6 +23,12 @@ require 'factory_trace/readers/trace_reader'
22
23
  require 'factory_trace/writers/writer'
23
24
  require 'factory_trace/writers/report_writer'
24
25
  require 'factory_trace/writers/trace_writer'
26
+
27
+ require 'factory_trace/monkey_patches/factory'
28
+ require 'factory_trace/monkey_patches/trait'
29
+ require 'factory_trace/monkey_patches/definition_proxy'
30
+ require 'factory_trace/monkey_patches/dsl'
31
+
25
32
  # Integrations
26
33
  require 'integrations/rspec' if defined?(RSpec::Core)
27
34
 
@@ -29,6 +36,7 @@ module FactoryTrace
29
36
  class << self
30
37
  def start
31
38
  return unless configuration.enabled
39
+ trace_definitions! if configuration.trace_definition?
32
40
 
33
41
  tracker.track!
34
42
  end
@@ -67,5 +75,12 @@ module FactoryTrace
67
75
  def tracker
68
76
  @tracker ||= Tracker.new
69
77
  end
78
+
79
+ def trace_definitions!
80
+ FactoryBot::Factory.prepend(FactoryTrace::MonkeyPatches::Factory)
81
+ FactoryBot::Trait.prepend(FactoryTrace::MonkeyPatches::Trait)
82
+ FactoryBot::Syntax::Default::DSL.prepend(FactoryTrace::MonkeyPatches::Default::DSL)
83
+ FactoryBot::DefinitionProxy.prepend(FactoryTrace::MonkeyPatches::DefinitionProxy)
84
+ end
70
85
  end
71
86
  end
@@ -1,12 +1,17 @@
1
1
  module FactoryTrace
2
2
  class Configuration
3
- attr_accessor :path, :enabled, :color, :mode
3
+ attr_accessor :path, :enabled, :color, :mode, :trace_definition
4
4
 
5
5
  def initialize
6
6
  @enabled = ENV.key?('FB_TRACE') || ENV.key?('FB_TRACE_FILE')
7
7
  @path = ENV['FB_TRACE_FILE']
8
8
  @color = path.nil?
9
9
  @mode = extract_mode(ENV['FB_TRACE']) || :full
10
+ @trace_definition = true
11
+ end
12
+
13
+ def trace_definition?
14
+ @trace_definition
10
15
  end
11
16
 
12
17
  def out
@@ -0,0 +1,13 @@
1
+ module FactoryTrace
2
+ module Helpers
3
+ module Caller
4
+ module_function
5
+
6
+ # @return [String] file and line where the original method was called
7
+ def location
8
+ location = caller_locations[1]
9
+ "#{location.absolute_path}:#{location.lineno}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -9,7 +9,8 @@ module FactoryTrace
9
9
  def trait(trait)
10
10
  FactoryTrace::Structures::Trait.new(
11
11
  trait.name.to_s,
12
- declaration_names: extract_declarations(trait)
12
+ declaration_names: extract_declarations(trait),
13
+ definition_path: (trait.definition_path if trait.respond_to?(:definition_path))
13
14
  )
14
15
  end
15
16
 
@@ -21,7 +22,8 @@ module FactoryTrace
21
22
  factory.names.map(&:to_s),
22
23
  factory.defined_traits.map(&method(:trait)),
23
24
  parent_name: factory.send(:parent).respond_to?(:name) ? factory.send(:parent).name.to_s : nil,
24
- declaration_names: extract_declarations(factory)
25
+ declaration_names: extract_declarations(factory),
26
+ definition_path: (factory.definition_path if factory.respond_to?(:definition_path))
25
27
  )
26
28
  end
27
29
 
@@ -0,0 +1,13 @@
1
+ module FactoryTrace
2
+ module MonkeyPatches
3
+ module DefinitionProxy
4
+ def factory(name, options = {}, &block)
5
+ @child_factories << [name, Helpers::Caller.location, options, block]
6
+ end
7
+
8
+ def trait(name, &block)
9
+ @definition.define_trait(FactoryBot::Trait.new(name, Helpers::Caller.location, &block))
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ module FactoryTrace
2
+ module MonkeyPatches
3
+ module Default
4
+ module DSL
5
+ def factory(name, options = {}, &block)
6
+ caller_location = options.delete(:caller_location) || Helpers::Caller.location
7
+ factory = FactoryBot::Factory.new(name, caller_location, options)
8
+ proxy = FactoryBot::DefinitionProxy.new(factory.definition)
9
+ proxy.instance_eval(&block) if block_given?
10
+
11
+ FactoryBot.register_factory(factory)
12
+
13
+ proxy.child_factories.each do |(child_name, child_caller_location, child_options, child_block)|
14
+ parent_factory = child_options.delete(:parent) || name
15
+ factory(child_name, child_options.merge(parent: parent_factory, caller_location: child_caller_location), &child_block)
16
+ end
17
+ end
18
+
19
+ def trait(name, &block)
20
+ FactoryBot.register_trait(FactoryBot::Trait.new(name, Helpers::Caller.location, &block))
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ module FactoryTrace
2
+ module MonkeyPatches
3
+ module Factory
4
+ attr_reader :definition_path
5
+
6
+ def initialize(name, definition_path, options = {})
7
+ @definition_path = definition_path
8
+ super(name, options)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module FactoryTrace
2
+ module MonkeyPatches
3
+ module Trait
4
+ attr_reader :definition_path
5
+
6
+ def initialize(name, definition_path, &block)
7
+ @definition_path = definition_path
8
+ super(name, &block)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -13,15 +13,15 @@ module FactoryTrace
13
13
  output = []
14
14
 
15
15
  defined.factories.each do |factory|
16
- output << {code: :unused, factory_names: factory.names} unless factory.status
16
+ output << append_definition_path({code: :unused, factory_names: factory.names}, factory) unless factory.status
17
17
 
18
18
  factory.traits.each do |trait|
19
- output << {code: :unused, factory_names: factory.names, trait_name: trait.name} unless trait.status
19
+ output << append_definition_path({code: :unused, factory_names: factory.names, trait_name: trait.name}, trait) unless trait.status
20
20
  end
21
21
  end
22
22
 
23
23
  defined.traits.each do |trait|
24
- output << {code: :unused, trait_name: trait.name} unless trait.status
24
+ output << append_definition_path({code: :unused, trait_name: trait.name}, trait) unless trait.status
25
25
  end
26
26
 
27
27
  output.unshift(code: :unused, value: output.size)
@@ -104,6 +104,15 @@ module FactoryTrace
104
104
  mark_trait(declaration_trait, declaration_factory, collection, status: status) if declaration_trait
105
105
  end
106
106
  end
107
+
108
+ # @param [Hash]
109
+ # @param [FactoryTrace::Structures::Factory|FactoryTrace::Structures::Trait]
110
+ #
111
+ # @return [Hash]
112
+ def self.append_definition_path(hash, object)
113
+ hash[:definition_path] = object.definition_path if object.definition_path
114
+ hash
115
+ end
107
116
  end
108
117
  end
109
118
  end
@@ -42,7 +42,7 @@ module FactoryTrace
42
42
  private
43
43
 
44
44
  def parse_trait(hash)
45
- FactoryTrace::Structures::Trait.new(hash['name'], declaration_names: hash['declaration_names'])
45
+ FactoryTrace::Structures::Trait.new(hash['name'], declaration_names: hash['declaration_names'], definition_path: hash['definition_path'])
46
46
  end
47
47
 
48
48
  def parse_factory(hash)
@@ -50,7 +50,8 @@ module FactoryTrace
50
50
  hash['names'],
51
51
  hash['traits'].map(&method(:parse_trait)),
52
52
  parent_name: hash['parent_name'],
53
- declaration_names: hash['declaration_names']
53
+ declaration_names: hash['declaration_names'],
54
+ definition_path: hash['definition_path']
54
55
  )
55
56
  end
56
57
 
@@ -3,17 +3,19 @@ module FactoryTrace
3
3
  class Factory
4
4
  include Helpers::Statusable
5
5
 
6
- attr_reader :names, :parent_name, :traits, :declaration_names
6
+ attr_reader :names, :parent_name, :traits, :declaration_names, :definition_path
7
7
 
8
8
  # @param [Array<String>] names
9
9
  # @param [Array<FactoryTrace::Structure::Trait>] traits
10
10
  # @param [String|nil] parent_name
11
11
  # @param [Array<String>] declaration_names
12
- def initialize(names, traits, parent_name: nil, declaration_names: [])
12
+ # @param [String] definition_path
13
+ def initialize(names, traits, parent_name: nil, declaration_names: [], definition_path: nil)
13
14
  @names = names
14
15
  @traits = traits
15
16
  @parent_name = parent_name
16
17
  @declaration_names = declaration_names
18
+ @definition_path = definition_path
17
19
  end
18
20
 
19
21
  # @return [Hash<Symbol, Object>]
@@ -22,7 +24,8 @@ module FactoryTrace
22
24
  names: names,
23
25
  traits: traits.map(&:to_h),
24
26
  parent_name: parent_name,
25
- declaration_names: declaration_names
27
+ declaration_names: declaration_names,
28
+ definition_path: definition_path
26
29
  }
27
30
  end
28
31
 
@@ -42,7 +45,8 @@ module FactoryTrace
42
45
  names == factory.names &&
43
46
  traits == factory.traits &&
44
47
  parent_name == factory.parent_name &&
45
- declaration_names == factory.declaration_names
48
+ declaration_names == factory.declaration_names &&
49
+ definition_path == factory.definition_path
46
50
  end
47
51
  end
48
52
  end
@@ -3,20 +3,23 @@ module FactoryTrace
3
3
  class Trait
4
4
  include Helpers::Statusable
5
5
 
6
- attr_reader :name, :declaration_names
6
+ attr_reader :name, :declaration_names, :definition_path
7
7
 
8
8
  # @param [String] name
9
9
  # @param [Array<String>] declaration_names
10
- def initialize(name, declaration_names: [])
10
+ # @param [String] definition_path
11
+ def initialize(name, declaration_names: [], definition_path: nil)
11
12
  @name = name
12
13
  @declaration_names = declaration_names
14
+ @definition_path = definition_path
13
15
  end
14
16
 
15
17
  # @return [Hash<Symbol, Object>]
16
18
  def to_h
17
19
  {
18
20
  name: name,
19
- declaration_names: declaration_names
21
+ declaration_names: declaration_names,
22
+ definition_path: definition_path
20
23
  }
21
24
  end
22
25
 
@@ -24,7 +27,7 @@ module FactoryTrace
24
27
  def ==(trait)
25
28
  return false unless trait.is_a?(FactoryTrace::Structures::Trait)
26
29
 
27
- name == trait.name && declaration_names == trait.declaration_names
30
+ name == trait.name && declaration_names == trait.declaration_names && definition_path == trait.definition_path
28
31
  end
29
32
  end
30
33
  end
@@ -1,3 +1,3 @@
1
1
  module FactoryTrace
2
- VERSION = '0.3.3'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -31,11 +31,11 @@ module FactoryTrace
31
31
  when result[:value]
32
32
  colorize(total_color, "total number of unique #{humanize_code(result[:code])} factories & traits: #{result[:value]}")
33
33
  when result[:factory_names] && result[:trait_name]
34
- "#{humanize_code(result[:code])} trait #{colorize(:blue, result[:trait_name])} of factory #{list(result[:factory_names])}"
34
+ append_definition_path(result) { "#{humanize_code(result[:code])} trait #{colorize(:blue, result[:trait_name])} of factory #{list(result[:factory_names])}" }
35
35
  when result[:factory_names]
36
- "#{humanize_code(result[:code])} factory #{list(result[:factory_names])}"
36
+ append_definition_path(result) { "#{humanize_code(result[:code])} factory #{list(result[:factory_names])}" }
37
37
  else
38
- "#{humanize_code(result[:code])} global trait #{colorize(:blue, result[:trait_name])}"
38
+ append_definition_path(result) { "#{humanize_code(result[:code])} global trait #{colorize(:blue, result[:trait_name])}" }
39
39
  end
40
40
  end
41
41
 
@@ -45,6 +45,13 @@ module FactoryTrace
45
45
  "#{COLORS[color]}#{msg}\e[0m"
46
46
  end
47
47
 
48
+ def append_definition_path(result)
49
+ msg = yield
50
+ return msg unless configuration.trace_definition? && result[:definition_path]
51
+
52
+ "#{msg} => #{result[:definition_path]}"
53
+ end
54
+
48
55
  def humanize_code(code)
49
56
  CODES[code]
50
57
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_trace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - djezzzl
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-28 00:00:00.000000000 Z
11
+ date: 2019-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: factory_bot
@@ -77,8 +77,13 @@ files:
77
77
  - exe/factory_trace
78
78
  - lib/factory_trace.rb
79
79
  - lib/factory_trace/configuration.rb
80
+ - lib/factory_trace/helpers/caller.rb
80
81
  - lib/factory_trace/helpers/converter.rb
81
82
  - lib/factory_trace/helpers/statusable.rb
83
+ - lib/factory_trace/monkey_patches/definition_proxy.rb
84
+ - lib/factory_trace/monkey_patches/dsl.rb
85
+ - lib/factory_trace/monkey_patches/factory.rb
86
+ - lib/factory_trace/monkey_patches/trait.rb
82
87
  - lib/factory_trace/preprocessors/extract_defined.rb
83
88
  - lib/factory_trace/preprocessors/extract_used.rb
84
89
  - lib/factory_trace/processors/find_unused.rb