From 8fa9eb3cede0b2f36bdf5c34a8912085244ed74b Mon Sep 17 00:00:00 2001 From: ffapitalle Date: Thu, 12 Dec 2019 05:12:53 -0300 Subject: [PATCH] Add --no-warnings option to suppress warning messages Use `--no-warnings` option to hide warning messages. It only shows problems marked as errors. --- docs/configuration.rst | 3 +++ tests/test_cli.py | 35 +++++++++++++++++++++++++++++++++++ yamllint/cli.py | 15 +++++++++++---- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 866208e..0322ad3 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -116,6 +116,9 @@ return code will be: * ``1`` if one or more errors occur * ``2`` if no errors occur, but one or more warnings occur +If the script is invoked with the ``--no-warnings`` option, it won't output +warning level problems, only error level ones. + YAML files extensions --------------------- diff --git a/tests/test_cli.py b/tests/test_cli.py index fbdf75f..014cf8f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -532,3 +532,38 @@ class CommandLineTestCase(unittest.TestCase): 'stdin:2:10: [error] syntax error: ' 'mapping values are not allowed here\n')) self.assertEqual(err, '') + + def test_run_no_warnings(self): + file = os.path.join(self.wd, 'a.yaml') + + sys.stdout, sys.stderr = StringIO(), StringIO() + with self.assertRaises(SystemExit) as ctx: + cli.run((file, '--no-warnings', '-f', '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, '') + + file = os.path.join(self.wd, 'warn.yaml') + + sys.stdout, sys.stderr = StringIO(), StringIO() + with self.assertRaises(SystemExit) as ctx: + cli.run((file, '--no-warnings', '-f', 'auto')) + + self.assertEqual(ctx.exception.code, 0) + + def test_run_no_warnings_and_strict(self): + file = os.path.join(self.wd, 'warn.yaml') + + sys.stdout, sys.stderr = StringIO(), StringIO() + with self.assertRaises(SystemExit) as ctx: + cli.run((file, '--no-warnings', '-s')) + + self.assertEqual(ctx.exception.code, 2) diff --git a/yamllint/cli.py b/yamllint/cli.py index 0ffaae4..26bdb1f 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -84,11 +84,14 @@ class Format(object): return line -def show_problems(problems, file, args_format): +def show_problems(problems, file, args_format, no_warn): max_level = 0 first = True for problem in problems: + max_level = max(max_level, PROBLEM_LEVELS[problem.level]) + if no_warn and (problem.level != 'error'): + continue if args_format == 'parsable': print(Format.parsable(problem, file)) elif args_format == 'colored' or \ @@ -102,7 +105,6 @@ def show_problems(problems, file, args_format): print(file) first = False print(Format.standard(problem, file)) - max_level = max(max_level, PROBLEM_LEVELS[problem.level]) if not first and args_format != 'parsable': print('') @@ -133,6 +135,9 @@ def run(argv=None): action='store_true', help='return non-zero exit code on warnings ' 'as well as errors') + parser.add_argument('--no-warnings', + action='store_true', + help='output only error level problems') parser.add_argument('-v', '--version', action='version', version='{} {}'.format(APP_NAME, APP_VERSION)) @@ -176,7 +181,8 @@ def run(argv=None): except EnvironmentError as e: print(e, file=sys.stderr) sys.exit(-1) - prob_level = show_problems(problems, file, args_format=args.format) + prob_level = show_problems(problems, file, args_format=args.format, + no_warn=args.no_warnings) max_level = max(max_level, prob_level) # read yaml from stdin @@ -186,7 +192,8 @@ def run(argv=None): except EnvironmentError as e: print(e, file=sys.stderr) sys.exit(-1) - prob_level = show_problems(problems, 'stdin', args_format=args.format) + prob_level = show_problems(problems, 'stdin', args_format=args.format, + no_warn=args.no_warnings) max_level = max(max_level, prob_level) if max_level == PROBLEM_LEVELS['error']: