Giter Site home page Giter Site logo

djezzzl / factory_trace Goto Github PK

View Code? Open in Web Editor NEW
374.0 4.0 7.0 167 KB

Simple tool to maintain factories and traits from FactoryBot

License: MIT License

Ruby 89.07% JavaScript 1.76% CSS 1.03% HTML 8.14%
ruby factory-bot testing factory-girl rspec minitest test-unit

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

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.

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>

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.

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.

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?

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

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

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)

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)>'

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)

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.