From 865783869693f2e51f55d5bb9f727a8fd77e6e46 Mon Sep 17 00:00:00 2001 From: Will Badart Date: Mon, 27 Apr 2020 09:21:42 -0700 Subject: [PATCH] Rename env variable, refactor tests Drops the ancient rc-file convention in favor of YAMLLINT_CONFIG_FILE to be more consistent with the rest of the documentation. Uses the `tempfile` module's `NamedTemporaryFile` context manager to handle the creation and cleanup of temp files (`NamedTemporaryFile` has a `delete` option, true by default, which removes the file once it's closed). --- tests/test_cli.py | 28 ++++++++++++++-------------- yamllint/cli.py | 5 +++-- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index fa74d83..ebbc293 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -24,6 +24,7 @@ import os import pty import shutil import sys +import tempfile import unittest from tests.common import build_temp_workspace @@ -286,24 +287,23 @@ class CommandLineTestCase(unittest.TestCase): self.assertEqual(ctx.returncode, 1) def test_run_with_user_yamllintrc_config_file(self): - config = os.path.join(self.wd, 'fake-local-config') - self.addCleanup(os.remove, config) - self.addCleanup(os.environ.__delitem__, 'YAMLLINTRC') - os.environ['YAMLLINTRC'] = config + self.addCleanup(os.environ.__delitem__, 'YAMLLINT_CONFIG_FILE') - with open(config, 'w') as f: + with tempfile.NamedTemporaryFile('w') as f: + os.environ['YAMLLINT_CONFIG_FILE'] = f.name f.write('rules: {trailing-spaces: disable}') + f.flush() + with RunContext(self) as ctx: + cli.run((os.path.join(self.wd, 'a.yaml'), )) + self.assertEqual(ctx.returncode, 0) - with RunContext(self) as ctx: - cli.run((os.path.join(self.wd, 'a.yaml'), )) - self.assertEqual(ctx.returncode, 0) - - with open(config, 'w') as f: + with tempfile.NamedTemporaryFile('w') as f: + os.environ['YAMLLINT_CONFIG_FILE'] = f.name f.write('rules: {trailing-spaces: enable}') - - with RunContext(self) as ctx: - cli.run((os.path.join(self.wd, 'a.yaml'), )) - self.assertEqual(ctx.returncode, 1) + f.flush() + with RunContext(self) as ctx: + cli.run((os.path.join(self.wd, 'a.yaml'), )) + self.assertEqual(ctx.returncode, 1) def test_run_version(self): with RunContext(self) as ctx: diff --git a/yamllint/cli.py b/yamllint/cli.py index ad54b44..2566c89 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -144,8 +144,9 @@ def run(argv=None): args = parser.parse_args(argv) - if 'YAMLLINTRC' in os.environ: - user_global_config = os.path.expanduser(os.environ['YAMLLINTRC']) + if 'YAMLLINT_CONFIG_FILE' in os.environ: + user_global_config = os.path.expanduser( + os.environ['YAMLLINT_CONFIG_FILE']) # User-global config is supposed to be in ~/.config/yamllint/config elif 'XDG_CONFIG_HOME' in os.environ: user_global_config = os.path.join(