Repository: pallets/click

Problem statement:
click.echo does not force colors on Windows with color=True
Using `click.echo(click.style("Foo", fg="red"), color=True)` on Windows will not print "Foo" in red if the output is not a terminal that supports colors, so it seems that `color=True` is ignored on Windows.

Here is an example code to reproduce the bug:
```
import click


def main():
    test_txt = "click_echo.txt"
    with open(test_txt, "w") as f:
        click.secho("Without setting color", file=f, fg="red")
        click.secho("With color=False", file=f, fg="red", color=False)
        click.secho("With color=True", file=f, fg="red", color=True)

    click.echo(f"Check the contents of {test_txt}")


if __name__ == "__main__":
    main()
```
After running it on Linux the third line of `click_echo.txt` will contain the ANSI color codes but on Windows all three lines will be the same (the behavior is the same if `echo` prints on the console and the script output is redirected to a file)

Environment:

- Python version: 3.9
- Click version: 8.1.7



Hints:
Windows uses a different mechanism for color than Unix. It's not possible to force it on. Or if it is, you'd need to take that up with colorama. 
I've looked into it and it seems that there is a bug in `click` itself: the `color` parameter is not passed when `auto_wrap_for_ansi()` is called on line 317 in `utils.py`:
```
    # ANSI style code support. For no message or bytes, nothing happens.
    # When outputting to a file instead of a terminal, strip codes.
    else:
        color = resolve_color_default(color)

        if should_strip_ansi(file, color):
            out = strip_ansi(out)
        elif WIN:
            if auto_wrap_for_ansi is not None:
                file = auto_wrap_for_ansi(file)  # type: ignore
            elif not color:
                out = strip_ansi(out)
```
If I change it to `file = auto_wrap_for_ansi(file, color)` then forcing colors seems to work.
Thanks for looking. Happy to review a PR.
FWIW, I had to skip color-based tests on Windows in a lot of my Click-based libraries:
- https://github.com/kdeldycke/meta-package-manager/commit/5ecbe5b4557fa338c53e42061d588cd62e7f8067
- https://github.com/kdeldycke/click-extra/commit/43419461ca732869ddf0e37214a41b957dcf7de5

So yes, I am pretty sure there is something wrong with colors on Windows. But I never found a way to reliably fix it and find the root cause.

Even with loads of hacks and workarounds in my [Click Extra](https://github.com/kdeldycke/click-extra) project, like:
- https://github.com/kdeldycke/click-extra/blob/44732144e3f1f4a3da059a084e02c4c3c8f95579/click_extra/commands.py#L96-L124
- https://github.com/kdeldycke/click-extra/blob/44732144e3f1f4a3da059a084e02c4c3c8f95579/click_extra/colorize.py#L258-L269

So yeah, @Tyl13 is probably onto something here with his #2731 PR ! :)

Related issues might include:
- https://github.com/pallets/click/issues/2110
- https://github.com/pallets/click/issues/2111

Patch:
diff --git a/src/click/utils.py b/src/click/utils.py
--- a/src/click/utils.py
+++ b/src/click/utils.py
@@ -311,7 +311,7 @@ def echo(
             out = strip_ansi(out)
         elif WIN:
             if auto_wrap_for_ansi is not None:
-                file = auto_wrap_for_ansi(file)  # type: ignore
+                file = auto_wrap_for_ansi(file, color)  # type: ignore
             elif not color:
                 out = strip_ansi(out)
 


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -36,9 +36,7 @@ def cli():
 
 
 def test_echo_custom_file():
-    import io
-
-    f = io.StringIO()
+    f = StringIO()
     click.echo("hello", file=f)
     assert f.getvalue() == "hello\n"
 
@@ -209,7 +207,6 @@ def test_echo_via_pager(monkeypatch, capfd, cat, test):
     assert out == expected_output
 
 
-@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")
 def test_echo_color_flag(monkeypatch, capfd):
     isatty = True
     monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)
@@ -232,9 +229,16 @@ def test_echo_color_flag(monkeypatch, capfd):
     assert out == f"{styled_text}\n"
 
     isatty = False
-    click.echo(styled_text)
-    out, err = capfd.readouterr()
-    assert out == f"{text}\n"
+    # Faking isatty() is not enough on Windows;
+    # the implementation caches the colorama wrapped stream
+    # so we have to use a new stream for each test
+    stream = StringIO()
+    click.echo(styled_text, file=stream)
+    assert stream.getvalue() == f"{text}\n"
+
+    stream = StringIO()
+    click.echo(styled_text, file=stream, color=True)
+    assert stream.getvalue() == f"{styled_text}\n"
 
 
 def test_prompt_cast_default(capfd, monkeypatch):