Code Monkey home page Code Monkey logo

factory_trace's Introduction

FactoryTrace

Gem Version

The main goal of the project is to provide an easy way to maintain FactoryBot inside your project in a good shape.

If the project helps you or your organization, I would be very grateful if you contribute or donate.
Your support is an incredible motivation and the biggest reward for my hard work.

Follow me and stay tuned for the updates:

What it does?

Currently, it helps to find unused factories and traits.

Example output (from Rails RSpec Example):

$ FB_TRACE=1 rspec
total number of unique used factories & traits: 3
total number of unique unused factories & traits: 3
unused factory admin => spec/factories.rb:10
unused trait with_address of factory admin => spec/factories.rb:11
unused global trait with_email => spec/factories.rb:16

Installation

Add this line to your application's Gemfile in the group you have factory_bot or factory_bot_rails:

gem 'factory_trace'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install factory_trace

Usage

For now, the gem supports RSpec out of the box. You don't need to add any hooks. Just run the specs, e.g.

# output to STDOUT
FB_TRACE=1 rspec
# OR output to any file
FB_TRACE_FILE=log/factory_trace.txt rspec

For any other case, add the following line where you want to start tracking usage of FactoryBot factories and traits:

FactoryTrace.start

Add this line where you want to stop tracking and get collected information:

FactoryTrace.stop

Partial execution

Imagine, you run your specs in parts (as many as you need) and then want to track total usage of factories and traits. For that, we have a trace_only mode. You can try following commands here.

# one part
FB_TRACE=trace_only FB_TRACE_FILE=fb_trace_result1.json bundle exec rspec spec/first_spec.rb
# another part
FB_TRACE=trace_only FB_TRACE_FILE=fb_trace_result2.json bundle exec rspec spec/second_spec.rb
# output the usage to the console
bundle exec factory_trace fb_trace_result1.json fb_trace_result2.json
# or to the file
FB_TRACE_FILE=fb_report.txt bundle exec factory_trace fb_trace_result1.json fb_trace_result2.json

Note: bundle exec factory_trace won't load your project. Thus it runs fast and it's easier to configure it on CI.

Configuration

You can configure FactoryTrace:

FactoryTrace.configure do |config|
  # default ENV.key?('FB_TRACE') || ENV.key?('FB_TRACE_FILE')
  config.enabled = true

  # default is ENV['FB_TRACE_FILE']
  # when nil outputs to STDOUT
  config.path = 'log/factory_trace.txt'

  # default is true when +path+ is nil
  config.color = true

  # default is ENV['FB_TRACE'] || :full
  # can be :full or :trace_only
  config.mode = :full

  # used to trace definitions places
  # default is true
  # can be true or false  
  config.trace_definition = true
end

Tip: if you have some errors try to disable trace_definition. That functionality does many monkey patches to FactoryBot. I will appreciate sharing an error stack trace so I can fix it.

Development

After checking out the repo, run bundle install to install dependencies. Then, run rake spec to run the tests. To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Funding

Open Collective Backers

You're an individual who wants to support the project with a monthly donation. Your logo will be available on the Github page. [Become a backer]

Open Collective Sponsors

You're an organization that wants to support the project with a monthly donation. Your logo will be available on the Github page. [Become a sponsor]

Contributing

Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the FactoryTrace project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Changelog

FactoryTrace's changelog is available here.

Copyright

Copyright (c) Evgeniy Demin. See LICENSE.txt for further details.

factory_trace's People

Contributors

chubchenko avatar djezzzl avatar jotolo avatar petergoldstein avatar soloveytani avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

factory_trace's Issues

Show usage statistics

Sometimes, some traits or child factories are used only few times, so it's possible to get rid of their separate definition as well.

So we could provide statistics usage of factories/traits with possible threshhold parameter to highlight low used ones.

Doesn't appear to support build/build_list

If I run my specs using rspec they all pass but if it use FB_TRACE=1 rspec I get loads of failures mostly relating to tests that use build or build_list.

Example of output:

 Failure/Error: let(:current_user) { build(:user) }
       
       ArgumentError:
         wrong number of arguments (given 1, expected 2)

Wrong arity for FactoryBot::Trait monkeypatch

Hi, I was trying out factory_trace and ran into this error when running my rspec suite:

