From 5062b91cac1cd3d6bcc277bc8a3fe82a062097d9 Mon Sep 17 00:00:00 2001 From: sedrubal Date: Thu, 18 Oct 2018 17:02:23 +0200 Subject: [PATCH] cli: Add -f colored to force colors `-f standard` shows non-colored output, `-f colored` shows colored output, `-f auto` is the new default, it chooses `standard` or `colored` depending on terminal capabilities. --- tests/test_cli.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++- yamllint/cli.py | 7 +++--- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 341e3fc..6d570f3 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -351,7 +351,7 @@ class CommandLineTestCase(unittest.TestCase): '\n' % file)) self.assertEqual(err, '') - def test_run_colored_output(self): + def test_run_default_format_output_in_tty(self): file = os.path.join(self.wd, 'a.yaml') # Create a pseudo-TTY and redirect stdout to it @@ -383,3 +383,59 @@ class CommandLineTestCase(unittest.TestCase): 'no new line character at the end of file ' '\033[2m(new-line-at-end-of-file)\033[0m\n' '\n' % file)) + + def test_run_default_format_output_without_tty(self): + file = os.path.join(self.wd, 'a.yaml') + + sys.stdout, sys.stderr = StringIO(), StringIO() + with self.assertRaises(SystemExit) as ctx: + cli.run((file, )) + + self.assertEqual(ctx.exception.code, 1) + + out, err = sys.stdout.getvalue(), sys.stderr.getvalue() + self.assertEqual(out, ( + '%s\n' + ' 2:4 error trailing spaces (trailing-spaces)\n' + ' 3:4 error no new line character at the end of file ' + '(new-line-at-end-of-file)\n' + '\n' % file)) + self.assertEqual(err, '') + + def test_run_auto_output_without_tty_output(self): + file = os.path.join(self.wd, 'a.yaml') + + sys.stdout, sys.stderr = StringIO(), StringIO() + with self.assertRaises(SystemExit) as ctx: + cli.run((file, '--format', 'auto')) + + self.assertEqual(ctx.exception.code, 1) + + out, err = sys.stdout.getvalue(), sys.stderr.getvalue() + self.assertEqual(out, ( + '%s\n' + ' 2:4 error trailing spaces (trailing-spaces)\n' + ' 3:4 error no new line character at the end of file ' + '(new-line-at-end-of-file)\n' + '\n' % file)) + self.assertEqual(err, '') + + def test_run_format_colored(self): + file = os.path.join(self.wd, 'a.yaml') + + sys.stdout, sys.stderr = StringIO(), StringIO() + with self.assertRaises(SystemExit) as ctx: + cli.run((file, '--format', 'colored')) + + self.assertEqual(ctx.exception.code, 1) + + out, err = sys.stdout.getvalue(), sys.stderr.getvalue() + self.assertEqual(out, ( + '\033[4m%s\033[0m\n' + ' \033[2m2:4\033[0m \033[31merror\033[0m ' + 'trailing spaces \033[2m(trailing-spaces)\033[0m\n' + ' \033[2m3:4\033[0m \033[31merror\033[0m ' + 'no new line character at the end of file ' + '\033[2m(new-line-at-end-of-file)\033[0m\n' + '\n' % file)) + self.assertEqual(err, '') diff --git a/yamllint/cli.py b/yamllint/cli.py index 0faee16..5cb84fc 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -96,8 +96,8 @@ def run(argv=None): action='store', help='custom configuration (as YAML source)') parser.add_argument('-f', '--format', - choices=('parsable', 'standard'), default='standard', - help='format for parsing output') + choices=('parsable', 'standard', 'colored', 'auto'), + default='auto', help='format for parsing output') parser.add_argument('-s', '--strict', action='store_true', help='return non-zero exit code on warnings ' @@ -143,7 +143,8 @@ def run(argv=None): for problem in linter.run(f, conf, filepath): if args.format == 'parsable': print(Format.parsable(problem, file)) - elif supports_color(): + elif args.format == 'colored' or \ + (args.format == 'auto' and supports_color()): if first: print('\033[4m%s\033[0m' % file) first = False