|
|
|
@ -29,6 +29,7 @@ import unittest
|
|
|
|
|
from tests.common import build_temp_workspace
|
|
|
|
|
|
|
|
|
|
from yamllint import cli
|
|
|
|
|
from yamllint import config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommandLineTestCase(unittest.TestCase):
|
|
|
|
@ -73,8 +74,9 @@ class CommandLineTestCase(unittest.TestCase):
|
|
|
|
|
shutil.rmtree(cls.wd)
|
|
|
|
|
|
|
|
|
|
def test_find_files_recursively(self):
|
|
|
|
|
conf = config.YamlLintConfig('extends: default')
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sorted(cli.find_files_recursively([self.wd])),
|
|
|
|
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
|
|
|
|
[os.path.join(self.wd, 'a.yaml'),
|
|
|
|
|
os.path.join(self.wd, 'empty.yml'),
|
|
|
|
|
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'),
|
|
|
|
@ -85,14 +87,14 @@ class CommandLineTestCase(unittest.TestCase):
|
|
|
|
|
items = [os.path.join(self.wd, 'sub/ok.yaml'),
|
|
|
|
|
os.path.join(self.wd, 'empty-dir')]
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sorted(cli.find_files_recursively(items)),
|
|
|
|
|
sorted(cli.find_files_recursively(items, conf)),
|
|
|
|
|
[os.path.join(self.wd, 'sub/ok.yaml')],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
items = [os.path.join(self.wd, 'empty.yml'),
|
|
|
|
|
os.path.join(self.wd, 's')]
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sorted(cli.find_files_recursively(items)),
|
|
|
|
|
sorted(cli.find_files_recursively(items, conf)),
|
|
|
|
|
[os.path.join(self.wd, 'empty.yml'),
|
|
|
|
|
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml')],
|
|
|
|
|
)
|
|
|
|
@ -100,11 +102,77 @@ class CommandLineTestCase(unittest.TestCase):
|
|
|
|
|
items = [os.path.join(self.wd, 'sub'),
|
|
|
|
|
os.path.join(self.wd, '/etc/another/file')]
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sorted(cli.find_files_recursively(items)),
|
|
|
|
|
sorted(cli.find_files_recursively(items, conf)),
|
|
|
|
|
[os.path.join(self.wd, '/etc/another/file'),
|
|
|
|
|
os.path.join(self.wd, 'sub/ok.yaml')],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
conf = config.YamlLintConfig('extends: default\n'
|
|
|
|
|
'yaml-files:\n'
|
|
|
|
|
' - \'*.yaml\' \n')
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
|
|
|
|
[os.path.join(self.wd, 'a.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, 'warn.yaml')]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
conf = config.YamlLintConfig('extends: default\n'
|
|
|
|
|
'yaml-files:\n'
|
|
|
|
|
' - \'*.yml\'\n')
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
|
|
|
|
[os.path.join(self.wd, 'empty.yml')]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
conf = config.YamlLintConfig('extends: default\n'
|
|
|
|
|
'yaml-files:\n'
|
|
|
|
|
' - \'*.json\'\n')
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
|
|
|
|
[os.path.join(self.wd, 'no-yaml.json')]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
conf = config.YamlLintConfig('extends: default\n'
|
|
|
|
|
'yaml-files:\n'
|
|
|
|
|
' - \'*\'\n')
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
|
|
|
|
[os.path.join(self.wd, 'a.yaml'),
|
|
|
|
|
os.path.join(self.wd, 'empty.yml'),
|
|
|
|
|
os.path.join(self.wd, 'no-yaml.json'),
|
|
|
|
|
os.path.join(self.wd, 'non-ascii/utf-8'),
|
|
|
|
|
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, 'warn.yaml')]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
conf = config.YamlLintConfig('extends: default\n'
|
|
|
|
|
'yaml-files:\n'
|
|
|
|
|
' - \'*.yaml\'\n'
|
|
|
|
|
' - \'*\'\n'
|
|
|
|
|
' - \'**\'\n')
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
|
|
|
|
[os.path.join(self.wd, 'a.yaml'),
|
|
|
|
|
os.path.join(self.wd, 'empty.yml'),
|
|
|
|
|
os.path.join(self.wd, 'no-yaml.json'),
|
|
|
|
|
os.path.join(self.wd, 'non-ascii/utf-8'),
|
|
|
|
|
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, 'warn.yaml')]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
conf = config.YamlLintConfig('extends: default\n'
|
|
|
|
|
'yaml-files:\n'
|
|
|
|
|
' - \'s/**\'\n'
|
|
|
|
|
' - \'**/utf-8\'\n')
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
|
|
|
|
[os.path.join(self.wd, 'non-ascii/utf-8')]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_run_with_bad_arguments(self):
|
|
|
|
|
sys.stdout, sys.stderr = StringIO(), StringIO()
|
|
|
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
|
|
@ -462,5 +530,40 @@ class CommandLineTestCase(unittest.TestCase):
|
|
|
|
|
out, err = sys.stdout.getvalue(), sys.stderr.getvalue()
|
|
|
|
|
self.assertEqual(out, (
|
|
|
|
|
'stdin:2:10: [error] syntax error: '
|
|
|
|
|
'mapping values are not allowed here\n'))
|
|
|
|
|
'mapping values are not allowed here (syntax)\n'))
|
|
|
|
|
self.assertEqual(err, '')
|
|
|
|
|
|
|
|
|
|
def test_run_no_warnings(self):
|
|
|
|
|
file = os.path.join(self.wd, 'a.yaml')
|
|
|
|
|
|
|
|
|
|
sys.stdout, sys.stderr = StringIO(), StringIO()
|
|
|
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
|
|
|
cli.run((file, '--no-warnings', '-f', 'auto'))
|
|
|
|
|
|
|
|
|
|
self.assertEqual(ctx.exception.code, 1)
|
|
|
|
|
|
|
|
|
|
out, err = sys.stdout.getvalue(), sys.stderr.getvalue()
|
|
|
|
|
self.assertEqual(out, (
|
|
|
|
|
'%s\n'
|
|
|
|
|
' 2:4 error trailing spaces (trailing-spaces)\n'
|
|
|
|
|
' 3:4 error no new line character at the end of file '
|
|
|
|
|
'(new-line-at-end-of-file)\n'
|
|
|
|
|
'\n' % file))
|
|
|
|
|
self.assertEqual(err, '')
|
|
|
|
|
|
|
|
|
|
file = os.path.join(self.wd, 'warn.yaml')
|
|
|
|
|
|
|
|
|
|
sys.stdout, sys.stderr = StringIO(), StringIO()
|
|
|
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
|
|
|
cli.run((file, '--no-warnings', '-f', 'auto'))
|
|
|
|
|
|
|
|
|
|
self.assertEqual(ctx.exception.code, 0)
|
|
|
|
|
|
|
|
|
|
def test_run_no_warnings_and_strict(self):
|
|
|
|
|
file = os.path.join(self.wd, 'warn.yaml')
|
|
|
|
|
|
|
|
|
|
sys.stdout, sys.stderr = StringIO(), StringIO()
|
|
|
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
|
|
|
cli.run((file, '--no-warnings', '-s'))
|
|
|
|
|
|
|
|
|
|
self.assertEqual(ctx.exception.code, 2)
|
|
|
|
|