/gems/3.1.0/gems/factory_trace-1.0.0/lib/factory_trace/monkey_patches/trait.rb:8:in `initialize'
/gems/3.1.0/gems/factory_bot-6.2.1/lib/factory_bot/enum.rb:26:in `new'
/gems/3.1.0/gems/factory_bot-6.2.1/lib/factory_bot/enum.rb:26:in `build_trait'
/gems/3.1.0/gems/factory_bot-6.2.1/lib/factory_bot/enum.rb:13:in `block in build_traits'
/gems/3.1.0/gems/factory_bot-6.2.1/lib/factory_bot/enum.rb:11:in `each'
/gems/3.1.0/gems/factory_bot-6.2.1/lib/factory_bot/enum.rb:11:in `map'
/gems/3.1.0/gems/factory_bot-6.2.1/lib/factory_bot/enum.rb:11:in `build_traits'
/gems/3.1.0/gems/factory_bot-6.2.1/lib/factory_bot/definition.rb:180:in `block in expand_enum_traits'
/gems/3.1.0/gems/factory_bot-6.2.1/lib/factory_bot/definition.rb:178:in `each'
/gems/3.1.0/gems/factory_bot-6.2.1/lib/factory_bot/definition.rb:178:in `expand_enum_traits'
/gems/3.1.0/gems/factory_bot-6.2.1/lib/factory_bot/definition.rb:52:in `compile'
/gems/3.1.0/gems/factory_bot-6.2.1/lib/factory_bot/factory.rb:91:in `compile'
/gems/3.1.0/gems/factory_bot-6.2.1/lib/factory_bot/factory_runner.rb:16:in `run'
/gems/3.1.0/gems/factory_bot-6.2.1/lib/factory_bot/strategy_syntax_method_registrar.rb:28:in `block in define_singular_strategy_method'
myapp/spec/workers/a_spec.rb:30:in `block (3 levels) in <top (required)>'
  • factory_trace monkeypatches FactoryBot::Trait here
  • and the monkeypatch changes the FactoryBot::Trait constructor from accepting one args to two args here
  • but the original FactoryBot::Trait only requires one argument here, and that constructor hasn't changed for 11 years.

Since existing FactoryBot code calls Trait with one arg, can the 2nd arg just be optional?

Add option to load files to executable

  • If factories are defined using some 3rd parties or even internals
  • Or they are defined in different than default place

The command bundle exec factory_trace <file1> <file2> ... <fileN> can fail. For that, we should add an option to load any required files before we execute the script.

Something like the following:
bundle exec factory_trace -r specs/supports/factory_bot <file1> <file2> ... <fileN>

NoMethodError: undefined method `has_prioritized_status?' for nil:NilClass

Hi, I wanted to used it with our rspec test suite using FB_TRACE=1 bundle exec rspec and after tests finished I get the following exception.

An error occurred in an `after(:suite)` hook.
Failure/Error: return if trait.has_prioritized_status?(status)

NoMethodError:
  undefined method `has_prioritized_status?' for nil:NilClass
# /Users/Aqualon/.rvm/gems/ruby-2.6.2/gems/factory_trace-0.3.1/lib/factory_trace/processors/find_unused.rb:88:in `mark_trait'
# /Users/Aqualon/.rvm/gems/ruby-2.6.2/gems/factory_trace-0.3.1/lib/factory_trace/processors/find_unused.rb:103:in `block in mark_declarations'
# /Users/Aqualon/.rvm/gems/ruby-2.6.2/gems/factory_trace-0.3.1/lib/factory_trace/processors/find_unused.rb:99:in `each'
# /Users/Aqualon/.rvm/gems/ruby-2.6.2/gems/factory_trace-0.3.1/lib/factory_trace/processors/find_unused.rb:99:in `mark_declarations'
# /Users/Aqualon/.rvm/gems/ruby-2.6.2/gems/factory_trace-0.3.1/lib/factory_trace/processors/find_unused.rb:80:in `mark_factory'
# /Users/Aqualon/.rvm/gems/ruby-2.6.2/gems/factory_trace-0.3.1/lib/factory_trace/processors/find_unused.rb:40:in `block in mark_as_used'
# /Users/Aqualon/.rvm/gems/ruby-2.6.2/gems/factory_trace-0.3.1/lib/factory_trace/processors/find_unused.rb:38:in `each'
# /Users/Aqualon/.rvm/gems/ruby-2.6.2/gems/factory_trace-0.3.1/lib/factory_trace/processors/find_unused.rb:38:in `mark_as_used'
# /Users/Aqualon/.rvm/gems/ruby-2.6.2/gems/factory_trace-0.3.1/lib/factory_trace/processors/find_unused.rb:11:in `call'
# /Users/Aqualon/.rvm/gems/ruby-2.6.2/gems/factory_trace-0.3.1/lib/factory_trace.rb:43:in `stop'
# /Users/Aqualon/.rvm/gems/ruby-2.6.2/gems/factory_trace-0.3.1/lib/integrations/rspec.rb:3:in `block (2 levels) in <top (required)>'

NoMethodError: mark_trait: undefined method has_prioritized_status? for nil:NilClass

Hi,

I'm trying to use this gem combined with parallel_tests gem. I'm taking the following steps but cannot make it through.

First, since in parallel tests, specs are run partially, I'm setting FactoryTrace configuration like below

FactoryTrace.configure do |config|
  config.path = "#{Rails.root}/trace_#{ENV['TEST_ENV_NUMBER'] == '' ? 1 : ENV['TEST_ENV_NUMBER']}.json"
