mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-19 07:29:09 -07:00
Don't register tests as filters (#55332)
* Don't register tests as filters. Fixes #55319 * Remove tests for deprecated functionality * Remove no-tests-as-filters sanity tests * Remove docs too * Revert "Remove docs too" This reverts commit 7daf457a742e67ec6e91964852c7f804507b46b8. * Make no-tests-as-filters doc an orphan
This commit is contained in:
parent
960df24272
commit
9f83139dcb
6 changed files with 4 additions and 151 deletions
2
changelogs/fragments/tests-as-filters-deprecation.yaml
Normal file
2
changelogs/fragments/tests-as-filters-deprecation.yaml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- Jinja tests - Remove deprecated functionality of registering tests as filters (https://github.com/ansible/ansible/issues/55319)
|
|
@ -1,3 +1,5 @@
|
||||||
|
:orphan:
|
||||||
|
|
||||||
Sanity Tests » no-tests-as-filters
|
Sanity Tests » no-tests-as-filters
|
||||||
==================================
|
==================================
|
||||||
|
|
||||||
|
|
|
@ -178,26 +178,6 @@ def _count_newlines_from_end(in_str):
|
||||||
return i
|
return i
|
||||||
|
|
||||||
|
|
||||||
def tests_as_filters_warning(name, func):
|
|
||||||
'''
|
|
||||||
Closure to enable displaying a deprecation warning when tests are used as a filter
|
|
||||||
|
|
||||||
This closure is only used when registering ansible provided tests as filters
|
|
||||||
|
|
||||||
This function should be removed in 2.9 along with registering ansible provided tests as filters
|
|
||||||
in Templar._get_filters
|
|
||||||
'''
|
|
||||||
@wraps(func)
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
display.deprecated(
|
|
||||||
'Using tests as filters is deprecated. Instead of using `result|%(name)s` use '
|
|
||||||
'`result is %(name)s`' % dict(name=name),
|
|
||||||
version='2.9'
|
|
||||||
)
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
class AnsibleUndefined(StrictUndefined):
|
class AnsibleUndefined(StrictUndefined):
|
||||||
'''
|
'''
|
||||||
A custom Undefined class, which returns further Undefined objects on access,
|
A custom Undefined class, which returns further Undefined objects on access,
|
||||||
|
@ -422,13 +402,6 @@ class Templar:
|
||||||
|
|
||||||
self._filters = dict()
|
self._filters = dict()
|
||||||
|
|
||||||
# TODO: Remove registering tests as filters in 2.9
|
|
||||||
for name, func in self._get_tests().items():
|
|
||||||
if name in builtin_filters:
|
|
||||||
# If we have a custom test named the same as a builtin filter, don't register as a filter
|
|
||||||
continue
|
|
||||||
self._filters[name] = tests_as_filters_warning(name, func)
|
|
||||||
|
|
||||||
for fp in self._filter_loader.all():
|
for fp in self._filter_loader.all():
|
||||||
self._filters.update(fp.filters())
|
self._filters.update(fp.filters())
|
||||||
|
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
{
|
|
||||||
"extensions": [
|
|
||||||
".yml",
|
|
||||||
".yaml"
|
|
||||||
],
|
|
||||||
"output": "path-line-column-message"
|
|
||||||
}
|
|
|
@ -1,82 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# (c) 2017, Matt Martz <matt@sivel.net>
|
|
||||||
#
|
|
||||||
# This file is part of Ansible
|
|
||||||
#
|
|
||||||
# Ansible is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# Ansible is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import re
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from ansible.plugins.test import core, files, mathstuff
|
|
||||||
|
|
||||||
TESTS = list(core.TestModule().tests().keys()) + list(files.TestModule().tests().keys()) + list(mathstuff.TestModule().tests().keys())
|
|
||||||
|
|
||||||
|
|
||||||
TEST_MAP = {
|
|
||||||
'version_compare': 'version',
|
|
||||||
'is_dir': 'directory',
|
|
||||||
'is_file': 'file',
|
|
||||||
'is_link': 'link',
|
|
||||||
'is_abs': 'abs',
|
|
||||||
'is_same_file': 'same_file',
|
|
||||||
'is_mount': 'mount',
|
|
||||||
'issubset': 'subset',
|
|
||||||
'issuperset': 'superset',
|
|
||||||
'isnan': 'nan',
|
|
||||||
'succeeded': 'successful',
|
|
||||||
'success': 'successful',
|
|
||||||
'change': 'changed',
|
|
||||||
'skip': 'skipped',
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
FILTER_RE = re.compile(r'(?P<left>[\w .\'"]+)(\s*)\|(\s*)(?P<filter>\w+)')
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
for path in sys.argv[1:] or sys.stdin.read().splitlines():
|
|
||||||
with open(path) as f:
|
|
||||||
text = f.read()
|
|
||||||
|
|
||||||
lines = text.splitlines(True)
|
|
||||||
previous = 0
|
|
||||||
offset = 0
|
|
||||||
lineno = 0
|
|
||||||
|
|
||||||
for match in FILTER_RE.finditer(text):
|
|
||||||
filter_name = match.group('filter')
|
|
||||||
test_name = TEST_MAP.get(filter_name, filter_name)
|
|
||||||
|
|
||||||
if test_name not in TESTS:
|
|
||||||
continue
|
|
||||||
|
|
||||||
left = match.group('left').strip()
|
|
||||||
start = match.start('left')
|
|
||||||
|
|
||||||
while start >= offset:
|
|
||||||
previous = offset
|
|
||||||
offset += len(lines[lineno])
|
|
||||||
lineno += 1
|
|
||||||
|
|
||||||
colno = start - previous + 1
|
|
||||||
|
|
||||||
print('%s:%d:%d: use `%s is %s` instead of `%s | %s`' % (path, lineno, colno, left, test_name, left, filter_name))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
|
@ -1,35 +0,0 @@
|
||||||
from ansible.template import Templar, display
|
|
||||||
from units.mock.loader import DictDataLoader
|
|
||||||
from jinja2.filters import FILTERS
|
|
||||||
from os.path import isabs
|
|
||||||
|
|
||||||
|
|
||||||
def test_tests_as_filters_warning(mocker):
|
|
||||||
fake_loader = DictDataLoader({
|
|
||||||
"/path/to/my_file.txt": "foo\n",
|
|
||||||
})
|
|
||||||
templar = Templar(loader=fake_loader, variables={})
|
|
||||||
filters = templar._get_filters(templar.environment.filters)
|
|
||||||
|
|
||||||
mocker.patch.object(display, 'deprecated')
|
|
||||||
|
|
||||||
# Call successful test, ensure the message is correct
|
|
||||||
filters['successful']({})
|
|
||||||
display.deprecated.assert_called_once_with(
|
|
||||||
'Using tests as filters is deprecated. Instead of using `result|successful` use `result is successful`', version='2.9'
|
|
||||||
)
|
|
||||||
|
|
||||||
# Call success test, ensure the message is correct
|
|
||||||
display.deprecated.reset_mock()
|
|
||||||
filters['success']({})
|
|
||||||
display.deprecated.assert_called_once_with(
|
|
||||||
'Using tests as filters is deprecated. Instead of using `result|success` use `result is success`', version='2.9'
|
|
||||||
)
|
|
||||||
|
|
||||||
# Call bool filter, ensure no deprecation message was displayed
|
|
||||||
display.deprecated.reset_mock()
|
|
||||||
filters['bool'](True)
|
|
||||||
assert display.deprecated.call_count == 0
|
|
||||||
|
|
||||||
# Ensure custom test does not override builtin filter
|
|
||||||
assert filters.get('abs') != isabs
|
|
Loading…
Add table
Add a link
Reference in a new issue