Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion httpie/output/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def write_message(
'flush': env.stdout_isatty or processing_options.stream
}
try:
if env.is_windows and 'colors' in processing_options.get_prettify(env):
if env.is_windows and env.stdout_isatty and 'colors' in processing_options.get_prettify(env):
write_stream_with_colors_win(**write_stream_kwargs)
else:
write_stream(**write_stream_kwargs)
Comment on lines +49 to 52
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a regression test for this Windows-specific branch selection: when env.is_windows is true, colors are requested (e.g. --pretty=all), and env.stdout_isatty is false (piped/redirected), we should use the raw write_stream() path (and not write_stream_with_colors_win()) so ANSI escape codes are preserved. Without a test, this behavior is easy to accidentally regress and reintroduce the color-stripping issue reported in #799.

Copilot uses AI. Check for mistakes.
Expand Down
17 changes: 17 additions & 0 deletions tests/test_windows.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest import mock

import pytest
from httpie.context import Environment

Expand All @@ -23,3 +25,18 @@ def test_output_file_pretty_not_allowed_on_windows(self, tmp_path, httpbin):
'--pretty=all', 'GET', httpbin + '/get',
env=env, tolerate_error_exit_status=True)
assert 'Only terminal output can be colorized on Windows' in r.stderr

def test_windows_piped_pretty_uses_raw_stream(self, httpbin):
"""When stdout is piped on Windows, write_stream() should be used
instead of write_stream_with_colors_win() so ANSI codes pass
through to pagers like `less -r`. Regression test for #799."""
env = MockEnvironment(
is_windows=True,
stdout_isatty=False,
colors=256,
)
with mock.patch('httpie.output.writer.write_stream') as write_stream, \
mock.patch('httpie.output.writer.write_stream_with_colors_win') as write_colors_win:
http('--pretty=all', 'GET', httpbin + '/get', env=env)
write_stream.assert_called()
write_colors_win.assert_not_called()
Loading