Commit 18a34cdb authored by Lin Jen-Shin's avatar Lin Jen-Shin

Merge branch '344781-follow-up-refactors-tests' into 'master'

Refactor is_ee/is_jh and add tests

See merge request gitlab-org/gitlab!75466
parents afc27f2f 2927425f
...@@ -180,5 +180,13 @@ module RuboCop ...@@ -180,5 +180,13 @@ module RuboCop
def rails_root def rails_root
File.expand_path('..', __dir__) File.expand_path('..', __dir__)
end end
def ee?
File.exist?(File.expand_path('../ee/app/models/license.rb', __dir__)) && !%w[true 1].include?(ENV['FOSS_ONLY'].to_s)
end
def jh?
ee? && Dir.exist?(File.expand_path('../jh', __dir__)) && !%w[true 1].include?(ENV['EE_ONLY'].to_s)
end
end end
end end
...@@ -255,14 +255,12 @@ module RuboCop ...@@ -255,14 +255,12 @@ module RuboCop
] ]
# For EE additionally process `ee/` feature flags # For EE additionally process `ee/` feature flags
is_ee = File.exist?(File.expand_path('../../../ee/app/models/license.rb', __dir__)) && !%w[true 1].include?(ENV['FOSS_ONLY'].to_s) if ee?
if is_ee
flags_paths << 'ee/config/feature_flags/**/*.yml' flags_paths << 'ee/config/feature_flags/**/*.yml'
end end
# For JH additionally process `jh/` feature flags # For JH additionally process `jh/` feature flags
is_jh = is_ee && Dir.exist?(File.expand_path('../../../jh', __dir__)) && !%w[true 1].include?(ENV['EE_ONLY'].to_s) if jh?
if is_jh
flags_paths << 'jh/config/feature_flags/**/*.yml' flags_paths << 'jh/config/feature_flags/**/*.yml'
end end
......
# frozen_string_literal: true
module Gitlab
module_function
def ee?
File.exist?(File.expand_path('../../ee/app/models/license.rb', __dir__)) && !%w[true 1].include?(ENV['FOSS_ONLY'].to_s)
end
def jh?
ee? && Dir.exist?(File.expand_path('../../jh', __dir__)) && !%w[true 1].include?(ENV['EE_ONLY'].to_s)
end
end
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
require 'set' require 'set'
require 'fileutils' require 'fileutils'
require_relative 'lib/gitlab'
class String class String
def red def red
...@@ -27,8 +28,7 @@ flags_paths = [ ...@@ -27,8 +28,7 @@ flags_paths = [
] ]
# For EE additionally process `ee/` feature flags # For EE additionally process `ee/` feature flags
is_ee = File.exist?('ee/app/models/license.rb') && !%w[true 1].include?(ENV['FOSS_ONLY'].to_s) if Gitlab.ee?
if is_ee
flags_paths << 'ee/config/feature_flags/**/*.yml' flags_paths << 'ee/config/feature_flags/**/*.yml'
# Geo feature flags are constructed dynamically and there's no explicit checks in the codebase so we mark all # Geo feature flags are constructed dynamically and there's no explicit checks in the codebase so we mark all
...@@ -43,8 +43,7 @@ if is_ee ...@@ -43,8 +43,7 @@ if is_ee
end end
# For JH additionally process `jh/` feature flags # For JH additionally process `jh/` feature flags
is_jh = is_ee && Dir.exist?('jh') && !%w[true 1].include?(ENV['EE_ONLY'].to_s) if Gitlab.jh?
if is_jh
flags_paths << 'jh/config/feature_flags/**/*.yml' flags_paths << 'jh/config/feature_flags/**/*.yml'
Dir.glob('jh/app/replicators/geo/*_replicator.rb').each_with_object(Set.new) do |path, memo| Dir.glob('jh/app/replicators/geo/*_replicator.rb').each_with_object(Set.new) do |path, memo|
......
...@@ -21,6 +21,8 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -21,6 +21,8 @@ RSpec.describe RuboCop::CodeReuseHelpers do
end.new end.new
end end
let(:ee_file_path) { File.expand_path('../../ee/app/models/license.rb', __dir__) }
describe '#send_to_constant?' do describe '#send_to_constant?' do
it 'returns true when sending to a constant' do it 'returns true when sending to a constant' do
node = build_and_parse_source('Foo.bar') node = build_and_parse_source('Foo.bar')
...@@ -312,4 +314,77 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -312,4 +314,77 @@ RSpec.describe RuboCop::CodeReuseHelpers do
cop.disallow_send_to(def_node, 'Finder', 'oops') cop.disallow_send_to(def_node, 'Finder', 'oops')
end end
end end
describe '#ee?' do
before do
stub_env('FOSS_ONLY', nil)
allow(File).to receive(:exist?).with(ee_file_path) { true }
end
it 'returns true when ee/app/models/license.rb exists' do
expect(cop.ee?).to eq(true)
end
end
describe '#jh?' do
context 'when jh directory exists and EE_ONLY is not set' do
before do
stub_env('EE_ONLY', nil)
allow(Dir).to receive(:exist?).with(File.expand_path('../../jh', __dir__)) { true }
end
context 'when ee/app/models/license.rb exists' do
before do
allow(File).to receive(:exist?).with(ee_file_path) { true }
end
context 'when FOSS_ONLY is not set' do
before do
stub_env('FOSS_ONLY', nil)
end
it 'returns true' do
expect(cop.jh?).to eq(true)
end
end
context 'when FOSS_ONLY is set to 1' do
before do
stub_env('FOSS_ONLY', '1')
end
it 'returns false' do
expect(cop.jh?).to eq(false)
end
end
end
context 'when ee/app/models/license.rb not exist' do
before do
allow(File).to receive(:exist?).with(ee_file_path) { false }
end
context 'when FOSS_ONLY is not set' do
before do
stub_env('FOSS_ONLY', nil)
end
it 'returns true' do
expect(cop.jh?).to eq(false)
end
end
context 'when FOSS_ONLY is set to 1' do
before do
stub_env('FOSS_ONLY', '1')
end
it 'returns false' do
expect(cop.jh?).to eq(false)
end
end
end
end
end
end end
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../scripts/lib/gitlab'
RSpec.describe 'scripts/lib/gitlab.rb' do
let(:ee_file_path) { File.expand_path('../../../ee/app/models/license.rb', __dir__) }
describe '.ee?' do
before do
stub_env('FOSS_ONLY', nil)
allow(File).to receive(:exist?).with(ee_file_path) { true }
end
it 'returns true when ee/app/models/license.rb exists' do
expect(Gitlab.ee?).to eq(true)
end
end
describe '.jh?' do
context 'when jh directory exists and EE_ONLY is not set' do
before do
stub_env('EE_ONLY', nil)
allow(Dir).to receive(:exist?).with(File.expand_path('../../../jh', __dir__)) { true }
end
context 'when ee/app/models/license.rb exists' do
before do
allow(File).to receive(:exist?).with(ee_file_path) { true }
end
context 'when FOSS_ONLY is not set' do
before do
stub_env('FOSS_ONLY', nil)
end
it 'returns true' do
expect(Gitlab.jh?).to eq(true)
end
end
context 'when FOSS_ONLY is set to 1' do
before do
stub_env('FOSS_ONLY', '1')
end
it 'returns false' do
expect(Gitlab.jh?).to eq(false)
end
end
end
context 'when ee/app/models/license.rb not exist' do
before do
allow(File).to receive(:exist?).with(ee_file_path) { false }
end
context 'when FOSS_ONLY is not set' do
before do
stub_env('FOSS_ONLY', nil)
end
it 'returns true' do
expect(Gitlab.jh?).to eq(false)
end
end
context 'when FOSS_ONLY is set to 1' do
before do
stub_env('FOSS_ONLY', '1')
end
it 'returns false' do
expect(Gitlab.jh?).to eq(false)
end
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment