Skip to content

Commit

Permalink
cli: read optional config from YAMLLINT_CONFIG
Browse files Browse the repository at this point in the history
This is a "minor" abi break since empty configs are
disregarded instead of failing.

Fixes: adrienverge#107
  • Loading branch information
Johan Bergström committed Jun 28, 2018
1 parent e4e99f0 commit e52c1ac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
14 changes: 13 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions yamllint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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:
Expand Down

0 comments on commit e52c1ac

Please sign in to comment.