factory_trace 0.1.2 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 26b7253d7d994cb1f591977dc3bcadc38d6f6cfe80526ef4377d85d411fbd380
4
- data.tar.gz: bcde5b14ecfcfbe16b9cada40618ae0c79e38be424b96b9b2967996a6ab1f1c2
3
+ metadata.gz: f8910590a0b5aab2a00f31e0aa41cc244161e8c387b47341b0f6c2a96d7e40c8
4
+ data.tar.gz: e6902745419605702ccc35def7d110b7c18b23b1407af3560b2d2e3f75a43e96
5
5
  SHA512:
6
- metadata.gz: 83ef3b6436248a5ed5c440966337b89e7e1dcbd72b025bbcfb6448cbee188aac24f57d3122c0f41e81f2d9af138aed04ed24d4d5ac5ec0a87ad0ba068dce085a
7
- data.tar.gz: fa02e34299d009aea40d99368e67e575152b30479e79a903464f45279ee33afdaa59d5bc51fd42fa1edee3ca7ecfea171705278778c0b1aa41522c81ce203a7f
6
+ metadata.gz: 15b006db74f1f06d6b8f5a16dff18158a407ed292e2a90b1859c7c6bd6fc503d1016e14516e2fa2e1081a4d6dd873fff0096e4fd2d98ab39c794e63c986a7bb5
7
+ data.tar.gz: 69a82579dc197d590f1578e04fcbd818e48c2bf5750d01f86ffb62e77d1fcb6341792dcae76ae0c50bfb8acb48d562fdac1f0caa9b1e6d6a292fc5853d9aae1b
data/exe/factory_trace ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift("#{__dir__}/../lib")
4
+ require 'factory_trace'
5
+
6
+ fail "You should pass at least one file with traced information.\nYou can generate it using only_trace mode." if ARGV.empty?
7
+
8
+ config = FactoryTrace.configuration
9
+ data = FactoryTrace::TraceReader.read_from_files(*ARGV)
10
+ data = FactoryTrace::FindUnused.call(data)
11
+ code = data.size > 2 ? 1 : 0
12
+
13
+ FactoryTrace::ReportWriter.new(config.out, config: config).write(data)
14
+ exit(code)
@@ -1,11 +1,12 @@
1
1
  module FactoryTrace
2
2
  class Configuration
3
- attr_accessor :path, :enabled, :color
3
+ attr_accessor :path, :enabled, :color, :mode
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
+ @mode = extract_mode(ENV['FB_TRACE']) || :full
9
10
  end
10
11
 
11
12
  def out
@@ -13,5 +14,16 @@ module FactoryTrace
13
14
 
14
15
  File.open(path, 'w')
15
16
  end
17
+
18
+ def mode?(*args)
19
+ args.include?(mode)
20
+ end
21
+
22
+ private
23
+
24
+ def extract_mode(value)
25
+ matcher = value && value.match(/full|trace_only/)
26
+ matcher && matcher[0].to_sym
27
+ end
16
28
  end
17
29
  end
@@ -1,13 +1,8 @@
1
1
  module FactoryTrace
2
2
  class FindUnused
3
-
4
- # @param [Hash<Symbol, Set<Symbol>>]
5
- def initialize(data)
6
- @initial_data = data
7
- end
8
-
3
+ # @param [Hash<Symbol, Set<Symbol>>] initial_data
9
4
  # @return [Array<Hash>]
10
- def check!
5
+ def self.call(initial_data)
11
6
  # This is required to exclude parent traits from +defined_traits+
12
7
  FactoryBot.reload
13
8
 
@@ -39,9 +34,8 @@ module FactoryTrace
39
34
  # Return new structure where each trait is moved to its own factory
40
35
  #
41
36
  # @param [Hash<Symbol, Set<Symbol>>]
42
- #
43
37
  # @return [Hash<String, Set<String>>]
44
- def prepare(data)
38
+ def self.prepare(data)
45
39
  # +_traits+ is for global traits
46
40
  output = {'_traits' => Set.new, '_total_used' => count_total(data)}
47
41
 
@@ -75,10 +69,8 @@ module FactoryTrace
75
69
  end
76
70
 
77
71
  # @param [Hash<Symbol, Set<Symbol>>]
78
- def count_total(data)
72
+ def self.count_total(data)
79
73
  data.reduce(0) { |result, (_factory, traits)| result + 1 + traits.size }
80
74
  end
81
-
82
- attr_reader :initial_data
83
75
  end
84
76
  end
@@ -0,0 +1,36 @@
1
+ require 'set'
2
+
3
+ module FactoryTrace
4
+ class TraceReader
5
+ def self.read_from_files(*file_names, config: Configuration.new)
6
+ file_names.reduce({}) do |hash, file_name|
7
+ reader = new(File.open(file_name, 'r'), config: config)
8
+ hash.merge(reader.read) { |_key, v1, v2| v1 | v2 }
9
+ end
10
+ end
11
+
12
+ def initialize(io, config: Configuration.new)
13
+ @io = io
14
+ @config = config
15
+ end
16
+
17
+ def read
18
+ @data ||= {}
19
+
20
+ io.each_line do |line|
21
+ factory, *traits = line.strip.split(',').map(&:to_sym)
22
+
23
+ if factory
24
+ @data[factory] ||= Set.new
25
+ @data[factory] |= traits
26
+ end
27
+ end
28
+
29
+ @data
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :io, :config
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module FactoryTrace
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -1,18 +1,13 @@
1
1
  module FactoryTrace
