Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rake task to retrieve a list of available factories #5093

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions core/lib/tasks/factories.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace :factories do
desc "List all factories"
task list: :environment do
# TODO: search in all the common factories.rb paths in the various solidus gems
# Tested with solidus + solidus_stripe
Gem.loaded_specs.values.each do |spec|
factory_file = File.join(spec.full_gem_path, "lib/#{spec.name}/testing_support/factories.rb")
require factory_file if File.file?(factory_file)
end

factories = FactoryBot.factories.sort_by { |f| f.build_class.to_s }

# Find the length of the longest factory name and class name
longest_name_length = factories.map { |f| f.name.length }.max
longest_class_length = factories.map { |f| f.build_class.to_s.length }.max

puts "Factory Name".ljust(longest_name_length) + " | " + "Class Name".ljust(longest_class_length)
puts "-" * longest_name_length + " | " + "-" * longest_class_length

factories.each do |factory|
factory_name = factory.name.to_s.ljust(longest_name_length)
factory_class = factory.build_class.to_s.ljust(longest_class_length)

puts "#{factory_name} | #{factory_class}"
end
end
end