end

so that the trace files would be generated based on the parallel test number. Then when running the tests;

$ FB_TRACE=trace_only PARALLEL_TEST_PROCESSORS=8 bundle exec rake parallel:spec

Afterwards I run the following commands to merge the partial results

$ trace=""
$ for i in {1..8}; do file=" trace_$i.json"; trace=$trace$file; done
$ FB_TRACE_FILE=fb_report.txt bundle exec factory_trace $trace

but I'm getting this error

/app/vendor/bundle/ruby/2.4.0/gems/factory_trace-0.3.2/lib/factory_trace/processors/find_unused.rb:88:in `mark_trait': undefined method `has_prioritized_status?' for nil:NilClass (NoMethodError) 
from /app/vendor/bundle/ruby/2.4.0/gems/factory_trace-0.3.2/lib/factory_trace/processors/find_unused.rb:44:in `block (2 levels) in mark_as_used' 
from /app/vendor/bundle/ruby/2.4.0/gems/factory_trace-0.3.2/lib/factory_trace/processors/find_unused.rb:42:in `each' 
from /app/vendor/bundle/ruby/2.4.0/gems/factory_trace-0.3.2/lib/factory_trace/processors/find_unused.rb:42:in `block in mark_as_used' 
from /app/vendor/bundle/ruby/2.4.0/gems/factory_trace-0.3.2/lib/factory_trace/processors/find_unused.rb:38:in `each' 
from /app/vendor/bundle/ruby/2.4.0/gems/factory_trace-0.3.2/lib/factory_trace/processors/find_unused.rb:38:in `mark_as_used' 
from /app/vendor/bundle/ruby/2.4.0/gems/factory_trace-0.3.2/lib/factory_trace/processors/find_unused.rb:11:in `call' 
from /app/vendor/bundle/ruby/2.4.0/gems/factory_trace-0.3.2/exe/factory_trace:10:in `<top (required)>' 
from /app/vendor/bundle/ruby/2.4.0/bin/factory_trace:23:in `load' 
from /app/vendor/bundle/ruby/2.4.0/bin/factory_trace:23:in `<main>' 

Do you happen to know what might be causing this?

all let calls to factory_bot return argument error with trace enabled

Hi! Trying to give the gem a shot and I'm running in to what I suspect is a monkey patching issue or similar?

rspec 3.11
factory_bot_rails 6.2
factory_bot 6.2
rails 7.0.x

rspec runs with no issues
FB_TRACE=1 rspec fails every spec that calls a factory with an argument error

Failure/Error: let!(:user) { create(:user) }
      
ArgumentError:
  wrong number of arguments (given 1, expected 2)

same result when attempting with the log file command too.

seemingly all (or most) factories are impacted. nothing odd in most of the factories.

example:

FactoryBot.define do
  factory :user do
    sequence(:first_name) { |n| "first_name#{n}" }
    sequence(:last_name) { |n| "last_name#{n}" }
    sequence(:email) { |n| "email_#{n}@example.com" }
    password { 'password_123123' }

    trait :after_signup do
    end
  end
end

Support for minitest

Are there plans to support minitests in the future? I'd like to use the gem but in our project we are currently running our tests with minitests

False negative when factory defined with default traits

Example:

# invoices factory
FactoryBot.define do
  factory :object do
    trait :with_user do
      association :user
    end

    trait :with_entity do
      association :entity
    end

    factory :valid_object, traits: [:with_user, :with_entity]
  end
end

FactoryTrace reports unused traits "with_user" and "with_entity" even if "valid_object" factory was used.

Many factories/traits are reported as unused while they are

I used this gem and ran an rspec with it on my project. There are almost as many unused factories/traits as used ones reported. By examing some of those "unused" ones, they are used actually (called in some examples and those examples were executed). What could have been wrong? Thanks.

factory_bot deprecation warnings when running trace

These warnings started showing up during my rspec runs (which calls trace):

DEPRECATION WARNING: register_factory is deprecated and will be removed from factory_bot 6.0 (called from block in <top (required)> at /Users/kuzmik/Development/project1/spec/factories/os_requirements.rb:22)
....
DEPRECATION WARNING: register_factory is deprecated and will be removed from factory_bot 6.0 (called from block in <top (required)> at /Users/kuzmik/Development/project1/spec/factories/requirements.rb:20)
DEPRECATION WARNING: traits is deprecated and will be removed from factory_bot 6.0 (called from load at /Users/kuzmik/.rbenv/versions/2.6.5/gemsets/bc-das/bin/rspec:23)

I tried giving the code a look to see if I could fix it and push a PR, but I came up short.

Rails 5.2.3

Gem versions (I don't THINK rspec is relevant, but hey why not include it):

factory_bot (5.1.1)
factory_bot_rails (5.1.1)
factory_trace (0.4.0)
rspec (3.8.0)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.