Commit 562dc2a1 authored by Georges Racinet on mutations.racinet.fr's avatar Georges Racinet on mutations.racinet.fr
Browse files

HgGitRepository: attribute to skip Git hooks for tests

This new attribute is not meant to be used in any other context
than RSpec tests, whence the name. It allows us to use higher
level methods such as `multi_action` in tests setups and will be
necessary to test them.

Upstream does something similar with no need to involve the tested code:
they launch Gitaly server with the `GITALY_TESTING_NO_GIT_HOOKS` environment
variable.

--HG--
branch : heptapod
parent 19a1f7f92c19
......@@ -12,6 +12,8 @@ module Mercurial
class HgGitRepository < Gitlab::Git::Repository
include Gitlab::Popen
attr_accessor :unit_tests_skip_hooks
MergeError = Class.new(StandardError)
HgError = Class.new(StandardError)
InvalidHgSetting = Class.new(StandardError)
......@@ -391,7 +393,7 @@ def hg_prepare_user_encoding(hg_env, user, message)
# in the usual environment variables
def hg_changeset_publish(user, hgsha, notify_gitlab: false)
env = hg_env_for_write(user)
env.delete('HEPTAPOD_SKIP_ALL_GITLAB_HOOKS') if notify_gitlab
env.delete('HEPTAPOD_SKIP_ALL_GITLAB_HOOKS') if notify_gitlab && !unit_tests_skip_hooks
out, status = popen([Gitlab.config.mercurial.bin_path,
'phase', '--public', '-r', hgsha],
@hgpath, env)
......@@ -491,7 +493,7 @@ def hg_merge_changeset(user, source_hgsha, target_hgsha, message,
else
hg_env_for_write(user)
end
hg_env.delete('HEPTAPOD_SKIP_ALL_GITLAB_HOOKS')
hg_env.delete('HEPTAPOD_SKIP_ALL_GITLAB_HOOKS') unless unit_tests_skip_hooks
hg_tmp_workdir(target_hgsha) do |share_path|
logger.info("#{logprefix} calling #{hg_exe} merge -r #{source_hgsha}")
......@@ -747,7 +749,7 @@ def rebase(
hg_env = hg_env_for_write(user)
# we need to notify GitLab: the merge request update is done at
# the receiving end of the post-receive hook
hg_env.delete('HEPTAPOD_SKIP_ALL_GITLAB_HOOKS')
hg_env.delete('HEPTAPOD_SKIP_ALL_GITLAB_HOOKS') unless unit_tests_skip_hooks
# don't see an option to rebase with an obsmarker note
username = hg_prepare_user_encoding(hg_env, user, "")
......@@ -906,7 +908,7 @@ def multi_action(user, branch_name:, message:, actions:,
hg_exe = Gitlab.config.mercurial.bin_path
hg_env = hg_env_for_write(user)
hg_env.delete('HEPTAPOD_SKIP_ALL_GITLAB_HOOKS')
hg_env.delete('HEPTAPOD_SKIP_ALL_GITLAB_HOOKS') unless unit_tests_skip_hooks
username = hg_prepare_user_encoding(hg_env, user, message)
......
......@@ -216,15 +216,18 @@
trait :hg_repository do
after :create do |project, _evaluator|
project.create_repository
# we need to disable hooks, because there's no obvious internal
# API to call for, hence this lower level call to #hg_commit
repo = project.repository.raw_repository
hg_env = repo.hg_env_for_write(project.creator) # yes, disables hooks
repo.hg_tmp_workdir(nil) do |share_path|
Pathname(share_path).join('README').write("this is Mercurial\n")
repo.hg_commit(share_path, "John Doe1 <jdoe1@heptapod.example>",
"Test commit 0", hg_env, true)
end
repo.unit_tests_skip_hooks = true
repo.multi_action(project.creator,
branch_name: 'branch/default',
message: "Commit 0",
author_email: "jdoe1@heptapod.test",
author_name: "John Doe1",
actions: [{ action: :create,
file_path: 'README',
content: "this is Mercurial\n" }])
repo.expire_has_local_branches_cache
end
end
......
......@@ -7,7 +7,11 @@
include Gitlab::EncodingHelper
include RepoHelpers
let(:mutable_repository) { Gitlab::Mercurial::HgGitRepository.new('default', TEST_MUTABLE_REPO_PATH, '', 'group/project') }
let(:mutable_repository) do
repo = Gitlab::Mercurial::HgGitRepository.new('default', TEST_MUTABLE_REPO_PATH, '', 'group/project')
repo.unit_tests_skip_hooks = true
repo
end
let(:mutable_repository_path) { File.join(TestEnv.repos_path, mutable_repository.relative_path).chomp('.git') + '.hg' }
let(:user) { build(:user) }
let(:non_empty_repository) do
......@@ -17,18 +21,16 @@
in_subgroup: false)
expect(status).to be(true)
# we need to disable hooks, because there's no obvious internal
# API to call for, hence this lower level call to #hg_commit
# TODO make that global based on an environment variable, same as upstream
# does for Git repositories.
hg_env = repo.hg_env_for_write(user) # yes, disables hooks
file_path = Pathname(mutable_repository_path).join('foo')
file_path.write("foo0\n")
repo.hg_commit(mutable_repository_path,
"John Doe1 <jdoe1@heptapod.example>",
"Commit 0", hg_env, true)
repo.multi_action(user,
branch_name: 'branch/default',
message: "Commit 0",
author_email: "jdoe1@heptapod.test",
author_name: "John Doe1",
actions: [{ action: :create,
file_path: 'foo',
content: "foo0\n" }])
repo.expire_has_local_branches_cache
expect(repo.empty?).to be(false)
repo
end
let(:non_empty_repository_path) { mutable_repository_path }
......
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