Skip to content

Load EE factories in Rails console and verify them in specs

Peter Leitzen requested to merge pl-factory-bot-reload into master

What does this MR do and why?

Previously, factories defined in ee/spec/factories/**/*.rb were not loaded in rails console.

Moreover, they were not verified via spec/models/factories_spec.rb. Some EE factories have been fixed to pass the specs (skip_create was added to play nice with create(...) for non-DB factories).

This MR defines file paths via application configuration instead of overriding definition_file_paths in multiple places.

This allows factories for EE and JH to be loaded also in Rails console if available and make them available in spec/models/factories_spec.rb.

This MR might help to resolve #369851 (closed) 🤞 (no reproduction/evidence though)

Closes #335714 (closed).

How to set up and validate locally

  • To test production env add this to config/database.yml:
production:
  <<: *test

Before

EE factories are missing

$ bin/rails  console
WARNING: This version of GitLab depends on gitlab-shell 14.10.0, but you're running 14.7.4. Please update gitlab-shell.
--------------------------------------------------------------------------------
 Ruby:         ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux]
 GitLab:       15.4.0-pre (25e0d649a8c) EE
 GitLab Shell: 14.7.4
 PostgreSQL:   12.10
-------------------------------------------------------------[ booted in 4.64s ]
Loading development environment (Rails 6.1.6.1)
[1] pry(main)> FactoryBot.definition_file_paths
=> [#<Pathname:/home/peter/devel/gitlab/gdk/gitlab/factories>, #<Pathname:/home/peter/devel/gitlab/gdk/gitlab/test/factories>, #<Pathname:/home/peter/devel/gitlab/gdk/gitlab/spec/factories>]
[2] pry(main)> FactoryBot.reload
=> ["/home/peter/devel/gitlab/gdk/gitlab/factories", "/home/peter/devel/gitlab/gdk/gitlab/test/factories", "/home/peter/devel/gitlab/gdk/gitlab/spec/factories"]

$ bin/rails  console -e production
WARNING: This version of GitLab depends on gitlab-shell 14.10.0, but you're running 14.7.4. Please update gitlab-shell.
--------------------------------------------------------------------------------
 Ruby:         ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux]
 GitLab:       15.4.0-pre (25e0d649a8c) EE
 GitLab Shell: 14.7.4
 PostgreSQL:   12.10
-------------------------------------------------------------[ booted in 9.44s ]
Loading production environment (Rails 6.1.6.1)
>> FactoryBot
Traceback (most recent call last):
        1: from (irb):1
NameError (uninitialized constant FactoryBot)

After

EE factories are available

$ bin/rails console
--------------------------------------------------------------------------------
 Ruby:         ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux]
 GitLab:       15.4.0-pre (ac4f1ac2435) EE
 GitLab Shell: Unknown
 PostgreSQL:   12.10
-------------------------------------------------------------[ booted in 0.00s ]
Loading test environment (Rails 6.1.6.1)

[1] pry(main)> FactoryBot.definition_file_paths
=> [#<Pathname:/home/peter/devel/gitlab/gdk/gitlab/factories>,
 #<Pathname:/home/peter/devel/gitlab/gdk/gitlab/test/factories>,
 #<Pathname:/home/peter/devel/gitlab/gdk/gitlab/spec/factories>,
 #<Pathname:/home/peter/devel/gitlab/gdk/gitlab/ee/spec/factories>]

[2] pry(main)> FactoryBot.definition_file_paths
=> [#<Pathname:/home/peter/devel/gitlab/gdk/gitlab/factories>,
 #<Pathname:/home/peter/devel/gitlab/gdk/gitlab/test/factories>,
 #<Pathname:/home/peter/devel/gitlab/gdk/gitlab/spec/factories>,
 #<Pathname:/home/peter/devel/gitlab/gdk/gitlab/ee/spec/factories>]
[2] pry(main)> FactoryBot.reload
=> ["/home/peter/devel/gitlab/gdk/gitlab/factories", "/home/peter/devel/gitlab/gdk/gitlab/test/factories", "/home/peter/devel/gitlab/gdk/gitlab/spec/factories", "/home/peter/devel/gitlab/gdk/gitlab/ee/spec/factories"]

# same for 
$ bin/rails console -e test


# Still works in FOSS

$ FOSS_ONLY=1 bin/rails  console
WARNING: This version of GitLab depends on gitlab-shell 14.10.0, but you're running 14.7.4. Please update gitlab-shell.
--------------------------------------------------------------------------------
 Ruby:         ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux]
 GitLab:       15.4.0-pre (5f5ad3c3a12) FOSS
 GitLab Shell: 14.7.4
 PostgreSQL:   12.10
-------------------------------------------------------------[ booted in 4.21s ]
Loading development environment (Rails 6.1.6.1)
[1] pry(main)> FactoryBot.definition_file_paths
=> [#<Pathname:/home/peter/devel/gitlab/gdk/gitlab/factories>, #<Pathname:/home/peter/devel/gitlab/gdk/gitlab/test/factories>, #<Pathname:/home/peter/devel/gitlab/gdk/gitlab/spec/factories>]
[2] pry(main)>


$ bin/rails  console -e production
WARNING: This version of GitLab depends on gitlab-shell 14.10.0, but you're running 14.7.4. Please update gitlab-shell.
--------------------------------------------------------------------------------
 Ruby:         ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux]
 GitLab:       15.4.0-pre (25e0d649a8c) EE
 GitLab Shell: 14.7.4
 PostgreSQL:   12.10
-------------------------------------------------------------[ booted in 9.44s ]
Loading production environment (Rails 6.1.6.1)
>> FactoryBot
Traceback (most recent call last):
        1: from (irb):1
NameError (uninitialized constant FactoryBot)

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Peter Leitzen

Merge request reports