Repository: pre-commit/pre-commit
Pull request #535 (2017-05-08 18:08:34)

Fixup log_file commit


Patch:
diff --git a/.coveragerc b/.coveragerc
index a94ba5c..958d944 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -4,7 +4,6 @@ source = .
 omit =
     .tox/*
     /usr/*
-    */tmp*
     setup.py
     # Don't complain if non-runnable code isn't run
     */__main__.py
diff --git a/pre_commit/clientlib.py b/pre_commit/clientlib.py
index bda5bfe..d386dcd 100644
--- a/pre_commit/clientlib.py
+++ b/pre_commit/clientlib.py
@@ -53,7 +53,7 @@ MANIFEST_HOOK_DICT = schema.Map(
         '^$',
     ),
     schema.Optional('language_version', schema.check_string, 'default'),
-    schema.OptionalNoDefault('log_file', schema.check_string),
+    schema.Optional('log_file', schema.check_string, ''),
     schema.Optional('minimum_pre_commit_version', schema.check_string, '0'),
     schema.Optional('stages', schema.check_array(schema.check_string), []),
 )
diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py
index 33ea41d..40917e1 100644
--- a/pre_commit/commands/run.py
+++ b/pre_commit/commands/run.py
@@ -121,10 +121,7 @@ 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(),
-                    logfile_name=hook.get('log_file'),
-                )
+                output.write_line(out.strip(), logfile_name=hook['log_file'])
         output.write_line()
 
     return retcode
diff --git a/pre_commit/output.py b/pre_commit/output.py
index 1fe6d51..3659609 100644
--- a/pre_commit/output.py
+++ b/pre_commit/output.py
@@ -4,6 +4,7 @@ import sys
 
 from pre_commit import color
 from pre_commit import five
+from pre_commit.util import noop_context
 
 
 def get_hook_message(
@@ -72,16 +73,16 @@ def write(s, stream=stdout_byte_stream):
 
 
 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()
+    output_streams = [stream]
+    if logfile_name:
+        ctx = open(logfile_name, 'ab')
+        output_streams.append(ctx)
+    else:
+        ctx = noop_context()
+
+    with ctx:
+        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()


Test patch:
diff --git a/tests/manifest_test.py b/tests/manifest_test.py
index 658210d..1296b21 100644
--- a/tests/manifest_test.py
+++ b/tests/manifest_test.py
@@ -29,6 +29,7 @@ def test_manifest_contents(manifest):
         'id': 'bash_hook',
         'language': 'script',
         'language_version': 'default',
+        'log_file': '',
         'minimum_pre_commit_version': '0',
         'name': 'Bash hook',
         'stages': [],
@@ -47,6 +48,7 @@ def test_hooks(manifest):
         'id': 'bash_hook',
         'language': 'script',
         'language_version': 'default',
+        'log_file': '',
         'minimum_pre_commit_version': '0',
         'name': 'Bash hook',
         'stages': [],