2014-05-29 16:06:08 -04:00
|
|
|
module FactoryGirl
|
|
|
|
class Linter
|
|
|
|
def self.lint!(factories_to_lint)
|
|
|
|
new(factories_to_lint).lint!
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(factories_to_lint)
|
|
|
|
@factories_to_lint = factories_to_lint
|
|
|
|
@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-04-29 06:23:52 -04:00
|
|
|
private :factories_to_lint, :invalid_factories
|
|
|
|
|
|
|
|
private
|
2014-05-29 16:06:08 -04:00
|
|
|
|
|
|
|
def calculate_invalid_factories
|
2014-10-17 12:03:22 -04:00
|
|
|
factories_to_lint.inject({}) do |result, factory|
|
|
|
|
begin
|
|
|
|
FactoryGirl.create(factory.name)
|
|
|
|
rescue => error
|
|
|
|
result[factory] = error
|
2014-05-29 16:06:08 -04:00
|
|
|
end
|
2014-10-17 12:03:22 -04:00
|
|
|
|
|
|
|
result
|
2014-05-29 16:06:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
2014-10-17 12:03:22 -04:00
|
|
|
lines = invalid_factories.map do |factory, exception|
|
|
|
|
"* #{factory.name} - #{exception.message} (#{exception.class.name})"
|
|
|
|
end
|
|
|
|
|
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
|