Repository: pre-commit/pre-commit

Issue:
Docker language fails on Windows - os.getuid()
[`docker_cmd`](https://github.com/pre-commit/pre-commit/blob/0cf2638b3b6a2cfde89cb7013e61b2d4a3e12875/pre_commit/languages/docker.py#L80) in languages/docker.py uses `os.getuid()` and `os.getgid()` that don't exist in Python on Windows.

A hook that uses Docker on Windows fails to execute with

```
An unexpected error has occurred: AttributeError: module 'os' has no attribute 'getuid'
Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\pre_commit\error_handler.py", line 46, in error_handler
    yield
  File "C:\Python36\lib\site-packages\pre_commit\main.py", line 294, in main
    return run(args.config, store, args)
  File "C:\Python36\lib\site-packages\pre_commit\commands\run.py", line 293, in run
    return _run_hooks(config, hooks, args, environ)
  File "C:\Python36\lib\site-packages\pre_commit\commands\run.py", line 214, in _run_hooks
    retval |= _run_single_hook(classifier, hook, args, skips, cols)
  File "C:\Python36\lib\site-packages\pre_commit\commands\run.py", line 124, in _run_single_hook
    tuple(filenames) if hook.pass_filenames else (),
  File "C:\Python36\lib\site-packages\pre_commit\repository.py", line 100, in run
    return lang.run_hook(self, file_args)
  File "C:\Python36\lib\site-packages\pre_commit\languages\docker.py", line 99, in run_hook
    cmd = docker_cmd() + entry_tag + cmd_rest
  File "C:\Python36\lib\site-packages\pre_commit\languages\docker.py", line 80, in docker_cmd
    '-u', '{}:{}'.format(os.getuid(), os.getgid()),
AttributeError: module 'os' has no attribute 'getuid'
```

The same hook works fine in Windows Subsystem for Linux.

Discussion:

Patch:
diff --git a/pre_commit/color.py b/pre_commit/color.py
index 2ede410..c785e2c 100644
--- a/pre_commit/color.py
+++ b/pre_commit/color.py
@@ -48,9 +48,6 @@ def use_color(setting):
     if setting not in COLOR_CHOICES:
         raise InvalidColorSetting(setting)
 
-    if os.environ.get('NO_COLOR'):
-        return False
-
     return (
         setting == 'always' or
         (setting == 'auto' and sys.stdout.isatty() and terminal_supports_color)
diff --git a/pre_commit/languages/docker.py b/pre_commit/languages/docker.py
index 59a53b4..4517050 100644
--- a/pre_commit/languages/docker.py
+++ b/pre_commit/languages/docker.py
@@ -73,11 +73,18 @@ def install_environment(
         os.mkdir(directory)
 
 
+def get_docker_user():  # pragma: windows no cover
+    try:
+        return '{}:{}'.format(os.getuid(), os.getgid())
+    except AttributeError:
+        return '1000:1000'
+
+
 def docker_cmd():  # pragma: windows no cover
     return (
         'docker', 'run',
         '--rm',
-        '-u', '{}:{}'.format(os.getuid(), os.getgid()),
+        '-u', get_docker_user(),
         # https://docs.docker.com/engine/reference/commandline/run/#mount-volumes-from-container-volumes-from
         # The `Z` option tells Docker to label the content with a private
         # unshared label. Only the current container can use a private volume.


Test patch:
diff --git a/tests/color_test.py b/tests/color_test.py
index 4ba3f32..6e11765 100644
--- a/tests/color_test.py
+++ b/tests/color_test.py
@@ -1,6 +1,5 @@
 from __future__ import unicode_literals
 
-import os
 import sys
 
 import mock
@@ -51,18 +50,3 @@ def test_use_color_tty_without_color_support():
 def test_use_color_raises_if_given_shenanigans():
     with pytest.raises(InvalidColorSetting):
         use_color('herpaderp')
-
-
-def test_no_color_env_unset():
-    with mock.patch.dict(os.environ, clear=True):
-        assert use_color('always') is True
-
-
-def test_no_color_env_empty():
-    with mock.patch.dict(os.environ, NO_COLOR=''):
-        assert use_color('always') is True
-
-
-def test_no_color_env_non_empty():
-    with mock.patch.dict(os.environ, NO_COLOR=' '):
-        assert use_color('always') is False
diff --git a/tests/languages/docker_test.py b/tests/languages/docker_test.py
index 9f7f55c..1a96e69 100644
--- a/tests/languages/docker_test.py
+++ b/tests/languages/docker_test.py
@@ -13,3 +13,14 @@ def test_docker_is_running_process_error():
         side_effect=CalledProcessError(*(None,) * 4),
     ):
         assert docker.docker_is_running() is False
+
+
+def test_docker_fallback_user():
+    def invalid_attribute():
+        raise AttributeError
+    with mock.patch.multiple(
+        'os', create=True,
+        getuid=invalid_attribute,
+        getgid=invalid_attribute,
+    ):
+        assert docker.get_docker_user() == '1000:1000'