Merge pull request #530 from alex-hutton/output-to-file-499

Adds support for 'log_file' in hook config
This commit is contained in:
Anthony Sottile
2017-05-08 10:51:57 -07:00
committed by GitHub
6 changed files with 59 additions and 6 deletions

View File

@@ -53,6 +53,7 @@ MANIFEST_HOOK_DICT = schema.Map(
'^$',
),
schema.Optional('language_version', schema.check_string, 'default'),
schema.OptionalNoDefault('log_file', schema.check_string),
schema.Optional('minimum_pre_commit_version', schema.check_string, '0'),
schema.Optional('stages', schema.check_array(schema.check_string), []),
)

View File

@@ -121,7 +121,10 @@ def _run_single_hook(hook, repo, args, skips, cols):
for out in (stdout, stderr):
assert type(out) is bytes, type(out)
if out.strip():
output.write_line(out.strip())
output.write_line(
out.strip(),
logfile_name=hook.get('log_file'),
)
output.write_line()
return retcode

View File

@@ -71,8 +71,17 @@ def write(s, stream=stdout_byte_stream):
stream.flush()
def write_line(s=None, stream=stdout_byte_stream):
if s is not None:
stream.write(five.to_bytes(s))
stream.write(b'\n')
stream.flush()
def write_line(s=None, stream=stdout_byte_stream, logfile_name=None):
def output_streams():
yield stream
try:
with open(logfile_name, 'ab') as logfile:
yield logfile
except (TypeError, IOError):
pass
for output_stream in output_streams():
if s is not None:
output_stream.write(five.to_bytes(s))
output_stream.write(b'\n')
output_stream.flush()

View File

@@ -0,0 +1,6 @@
- id: logfile test hook
name: Logfile test hook
entry: bin/hook.sh
language: script
files: .
log_file: test.log

View File

@@ -0,0 +1,5 @@
#!/usr/bin/env bash
echo "This is STDOUT output"
echo "This is STDERR output" 1>&2
exit 1

View File

@@ -211,6 +211,35 @@ def test_run(
)
def test_run_output_logfile(
cap_out,
tempdir_factory,
mock_out_store_directory,
):
expected_output = (
b'This is STDOUT output\n',
b'This is STDERR output\n',
)
git_path = make_consuming_repo(tempdir_factory, 'logfile_repo')
with cwd(git_path):
_test_run(
cap_out,
git_path, {},
expected_output,
expected_ret=1,
stage=True
)
logfile_path = os.path.join(git_path, 'test.log')
assert os.path.exists(logfile_path)
with open(logfile_path, 'rb') as logfile:
logfile_content = logfile.readlines()
for expected_output_part in expected_output:
assert expected_output_part in logfile_content
def test_always_run(
cap_out, repo_with_passing_hook, mock_out_store_directory,
):