Make jh spec_helper load after ee spec_helper to improve flexibility of jh test

Problem

While executing the test file, we have a sequence like this

# spec/spec_helper.rb
require_relative('../jh/spec/spec_helper') if Gitlab.jh?
require_relative('../ee/spec/spec_helper') if Gitlab.ee?

This order seems odd and we need to adjust it.

Why

In the unit test of ee, we use prepend_mod to find jh module with the same name to complete the override. So when executing the unit test of ee, the jh module must have been required.

Option 1 (Bad)

Use autoload_paths in file config/application.rb to load jh module like below:

# https://jihulab.com/gitlab-cn/gitlab/-/blob/main-jh/config/application.rb#L139
Gitlab.ee { config.autoload_paths.push("#{config.root}/ee/lib/generators") }

These loaded actions will be executed before executing the unit test, so it can meet our needs.

BUT!

config/application.rb file is in the upstream code, it is difficult to convince the upstream colleagues to add a autoload_paths jh/xxx

Option 2 (Good!)

We still load jh module at the beginning, but no longer use autoload_paths, we use require instead.

Just like this:

Dir[Rails.root.join("jh/spec/...")].sort.each { |f| require f }

require operation should be performed in the jh/ directory, which is more free and flexible.

Baodong 编辑于