Created
August 11, 2023 17:18
-
-
Save meg-gutshall/0a5b34bd1540de6ff69541ef3fe03403 to your computer and use it in GitHub Desktop.
Colorized FactoryBot Linter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'colorize' | |
require 'English' | |
namespace :factory_bot do | |
desc 'Verify that all FactoryBot factories are valid' | |
task lint: :environment do | |
return unless Rails.env.development? | |
FactoryBot.lint factories_to_lint, traits: true | |
fail ActiveRecord::Rollback | |
end | |
end | |
def factories_to_lint | |
FactoryBot.factories.reject do |factory| | |
factory.name =~ /^old_/ | |
end | |
end | |
module FactoryBot | |
class Linter | |
class FactoryError | |
def message | |
message = @wrapped_error.message | |
'* '.colorize(:light_red) + | |
location.to_s.colorize(:light_yellow) + | |
' - '.colorize(:light_red) + | |
message.to_s.colorize(:light_blue) + | |
" (#{@wrapped_error.class.name})".colorize(:light_red) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment