Update git state integration tests for PY3 compatibility

Python 3 didn't like some of the stuff that we were doing with
``subprocess.check_call()`` in these tests, so to fix this I have redone
that stuff with calls to functions in the git execution module. In order
to avoid problems with running tests with no global gitconfig, I needed
to add an argument called ``git_opts`` to most of the funcs in the git
execution module (well I didn't *need* to do it to most of the funcs, it
just seemed like we shouldn't only be supporting this argument in a
single function).

This new ``git_opts`` argument is specifically for passing arguments to
the git command itself (not the subcommand). For example, ``git -c
user.name="Foo Bar" commit .....`` is different than running ``git
commit -c user.name="Foo Bar" .....``, because the ``commit`` subcommand
for git also accepts ``-c``.
This commit is contained in:
Erik Johnson 2017-03-14 18:07:06 -05:00
parent c09f8e283c
commit ef110baa99
2 changed files with 281 additions and 88 deletions

View File

@ -355,6 +355,7 @@ def _which_git_config(global_, cwd, user, password):
def add(cwd,
filename,
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False):
@ -378,6 +379,13 @@ def add(cwd,
necessary to precede them with ``opts=`` (as in the CLI examples
below) to avoid causing errors with Salt's own argument parsing.
git_opts
Any additional options to add to git command itself (not the ``add``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -407,7 +415,8 @@ def add(cwd,
cwd = _expand_path(cwd, user)
if not isinstance(filename, six.string_types):
filename = str(filename)
command = ['git', 'add', '--verbose']
command = ['git'] + _format_opts(git_opts)
command.extend(['add', '--verbose'])
command.extend(
[x for x in _format_opts(opts) if x not in ('-v', '--verbose')]
)
@ -423,6 +432,7 @@ def archive(cwd,
output,
rev='HEAD',
prefix=None,
git_opts='',
user=None,
password=None,
ignore_retcode=False,
@ -491,6 +501,13 @@ def archive(cwd,
specifying a prefix, if the prefix is intended to create a
top-level directory.
git_opts
Any additional options to add to git command itself (not the
``archive`` subcommand), in a single string. This is useful for passing
``-c`` to run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -527,7 +544,8 @@ def archive(cwd,
if kwargs:
salt.utils.invalid_kwargs(kwargs)
command = ['git', 'archive']
command = ['git'] + _format_opts(git_opts)
command.append('archive')
# If prefix was set to '' then we skip adding the --prefix option
if prefix != '':
if prefix:
@ -557,6 +575,7 @@ def archive(cwd,
def branch(cwd,
name=None,
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False):
@ -584,6 +603,13 @@ def branch(cwd,
examples below) to avoid causing errors with Salt's own argument
parsing.
git_opts
Any additional options to add to git command itself (not the ``branch``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -617,7 +643,8 @@ def branch(cwd,
salt myminion git.branch /path/to/repo newbranch opts='-m oldbranch'
'''
cwd = _expand_path(cwd, user)
command = ['git', 'branch']
command = ['git'] + _format_opts(git_opts)
command.append('branch')
command.extend(_format_opts(opts))
if name is not None:
command.append(name)
@ -633,6 +660,7 @@ def checkout(cwd,
rev=None,
force=False,
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False):
@ -650,6 +678,14 @@ def checkout(cwd,
necessary to precede them with ``opts=`` (as in the CLI examples
below) to avoid causing errors with Salt's own argument parsing.
git_opts
Any additional options to add to git command itself (not the
``checkout`` subcommand), in a single string. This is useful for
passing ``-c`` to run git with temporary changes to the git
configuration.
.. versionadded:: Nitrogen
rev
The remote branch or revision to checkout.
@ -692,7 +728,8 @@ def checkout(cwd,
salt myminion git.checkout /path/to/repo opts='-b newbranch'
'''
cwd = _expand_path(cwd, user)
command = ['git', 'checkout']
command = ['git'] + _format_opts(git_opts)
command.append('checkout')
if force:
command.append('--force')
opts = _format_opts(opts)
@ -720,6 +757,7 @@ def clone(cwd,
url=None, # Remove default value once 'repository' arg is removed
name=None,
opts='',
git_opts='',
user=None,
password=None,
identity=None,
@ -752,6 +790,13 @@ def clone(cwd,
opts
Any additional options to add to the command line, in a single string
git_opts
Any additional options to add to git command itself (not the ``clone``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -827,7 +872,8 @@ def clone(cwd,
except ValueError as exc:
raise SaltInvocationError(exc.__str__())
command = ['git', 'clone']
command = ['git'] + _format_opts(git_opts)
command.append('clone')
command.extend(_format_opts(opts))
command.extend(['--', url])
if name is not None:
@ -859,6 +905,7 @@ def clone(cwd,
def commit(cwd,
message,
opts='',
git_opts='',
user=None,
password=None,
filename=None,
@ -873,7 +920,8 @@ def commit(cwd,
Commit message
opts
Any additional options to add to the command line, in a single string
Any additional options to add to the command line, in a single string.
These opts will be added to the end of the git command being run.
.. note::
On the Salt CLI, if the opts are preceded with a dash, it is
@ -883,6 +931,13 @@ def commit(cwd,
The ``-m`` option should not be passed here, as the commit message
will be defined by the ``message`` argument.
git_opts
Any additional options to add to git command itself (not the ``commit``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -921,7 +976,8 @@ def commit(cwd,
salt myminion git.commit /path/to/repo 'The commit message' filename=foo/bar.py
'''
cwd = _expand_path(cwd, user)
command = ['git', 'commit', '-m', message]
command = ['git'] + _format_opts(git_opts)
command.extend(['commit', '-m', message])
command.extend(_format_opts(opts))
if filename:
if not isinstance(filename, six.string_types):
@ -1472,6 +1528,7 @@ def diff(cwd,
item1=None,
item2=None,
opts='',
git_opts='',
user=None,
password=None,
no_index=False,
@ -1501,6 +1558,13 @@ def diff(cwd,
necessary to precede them with ``opts=`` (as in the CLI examples
below) to avoid causing errors with Salt's own argument parsing.
git_opts
Any additional options to add to git command itself (not the ``diff``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -1561,7 +1625,8 @@ def diff(cwd,
'The \'no_index\' and \'cached\' options cannot be used together'
)
command = ['git', 'diff']
command = ['git'] + _format_opts(git_opts)
command.append('diff')
command.extend(_format_opts(opts))
if paths is not None and not isinstance(paths, (list, tuple)):
@ -1620,6 +1685,7 @@ def fetch(cwd,
force=False,
refspecs=None,
opts='',
git_opts='',
user=None,
password=None,
identity=None,
@ -1660,6 +1726,13 @@ def fetch(cwd,
necessary to precede them with ``opts=`` (as in the CLI examples
below) to avoid causing errors with Salt's own argument parsing.
git_opts
Any additional options to add to git command itself (not the ``fetch``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -1715,7 +1788,8 @@ def fetch(cwd,
salt myminion git.fetch /path/to/repo identity=/root/.ssh/id_rsa
'''
cwd = _expand_path(cwd, user)
command = ['git', 'fetch']
command = ['git'] + _format_opts(git_opts)
command.append('fetch')
if force:
command.append('--force')
command.extend(
@ -1780,6 +1854,7 @@ def init(cwd,
separate_git_dir=None,
shared=None,
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False):
@ -1818,6 +1893,13 @@ def init(cwd,
necessary to precede them with ``opts=`` (as in the CLI examples
below) to avoid causing errors with Salt's own argument parsing.
git_opts
Any additional options to add to git command itself (not the ``init``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -1849,7 +1931,8 @@ def init(cwd,
salt myminion git.init /path/to/bare/repo.git bare=True
'''
cwd = _expand_path(cwd, user)
command = ['git', 'init']
command = ['git'] + _format_opts(git_opts)
command.append('init')
if bare:
command.append('--bare')
if template is not None:
@ -2334,6 +2417,7 @@ def ls_remote(cwd=None,
remote='origin',
ref=None,
opts='',
git_opts='',
user=None,
password=None,
identity=None,
@ -2374,6 +2458,14 @@ def ls_remote(cwd=None,
.. versionadded:: 2015.8.0
git_opts
Any additional options to add to git command itself (not the
``ls-remote`` subcommand), in a single string. This is useful for
passing ``-c`` to run git with temporary changes to the git
configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -2447,7 +2539,8 @@ def ls_remote(cwd=None,
https_only=True)
except ValueError as exc:
raise SaltInvocationError(exc.__str__())
command = ['git', 'ls-remote']
command = ['git'] + _format_opts(git_opts)
command.append('ls-remote')
command.extend(_format_opts(opts))
if not isinstance(remote, six.string_types):
remote = str(remote)
@ -2476,6 +2569,7 @@ def ls_remote(cwd=None,
def merge(cwd,
rev=None,
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False,
@ -2500,6 +2594,13 @@ def merge(cwd,
necessary to precede them with ``opts=`` (as in the CLI examples
below) to avoid causing errors with Salt's own argument parsing.
git_opts
Any additional options to add to git command itself (not the ``merge``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -2535,7 +2636,8 @@ def merge(cwd,
salt.utils.invalid_kwargs(kwargs)
cwd = _expand_path(cwd, user)
command = ['git', 'merge']
command = ['git'] + _format_opts(git_opts)
command.append('merge')
command.extend(_format_opts(opts))
if rev:
if not isinstance(rev, six.string_types):
@ -2555,6 +2657,7 @@ def merge_base(cwd,
independent=False,
fork_point=None,
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False,
@ -2619,6 +2722,14 @@ def merge_base(cwd,
This option should not be necessary unless new CLI arguments are
added to `git-merge-base(1)`_ and are not yet supported in Salt.
git_opts
Any additional options to add to git command itself (not the
``merge-base`` subcommand), in a single string. This is useful for
passing ``-c`` to run git with temporary changes to the git
configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -2708,7 +2819,8 @@ def merge_base(cwd,
password=password,
ignore_retcode=ignore_retcode) == first_commit
command = ['git', 'merge-base']
command = ['git'] + _format_opts(git_opts)
command.append('merge-base')
command.extend(_format_opts(opts))
if all_:
command.append('--all')
@ -2814,6 +2926,7 @@ def merge_tree(cwd,
def pull(cwd,
opts='',
git_opts='',
user=None,
password=None,
identity=None,
@ -2833,6 +2946,13 @@ def pull(cwd,
necessary to precede them with ``opts=`` (as in the CLI examples
below) to avoid causing errors with Salt's own argument parsing.
git_opts
Any additional options to add to git command itself (not the ``pull``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -2886,7 +3006,8 @@ def pull(cwd,
salt myminion git.pull /path/to/repo opts='--rebase origin master'
'''
cwd = _expand_path(cwd, user)
command = ['git', 'pull']
command = ['git'] + _format_opts(git_opts)
command.append('pull')
command.extend(_format_opts(opts))
return _git_run(command,
cwd=cwd,
@ -2901,6 +3022,7 @@ def push(cwd,
remote=None,
ref=None,
opts='',
git_opts='',
user=None,
password=None,
identity=None,
@ -2933,6 +3055,13 @@ def push(cwd,
necessary to precede them with ``opts=`` (as in the CLI examples
below) to avoid causing errors with Salt's own argument parsing.
git_opts
Any additional options to add to git command itself (not the ``push``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -2996,7 +3125,8 @@ def push(cwd,
salt.utils.invalid_kwargs(kwargs)
cwd = _expand_path(cwd, user)
command = ['git', 'push']
command = ['git'] + _format_opts(git_opts)
command.append('push')
command.extend(_format_opts(opts))
if not isinstance(remote, six.string_types):
remote = str(remote)
@ -3015,6 +3145,7 @@ def push(cwd,
def rebase(cwd,
rev='master',
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False):
@ -3035,6 +3166,13 @@ def rebase(cwd,
necessary to precede them with ``opts=`` (as in the CLI examples
below) to avoid causing errors with Salt's own argument parsing.
git_opts
Any additional options to add to git command itself (not the ``rebase``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -3066,7 +3204,8 @@ def rebase(cwd,
opts = _format_opts(opts)
if any(x for x in opts if x in ('-i', '--interactive')):
raise SaltInvocationError('Interactive rebases are not supported')
command = ['git', 'rebase']
command = ['git'] + _format_opts(git_opts)
command.append('rebase')
command.extend(opts)
if not isinstance(rev, six.string_types):
rev = str(rev)
@ -3455,6 +3594,7 @@ def remotes(cwd,
def reset(cwd,
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False):
@ -3472,6 +3612,13 @@ def reset(cwd,
necessary to precede them with ``opts=`` (as in the CLI examples
below) to avoid causing errors with Salt's own argument parsing.
git_opts
Any additional options to add to git command itself (not the ``reset``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -3501,7 +3648,8 @@ def reset(cwd,
salt myminion git.reset /path/to/repo opts='--hard origin/master'
'''
cwd = _expand_path(cwd, user)
command = ['git', 'reset']
command = ['git'] + _format_opts(git_opts)
command.append('reset')
command.extend(_format_opts(opts))
return _git_run(command,
cwd=cwd,
@ -3513,6 +3661,7 @@ def reset(cwd,
def rev_parse(cwd,
rev=None,
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False):
@ -3534,6 +3683,14 @@ def rev_parse(cwd,
opts
Any additional options to add to the command line, in a single string
git_opts
Any additional options to add to git command itself (not the
``rev-parse`` subcommand), in a single string. This is useful for
passing ``-c`` to run git with temporary changes to the git
configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -3569,7 +3726,8 @@ def rev_parse(cwd,
salt myminion git.rev_parse /path/to/repo opts='--is-bare-repository'
'''
cwd = _expand_path(cwd, user)
command = ['git', 'rev-parse']
command = ['git'] + _format_opts(git_opts)
command.append('rev-parse')
command.extend(_format_opts(opts))
if rev is not None:
if not isinstance(rev, six.string_types):
@ -3639,6 +3797,7 @@ def revision(cwd,
def rm_(cwd,
filename,
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False):
@ -3663,6 +3822,13 @@ def rm_(cwd,
necessary to precede them with ``opts=`` (as in the CLI examples
below) to avoid causing errors with Salt's own argument parsing.
git_opts
Any additional options to add to git command itself (not the ``rm``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -3691,7 +3857,8 @@ def rm_(cwd,
salt myminion git.rm /path/to/repo foo/baz opts='-r'
'''
cwd = _expand_path(cwd, user)
command = ['git', 'rm']
command = ['git'] + _format_opts(git_opts)
command.append('rm')
command.extend(_format_opts(opts))
command.extend(['--', filename])
return _git_run(command,
@ -3704,6 +3871,7 @@ def rm_(cwd,
def stash(cwd,
action='save',
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False):
@ -3720,6 +3888,13 @@ def stash(cwd,
``'show'``, etc.). Omitting this argument will simply run ``git
stash``.
git_opts
Any additional options to add to git command itself (not the ``stash``
subcommand), in a single string. This is useful for passing ``-c`` to
run git with temporary changes to the git configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -3753,7 +3928,8 @@ def stash(cwd,
# No numeric actions but this will prevent a traceback when the git
# command is run.
action = str(action)
command = ['git', 'stash', action]
command = ['git'] + _format_opts(git_opts)
command.extend(['stash', action])
command.extend(_format_opts(opts))
return _git_run(command,
cwd=cwd,
@ -3824,6 +4000,7 @@ def status(cwd,
def submodule(cwd,
command,
opts='',
git_opts='',
user=None,
password=None,
identity=None,
@ -3858,6 +4035,14 @@ def submodule(cwd,
necessary to precede them with ``opts=`` (as in the CLI examples
below) to avoid causing errors with Salt's own argument parsing.
git_opts
Any additional options to add to git command itself (not the
``submodule`` subcommand), in a single string. This is useful for
passing ``-c`` to run git with temporary changes to the git
configuration.
.. versionadded:: Nitrogen
init : False
If ``True``, ensures that new submodules are initialized
@ -3943,7 +4128,8 @@ def submodule(cwd,
)
if not isinstance(command, six.string_types):
command = str(command)
cmd = ['git', 'submodule', command]
cmd = ['git'] + _format_opts(git_opts)
cmd.extend(['submodule', command])
cmd.extend(_format_opts(opts))
return _git_run(cmd,
cwd=cwd,
@ -3958,6 +4144,7 @@ def symbolic_ref(cwd,
ref,
value=None,
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False):
@ -3983,6 +4170,14 @@ def symbolic_ref(cwd,
opts
Any additional options to add to the command line, in a single string
git_opts
Any additional options to add to git command itself (not the
``symbolic-refs`` subcommand), in a single string. This is useful for
passing ``-c`` to run git with temporary changes to the git
configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -4014,7 +4209,8 @@ def symbolic_ref(cwd,
salt myminion git.symbolic_ref /path/to/repo FOO opts='--delete'
'''
cwd = _expand_path(cwd, user)
command = ['git', 'symbolic-ref']
command = ['git'] + _format_opts(git_opts)
command.append('symbolic-ref')
opts = _format_opts(opts)
if value is not None and any(x in opts for x in ('-d', '--delete')):
raise SaltInvocationError(
@ -4089,6 +4285,7 @@ def worktree_add(cwd,
force=None,
detach=False,
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False,
@ -4141,6 +4338,14 @@ def worktree_add(cwd,
argument is unnecessary unless new CLI arguments are added to
`git-worktree(1)`_ and are not yet supported in Salt.
git_opts
Any additional options to add to git command itself (not the
``worktree`` subcommand), in a single string. This is useful for
passing ``-c`` to run git with temporary changes to the git
configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -4179,7 +4384,8 @@ def worktree_add(cwd,
'Only one of \'branch\' and \'detach\' is allowed'
)
command = ['git', 'worktree', 'add']
command = ['git'] + _format_opts(git_opts)
command.extend(['worktree', 'add'])
if detach:
if force:
log.warning(
@ -4215,6 +4421,7 @@ def worktree_prune(cwd,
verbose=True,
expire=None,
opts='',
git_opts='',
user=None,
password=None,
ignore_retcode=False):
@ -4253,6 +4460,14 @@ def worktree_prune(cwd,
argument is unnecessary unless new CLI arguments are added to
`git-worktree(1)`_ and are not yet supported in Salt.
git_opts
Any additional options to add to git command itself (not the
``worktree`` subcommand), in a single string. This is useful for
passing ``-c`` to run git with temporary changes to the git
configuration.
.. versionadded:: Nitrogen
user
User under which to run the git command. By default, the command is run
by the user under which the minion is running.
@ -4283,7 +4498,8 @@ def worktree_prune(cwd,
'''
_check_worktree_support()
cwd = _expand_path(cwd, user)
command = ['git', 'worktree', 'prune']
command = ['git'] + _format_opts(git_opts)
command.append(['worktree', 'prune'])
if dry_run:
command.append('--dry-run')
if verbose:

View File

@ -10,7 +10,6 @@ import os
import shutil
import socket
import string
import subprocess
import tempfile
# Import Salt Testing libs
@ -224,8 +223,8 @@ class GitTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
try:
# Mirror the repo
self.run_function('git.clone',
[mirror_dir, repo_url, None, '--mirror'])
self.run_function(
'git.clone', [mirror_dir], url=repo_url, opts='--mirror')
# Make sure the directory for the mirror now exists
self.assertTrue(os.path.exists(mirror_dir))
@ -242,7 +241,11 @@ class GitTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
head_pre = _head(admin_dir)
with open(os.path.join(admin_dir, 'LICENSE'), 'a') as fp_:
fp_.write('Hello world!')
self.run_function('git.commit', [admin_dir, 'Added a line', '-a'])
self.run_function(
'git.commit', [admin_dir, 'added a line'],
git_opts='-c user.name="Foo Bar" -c user.email=foo@bar.com',
opts='-a',
)
# Make sure HEAD is pointing to a new SHA so we know we properly
# committed our change.
head_post = _head(admin_dir)
@ -270,7 +273,6 @@ class GitTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
is the rev used for the git.latest state.
'''
name = os.path.join(integration.TMP, 'salt_repo')
cwd = os.getcwd()
try:
# Clone repo
ret = self.run_state(
@ -283,17 +285,14 @@ class GitTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
# Check out a new branch in the clone and make a commit, to ensure
# that when we re-run the state, it is not a fast-forward change
os.chdir(name)
with salt.utils.fopen(os.devnull, 'w') as devnull:
subprocess.check_call(['git', 'checkout', '-b', 'new_branch'],
stdout=devnull, stderr=devnull)
with salt.utils.fopen('foo', 'w'):
pass
subprocess.check_call(['git', 'add', '.'],
stdout=devnull, stderr=devnull)
subprocess.check_call(['git', 'commit', '-m', 'add file'],
stdout=devnull, stderr=devnull)
os.chdir(cwd)
self.run_function('git.checkout', [name, 'new_branch'], opts='-b')
with salt.utils.fopen(os.path.join(name, 'foo'), 'w'):
pass
self.run_function('git.add', [name, '.'])
self.run_function(
'git.commit', [name, 'add file'],
git_opts='-c user.name="Foo Bar" -c user.email=foo@bar.com',
)
# Re-run the state, this should fail with a specific hint in the
# comment field.
@ -308,9 +307,6 @@ class GitTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
comment = ret[next(iter(ret))]['comment']
self.assertTrue(hint in comment)
finally:
# Make sure that we change back to the original cwd even if there
# was a traceback in the test.
os.chdir(cwd)
shutil.rmtree(name, ignore_errors=True)
def test_latest_changed_local_branch_rev_head(self):
@ -342,33 +338,21 @@ class GitTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
'''
Ensure that we don't exit early when checking for a fast-forward
'''
orig_cwd = os.getcwd()
name = tempfile.mkdtemp(dir=integration.TMP)
target = os.path.join(integration.TMP, 'test_latest_updated_remote_rev')
# Initialize a new git repository
subprocess.check_call(['git', 'init', '--quiet', name])
self.run_function('git.init', [name])
try:
os.chdir(name)
# Set user.name and user.email config attributes if not present
for key, value in (('user.name', 'Jenkins'),
('user.email', 'qa@saltstack.com')):
# Check if key is missing
keycheck = subprocess.Popen(
['git', 'config', '--get', key],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if keycheck.wait() != 0:
# Set the key if it is not present
subprocess.check_call(
['git', 'config', key, value])
# Add and commit a file
with salt.utils.fopen('foo.txt', 'w') as fp_:
with salt.utils.fopen(os.path.join(name, 'foo.txt'), 'w') as fp_:
fp_.write('Hello world\n')
subprocess.check_call(['git', 'add', '.'])
subprocess.check_call(['git', 'commit', '-qm', 'init'])
self.run_function('git.add', [name, '.'])
self.run_function(
'git.commit', [name, 'initial commit'],
git_opts='-c user.name="Foo Bar" -c user.email=foo@bar.com',
)
# Run the state to clone the repo we just created
ret = self.run_state(
@ -381,7 +365,11 @@ class GitTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
# Add another commit
with salt.utils.fopen('foo.txt', 'w') as fp_:
fp_.write('Added a line\n')
subprocess.check_call(['git', 'commit', '-aqm', 'added a line'])
self.run_function(
'git.commit', [name, 'added a line'],
git_opts='-c user.name="Foo Bar" -c user.email=foo@bar.com',
opts='-a',
)
# Run the state again. It should pass, if it doesn't then there was
# a problem checking whether or not the change is a fast-forward.
@ -392,7 +380,6 @@ class GitTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
)
self.assertSaltTrueReturn(ret)
finally:
os.chdir(orig_cwd)
for path in (name, target):
try:
shutil.rmtree(path)
@ -463,7 +450,7 @@ class GitTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
'''
name = tempfile.mkdtemp(dir=integration.TMP)
self.addCleanup(shutil.rmtree, name, ignore_errors=True)
subprocess.check_call(['git', 'init', '--quiet', name])
self.run_function('git.init', [name])
ret = self.run_state(
'git.config_set',
@ -484,35 +471,25 @@ class LocalRepoGitTest(integration.ModuleCase, integration.SaltReturnAssertsMixI
Test the case where the remote branch has been removed
https://github.com/saltstack/salt/issues/36242
'''
cwd = os.getcwd()
repo = tempfile.mkdtemp(dir=integration.TMP)
admin = tempfile.mkdtemp(dir=integration.TMP)
name = tempfile.mkdtemp(dir=integration.TMP)
for dirname in (repo, admin, name):
self.addCleanup(shutil.rmtree, dirname, ignore_errors=True)
self.addCleanup(os.chdir, cwd)
with salt.utils.fopen(os.devnull, 'w') as devnull:
# Create bare repo
subprocess.check_call(['git', 'init', '--bare', repo],
stdout=devnull, stderr=devnull)
# Clone bare repo
subprocess.check_call(['git', 'clone', repo, admin],
stdout=devnull, stderr=devnull)
# Create, add, commit, and push file
os.chdir(admin)
with salt.utils.fopen('foo', 'w'):
pass
subprocess.check_call(['git', 'add', '.'],
stdout=devnull, stderr=devnull)
subprocess.check_call(['git', 'commit', '-m', 'init'],
stdout=devnull, stderr=devnull)
subprocess.check_call(['git', 'push', 'origin', 'master'],
stdout=devnull, stderr=devnull)
# Change back to the original cwd
os.chdir(cwd)
# Create bare repo
self.run_function('git.init', [repo], bare=True)
# Clone bare repo
self.run_function('git.clone', [admin], url=repo)
# Create, add, commit, and push file
with salt.utils.fopen(os.path.join(admin, 'foo'), 'w'):
pass
self.run_function('git.add', [admin, '.'])
self.run_function(
'git.commit', [admin, 'initial commit'],
git_opts='-c user.name="Foo Bar" -c user.email=foo@bar.com',
)
self.run_function('git.push', [admin], remote='origin', ref='master')
# Rename remote 'master' branch to 'develop'
os.rename(