diff options
Diffstat (limited to 'tests/test_cli.py')
-rw-r--r-- | tests/test_cli.py | 88 |
1 files changed, 56 insertions, 32 deletions
diff --git a/tests/test_cli.py b/tests/test_cli.py index 444f2f9..e0ae0fe 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -13,7 +13,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -from io import StringIO import fcntl import locale import os @@ -22,34 +21,11 @@ import shutil import sys import tempfile import unittest +from io import StringIO -from tests.common import build_temp_workspace, temp_workspace - -from yamllint import cli -from yamllint import config - - -class RunContext: - """Context manager for ``cli.run()`` to capture exit code and streams.""" - - def __init__(self, case): - self.stdout = self.stderr = None - self._raises_ctx = case.assertRaises(SystemExit) - - def __enter__(self): - self._raises_ctx.__enter__() - sys.stdout = self.outstream = StringIO() - sys.stderr = self.errstream = StringIO() - return self - - def __exit__(self, *exc_info): - self.stdout, sys.stdout = self.outstream.getvalue(), sys.__stdout__ - self.stderr, sys.stderr = self.errstream.getvalue(), sys.__stderr__ - return self._raises_ctx.__exit__(*exc_info) +from tests.common import build_temp_workspace, RunContext, temp_workspace - @property - def returncode(self): - return self._raises_ctx.exception.code +from yamllint import cli, config # Check system's UTF-8 availability @@ -57,9 +33,30 @@ def utf8_available(): try: locale.setlocale(locale.LC_ALL, 'C.UTF-8') locale.setlocale(locale.LC_ALL, (None, None)) - return True except locale.Error: # pragma: no cover return False + else: + return True + + +def setUpModule(): + # yamllint uses these environment variables to find a config file. + env_vars_that_could_interfere = ( + 'YAMLLINT_CONFIG_FILE', + 'XDG_CONFIG_HOME', + # These variables are used to determine where the user’s home + # directory is. See + # https://docs.python.org/3/library/os.path.html#os.path.expanduser + 'HOME', + 'USERPROFILE', + 'HOMEPATH', + 'HOMEDRIVE' + ) + for name in env_vars_that_could_interfere: + try: + del os.environ[name] + except KeyError: + pass class CommandLineTestCase(unittest.TestCase): @@ -88,6 +85,9 @@ class CommandLineTestCase(unittest.TestCase): 'key: other value\n', # empty dir 'empty-dir': [], + # symbolic link + 'symlinks/file-without-yaml-extension': '42\n', + 'symlinks/link.yaml': 'symlink://file-without-yaml-extension', # non-YAML file 'no-yaml.json': '---\n' 'key: value\n', @@ -116,8 +116,6 @@ class CommandLineTestCase(unittest.TestCase): shutil.rmtree(cls.wd) - @unittest.skipIf(not utf8_available() and sys.version_info < (3, 7), - 'UTF-8 paths not supported') def test_find_files_recursively(self): conf = config.YamlLintConfig('extends: default') self.assertEqual( @@ -130,6 +128,7 @@ class CommandLineTestCase(unittest.TestCase): os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'), os.path.join(self.wd, 'sub/directory.yaml/empty.yml'), os.path.join(self.wd, 'sub/ok.yaml'), + os.path.join(self.wd, 'symlinks/link.yaml'), os.path.join(self.wd, 'warn.yaml')], ) @@ -167,6 +166,7 @@ class CommandLineTestCase(unittest.TestCase): os.path.join(self.wd, 'en.yaml'), os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'), os.path.join(self.wd, 'sub/ok.yaml'), + os.path.join(self.wd, 'symlinks/link.yaml'), os.path.join(self.wd, 'warn.yaml')] ) @@ -204,6 +204,8 @@ class CommandLineTestCase(unittest.TestCase): os.path.join(self.wd, 'sub/directory.yaml/empty.yml'), os.path.join(self.wd, 'sub/directory.yaml/not-yaml.txt'), os.path.join(self.wd, 'sub/ok.yaml'), + os.path.join(self.wd, 'symlinks/file-without-yaml-extension'), + os.path.join(self.wd, 'symlinks/link.yaml'), os.path.join(self.wd, 'warn.yaml')] ) @@ -225,6 +227,8 @@ class CommandLineTestCase(unittest.TestCase): os.path.join(self.wd, 'sub/directory.yaml/empty.yml'), os.path.join(self.wd, 'sub/directory.yaml/not-yaml.txt'), os.path.join(self.wd, 'sub/ok.yaml'), + os.path.join(self.wd, 'symlinks/file-without-yaml-extension'), + os.path.join(self.wd, 'symlinks/link.yaml'), os.path.join(self.wd, 'warn.yaml')] ) @@ -306,14 +310,13 @@ class CommandLineTestCase(unittest.TestCase): cli.run(('-c', f.name, os.path.join(self.wd, 'a.yaml'))) self.assertEqual(ctx.returncode, 1) - @unittest.skipIf(os.environ.get('GITHUB_RUN_ID'), '$HOME not overridable') def test_run_with_user_global_config_file(self): home = os.path.join(self.wd, 'fake-home') dir = os.path.join(home, '.config', 'yamllint') os.makedirs(dir) config = os.path.join(dir, 'config') - self.addCleanup(os.environ.update, HOME=os.environ['HOME']) + self.addCleanup(os.environ.__delitem__, 'HOME') os.environ['HOME'] = home with open(config, 'w') as f: @@ -690,6 +693,7 @@ class CommandLineTestCase(unittest.TestCase): os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'), os.path.join(self.wd, 'sub/directory.yaml/empty.yml'), os.path.join(self.wd, 'sub/ok.yaml'), + os.path.join(self.wd, 'symlinks/link.yaml'), os.path.join(self.wd, 'warn.yaml')] ) @@ -706,9 +710,29 @@ class CommandLineTestCase(unittest.TestCase): os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'), os.path.join(self.wd, 'sub/directory.yaml/not-yaml.txt'), os.path.join(self.wd, 'sub/ok.yaml'), + os.path.join(self.wd, 'symlinks/link.yaml'), os.path.join(self.wd, 'warn.yaml')] ) + config = 'ignore: ["*.yaml", "*.yml", "!a.yaml"]' + with RunContext(self) as ctx: + cli.run(('--list-files', '-d', config, self.wd)) + self.assertEqual(ctx.returncode, 0) + self.assertEqual( + sorted(ctx.stdout.splitlines()), + [os.path.join(self.wd, 'a.yaml')] + ) + with RunContext(self) as ctx: + cli.run(('--list-files', '-d', config, + os.path.join(self.wd, 'a.yaml'), + os.path.join(self.wd, 'en.yaml'), + os.path.join(self.wd, 'c.yaml'))) + self.assertEqual(ctx.returncode, 0) + self.assertEqual( + sorted(ctx.stdout.splitlines()), + [os.path.join(self.wd, 'a.yaml')] + ) + class CommandLineConfigTestCase(unittest.TestCase): def test_config_file(self): |