Skip to content

Commit

Permalink
Add duration to verbose run
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Dec 29, 2019
1 parent 1074b39 commit 0c0427b
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 55 deletions.
8 changes: 8 additions & 0 deletions pre_commit/commands/run.py
Expand Up @@ -4,6 +4,7 @@
import os
import re
import subprocess
import time

from identify.identify import tags_from_path

Expand Down Expand Up @@ -99,6 +100,7 @@ def _run_single_hook(classifier, hook, skips, cols, verbose, use_color):
cols=cols,
),
)
duration = None
retcode = 0
files_modified = False
out = b''
Expand All @@ -113,6 +115,7 @@ def _run_single_hook(classifier, hook, skips, cols, verbose, use_color):
cols=cols,
),
)
duration = None
retcode = 0
files_modified = False
out = b''
Expand All @@ -123,7 +126,9 @@ def _run_single_hook(classifier, hook, skips, cols, verbose, use_color):
diff_cmd = ('git', 'diff', '--no-ext-diff')
diff_before = cmd_output_b(*diff_cmd, retcode=None)
filenames = tuple(filenames) if hook.pass_filenames else ()
time_before = time.time()
retcode, out = hook.run(filenames, use_color)
duration = round(time.time() - time_before, 2) or 0
diff_after = cmd_output_b(*diff_cmd, retcode=None)

# if the hook makes changes, fail the commit
Expand All @@ -141,6 +146,9 @@ def _run_single_hook(classifier, hook, skips, cols, verbose, use_color):
if verbose or hook.verbose or retcode or files_modified:
_subtle_line('- hook id: {}'.format(hook.id), use_color)

if (verbose or hook.verbose) and duration is not None:
_subtle_line('- duration: {}s'.format(duration), use_color)

if retcode:
_subtle_line('- exit code: {}'.format(retcode), use_color)

Expand Down
9 changes: 4 additions & 5 deletions testing/resources/arbitrary_bytes_repo/.pre-commit-hooks.yaml
@@ -1,6 +1,5 @@
- id: python3-hook
name: Python 3 Hook
entry: python3-hook
language: python
language_version: python3
- id: hook
name: hook
entry: ./hook.sh
language: script
files: \.py$
7 changes: 7 additions & 0 deletions testing/resources/arbitrary_bytes_repo/hook.sh
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
# Intentionally write mixed encoding to the output. This should not crash
# pre-commit and should write bytes to the output.
# '☃'.encode('UTF-8') + '²'.encode('latin1')
echo -e '\xe2\x98\x83\xb2'
# exit 1 to trigger printing
exit 1
13 changes: 0 additions & 13 deletions testing/resources/arbitrary_bytes_repo/python3_hook.py

This file was deleted.

8 changes: 0 additions & 8 deletions testing/resources/arbitrary_bytes_repo/setup.py

This file was deleted.

77 changes: 49 additions & 28 deletions tests/commands/run_test.py
Expand Up @@ -5,6 +5,7 @@
import os.path
import pipes
import sys
import time

import mock
import pytest
Expand All @@ -25,6 +26,7 @@
from testing.fixtures import modify_config
from testing.fixtures import read_config
from testing.fixtures import sample_meta_config
from testing.fixtures import write_config
from testing.util import cmd_output_mocked_pre_commit_home
from testing.util import cwd
from testing.util import git_commit
Expand Down Expand Up @@ -163,36 +165,55 @@ def test_exclude_types_hook_repository(cap_out, store, tempdir_factory):
assert b'exe' not in printed


def test_global_exclude(cap_out, store, tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(git_path):
with modify_config() as config:
config['exclude'] = '^foo.py$'
open('foo.py', 'a').close()
open('bar.py', 'a').close()
cmd_output('git', 'add', '.')
opts = run_opts(verbose=True)
ret, printed = _do_run(cap_out, store, git_path, opts)
assert ret == 0
# Does not contain foo.py since it was excluded
expected = b'- hook id: bash_hook\n\nbar.py\nHello World\n\n'
assert printed.endswith(expected)
def test_global_exclude(cap_out, store, in_git_dir):
config = {
'exclude': r'^foo\.py$',
'repos': [{'repo': 'meta', 'hooks': [{'id': 'identity'}]}],
}
write_config('.', config)
open('foo.py', 'a').close()
open('bar.py', 'a').close()
cmd_output('git', 'add', '.')
opts = run_opts(verbose=True)
ret, printed = _do_run(cap_out, store, str(in_git_dir), opts)
assert ret == 0
# Does not contain foo.py since it was excluded
assert printed.startswith(b'identity' + b'.' * 65 + b'Passed\n')
assert printed.endswith(b'\n\n.pre-commit-config.yaml\nbar.py\n\n')


def test_global_files(cap_out, store, tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(git_path):
with modify_config() as config:
config['files'] = '^bar.py$'
open('foo.py', 'a').close()
open('bar.py', 'a').close()
cmd_output('git', 'add', '.')
opts = run_opts(verbose=True)
ret, printed = _do_run(cap_out, store, git_path, opts)
assert ret == 0
# Does not contain foo.py since it was not included
expected = b'- hook id: bash_hook\n\nbar.py\nHello World\n\n'
assert printed.endswith(expected)
def test_global_files(cap_out, store, in_git_dir):
config = {
'files': r'^bar\.py$',
'repos': [{'repo': 'meta', 'hooks': [{'id': 'identity'}]}],
}
write_config('.', config)
open('foo.py', 'a').close()
open('bar.py', 'a').close()
cmd_output('git', 'add', '.')
opts = run_opts(verbose=True)
ret, printed = _do_run(cap_out, store, str(in_git_dir), opts)
assert ret == 0
# Does not contain foo.py since it was excluded
assert printed.startswith(b'identity' + b'.' * 65 + b'Passed\n')
assert printed.endswith(b'\n\nbar.py\n\n')


@pytest.mark.parametrize(
('t1', 't2', 'expected'),
(
(1.234, 2., b'\n- duration: 0.77s\n'),
(1., 1., b'\n- duration: 0s\n'),
),
)
def test_verbose_duration(cap_out, store, in_git_dir, t1, t2, expected):
write_config('.', {'repo': 'meta', 'hooks': [{'id': 'identity'}]})
cmd_output('git', 'add', '.')
opts = run_opts(verbose=True)
with mock.patch.object(time, 'time', side_effect=(t1, t2)):
ret, printed = _do_run(cap_out, store, str(in_git_dir), opts)
assert ret == 0
assert expected in printed


@pytest.mark.parametrize(
Expand Down
7 changes: 6 additions & 1 deletion tests/commands/try_repo_test.py
Expand Up @@ -3,6 +3,9 @@

import os.path
import re
import time

import mock

from pre_commit import git
from pre_commit.commands.try_repo import try_repo
Expand Down Expand Up @@ -40,7 +43,8 @@ def _run_try_repo(tempdir_factory, **kwargs):


def test_try_repo_repo_only(cap_out, tempdir_factory):
_run_try_repo(tempdir_factory, verbose=True)
with mock.patch.object(time, 'time', return_value=0.0):
_run_try_repo(tempdir_factory, verbose=True)
start, config, rest = _get_out(cap_out)
assert start == ''
assert re.match(
Expand All @@ -58,6 +62,7 @@ def test_try_repo_repo_only(cap_out, tempdir_factory):
- hook id: bash_hook
Bash hook................................................................Passed
- hook id: bash_hook2
- duration: 0s
test-file
Expand Down

0 comments on commit 0c0427b

Please sign in to comment.