Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: read optional config from YAMLLINT_CONFIG #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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