2
- class Printer
2
+ class ReportWriter < Writer
3
3
  COLORS = {
4
4
  blue: "\e[34m",
5
5
  green: "\e[32m",
6
6
  red: "\e[31m"
7
7
  }.freeze
8
8
 
9
- def initialize(io, config: Configuration.new)
10
- @io = io
11
- @config = config
12
- end
13
-
14
9
  # @param [Array<Hash>] results
15
- def print(results)
10
+ def write(results)
16
11
  total_color = results.size == 2 ? :green : :red
17
12
 
18
13
  results.each do |result|
@@ -42,7 +37,5 @@ module FactoryTrace
42
37
 
43
38
  "#{COLORS[color]}#{msg}\e[0m"
44
39
  end
45
-
46
- attr_reader :io, :config
47
40
  end
48
41
  end
@@ -0,0 +1,11 @@
1
+ module FactoryTrace
2
+ class TraceWriter < Writer
3
+ # @param [Hash<Symbol, Set<Hash>>] results
4
+ def write(results)
5
+ results.each do |key, set|
6
+ line = [key, set.to_a.join(',')].join(',')
7
+ io.puts(line)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ module FactoryTrace
2
+ class Writer
3
+ def self.factory(io, config: Configuration.new)
4
+ writer =
5
+ if config.mode?(:full)
6
+ ReportWriter
7
+ elsif config.mode?(:trace_only)
8
+ TraceWriter
9
+ end
10
+
11
+ writer.new(io, config: config)
12
+ end
13
+
14
+ def initialize(io, config: Configuration.new)
15
+ @io = io
16
+ @config = config
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :io, :config
22
+ end
23
+ end
data/lib/factory_trace.rb CHANGED
@@ -5,7 +5,10 @@ require 'factory_trace/configuration'
5
5
  require 'factory_trace/version'
6
6
  require 'factory_trace/tracker'
7
7
  require 'factory_trace/find_unused'
8
- require 'factory_trace/printer'
8
+ require 'factory_trace/readers/trace_reader'
9
+ require 'factory_trace/writers/writer'
10
+ require 'factory_trace/writers/report_writer'
11
+ require 'factory_trace/writers/trace_writer'
9
12
  # Integrations
10
13
  require 'integrations/rspec' if defined?(RSpec::Core)
11
14
 
@@ -20,27 +23,33 @@ module FactoryTrace
20
23
  def stop
21
24
  return unless configuration.enabled
22
25
 
23
- result = FindUnused.new(tracker.storage).check!
24
-
25
- printer.print(result)
26
+ writer.write(results)
26
27
  end
27
28
 
28
29
  def configure
29
30
  yield(configuration)
30
31
  end
31
32
 
33
+ def configuration
34
+ @configuration ||= Configuration.new
35
+ end
36
+
32
37
  private
33
38
 
34
- def tracker
35
- @tracker ||= Tracker.new
39
+ def results
40
+ if configuration.mode?(:full)
41
+ FindUnused.call(tracker.storage)
42
+ elsif configuration.mode?(:trace_only)
43
+ tracker.storage
44
+ end
36
45
  end
37
46
 
38
- def printer
39
- @printer ||= Printer.new(configuration.out, config: configuration)
47
+ def tracker
48
+ @tracker ||= Tracker.new
40
49
  end
41
50
 
42
- def configuration
43
- @configuration ||= Configuration.new
51
+ def writer
52
+ @writer ||= Writer.factory(configuration.out, config: configuration)
44
53
  end
45
54
  end
46
55
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_trace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - djezzzl
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
11
  date: 2019-04-30 00:00:00.000000000 Z
12
12
  dependencies:
@@ -69,16 +69,21 @@ dependencies:
69
69
  description:
70
70
  email:
71
71
  - lawliet.djez@gmail.com
72
- executables: []
72
+ executables:
73
+ - factory_trace
73
74
  extensions: []
74
75
  extra_rdoc_files: []
75
76
  files:
77
+ - exe/factory_trace
76
78
  - lib/factory_trace.rb
77
79
  - lib/factory_trace/configuration.rb
78
80
  - lib/factory_trace/find_unused.rb
79
- - lib/factory_trace/printer.rb
81
+ - lib/factory_trace/readers/trace_reader.rb
80
82
  - lib/factory_trace/tracker.rb
81
83
  - lib/factory_trace/version.rb
84
+ - lib/factory_trace/writers/report_writer.rb
85
+ - lib/factory_trace/writers/trace_writer.rb
86
+ - lib/factory_trace/writers/writer.rb
82
87
  - lib/integrations/rspec.rb
83
88
  homepage: https://github.com/djezzzl/factory_trace
84
89
  licenses: