Skip to content

Commit e52c1ac

Browse files
author
Johan Bergström
committed
cli: read optional config from YAMLLINT_CONFIG
This is a "minor" abi break since empty configs are disregarded instead of failing. Fixes: adrienverge#107
1 parent e4e99f0 commit e52c1ac

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

tests/test_cli.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,19 @@ def test_run_with_empty_config(self):
165165

166166
out, err = sys.stdout.getvalue(), sys.stderr.getvalue()
167167
self.assertEqual(out, '')
168-
self.assertRegexpMatches(err, r'^invalid config: not a dict')
168+
self.assertRegexpMatches(err, r'No such file or directory')
169+
170+
def test_run_with_environment_variable(self):
171+
sys.stdout, sys.stderr = StringIO(), StringIO()
172+
rules = '{extends: default, rules: {document-start: {present: false}}}'
173+
with self.assertRaises(SystemExit) as ctx:
174+
os.environ['YAMLLINT_CONFIG'] = rules
175+
cli.run((os.path.join(self.wd, 'warn.yaml'), ))
176+
177+
self.assertEqual(ctx.exception.code, 0)
178+
out = sys.stdout.getvalue()
179+
self.assertEqual(out, '')
180+
del os.environ['YAMLLINT_CONFIG']
169181

170182
def test_run_with_config_file(self):
171183
with open(os.path.join(self.wd, 'config'), 'w') as f:

yamllint/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def run(argv=None):
9494
help='path to a custom configuration')
9595
config_group.add_argument('-d', '--config-data', dest='config_data',
9696
action='store',
97+
default=os.environ.get('YAMLLINT_CONFIG', ''),
9798
help='custom configuration (as YAML source)')
9899
parser.add_argument('-f', '--format',
99100
choices=('parsable', 'standard'), default='standard',
@@ -117,8 +118,8 @@ def run(argv=None):
117118
user_global_config = os.path.expanduser('~/.config/yamllint/config')
118119

119120
try:
120-
if args.config_data is not None:
121-
if args.config_data != '' and ':' not in args.config_data:
121+
if args.config_data != '':
122+
if ':' not in args.config_data:
122123
args.config_data = 'extends: ' + args.config_data
123124
conf = YamlLintConfig(content=args.config_data)
124125
elif args.config_file is not None:

0 commit comments

Comments
 (0)