From e52c1acd2a40b43d82b0abb5b99a081ec031fd94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Bergstr=C3=B6m?= Date: Thu, 28 Jun 2018 11:10:16 -0400 Subject: [PATCH] cli: read optional config from YAMLLINT_CONFIG This is a "minor" abi break since empty configs are disregarded instead of failing. Fixes: https://github.com/adrienverge/yamllint/issues/107 --- tests/test_cli.py | 14 +++++++++++++- yamllint/cli.py | 5 +++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 341e3fc2..fc7b96d3 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -165,7 +165,19 @@ def test_run_with_empty_config(self): out, err = sys.stdout.getvalue(), sys.stderr.getvalue() self.assertEqual(out, '') - self.assertRegexpMatches(err, r'^invalid config: not a dict') + self.assertRegexpMatches(err, r'No such file or directory') + + def test_run_with_environment_variable(self): + sys.stdout, sys.stderr = StringIO(), StringIO() + rules = '{extends: default, rules: {document-start: {present: false}}}' + with self.assertRaises(SystemExit) as ctx: + os.environ['YAMLLINT_CONFIG'] = rules + cli.run((os.path.join(self.wd, 'warn.yaml'), )) + + self.assertEqual(ctx.exception.code, 0) + out = sys.stdout.getvalue() + self.assertEqual(out, '') + del os.environ['YAMLLINT_CONFIG'] def test_run_with_config_file(self): with open(os.path.join(self.wd, 'config'), 'w') as f: diff --git a/yamllint/cli.py b/yamllint/cli.py index 0faee16c..20ce0c6e 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -94,6 +94,7 @@ def run(argv=None): help='path to a custom configuration') config_group.add_argument('-d', '--config-data', dest='config_data', action='store', + default=os.environ.get('YAMLLINT_CONFIG', ''), help='custom configuration (as YAML source)') parser.add_argument('-f', '--format', choices=('parsable', 'standard'), default='standard', @@ -117,8 +118,8 @@ def run(argv=None): user_global_config = os.path.expanduser('~/.config/yamllint/config') try: - if args.config_data is not None: - if args.config_data != '' and ':' not in args.config_data: + if args.config_data != '': + if ':' not in args.config_data: args.config_data = 'extends: ' + args.config_data conf = YamlLintConfig(content=args.config_data) elif args.config_file is not None: