factory_bot-awesome_linter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 617261095c26b3a682b99159a3cde1ec380c854be48bef7be0828db2e50c02a0
4
+ data.tar.gz: 9d2ab786572209fb46735949648e2efb78c1dcfd55ee4a5e6ea1cac2bd51df4f
5
+ SHA512:
6
+ metadata.gz: b5f9217d10a5ece6f8375f750866de7d77d9ea58be815a3296468ed095d518d471c4e5951138c1ab0e54e8784effcb559b35ba2b5de04bbeb3b6b0b6c80b0eff
7
+ data.tar.gz: dfdf9392b11e132c1fad5c3ce4f07182e1c1f599623ef72ed01132305d88f5afeb3d577f7d605fa44c8f30021ee9bf2c2185765a38e006e9578515fd8c33bb05
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'factory_bot'
4
+ require 'ruby-progressbar'
5
+
6
+ module FactoryBot
7
+ class AwesomeLinter
8
+ def self.lint!(*args, **kwargs)
9
+ new(*args, **kwargs).lint!
10
+ end
11
+
12
+ def initialize(*args, strategy: :create, traits: false, verbose: false)
13
+ if args.any?
14
+ @factories_to_lint = args[0]
15
+ else
16
+ FactoryBot.reload
17
+ @factories_to_lint = FactoryBot.factories
18
+ end
19
+
20
+ @factory_strategy = strategy
21
+ @traits = traits
22
+ @verbose = verbose
23
+ @progress_bar = ProgressBar.create(format: "\e[0;32m %c/%C |%w>%i| %e \e[0m")
24
+ @invalid_factories = []
25
+ end
26
+
27
+ attr_reader :progress_bar, :factories_to_lint, :invalid_factories
28
+
29
+ def lint!
30
+ progress_bar.total = calculate_total
31
+
32
+ factories_to_lint.each do |factory|
33
+ lint_factory(factory)
34
+ progress_bar.increment
35
+
36
+ next unless @traits
37
+
38
+ factory.definition.defined_traits.each do |trait|
39
+ lint_trait(factory, trait)
40
+ progress_bar.increment
41
+ end
42
+ end
43
+
44
+ progress_bar.stop
45
+
46
+ output_invalid_factories
47
+ invalid_factories.empty?
48
+ end
49
+
50
+ def calculate_total
51
+ factories_to_lint.reduce(0) do |count, factory|
52
+ # Compile factory to count enum traits
53
+ factory.compile
54
+
55
+ count + 1 + factory.definition.defined_traits.size
56
+ end
57
+ end
58
+
59
+ def lint_factory(factory)
60
+ cleaning do
61
+ FactoryBot.public_send(@factory_strategy, factory.name)
62
+ end
63
+ rescue StandardError => e
64
+ invalid_factory! e, factory
65
+ end
66
+
67
+ def lint_trait(factory, trait)
68
+ cleaning do
69
+ FactoryBot.public_send(@factory_strategy, factory.name, trait.name)
70
+ end
71
+ rescue StandardError => e
72
+ invalid_factory! e, factory
73
+ end
74
+
75
+ def invalid_factory!(error, factory, trait = nil)
76
+ name = ":#{factory.name}"
77
+ name += " + :#{trait.name}" if trait
78
+
79
+ invalid_factories << name
80
+
81
+ progress_bar.format = "\e[0;31m %c/%C |%w>%i| %e \e[0m"
82
+
83
+ progress_bar.log "\n - Invalid factory: #{name}"
84
+ progress_bar.log "\n \e[0;31m#{error.message}\e[0m"
85
+
86
+ error.backtrace[0..10].each do |s|
87
+ progress_bar.log " \e[0;36m# #{s}\e[0m"
88
+ end
89
+
90
+ progress_bar.log "\n"
91
+ end
92
+
93
+ def output_invalid_factories
94
+ return if invalid_factories.empty?
95
+
96
+ $stdout.puts "\nInvalid factories:"
97
+
98
+ invalid_factories.each do |name|
99
+ $stdout.puts " \e[0;31m- #{name}\e[0m"
100
+ end
101
+
102
+ $stdout.puts
103
+ end
104
+
105
+ def cleaning(&block)
106
+ if defined?(DatabaseCleaner)
107
+ DatabaseCleaner.cleaning(&block)
108
+ elsif defined?(ActiveRecord)
109
+ @activerecord_connection ||= ActiveRecord::Base.connection
110
+ @activerecord_connection.transaction(&block)
111
+ else
112
+ raise "No cleaning strategie available, you may require or include database-cleaner in your Gemfile"
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,5 @@
1
+ module FactoryBot
2
+ class AwesomeLinter
3
+ VERSION = '0.0.1'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_bot-awesome_linter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Savater Sebastien
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: factory_bot
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-progressbar
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.11.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.11.0
41
+ description:
42
+ email:
43
+ - savater.sebastien@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/factory_bot-awesome_linter.rb
49
+ - lib/factory_bot-awesome_linter/version.rb
50
+ homepage: https://github.com/inkstak/factory_bot-awesome_linter
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.0.8
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Enhanced linter for FactoryBot
73
+ test_files: []