2017-10-20 15:20:28 -04:00
|
|
|
module FactoryBot
|
2014-05-29 16:06:08 -04:00
|
|
|
class Linter
|
2018-11-21 15:28:28 -05:00
|
|
|
def initialize(factories, strategy: :create, traits: false, verbose: false)
|
2017-09-28 08:22:16 -04:00
|
|
|
@factories_to_lint = factories
|
2018-11-21 22:17:24 -05:00
|
|
|
@factory_strategy = strategy
|
|
|
|
@traits = traits
|
2018-11-21 15:28:28 -05:00
|
|
|
@verbose = verbose
|
2014-05-29 16:06:08 -04:00
|
|
|
@invalid_factories = calculate_invalid_factories
|
|
|
|
end
|
|
|
|
|
|
|
|
def lint!
|
|
|
|
if invalid_factories.any?
|
|
|
|
raise InvalidFactoryError, error_message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-29 06:23:52 -04:00
|
|
|
private
|
2014-05-29 16:06:08 -04:00
|
|
|
|
2017-09-28 08:22:16 -04:00
|
|
|
attr_reader :factories_to_lint, :invalid_factories, :factory_strategy
|
|
|
|
|
2014-05-29 16:06:08 -04:00
|
|
|
def calculate_invalid_factories
|
2015-07-31 23:07:49 -04:00
|
|
|
factories_to_lint.reduce(Hash.new([])) do |result, factory|
|
2018-11-21 22:17:24 -05:00
|
|
|
errors = lint(factory)
|
2015-07-31 23:07:49 -04:00
|
|
|
result[factory] |= errors unless errors.empty?
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FactoryError
|
|
|
|
def initialize(wrapped_error, factory)
|
|
|
|
@wrapped_error = wrapped_error
|
|
|
|
@factory = factory
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
|
|
|
message = @wrapped_error.message
|
|
|
|
"* #{location} - #{message} (#{@wrapped_error.class.name})"
|
|
|
|
end
|
|
|
|
|
2018-11-21 15:28:28 -05:00
|
|
|
def verbose_message
|
|
|
|
<<~MESSAGE
|
|
|
|
#{message}
|
|
|
|
#{@wrapped_error.backtrace.join("\n ")}
|
|
|
|
MESSAGE
|
|
|
|
end
|
|
|
|
|
2015-07-31 23:07:49 -04:00
|
|
|
def location
|
|
|
|
@factory.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FactoryTraitError < FactoryError
|
|
|
|
def initialize(wrapped_error, factory, trait_name)
|
|
|
|
super(wrapped_error, factory)
|
|
|
|
@trait_name = trait_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def location
|
|
|
|
"#{@factory.name}+#{@trait_name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-21 22:17:24 -05:00
|
|
|
def lint(factory)
|
|
|
|
if @traits
|
|
|
|
lint_factory(factory) + lint_traits(factory)
|
|
|
|
else
|
|
|
|
lint_factory(factory)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-31 23:07:49 -04:00
|
|
|
def lint_factory(factory)
|
|
|
|
result = []
|
|
|
|
begin
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.public_send(factory_strategy, factory.name)
|
2019-05-12 22:39:55 -04:00
|
|
|
rescue StandardError => e
|
|
|
|
result |= [FactoryError.new(e, factory)]
|
2015-07-31 23:07:49 -04:00
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def lint_traits(factory)
|
|
|
|
result = []
|
|
|
|
factory.definition.defined_traits.map(&:name).each do |trait_name|
|
2020-04-24 17:11:12 -04:00
|
|
|
FactoryBot.public_send(factory_strategy, factory.name, trait_name)
|
|
|
|
rescue StandardError => e
|
|
|
|
result |= [FactoryTraitError.new(e, factory, trait_name)]
|
2014-05-29 16:06:08 -04:00
|
|
|
end
|
2015-07-31 23:07:49 -04:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2014-05-29 16:06:08 -04:00
|
|
|
def error_message
|
2015-07-31 23:07:49 -04:00
|
|
|
lines = invalid_factories.map do |_factory, exceptions|
|
2018-11-21 15:28:28 -05:00
|
|
|
exceptions.map(&error_message_type)
|
2015-07-31 23:07:49 -04:00
|
|
|
end.flatten
|
2014-10-17 12:03:22 -04:00
|
|
|
|
2018-10-07 22:46:58 -04:00
|
|
|
<<~ERROR_MESSAGE.strip
|
|
|
|
The following factories are invalid:
|
2014-05-29 16:06:08 -04:00
|
|
|
|
2018-10-07 22:46:58 -04:00
|
|
|
#{lines.join("\n")}
|
2014-05-29 16:06:08 -04:00
|
|
|
ERROR_MESSAGE
|
|
|
|
end
|
2018-11-21 15:28:28 -05:00
|
|
|
|
|
|
|
def error_message_type
|
|
|
|
if @verbose
|
|
|
|
:verbose_message
|
|
|
|
else
|
|
|
|
:message
|
|
|
|
end
|
|
|
|
end
|
2014-05-29 16:06:08 -04:00
|
|
|
end
|
|
|
|
end
|