From 59d5bffbec133687284965ee643582e059d9dcea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Fri, 25 Mar 2016 13:25:28 +0100 Subject: [PATCH] Tests: cli: Detect and handle the `-d ''` case --- tests/test_cli.py | 14 +++++++++++++- yamllint/cli.py | 2 +- yamllint/config.py | 3 +++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index a477061..f0a2c94 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -139,7 +139,19 @@ class CommandLineTestCase(unittest.TestCase): self.assertEqual(ctx.exception.code, -1) out, err = sys.stdout.getvalue(), sys.stderr.getvalue() - self.assertRegexpMatches(out + err, r'^invalid config') + self.assertEqual(out, '') + self.assertRegexpMatches(err, r'^invalid config: no such rule') + + def test_run_with_empty_config(self): + sys.stdout, sys.stderr = StringIO(), StringIO() + with self.assertRaises(SystemExit) as ctx: + cli.run(('-d', '', 'file')) + + self.assertEqual(ctx.exception.code, -1) + + out, err = sys.stdout.getvalue(), sys.stderr.getvalue() + self.assertEqual(out, '') + self.assertRegexpMatches(err, r'^invalid config: not a dict') def test_run_version(self): sys.stdout, sys.stderr = StringIO(), StringIO() diff --git a/yamllint/cli.py b/yamllint/cli.py index 3b67011..c308c80 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -88,7 +88,7 @@ def run(argv=None): try: if args.config_data is not None: - if ':' not in args.config_data: + if args.config_data != '' and ':' 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: diff --git a/yamllint/config.py b/yamllint/config.py index d0d1853..48ea380 100644 --- a/yamllint/config.py +++ b/yamllint/config.py @@ -59,6 +59,9 @@ class YamlLintConfig(object): except Exception as e: raise YamlLintConfigError('invalid config: %s' % e) + if type(conf) != dict: + raise YamlLintConfigError('invalid config: not a dict') + self.rules = conf.get('rules', {}) # Does this conf override another conf that we need to load?