From e9cf1020de206607c679ca8bdfd1414a2446fbb4 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Fri, 11 May 2018 13:28:26 +0200 Subject: [PATCH 1/3] Improve fast specs helper to autoload the library --- spec/fast_spec_helper.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/spec/fast_spec_helper.rb b/spec/fast_spec_helper.rb index 978113a08a4..134eb25e4b1 100644 --- a/spec/fast_spec_helper.rb +++ b/spec/fast_spec_helper.rb @@ -3,14 +3,8 @@ require 'bundler/setup' ENV['GITLAB_ENV'] = 'test' ENV['IN_MEMORY_APPLICATION_SETTINGS'] = 'true' -unless Object.respond_to?(:require_dependency) - class Object - alias_method :require_dependency, :require - end -end - -# Defines Settings and Gitlab.config which are at the center of the app require_relative '../config/settings' -require_relative '../lib/gitlab' unless defined?(Gitlab.config) - require_relative 'support/rspec' +require 'active_support/all' + +ActiveSupport::Dependencies.autoload_paths << 'lib' From d134c0fb70eb9330680872edefa8841641931018 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 15 May 2018 10:38:00 +0200 Subject: [PATCH 2/3] Update docs describing `fast_spec_helper` best practices --- .../testing_guide/best_practices.md | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/doc/development/testing_guide/best_practices.md b/doc/development/testing_guide/best_practices.md index 61fa5459b91..5aa2068b495 100644 --- a/doc/development/testing_guide/best_practices.md +++ b/doc/development/testing_guide/best_practices.md @@ -134,11 +134,30 @@ really fast since: - gitlab-shell and Gitaly setup are skipped - Test repositories setup are skipped -Note that in some cases, you might have to add some `require_dependency 'foo'` -in your file under test since Rails autoloading is not available in these cases. +`fast_spec_helper` also support autoloading classes that are located inside the +`lib/` directory. It means that as long as your class / module is using only +code from the `lib/` directory you will not need to explicitly load any +dependencies. `fast_spec_helper` also loads all ActiveSupport extensions, +including core extensions that are commonly used in the Rails environment. -This shouldn't be a problem since explicitely listing dependencies should be -considered a good practice anyway. +Note that in some cases, you might still have to load some dependencies using +`require_dependency` in your `*_spec.rb` file, like when a code is using gems. + +For example, if you want to test your code that is calling the +`Gitlab::UntrustedRegexp` class, which under the hood uses `re2` library, you +should be able to define a test using follow code snippet. + +```ruby +require 'fast_spec_helper' +require_dependency 're2' + +describe Gitlab::MyModule::MyClass do + # ... +end +``` + +It takes around one second to load tests that are using `fast_spec_helper` +instead of 30+ seconds in case of a regular `spec_helper`. ### `let` variables From 7186f0de657f510e27e24b6845f99ded158118af Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 15 May 2018 13:34:49 +0200 Subject: [PATCH 3/3] Improve testing best practices guidelines It mentions that explicitly defining dependencies in sources is preferred over defining such dependencies only in spec files. --- doc/development/testing_guide/best_practices.md | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/doc/development/testing_guide/best_practices.md b/doc/development/testing_guide/best_practices.md index 5aa2068b495..c4d350e3676 100644 --- a/doc/development/testing_guide/best_practices.md +++ b/doc/development/testing_guide/best_practices.md @@ -141,20 +141,14 @@ dependencies. `fast_spec_helper` also loads all ActiveSupport extensions, including core extensions that are commonly used in the Rails environment. Note that in some cases, you might still have to load some dependencies using -`require_dependency` in your `*_spec.rb` file, like when a code is using gems. +`require_dependency` when a code is using gems or a dependency is not located +in `lib/`. For example, if you want to test your code that is calling the `Gitlab::UntrustedRegexp` class, which under the hood uses `re2` library, you -should be able to define a test using follow code snippet. - -```ruby -require 'fast_spec_helper' -require_dependency 're2' - -describe Gitlab::MyModule::MyClass do - # ... -end -``` +should either add `require_dependency 're2'` to files in your library that +need `re2` gem, to make this requirement explicit, or you can add it to the +spec itself, but the former is preferred. It takes around one second to load tests that are using `fast_spec_helper` instead of 30+ seconds in case of a regular `spec_helper`.