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
9 changes: 9 additions & 0 deletions src/azure-cli-core/azure/cli/core/_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import sys

import knack.output


Expand All @@ -11,6 +13,13 @@ class AzOutputProducer(knack.output.OutputProducer):
def check_valid_format_type(self, format_type):
return format_type in self._FORMAT_DICT

def out(self, obj, formatter=None, out_file=None):
file = out_file or sys.stdout
if get_output_format(self.cli_ctx) == "tsv":
if hasattr(file, "reconfigure"):
file.reconfigure(newline="\n")
return super().out(obj, formatter=formatter, out_file=file)


def get_output_format(cli_ctx):
return cli_ctx.invocation.data.get("output", None)
Expand Down
21 changes: 21 additions & 0 deletions src/azure-cli-core/azure/cli/core/tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# --------------------------------------------------------------------------------------------

import unittest
from io import BytesIO, TextIOWrapper
from types import SimpleNamespace


class TestCoreCLIOutput(unittest.TestCase):
Expand All @@ -16,6 +18,25 @@ def test_create_AzOutputProducer(self):
self.assertIn('yaml', output_producer._FORMAT_DICT)
self.assertIn('none', output_producer._FORMAT_DICT)

def test_tsv_output_uses_lf_newlines(self):
from azure.cli.core._output import AzOutputProducer
from azure.cli.core.mock import DummyCli
from knack.util import CommandResultItem

cli = DummyCli()
cli.invocation = SimpleNamespace(data={'output': 'tsv'})
output_producer = AzOutputProducer(cli)
buffer = BytesIO()
out_file = TextIOWrapper(buffer, encoding='utf-8', newline='\r\n')

output_producer.out(
CommandResultItem(result=['first', 'second']),
formatter=output_producer.get_formatter('tsv'),
out_file=out_file)
out_file.flush()

self.assertEqual(b'first\nsecond\n', buffer.getvalue())

# regression test for https://github.com/Azure/azure-cli/issues/9263
def test_yaml_output_with_ordered_dict(self):
from azure.cli.core._output import AzOutputProducer
Expand Down
Loading