mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 00:04:42 +04:00
Add --no-stash option.
This commit is contained in:
parent
5386b0eea0
commit
8a8b2241a6
4 changed files with 54 additions and 11 deletions
|
|
@ -21,6 +21,7 @@ from pre_commit.jsonschema_extensions import remove_defaults
|
|||
from pre_commit.logging_handler import LoggingHandler
|
||||
from pre_commit.repository import Repository
|
||||
from pre_commit.staged_files_only import staged_files_only
|
||||
from pre_commit.util import noop_context
|
||||
|
||||
|
||||
logger = logging.getLogger('pre_commit')
|
||||
|
|
@ -233,7 +234,12 @@ def run(runner, args, write=sys.stdout.write):
|
|||
logger.addHandler(LoggingHandler(args.color, write=write))
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
with staged_files_only(runner.cmd_runner):
|
||||
if args.no_stash:
|
||||
ctx = noop_context()
|
||||
else:
|
||||
ctx = staged_files_only(runner.cmd_runner)
|
||||
|
||||
with ctx:
|
||||
if args.hook:
|
||||
return _run_hook(runner, args.hook, args, write=write)
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -31,6 +31,10 @@ def run(argv):
|
|||
'--color', default='auto', type=color.use_color,
|
||||
help='Whether to use color in output. Defaults to `auto`',
|
||||
)
|
||||
run_parser.add_argument(
|
||||
'--no-stash', default=False, action='store_true',
|
||||
help='Use this option to prevent auto stashing of unstaged files.',
|
||||
)
|
||||
run_parser.add_argument('--verbose', '-v', action='store_true', default=False)
|
||||
|
||||
help = subparsers.add_parser('help', help='Show help for a specific command.')
|
||||
|
|
|
|||
|
|
@ -62,3 +62,9 @@ def clean_path_on_failure(path):
|
|||
if os.path.exists(path):
|
||||
shutil.rmtree(path)
|
||||
raise
|
||||
|
||||
|
||||
# TODO: asottile.contextlib this with a forward port of nested
|
||||
@contextlib.contextmanager
|
||||
def noop_context():
|
||||
yield
|
||||
|
|
|
|||
|
|
@ -192,20 +192,30 @@ def get_write_mock_output(write_mock):
|
|||
return ''.join(call[0][0] for call in write_mock.call_args_list)
|
||||
|
||||
|
||||
def _get_opts(all_files=False, color=False, verbose=False, hook=None, no_stash=False):
|
||||
return auto_namedtuple(
|
||||
all_files=all_files,
|
||||
color=color,
|
||||
verbose=verbose,
|
||||
hook=hook,
|
||||
no_stash=no_stash,
|
||||
)
|
||||
|
||||
|
||||
def _do_run(repo, args):
|
||||
runner = Runner(repo)
|
||||
write_mock = mock.Mock()
|
||||
ret = commands.run(runner, args, write=write_mock)
|
||||
printed = get_write_mock_output(write_mock)
|
||||
return ret, printed
|
||||
|
||||
|
||||
def _test_run(repo, options, expected_outputs, expected_ret, stage):
|
||||
if stage:
|
||||
stage_a_file()
|
||||
runner = Runner(repo)
|
||||
args = auto_namedtuple(
|
||||
**dict(
|
||||
dict(all_files=False, color=False, verbose=False, hook=None),
|
||||
**options
|
||||
)
|
||||
)
|
||||
write_mock = mock.Mock()
|
||||
ret = commands.run(runner, args, write=write_mock)
|
||||
args = _get_opts(**options)
|
||||
ret, printed = _do_run(repo, args)
|
||||
assert ret == expected_ret
|
||||
printed = get_write_mock_output(write_mock)
|
||||
for expected_output_part in expected_outputs:
|
||||
assert expected_output_part in printed
|
||||
|
||||
|
|
@ -240,3 +250,20 @@ def test_run_all_hooks_failing(repo_with_failing_hook):
|
|||
)
|
||||
def test_run(repo_with_passing_hook, options, outputs, expected_ret, stage):
|
||||
_test_run(repo_with_passing_hook, options, outputs, expected_ret, stage)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('no_stash', (True, False))
|
||||
def test_no_stash(repo_with_passing_hook, no_stash):
|
||||
stage_a_file()
|
||||
# Make unstaged changes
|
||||
with open('foo.py', 'w') as foo_file:
|
||||
foo_file.write('import os\n')
|
||||
|
||||
args = _get_opts(no_stash=no_stash)
|
||||
ret, printed = _do_run(repo_with_passing_hook, args)
|
||||
assert ret == 0
|
||||
warning_msg = '[WARNING] Unstaged files detected.'
|
||||
if no_stash:
|
||||
assert warning_msg not in printed
|
||||
else:
|
||||
assert warning_msg in printed
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue