mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
a56b68bc9c
* Alphabetize gem listing in various Gemfiles [Rubocop Bundler/OrderedGems] * Fix alignment of if/else/end statement [Rubocop Layout/ElseAlignment] * Method definitions should have a empty line between them [Rubocop Layout/EmptyLineBetweenDefs] * Modules, Classes, and blocks should have an empty line around them [Rubocop] Cops: Layout/EmptyLinesAroundBlockBody Layout/EmptyLinesAroundModuleBody Layout/EmptyLinesAroundClassBody Layout/EmptyLinesAroundAccessModifier * Keep a blank line before and after access modifiers [Rubocop Layout/EmptyLinesAroundAccessModifier] * Remove misc extra whitespace [Rubocop Layout/ExtraSpacing] * Indent the first line of the right-hand-side of a multi-line assignment [Rubocop Layout/IndentAssignment] * Remove extraneous whitespace [Rubocop] Cops: Layout/IndentationWidth Layout/LeadingCommentSpace Layout/SpaceAroundEqualsInParameterDefault Layout/SpaceInsideArrayLiteralBrackets Layout/SpaceInsideBlockBraces Layout/SpaceInsideParens Layout/TrailingBlankLines * Revert rubocop changes to gemfiles; exclude files from rubocop checks The files in gemfiles/ are generated by Appraisal, so we shouldn't edit them. Instead, let's tell RuboCop to exclude this directory.
96 lines
2.3 KiB
Ruby
96 lines
2.3 KiB
Ruby
module FactoryBot
|
|
class Linter
|
|
def initialize(factories, linting_strategy, factory_strategy = :create)
|
|
@factories_to_lint = factories
|
|
@linting_method = "lint_#{linting_strategy}"
|
|
@factory_strategy = factory_strategy
|
|
@invalid_factories = calculate_invalid_factories
|
|
end
|
|
|
|
def lint!
|
|
if invalid_factories.any?
|
|
raise InvalidFactoryError, error_message
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :factories_to_lint, :invalid_factories, :factory_strategy
|
|
|
|
def calculate_invalid_factories
|
|
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
|
|
FactoryBot.public_send(factory_strategy, 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|
|
|
begin
|
|
FactoryBot.public_send(factory_strategy, factory.name, trait_name)
|
|
rescue => error
|
|
result |=
|
|
[FactoryTraitError.new(error, factory, trait_name)]
|
|
end
|
|
end
|
|
result
|
|
end
|
|
|
|
def lint_factory_and_traits(factory)
|
|
errors = lint_factory(factory)
|
|
errors |= lint_traits(factory)
|
|
errors
|
|
end
|
|
|
|
def error_message
|
|
lines = invalid_factories.map do |_factory, exceptions|
|
|
exceptions.map(&:message)
|
|
end.flatten
|
|
|
|
<<-ERROR_MESSAGE.strip
|
|
The following factories are invalid:
|
|
|
|
#{lines.join("\n")}
|
|
ERROR_MESSAGE
|
|
end
|
|
end
|
|
end
|