2014-05-29 16:06:08 -04:00
|
|
|
module FactoryGirl
|
|
|
|
class Linter
|
|
|
|
|
2015-07-31 23:07:49 -04:00
|
|
|
def initialize(factories_to_lint, linting_strategy)
|
2014-05-29 16:06:08 -04:00
|
|
|
@factories_to_lint = factories_to_lint
|
2015-07-31 23:07:49 -04:00
|
|
|
@linting_method = "lint_#{linting_strategy}"
|
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
|
|
|
|
|
|
|
|
attr_reader :factories_to_lint, :invalid_factories
|
2015-07-31 23:07:49 -04:00
|
|
|
private :factories_to_lint, :invalid_factories
|
2015-04-29 06:23:52 -04:00
|
|
|
|
|
|
|
private
|
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|
|
|
|
|
errors = send(@linting_method, factory)
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def lint_factory(factory)
|
|
|
|
result = []
|
|
|
|
begin
|
|
|
|
FactoryGirl.create(factory.name)
|
|
|
|
rescue => error
|
|
|
|
result |= [FactoryError.new(error, factory)]
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def lint_traits(factory)
|
|
|
|
result = []
|
|
|
|
factory.definition.defined_traits.map(&:name).each do |trait_name|
|
2014-10-17 12:03:22 -04:00
|
|
|
begin
|
2015-07-31 23:07:49 -04:00
|
|
|
FactoryGirl.create(factory.name, trait_name)
|
2014-10-17 12:03:22 -04:00
|
|
|
rescue => error
|
2015-07-31 23:07:49 -04:00
|
|
|
result |=
|
|
|
|
[FactoryTraitError.new(error, factory, trait_name)]
|
2014-05-29 16:06:08 -04:00
|
|
|
end
|
|
|
|
end
|
2015-07-31 23:07:49 -04:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def lint_factory_and_traits(factory)
|
|
|
|
errors = lint_factory(factory)
|
|
|
|
errors |= lint_traits(factory)
|
|
|
|
errors
|
2014-05-29 16:06:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
2015-07-31 23:07:49 -04:00
|
|
|
lines = invalid_factories.map do |_factory, exceptions|
|
2016-05-20 04:53:28 -04:00
|
|
|
exceptions.map(&:message)
|
2015-07-31 23:07:49 -04:00
|
|
|
end.flatten
|
2014-10-17 12:03:22 -04:00
|
|
|
|
2014-05-29 16:06:08 -04:00
|
|
|
<<-ERROR_MESSAGE.strip
|
|
|
|
The following factories are invalid:
|
|
|
|
|
2014-10-17 12:03:22 -04:00
|
|
|
#{lines.join("\n")}
|
2014-05-29 16:06:08 -04:00
|
|
|
ERROR_MESSAGE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|