factory_trace 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/factory_trace.rb +12 -2
- data/lib/factory_trace/find_unused.rb +13 -53
- data/lib/factory_trace/readers/trace_reader.rb +23 -7
- data/lib/factory_trace/storage_handler.rb +61 -0
- data/lib/factory_trace/version.rb +1 -1
- data/lib/factory_trace/writers/report_writer.rb +5 -5
- data/lib/factory_trace/writers/trace_writer.rb +12 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 484192f056c62919a10db1bcabe6d3f769fc7be2973e1278e9eff0874c5a0d96
|
4
|
+
data.tar.gz: 1b339e7c7b1d0a0fa8828631fff9a33926347cdafc22446061aa57b7483c2686
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 866ba5578d028ca9eb9c623ea38c03ac5df59383831d3d0b11ee765e9f181e69260e49621fea2d353836361f8b6836b6b092fe73f7be948757e0782a6b5f0e05
|
7
|
+
data.tar.gz: dd8cf671469977ab78a6821398a27e39c999b981eb6938bf016ab41be56684d16f259cce7663c154f6f9fd1c1b1d94f62288177e6a3e6e31a841affeb8c3f3b9
|
data/lib/factory_trace.rb
CHANGED
@@ -4,7 +4,10 @@ require 'factory_bot'
|
|
4
4
|
require 'factory_trace/configuration'
|
5
5
|
require 'factory_trace/version'
|
6
6
|
require 'factory_trace/tracker'
|
7
|
+
|
7
8
|
require 'factory_trace/find_unused'
|
9
|
+
require 'factory_trace/storage_handler'
|
10
|
+
|
8
11
|
require 'factory_trace/readers/trace_reader'
|
9
12
|
require 'factory_trace/writers/writer'
|
10
13
|
require 'factory_trace/writers/report_writer'
|
@@ -23,6 +26,9 @@ module FactoryTrace
|
|
23
26
|
def stop
|
24
27
|
return unless configuration.enabled
|
25
28
|
|
29
|
+
# This is required to exclude parent traits from +defined_traits+
|
30
|
+
FactoryBot.reload
|
31
|
+
|
26
32
|
writer.write(results)
|
27
33
|
end
|
28
34
|
|
@@ -38,12 +44,16 @@ module FactoryTrace
|
|
38
44
|
|
39
45
|
def results
|
40
46
|
if configuration.mode?(:full)
|
41
|
-
FindUnused.call(
|
47
|
+
FindUnused.call(preprocessed)
|
42
48
|
elsif configuration.mode?(:trace_only)
|
43
|
-
|
49
|
+
preprocessed
|
44
50
|
end
|
45
51
|
end
|
46
52
|
|
53
|
+
def preprocessed
|
54
|
+
@preprocessed ||= StorageHandler.prepare(tracker.storage)
|
55
|
+
end
|
56
|
+
|
47
57
|
def tracker
|
48
58
|
@tracker ||= Tracker.new
|
49
59
|
end
|
@@ -1,76 +1,36 @@
|
|
1
1
|
module FactoryTrace
|
2
2
|
class FindUnused
|
3
|
-
# @param [Hash
|
3
|
+
# @param [Array<Hash>] initial_data
|
4
4
|
# @return [Array<Hash>]
|
5
5
|
def self.call(initial_data)
|
6
|
-
|
7
|
-
FactoryBot.reload
|
8
|
-
|
9
|
-
data = prepare(initial_data)
|
6
|
+
all, used = initial_data
|
10
7
|
|
11
8
|
output = []
|
12
9
|
|
13
|
-
|
14
|
-
unless
|
15
|
-
output << {code: :unused,
|
10
|
+
all.each do |factory_name, trait_names|
|
11
|
+
unless used.key?(factory_name)
|
12
|
+
output << {code: :unused, factory_name: factory_name}
|
16
13
|
next
|
17
14
|
end
|
18
15
|
|
19
|
-
factory.defined_traits.each do |trait|
|
20
|
-
output << {code: :unused, factory: factory, trait: trait} unless data[factory.name.to_s].include?(trait.name.to_s)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
FactoryBot.traits.each do |trait|
|
25
|
-
output << {code: :unused, trait: trait} unless data['_traits'].include?(trait.name.to_s)
|
26
|
-
end
|
27
|
-
|
28
|
-
output.unshift(code: :unused, value: output.size)
|
29
|
-
output.unshift(code: :used, value: data['_total_used'])
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
# Return new structure where each trait is moved to its own factory
|
35
|
-
#
|
36
|
-
# @param [Hash<Symbol, Set<Symbol>>]
|
37
|
-
# @return [Hash<String, Set<String>>]
|
38
|
-
def self.prepare(data)
|
39
|
-
# +_traits+ is for global traits
|
40
|
-
output = {'_traits' => Set.new, '_total_used' => count_total(data)}
|
41
|
-
|
42
|
-
data.each do |factory_name, trait_names|
|
43
|
-
factory_name = factory_name.to_s
|
44
|
-
|
45
|
-
output[factory_name] ||= Set.new
|
46
|
-
|
47
16
|
trait_names.each do |trait_name|
|
48
|
-
|
49
|
-
|
50
|
-
factory = FactoryBot.factories[factory_name]
|
51
|
-
trait_found = false
|
52
|
-
|
53
|
-
until factory.is_a?(FactoryBot::NullFactory)
|
54
|
-
if factory.defined_traits.any? { |trait| trait.name.to_s == trait_name }
|
55
|
-
trait_found = true
|
56
|
-
output[factory.name.to_s] ||= Set.new
|
57
|
-
output[factory.name.to_s] << trait_name
|
58
|
-
break
|
59
|
-
end
|
60
|
-
factory = factory.send(:parent)
|
17
|
+
unless used[factory_name].include?(trait_name)
|
18
|
+
output << {code: :unused, trait_name: trait_name}.tap { |h| h[:factory_name] = factory_name unless factory_name == '_traits' }
|
61
19
|
end
|
62
|
-
|
63
|
-
# Belongs to global traits
|
64
|
-
output['_traits'] << trait_name unless trait_found
|
65
20
|
end
|
66
21
|
end
|
67
22
|
|
23
|
+
output.unshift(code: :unused, value: output.size)
|
24
|
+
output.unshift(code: :used, value: count_total(used))
|
25
|
+
|
68
26
|
output
|
69
27
|
end
|
70
28
|
|
29
|
+
private
|
30
|
+
|
71
31
|
# @param [Hash<Symbol, Set<Symbol>>]
|
72
32
|
def self.count_total(data)
|
73
|
-
data.reduce(0) { |result, (
|
33
|
+
data.reduce(0) { |result, (factory_name, trait_names)| result + (factory_name != '_traits' ? 1 : 0) + trait_names.size }
|
74
34
|
end
|
75
35
|
end
|
76
36
|
end
|
@@ -2,10 +2,18 @@ require 'set'
|
|
2
2
|
|
3
3
|
module FactoryTrace
|
4
4
|
class TraceReader
|
5
|
+
# Read the data from files and merge it
|
6
|
+
# First hash - all factories
|
7
|
+
# Second hash - used factories
|
8
|
+
#
|
9
|
+
# @return [Array<Hash>]
|
5
10
|
def self.read_from_files(*file_names, config: Configuration.new)
|
6
|
-
file_names.reduce({}) do |
|
11
|
+
file_names.reduce([{}, {}]) do |array, file_name|
|
7
12
|
reader = new(File.open(file_name, 'r'), config: config)
|
8
|
-
|
13
|
+
new = reader.read
|
14
|
+
array.each_with_index.map do |hash, index|
|
15
|
+
hash.merge(new[index]) { |_key, v1, v2| v1 | v2 }
|
16
|
+
end
|
9
17
|
end
|
10
18
|
end
|
11
19
|
|
@@ -14,19 +22,27 @@ module FactoryTrace
|
|
14
22
|
@config = config
|
15
23
|
end
|
16
24
|
|
25
|
+
# Read the data from file
|
26
|
+
# First hash - all factories
|
27
|
+
# Second hash - used factories
|
28
|
+
#
|
29
|
+
# @return [Array<Hash>]
|
17
30
|
def read
|
18
|
-
|
31
|
+
data = [{}, {}]
|
32
|
+
point = -1
|
19
33
|
|
20
34
|
io.each_line do |line|
|
21
|
-
|
35
|
+
next point += 1 if line.match?(/-all-|-used-/)
|
36
|
+
|
37
|
+
factory, *traits = line.strip.split(',')
|
22
38
|
|
23
39
|
if factory
|
24
|
-
|
25
|
-
|
40
|
+
data[point][factory] ||= Set.new
|
41
|
+
data[point][factory] |= traits
|
26
42
|
end
|
27
43
|
end
|
28
44
|
|
29
|
-
|
45
|
+
data
|
30
46
|
end
|
31
47
|
|
32
48
|
private
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module FactoryTrace
|
4
|
+
class StorageHandler
|
5
|
+
# @param [Hash<Symbol, Set<Symbol>>] initial_data
|
6
|
+
#
|
7
|
+
# First hash - all factories
|
8
|
+
# Second hash - used factories
|
9
|
+
#
|
10
|
+
# @return [Array<Hash>]
|
11
|
+
def self.prepare(initial_data)
|
12
|
+
[collect_all, convert(initial_data)]
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def self.collect_all
|
18
|
+
FactoryBot.factories.reduce({'_traits' => Set.new(FactoryBot.traits.map(&:name).map(&:to_s))}) do |hash, factory|
|
19
|
+
hash[factory.name.to_s] = Set.new(factory.defined_traits.map(&:name).map(&:to_s))
|
20
|
+
hash
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Return new structure where each trait is moved to its own factory
|
25
|
+
#
|
26
|
+
# @param [Hash<Symbol, Set<Symbol>>]
|
27
|
+
# @return [Hash<String, Set<String>>]
|
28
|
+
def self.convert(data)
|
29
|
+
# +_traits+ is for global traits
|
30
|
+
output = {'_traits' => Set.new}
|
31
|
+
|
32
|
+
data.each do |factory_name, trait_names|
|
33
|
+
factory_name = factory_name.to_s
|
34
|
+
|
35
|
+
output[factory_name] ||= Set.new
|
36
|
+
|
37
|
+
trait_names.each do |trait_name|
|
38
|
+
trait_name = trait_name.to_s
|
39
|
+
|
40
|
+
factory = FactoryBot.factories[factory_name]
|
41
|
+
trait_found = false
|
42
|
+
|
43
|
+
until factory.is_a?(FactoryBot::NullFactory)
|
44
|
+
if factory.defined_traits.any? { |trait| trait.name.to_s == trait_name }
|
45
|
+
trait_found = true
|
46
|
+
output[factory.name.to_s] ||= Set.new
|
47
|
+
output[factory.name.to_s] << trait_name
|
48
|
+
break
|
49
|
+
end
|
50
|
+
factory = factory.send(:parent)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Belongs to global traits
|
54
|
+
output['_traits'] << trait_name unless trait_found
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
output
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -23,12 +23,12 @@ module FactoryTrace
|
|
23
23
|
case
|
24
24
|
when result[:value]
|
25
25
|
colorize(total_color, "total number of unique #{result[:code]} factories & traits: #{result[:value]}")
|
26
|
-
when result[:
|
27
|
-
"#{result[:code]} trait #{colorize(:blue, result[:
|
28
|
-
when result[:
|
29
|
-
"#{result[:code]} factory #{colorize(:blue, result[:
|
26
|
+
when result[:factory_name] && result[:trait_name]
|
27
|
+
"#{result[:code]} trait #{colorize(:blue, result[:trait_name])} of factory #{colorize(:blue, result[:factory_name])}"
|
28
|
+
when result[:factory_name]
|
29
|
+
"#{result[:code]} factory #{colorize(:blue, result[:factory_name])}"
|
30
30
|
else
|
31
|
-
"#{result[:code]} global trait #{colorize(:blue, result[:
|
31
|
+
"#{result[:code]} global trait #{colorize(:blue, result[:trait_name])}"
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
@@ -1,8 +1,18 @@
|
|
1
1
|
module FactoryTrace
|
2
2
|
class TraceWriter < Writer
|
3
|
-
# @param [
|
3
|
+
# @param [Array<Hash>] results
|
4
4
|
def write(results)
|
5
|
-
|
5
|
+
io.puts('-all-')
|
6
|
+
output(results[0])
|
7
|
+
io.puts('-used-')
|
8
|
+
output(results[1])
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
# @param [Hash<Symbol, Set<Hash>>] data
|
14
|
+
def output(data)
|
15
|
+
data.each do |key, set|
|
6
16
|
line = [key, set.to_a.join(',')].join(',')
|
7
17
|
io.puts(line)
|
8
18
|
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.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- djezzzl
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: factory_bot
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- lib/factory_trace/configuration.rb
|
80
80
|
- lib/factory_trace/find_unused.rb
|
81
81
|
- lib/factory_trace/readers/trace_reader.rb
|
82
|
+
- lib/factory_trace/storage_handler.rb
|
82
83
|
- lib/factory_trace/tracker.rb
|
83
84
|
- lib/factory_trace/version.rb
|
84
85
|
- lib/factory_trace/writers/report_writer.rb
|