Commit 5b999de2 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'introduce-gitlab-edition-module' into 'master'

Introduce a new GitlabEdition module for simple edition-checks

See merge request gitlab-org/gitlab!76851
parents be203210 5bda46f7
# frozen_string_literal: true
require 'pathname'
require 'forwardable'
require_relative 'gitlab_edition'
module Gitlab
def self.root
Pathname.new(File.expand_path('..', __dir__))
class << self
extend Forwardable
def_delegators :GitlabEdition, :root, :extensions, :ee?, :ee, :jh?, :jh
end
def self.version_info
......@@ -89,47 +94,6 @@ module Gitlab
Rails.env.development? || Rails.env.test?
end
def self.extensions
if jh?
%w[ee jh]
elsif ee?
%w[ee]
else
%w[]
end
end
def self.ee?
@is_ee ||=
# We use this method when the Rails environment is not loaded. This
# means that checking the presence of the License class could result in
# this method returning `false`, even for an EE installation.
#
# The `FOSS_ONLY` is always `string` or `nil`
# Thus the nil or empty string will result
# in using default value: false
#
# The behavior needs to be synchronised with
# config/helpers/is_ee_env.js
root.join('ee/app/models/license.rb').exist? &&
!%w[true 1].include?(ENV['FOSS_ONLY'].to_s)
end
def self.jh?
@is_jh ||=
ee? &&
root.join('jh').exist? &&
!%w[true 1].include?(ENV['EE_ONLY'].to_s)
end
def self.ee
yield if ee?
end
def self.jh
yield if jh?
end
def self.http_proxy_env?
HTTP_PROXY_ENV_VARS.any? { |name| ENV[name] }
end
......
# frozen_string_literal: true
require 'pathname'
module GitlabEdition
def self.root
Pathname.new(File.expand_path('..', __dir__))
end
def self.extensions
if jh?
%w[ee jh]
elsif ee?
%w[ee]
else
%w[]
end
end
def self.ee?
@is_ee ||=
# We use this method when the Rails environment is not loaded. This
# means that checking the presence of the License class could result in
# this method returning `false`, even for an EE installation.
#
# The `FOSS_ONLY` is always `string` or `nil`
# Thus the nil or empty string will result
# in using default value: false
#
# The behavior needs to be synchronised with
# config/helpers/is_ee_env.js
root.join('ee/app/models/license.rb').exist? &&
!%w[true 1].include?(ENV['FOSS_ONLY'].to_s)
end
def self.jh?
@is_jh ||=
ee? &&
root.join('jh').exist? &&
!%w[true 1].include?(ENV['EE_ONLY'].to_s)
end
def self.ee
yield if ee?
end
def self.jh
yield if jh?
end
end
# frozen_string_literal: true
require 'forwardable'
require_relative '../lib/gitlab_edition'
module RuboCop
module CodeReuseHelpers
extend Forwardable
def_delegators :GitlabEdition, :ee?, :jh?
# Returns true for a `(send const ...)` node.
def send_to_constant?(node)
node.type == :send && node.children&.first&.type == :const
......@@ -180,13 +188,5 @@ module RuboCop
def rails_root
File.expand_path('..', __dir__)
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
......@@ -3,7 +3,7 @@
require 'set'
require 'fileutils'
require_relative 'lib/gitlab'
require_relative '../lib/gitlab_edition'
class String
def red
......@@ -28,7 +28,7 @@ flags_paths = [
]
# For EE additionally process `ee/` feature flags
if Gitlab.ee?
if GitlabEdition.ee?
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
......@@ -43,7 +43,7 @@ if Gitlab.ee?
end
# For JH additionally process `jh/` feature flags
if Gitlab.jh?
if GitlabEdition.jh?
flags_paths << 'jh/config/feature_flags/**/*.yml'
Dir.glob('jh/app/replicators/geo/*_replicator.rb').each_with_object(Set.new) do |path, memo|
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe GitlabEdition do
before do
# Make sure the ENV is clean
stub_env('FOSS_ONLY', nil)
stub_env('EE_ONLY', nil)
described_class.instance_variable_set(:@is_ee, nil)
described_class.instance_variable_set(:@is_jh, nil)
end
after do
described_class.instance_variable_set(:@is_ee, nil)
described_class.instance_variable_set(:@is_jh, nil)
end
describe '.root' do
it 'returns the root path of the app' do
expect(described_class.root).to eq(Pathname.new(File.expand_path('../..', __dir__)))
end
end
describe 'extensions' do
context 'when .jh? is true' do
before do
allow(described_class).to receive(:jh?).and_return(true)
end
it 'returns %w[ee jh]' do
expect(described_class.extensions).to match_array(%w[ee jh])
end
end
context 'when .ee? is true' do
before do
allow(described_class).to receive(:jh?).and_return(false)
allow(described_class).to receive(:ee?).and_return(true)
end
it 'returns %w[ee]' do
expect(described_class.extensions).to match_array(%w[ee])
end
end
context 'when neither .jh? and .ee? are true' do
before do
allow(described_class).to receive(:jh?).and_return(false)
allow(described_class).to receive(:ee?).and_return(false)
end
it 'returns the exyensions according to the current edition' do
expect(described_class.extensions).to be_empty
end
end
end
describe '.ee? and .jh?' do
def stub_path(*paths, **arguments)
root = Pathname.new('dummy')
pathname = double(:path, **arguments)
allow(described_class)
.to receive(:root)
.and_return(root)
allow(root).to receive(:join)
paths.each do |path|
allow(root)
.to receive(:join)
.with(path)
.and_return(pathname)
end
end
describe '.ee?' do
context 'for EE' do
before do
stub_path('ee/app/models/license.rb', exist?: true)
end
context 'when using FOSS_ONLY=1' do
before do
stub_env('FOSS_ONLY', '1')
end
it 'returns not to be EE' do
expect(described_class).not_to be_ee
end
end
context 'when using FOSS_ONLY=0' do
before do
stub_env('FOSS_ONLY', '0')
end
it 'returns to be EE' do
expect(described_class).to be_ee
end
end
context 'when using default FOSS_ONLY' do
it 'returns to be EE' do
expect(described_class).to be_ee
end
end
end
context 'for CE' do
before do
stub_path('ee/app/models/license.rb', exist?: false)
end
it 'returns not to be EE' do
expect(described_class).not_to be_ee
end
end
end
describe '.jh?' do
context 'for JH' do
before do
stub_path(
'ee/app/models/license.rb',
'jh',
exist?: true)
end
context 'when using default FOSS_ONLY and EE_ONLY' do
it 'returns to be JH' do
expect(described_class).to be_jh
end
end
context 'when using FOSS_ONLY=1' do
before do
stub_env('FOSS_ONLY', '1')
end
it 'returns not to be JH' do
expect(described_class).not_to be_jh
end
end
context 'when using EE_ONLY=1' do
before do
stub_env('EE_ONLY', '1')
end
it 'returns not to be JH' do
expect(described_class).not_to be_jh
end
end
end
end
end
end
......@@ -3,9 +3,19 @@
require 'spec_helper'
RSpec.describe Gitlab do
describe '.root' do
it 'returns the root path of the app' do
expect(described_class.root).to eq(Pathname.new(File.expand_path('../..', __dir__)))
%w[root extensions ee? jh?].each do |method_name|
it "delegates #{method_name} to GitlabEdition" do
expect(GitlabEdition).to receive(method_name)
described_class.public_send(method_name)
end
end
%w[ee jh].each do |method_name|
it "delegates #{method_name} to GitlabEdition" do
expect(GitlabEdition).to receive(method_name)
described_class.public_send(method_name) {}
end
end
......@@ -248,121 +258,6 @@ RSpec.describe Gitlab do
end
end
describe 'ee? and jh?' do
before do
# Make sure the ENV is clean
stub_env('FOSS_ONLY', nil)
stub_env('EE_ONLY', nil)
described_class.instance_variable_set(:@is_ee, nil)
described_class.instance_variable_set(:@is_jh, nil)
end
after do
described_class.instance_variable_set(:@is_ee, nil)
described_class.instance_variable_set(:@is_jh, nil)
end
def stub_path(*paths, **arguments)
root = Pathname.new('dummy')
pathname = double(:path, **arguments)
allow(described_class)
.to receive(:root)
.and_return(root)
allow(root).to receive(:join)
paths.each do |path|
allow(root)
.to receive(:join)
.with(path)
.and_return(pathname)
end
end
describe '.ee?' do
context 'for EE' do
before do
stub_path('ee/app/models/license.rb', exist?: true)
end
context 'when using FOSS_ONLY=1' do
before do
stub_env('FOSS_ONLY', '1')
end
it 'returns not to be EE' do
expect(described_class).not_to be_ee
end
end
context 'when using FOSS_ONLY=0' do
before do
stub_env('FOSS_ONLY', '0')
end
it 'returns to be EE' do
expect(described_class).to be_ee
end
end
context 'when using default FOSS_ONLY' do
it 'returns to be EE' do
expect(described_class).to be_ee
end
end
end
context 'for CE' do
before do
stub_path('ee/app/models/license.rb', exist?: false)
end
it 'returns not to be EE' do
expect(described_class).not_to be_ee
end
end
end
describe '.jh?' do
context 'for JH' do
before do
stub_path(
'ee/app/models/license.rb',
'jh',
exist?: true)
end
context 'when using default FOSS_ONLY and EE_ONLY' do
it 'returns to be JH' do
expect(described_class).to be_jh
end
end
context 'when using FOSS_ONLY=1' do
before do
stub_env('FOSS_ONLY', '1')
end
it 'returns not to be JH' do
expect(described_class).not_to be_jh
end
end
context 'when using EE_ONLY=1' do
before do
stub_env('EE_ONLY', '1')
end
it 'returns not to be JH' do
expect(described_class).not_to be_jh
end
end
end
end
end
describe '.http_proxy_env?' do
it 'returns true when lower case https' do
stub_env('https_proxy', 'https://my.proxy')
......
......@@ -315,76 +315,11 @@ RSpec.describe RuboCop::CodeReuseHelpers do
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
%w[ee? jh?].each do |method_name|
it "delegates #{method_name} to GitlabEdition" do
expect(GitlabEdition).to receive(method_name)
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
cop.public_send(method_name)
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