Compare commits

...

4 Commits

Author SHA1 Message Date
Adrien Vergé
8b758d4e7e yamllint version 1.24.2 2020-07-16 09:35:08 +02:00
Wolfgang Walther
b5b436a3a4 Add global "locale" config option and make key-ordering rule locale-aware
Support sorting by locale with strcoll(). Properly handle case and accents.

Note: this is a second implementation, for context see:
https://github.com/adrienverge/yamllint/pull/280
https://github.com/adrienverge/yamllint/issues/285
https://github.com/adrienverge/yamllint/pull/288
2020-07-16 09:34:13 +02:00
Adrien Vergé
0fceca2354 yamllint version 1.24.1 2020-07-15 14:49:51 +02:00
Adrien Vergé
9403f1f3ec Revert "Add global "locale" config option"
This reverts commit 9e90c77, because it caused a bug that affected
different people just after being released:
https://github.com/adrienverge/yamllint/issues/285
https://github.com/adrienverge/yamllint/issues/286
2020-07-15 14:48:48 +02:00
7 changed files with 40 additions and 14 deletions

View File

@@ -1,6 +1,16 @@
Changelog
=========
1.24.2 (2020-07-16)
-------------------
- Add ``locale`` config option and make ``key-ordering`` locale-aware
1.24.1 (2020-07-15)
-------------------
- Revert ``locale`` config option from version 1.24.0 because of a bug
1.24.0 (2020-07-15)
-------------------

View File

@@ -197,10 +197,10 @@ It is possible to set the ``locale`` option globally. This is passed to Python's
`locale.setlocale
<https://docs.python.org/3/library/locale.html#locale.setlocale>`_,
so an empty string ``""`` will use the system default locale, while e.g.
``"en_US.UTF-8"`` will use that. If unset, the default is ``"C.UTF-8"``.
``"en_US.UTF-8"`` will use that.
Currently this only affects the ``key-ordering`` rule. The default will order
by Unicode code point number, while other locales will sort case and accents
by Unicode code point number, while locales will sort case and accents
properly as well.
.. code-block:: yaml

View File

@@ -114,7 +114,7 @@ class KeyOrderingTestCase(RuleTestCase):
']\n', conf)
def test_locale_case(self):
self.addCleanup(locale.setlocale, locale.LC_ALL, 'C.UTF-8')
self.addCleanup(locale.setlocale, locale.LC_ALL, (None, None))
try:
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
except locale.Error:
@@ -133,7 +133,7 @@ class KeyOrderingTestCase(RuleTestCase):
problem=(4, 1))
def test_locale_accents(self):
self.addCleanup(locale.setlocale, locale.LC_ALL, 'C.UTF-8')
self.addCleanup(locale.setlocale, locale.LC_ALL, (None, None))
try:
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
except locale.Error:

View File

@@ -331,11 +331,14 @@ class CommandLineTestCase(unittest.TestCase):
self.assertEqual(ctx.returncode, 1)
def test_run_with_locale(self):
self.addCleanup(locale.setlocale, locale.LC_ALL, 'C.UTF-8')
# check for availability of locale, otherwise skip the test
# reset to default before running the test,
# as the first two runs don't use setlocale()
try:
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
except locale.Error:
self.skipTest('locale en_US.UTF-8 not available')
locale.setlocale(locale.LC_ALL, (None, None))
# C + en.yaml should fail
with RunContext(self) as ctx:
@@ -343,6 +346,16 @@ class CommandLineTestCase(unittest.TestCase):
os.path.join(self.wd, 'en.yaml')))
self.assertEqual(ctx.returncode, 1)
# C + c.yaml should pass
with RunContext(self) as ctx:
cli.run(('-d', 'rules: { key-ordering: enable }',
os.path.join(self.wd, 'c.yaml')))
self.assertEqual(ctx.returncode, 0)
# the next two runs use setlocale() inside,
# so we need to clean up afterwards
self.addCleanup(locale.setlocale, locale.LC_ALL, (None, None))
# en_US + en.yaml should pass
with RunContext(self) as ctx:
cli.run(('-d', 'locale: en_US.UTF-8\n'
@@ -357,12 +370,6 @@ class CommandLineTestCase(unittest.TestCase):
os.path.join(self.wd, 'c.yaml')))
self.assertEqual(ctx.returncode, 1)
# C + c.yaml should pass
with RunContext(self) as ctx:
cli.run(('-d', 'rules: { key-ordering: enable }',
os.path.join(self.wd, 'c.yaml')))
self.assertEqual(ctx.returncode, 0)
def test_run_version(self):
with RunContext(self) as ctx:
cli.run(('--version', ))
@@ -421,6 +428,14 @@ class CommandLineTestCase(unittest.TestCase):
def test_run_non_ascii_file(self):
path = os.path.join(self.wd, 'non-ascii', 'éçäγλνπ¥', 'utf-8')
# Make sure the default localization conditions on this "system"
# support UTF-8 encoding.
try:
locale.setlocale(locale.LC_ALL, (None, 'UTF-8'))
except locale.Error:
self.skipTest('no UTF-8 locale available')
self.addCleanup(locale.setlocale, locale.LC_ALL, (None, None))
with RunContext(self) as ctx:
cli.run(('-f', 'parsable', path))
self.assertEqual((ctx.returncode, ctx.stdout, ctx.stderr), (0, '', ''))

View File

@@ -22,7 +22,7 @@ indentation, etc."""
APP_NAME = 'yamllint'
APP_VERSION = '1.24.0'
APP_VERSION = '1.24.2'
APP_DESCRIPTION = __doc__
__author__ = u'Adrien Vergé'

View File

@@ -176,7 +176,8 @@ def run(argv=None):
print(e, file=sys.stderr)
sys.exit(-1)
locale.setlocale(locale.LC_ALL, conf.locale)
if conf.locale is not None:
locale.setlocale(locale.LC_ALL, conf.locale)
max_level = 0

View File

@@ -35,7 +35,7 @@ class YamlLintConfig(object):
self.yaml_files = pathspec.PathSpec.from_lines(
'gitwildmatch', ['*.yaml', '*.yml', '.yamllint'])
self.locale = 'C.UTF-8'
self.locale = None
if file is not None:
with open(file) as f: