Skip to content

Commit 6a7798a

Browse files
committed
Avoid test failure due to git hooks
If user has some default git hooks configured, running tests could fail. This disables git hooks while running tests. Change-Id: If1f80448f4b9210f78ca858bb05bf1071e2e4144
1 parent 7a18680 commit 6a7798a

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

rdopkg/utils/cmd.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from builtins import str
33

44
import json
5+
import os
56
import re
67
import subprocess
78

@@ -43,6 +44,7 @@ def run(cmd, *params, **kwargs):
4344
print_stdout = kwargs.get('print_stdout', False)
4445
print_stderr = kwargs.get('print_stderr', False)
4546
print_output = kwargs.get('print_output', False)
47+
env = kwargs.get('env', None)
4648

4749
cmd = [cmd] + list(params)
4850
cmd_str = ' '.join(cmd)
@@ -69,7 +71,7 @@ def run(cmd, *params, **kwargs):
6971

7072
try:
7173
prc = subprocess.Popen(cmd, stdin=stdin, stdout=stdout,
72-
stderr=stderr)
74+
stderr=stderr, env=env)
7375
except OSError:
7476
raise exception.CommandNotFound(cmd=cmd[0])
7577
out, err = prc.communicate(input=input)
@@ -114,6 +116,16 @@ def __call__(self, *params, **kwargs):
114116
class Git(ShellCommand):
115117
command = "git"
116118

119+
def __call__(self, *params, **kwargs):
120+
# allows us to run git in isolated mode, avoiding interaction with user
121+
# specific config, like ~/.git-templates/hooks (used for testing)
122+
if kwargs.get('isolated', False):
123+
env = kwargs.get('env', os.environ.copy())
124+
env['GIT_CONFIG_NOSYSTEM'] = '1'
125+
env['GIT_CONFIG_NOGLOBAL'] = '1'
126+
kwargs['env'] = env
127+
return run(self.command, *params, **kwargs)
128+
117129
def create_branch_from_remote(self, branch, remote_branch=None):
118130
lbr = self.local_branches()
119131
if branch in lbr or remote_branch in lbr:

tests/test_common.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def prep_spec_test(tmpdir, distgit):
4747
with dist_path.as_cwd():
4848
git('init')
4949
git('add', '.')
50-
git('commit', '-m', 'Initial import')
50+
git('commit', '-m', 'Initial import',
51+
isolated=True)
5152
return dist_path
5253

5354

@@ -57,7 +58,8 @@ def prep_patches_branch(tag='1.2.3'):
5758
f.write("#not really a patch\n")
5859
f.close()
5960
git('add', 'foofile')
60-
git('commit', '-m', 'Create this test branch')
61+
git('commit', '-m', 'Create this test branch',
62+
isolated=True)
6163
if tag:
6264
git('tag', tag)
6365
git('checkout', 'master')
@@ -68,7 +70,8 @@ def do_patch(fn, content, msg):
6870
f.write(content)
6971
f.close()
7072
git('add', fn)
71-
git('commit', '-m', msg)
73+
git('commit', '-m', msg,
74+
isolated=True)
7275

7376

7477
def add_patches(extra=False, filtered=False, tag=None):

0 commit comments

Comments
 (0)