Commit 4cc6c539 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Override to specify that we're overriding

parent bf6149a5
module EE
module Projects
module GitHttpController
def render_ok
raise NotImplementedError.new unless defined?(super)
extend ::Gitlab::Utils::Override
override :render_ok
def render_ok
set_workhorse_internal_api_content_type
render json: ::Gitlab::Workhorse.git_http_ok(repository, wiki?, user, action_name, show_all_refs: geo_request?)
end
......@@ -18,17 +19,15 @@ module EE
authentication_result.geo?(project)
end
override :access_actor
def access_actor
raise NotImplementedError.new unless defined?(super)
return :geo if geo?
super
end
override :authenticate_user
def authenticate_user
raise NotImplementedError.new unless defined?(super)
return super unless geo_request?
payload = ::Gitlab::Geo::JwtRequestDecoder.new(request.headers['Authorization']).decode
......
module Gitlab
module Utils
module Override
# Instead of writing patterns like this:
#
# def f
# raise NotImplementedError unless defined?(super)
#
# true
# end
#
# We could write it like:
#
# include ::Gitlab::Utils::Override
#
# override :f
# def f
# true
# end
#
# This would make sure we're overriding something. See:
# https://gitlab.com/gitlab-org/gitlab-ee/issues/1819
def override(method_name)
return unless ENV['STATIC_VERIFICATION']
return if instance_methods.include?(method_name)
raise NotImplementedError.new("Method #{method_name} doesn't exist")
end
end
end
end
......@@ -7,4 +7,9 @@ namespace :dev do
Rake::Task["gitlab:setup"].invoke
Rake::Task["gitlab:shell:setup"].invoke
end
desc "GitLab | Eager load application"
task load: :environment do
Rails.application.eager_load!
end
end
......@@ -12,9 +12,10 @@ tasks = [
%w[bundle exec license_finder],
%w[yarn run eslint],
%w[bundle exec rubocop --parallel],
%w[scripts/lint-conflicts.sh],
%w[bundle exec rake gettext:lint],
%w[scripts/lint-changelog-yaml]
%w[env STATIC_VERIFICATION=true bundle exec rake dev:load],
%w[scripts/lint-changelog-yaml],
%w[scripts/lint-conflicts.sh]
]
failed_tasks = tasks.reduce({}) do |failures, task|
......
require 'spec_helper'
describe Gitlab::Utils::Override do
let(:base) { Struct.new(:good) }
let(:derived) do
Class.new(base) do
extend Gitlab::Utils::Override # rubocop:disable RSpec/DescribedClass
end
end
shared_examples 'good derivation' do
subject do
derived.module_eval do
override :good
def good
super.succ
end
end
derived
end
end
shared_examples 'bad derivation' do
subject do
derived.module_eval do
override :bad
def bad
true
end
end
derived
end
end
describe '#override' do
context 'when STATIC_VERIFICATION is set' do
before do
stub_env('STATIC_VERIFICATION', 'true')
end
it_behaves_like 'good derivation' do
it 'checks ok for overriding method' do
result = subject.new(0).good
expect(result).to eq(1)
end
end
it_behaves_like 'bad derivation' do
it 'raises NotImplementedError when it is not overriding anything' do
expect { subject }.to raise_error(NotImplementedError)
end
end
end
context 'when STATIC_VERIFICATION is not set' do
it_behaves_like 'good derivation' do
it 'does not complain when it is overriding anything' do
result = subject.new(0).good
expect(result).to eq(1)
end
end
it_behaves_like 'bad derivation' do
it 'does not complain when it is not overriding anything' do
result = subject.new(0).bad
expect(result).to eq(true)
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